indvdum@0: $str[$pos - 1]) : array(); indvdum@0: indvdum@0: // UTF char length array indvdum@0: // This array is used to determine the length of a UTF character. indvdum@0: // Be $c the result of ($str[$pos] & "\xF0") --where $str is the string we're operating on and $pos indvdum@0: // the position of the cursor--, if $utf_len_mask[$c] does not exist, the byte is an ASCII char. indvdum@0: // Otherwise, if $utf_len_mask[$c] is greater than 0, we have a the leading byte of a multibyte character indvdum@0: // whose length is $utf_len_mask[$c] and if it is equal to 0, the byte is a trailing byte. indvdum@0: $utf_len_mask = array( indvdum@0: // Leading bytes masks indvdum@0: "\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4, indvdum@0: // Trailing bytes masks indvdum@0: "\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0 indvdum@0: ); indvdum@0: indvdum@0: $extra_check = array( indvdum@0: "\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1, indvdum@0: "\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1, indvdum@0: "\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1 indvdum@0: ); indvdum@0: indvdum@0: $utf_validation_mask = array( indvdum@0: 2 => "\xE0\xC0", indvdum@0: 3 => "\xF0\xC0\xC0", indvdum@0: 4 => "\xF8\xC0\xC0\xC0" indvdum@0: ); indvdum@0: indvdum@0: $utf_validation_check = array( indvdum@0: 2 => "\xC0\x80", indvdum@0: 3 => "\xE0\x80\x80", indvdum@0: 4 => "\xF0\x80\x80\x80" indvdum@0: ); indvdum@0: indvdum@0: // Main loop indvdum@0: do indvdum@0: { indvdum@0: // STEP 0: Capture the current char and buffer it indvdum@0: $c = $str[$pos]; indvdum@0: $c_mask = $c & "\xF0"; indvdum@0: indvdum@0: if (isset($utf_len_mask[$c_mask])) indvdum@0: { indvdum@0: // Byte at $pos is either a leading byte or a missplaced trailing byte indvdum@0: if ($utf_len = $utf_len_mask[$c_mask]) indvdum@0: { indvdum@0: // Capture the char indvdum@0: $buffer[++$i & 7] = $utf_char = substr($str, $pos, $utf_len); indvdum@0: indvdum@0: // Let's find out if a thorough check is needed indvdum@0: if (isset($qc[$utf_char])) indvdum@0: { indvdum@0: // If the UTF char is in the qc array then it may not be in normal form. We do nothing here, the actual processing is below this "if" block indvdum@0: } indvdum@0: else if (isset($utf_combining_class[$utf_char])) indvdum@0: { indvdum@0: if ($utf_combining_class[$utf_char] < $last_cc) indvdum@0: { indvdum@0: // A combining character that is NOT canonically ordered indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // A combining character that IS canonically ordered, skip to the next char indvdum@0: $last_cc = $utf_combining_class[$utf_char]; indvdum@0: indvdum@0: $pos += $utf_len; indvdum@0: continue; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // At this point, $utf_char holds a UTF char that we know is not a NF[K]C_QC and is not a combining character. indvdum@0: // It can be a singleton, a canonical composite, a replacement char or an even an ill-formed bunch of bytes. Let's find out indvdum@0: $last_cc = 0; indvdum@0: indvdum@0: // Check that we have the correct number of trailing bytes indvdum@0: if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len]) indvdum@0: { indvdum@0: // Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char indvdum@0: // has been encoded in a five- or six- byte sequence indvdum@0: if ($utf_char[0] >= "\xF8") indvdum@0: { indvdum@0: if ($utf_char[0] < "\xFC") indvdum@0: { indvdum@0: $trailing_bytes = 4; indvdum@0: } indvdum@0: else if ($utf_char[0] > "\xFD") indvdum@0: { indvdum@0: $trailing_bytes = 0; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $trailing_bytes = 5; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $trailing_bytes = $utf_len - 1; indvdum@0: } indvdum@0: indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT; indvdum@0: $pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes); indvdum@0: $tmp_pos = $pos; indvdum@0: indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: if (isset($extra_check[$c])) indvdum@0: { indvdum@0: switch ($c) indvdum@0: { indvdum@0: // Note: 0xED is quite common in Korean indvdum@0: case "\xED": indvdum@0: if ($utf_char >= "\xED\xA0\x80") indvdum@0: { indvdum@0: // Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF) indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT; indvdum@0: $pos += $utf_len; indvdum@0: $tmp_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: // Note: 0xEF is quite common in Japanese indvdum@0: case "\xEF": indvdum@0: if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF") indvdum@0: { indvdum@0: // U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF) indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT; indvdum@0: $pos += $utf_len; indvdum@0: $tmp_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case "\xC0": indvdum@0: case "\xC1": indvdum@0: if ($utf_char <= "\xC1\xBF") indvdum@0: { indvdum@0: // Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT; indvdum@0: $pos += $utf_len; indvdum@0: $tmp_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case "\xE0": indvdum@0: if ($utf_char <= "\xE0\x9F\xBF") indvdum@0: { indvdum@0: // Unicode char U+0000..U+07FF encoded in 3 bytes indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT; indvdum@0: $pos += $utf_len; indvdum@0: $tmp_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case "\xF0": indvdum@0: if ($utf_char <= "\xF0\x8F\xBF\xBF") indvdum@0: { indvdum@0: // Unicode char U+0000..U+FFFF encoded in 4 bytes indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT; indvdum@0: $pos += $utf_len; indvdum@0: $tmp_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: // Five- and six- byte sequences do not need being checked for here anymore indvdum@0: if ($utf_char > UTF8_MAX) indvdum@0: { indvdum@0: // Out of the Unicode range indvdum@0: if ($utf_char[0] < "\xF8") indvdum@0: { indvdum@0: $trailing_bytes = 3; indvdum@0: } indvdum@0: else if ($utf_char[0] < "\xFC") indvdum@0: { indvdum@0: $trailing_bytes = 4; indvdum@0: } indvdum@0: else if ($utf_char[0] > "\xFD") indvdum@0: { indvdum@0: $trailing_bytes = 0; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $trailing_bytes = 5; indvdum@0: } indvdum@0: indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT; indvdum@0: $pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes); indvdum@0: $tmp_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // The char is a valid starter, move the cursor and go on indvdum@0: $pos += $utf_len; indvdum@0: continue; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // A trailing byte came out of nowhere, we will advance the cursor and treat the this byte and all following trailing bytes as if indvdum@0: // each of them was a Unicode replacement char indvdum@0: $spn = strspn($str, UTF8_TRAILING_BYTES, $pos); indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn); indvdum@0: indvdum@0: $pos += $spn; indvdum@0: $tmp_pos = $pos; indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: indvdum@0: // STEP 1: Decompose current char indvdum@0: indvdum@0: // We have found a character that is either: indvdum@0: // - in the NFC_QC/NFKC_QC list indvdum@0: // - a non-starter char that is not canonically ordered indvdum@0: // indvdum@0: // We are going to capture the shortest UTF sequence that satisfies these two conditions: indvdum@0: // indvdum@0: // 1 - If the sequence does not start at the begginning of the string, it must begin with a starter, indvdum@0: // and that starter must not have the NF[K]C_QC property equal to "MAYBE" indvdum@0: // indvdum@0: // 2 - If the sequence does not end at the end of the string, it must end with a non-starter and be indvdum@0: // immediately followed by a starter that is not on the QC list indvdum@0: // indvdum@0: $utf_seq = array(); indvdum@0: $last_cc = 0; indvdum@0: $lpos = $pos; indvdum@0: $pos += $utf_len; indvdum@0: indvdum@0: if (isset($decomp_map[$utf_char])) indvdum@0: { indvdum@0: $_pos = 0; indvdum@0: $_len = strlen($decomp_map[$utf_char]); indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $_utf_len =& $utf_len_mask[$decomp_map[$utf_char][$_pos] & "\xF0"]; indvdum@0: indvdum@0: if (isset($_utf_len)) indvdum@0: { indvdum@0: $utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len); indvdum@0: $_pos += $_utf_len; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $utf_seq[] = $decomp_map[$utf_char][$_pos]; indvdum@0: ++$_pos; indvdum@0: } indvdum@0: } indvdum@0: while ($_pos < $_len); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // The char is not decomposable indvdum@0: $utf_seq = array($utf_char); indvdum@0: } indvdum@0: indvdum@0: indvdum@0: // STEP 2: Capture the starter indvdum@0: indvdum@0: // Check out the combining class of the first character of the UTF sequence indvdum@0: $k = 0; indvdum@0: if (isset($utf_combining_class[$utf_seq[0]]) || $qc[$utf_char] == UNICODE_QC_MAYBE) indvdum@0: { indvdum@0: // Not a starter, inspect previous characters indvdum@0: // The last 8 characters are kept in a buffer so that we don't have to capture them everytime. indvdum@0: // This is enough for all real-life strings but even if it wasn't, we can capture characters in backward mode, indvdum@0: // although it is slower than this method. indvdum@0: // indvdum@0: // In the following loop, $j starts at the previous buffered character ($i - 1, because current character is indvdum@0: // at offset $i) and process them in backward mode until we find a starter. indvdum@0: // indvdum@0: // $k is the index on each UTF character inside of our UTF sequence. At this time, $utf_seq contains one or more indvdum@0: // characters numbered 0 to n. $k starts at 0 and for each char we prepend we pre-decrement it and for numbering indvdum@0: $starter_found = 0; indvdum@0: $j_min = max(1, $i - 7); indvdum@0: indvdum@0: for ($j = $i - 1; $j >= $j_min && $lpos > $tmp_pos; --$j) indvdum@0: { indvdum@0: $utf_char = $buffer[$j & 7]; indvdum@0: $lpos -= strlen($utf_char); indvdum@0: indvdum@0: if (isset($decomp_map[$utf_char])) indvdum@0: { indvdum@0: // The char is a composite, decompose for storage indvdum@0: $decomp_seq = array(); indvdum@0: $_pos = 0; indvdum@0: $_len = strlen($decomp_map[$utf_char]); indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $c = $decomp_map[$utf_char][$_pos]; indvdum@0: $_utf_len =& $utf_len_mask[$c & "\xF0"]; indvdum@0: indvdum@0: if (isset($_utf_len)) indvdum@0: { indvdum@0: $decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len); indvdum@0: $_pos += $_utf_len; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $decomp_seq[] = $c; indvdum@0: ++$_pos; indvdum@0: } indvdum@0: } indvdum@0: while ($_pos < $_len); indvdum@0: indvdum@0: // Prepend the UTF sequence with our decomposed sequence indvdum@0: if (isset($decomp_seq[1])) indvdum@0: { indvdum@0: // The char expanded into several chars indvdum@0: $decomp_cnt = sizeof($decomp_seq); indvdum@0: indvdum@0: foreach ($decomp_seq as $decomp_i => $decomp_char) indvdum@0: { indvdum@0: $utf_seq[$k + $decomp_i - $decomp_cnt] = $decomp_char; indvdum@0: } indvdum@0: $k -= $decomp_cnt; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Decomposed to a single char, easier to prepend indvdum@0: $utf_seq[--$k] = $decomp_seq[0]; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $utf_seq[--$k] = $utf_char; indvdum@0: } indvdum@0: indvdum@0: if (!isset($utf_combining_class[$utf_seq[$k]])) indvdum@0: { indvdum@0: // We have found our starter indvdum@0: $starter_found = 1; indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (!$starter_found && $lpos > $tmp_pos) indvdum@0: { indvdum@0: // The starter was not found in the buffer, let's rewind some more indvdum@0: do indvdum@0: { indvdum@0: // $utf_len_mask contains the masks of both leading bytes and trailing bytes. If $utf_en > 0 then it's a leading byte, otherwise it's a trailing byte. indvdum@0: $c = $str[--$lpos]; indvdum@0: $c_mask = $c & "\xF0"; indvdum@0: indvdum@0: if (isset($utf_len_mask[$c_mask])) indvdum@0: { indvdum@0: // UTF byte indvdum@0: if ($utf_len = $utf_len_mask[$c_mask]) indvdum@0: { indvdum@0: // UTF *leading* byte indvdum@0: $utf_char = substr($str, $lpos, $utf_len); indvdum@0: indvdum@0: if (isset($decomp_map[$utf_char])) indvdum@0: { indvdum@0: // Decompose the character indvdum@0: $decomp_seq = array(); indvdum@0: $_pos = 0; indvdum@0: $_len = strlen($decomp_map[$utf_char]); indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $c = $decomp_map[$utf_char][$_pos]; indvdum@0: $_utf_len =& $utf_len_mask[$c & "\xF0"]; indvdum@0: indvdum@0: if (isset($_utf_len)) indvdum@0: { indvdum@0: $decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len); indvdum@0: $_pos += $_utf_len; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $decomp_seq[] = $c; indvdum@0: ++$_pos; indvdum@0: } indvdum@0: } indvdum@0: while ($_pos < $_len); indvdum@0: indvdum@0: // Prepend the UTF sequence with our decomposed sequence indvdum@0: if (isset($decomp_seq[1])) indvdum@0: { indvdum@0: // The char expanded into several chars indvdum@0: $decomp_cnt = sizeof($decomp_seq); indvdum@0: foreach ($decomp_seq as $decomp_i => $utf_char) indvdum@0: { indvdum@0: $utf_seq[$k + $decomp_i - $decomp_cnt] = $utf_char; indvdum@0: } indvdum@0: $k -= $decomp_cnt; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Decomposed to a single char, easier to prepend indvdum@0: $utf_seq[--$k] = $decomp_seq[0]; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $utf_seq[--$k] = $utf_char; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // ASCII char indvdum@0: $utf_seq[--$k] = $c; indvdum@0: } indvdum@0: } indvdum@0: while ($lpos > $tmp_pos); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: indvdum@0: // STEP 3: Capture following combining modifiers indvdum@0: indvdum@0: while ($pos < $len) indvdum@0: { indvdum@0: $c_mask = $str[$pos] & "\xF0"; indvdum@0: indvdum@0: if (isset($utf_len_mask[$c_mask])) indvdum@0: { indvdum@0: if ($utf_len = $utf_len_mask[$c_mask]) indvdum@0: { indvdum@0: $utf_char = substr($str, $pos, $utf_len); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // A trailing byte came out of nowhere indvdum@0: // Trailing bytes are replaced with Unicode replacement chars, we will just ignore it for now, break out of the loop indvdum@0: // as if it was a starter (replacement chars ARE starters) and let the next loop replace it indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: if (isset($utf_combining_class[$utf_char]) || isset($qc[$utf_char])) indvdum@0: { indvdum@0: // Combining character, add it to the sequence and move the cursor indvdum@0: if (isset($decomp_map[$utf_char])) indvdum@0: { indvdum@0: // Decompose the character indvdum@0: $_pos = 0; indvdum@0: $_len = strlen($decomp_map[$utf_char]); indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $c = $decomp_map[$utf_char][$_pos]; indvdum@0: $_utf_len =& $utf_len_mask[$c & "\xF0"]; indvdum@0: indvdum@0: if (isset($_utf_len)) indvdum@0: { indvdum@0: $utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len); indvdum@0: $_pos += $_utf_len; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $utf_seq[] = $c; indvdum@0: ++$_pos; indvdum@0: } indvdum@0: } indvdum@0: while ($_pos < $_len); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $utf_seq[] = $utf_char; indvdum@0: } indvdum@0: indvdum@0: $pos += $utf_len; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Combining class 0 and no QC, break out of the loop indvdum@0: // Note: we do not know if that character is valid. If it's not, the next iteration will replace it indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // ASCII chars are starters indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: indvdum@0: // STEP 4: Sort and combine indvdum@0: indvdum@0: // Here we sort... indvdum@0: $k_max = $k + sizeof($utf_seq); indvdum@0: indvdum@0: if (!$k && $k_max == 1) indvdum@0: { indvdum@0: // There is only one char in the UTF sequence, add it then jump to the next iteration of main loop indvdum@0: // Note: the two commented lines below can be enabled under PHP5 for a very small performance gain in most cases indvdum@0: // if (substr_compare($str, $utf_seq[0], $lpos, $pos - $lpos)) indvdum@0: // { indvdum@0: $tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $utf_seq[0]; indvdum@0: $tmp_pos = $pos; indvdum@0: // } indvdum@0: indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: // ...there we combine indvdum@0: if (isset($utf_combining_class[$utf_seq[$k]])) indvdum@0: { indvdum@0: $starter = $nf_seq = ''; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $starter = $utf_seq[$k++]; indvdum@0: $nf_seq = ''; indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: indvdum@0: // We add an empty char at the end of the UTF char sequence. It will act as a starter and trigger the sort/combine routine indvdum@0: // at the end of the string without altering it indvdum@0: $utf_seq[] = ''; indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $utf_char = $utf_seq[$k++]; indvdum@0: indvdum@0: if (isset($utf_combining_class[$utf_char])) indvdum@0: { indvdum@0: $utf_sort[$utf_combining_class[$utf_char]][] = $utf_char; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: if (empty($utf_sort)) indvdum@0: { indvdum@0: // No combining characters... check for a composite of the two starters indvdum@0: if (isset($utf_canonical_comp[$starter . $utf_char])) indvdum@0: { indvdum@0: // Good ol' composite character indvdum@0: $starter = $utf_canonical_comp[$starter . $utf_char]; indvdum@0: } indvdum@0: else if (isset($utf_jamo_type[$utf_char])) indvdum@0: { indvdum@0: // Current char is a composable jamo indvdum@0: if (isset($utf_jamo_type[$starter]) && $utf_jamo_type[$starter] == UNICODE_JAMO_L && $utf_jamo_type[$utf_char] == UNICODE_JAMO_V) indvdum@0: { indvdum@0: // We have a L jamo followed by a V jamo, we are going to prefetch the next char to see if it's a T jamo indvdum@0: if (isset($utf_jamo_type[$utf_seq[$k]]) && $utf_jamo_type[$utf_seq[$k]] == UNICODE_JAMO_T) indvdum@0: { indvdum@0: // L+V+T jamos, combine to a LVT Hangul syllable ($k is incremented) indvdum@0: $cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char] + $utf_jamo_index[$utf_seq[$k]]; indvdum@0: ++$k; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // L+V jamos, combine to a LV Hangul syllable indvdum@0: $cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char]; indvdum@0: } indvdum@0: indvdum@0: $starter = chr(0xE0 | ($cp >> 12)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F)); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Non-composable jamo, just add it to the sequence indvdum@0: $nf_seq .= $starter; indvdum@0: $starter = $utf_char; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // No composite, just add the first starter to the sequence then continue with the other one indvdum@0: $nf_seq .= $starter; indvdum@0: $starter = $utf_char; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: // For each class of combining characters indvdum@0: foreach ($utf_sort as $cc => $utf_chars) indvdum@0: { indvdum@0: $j = 0; indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: // Look for a composite indvdum@0: if (isset($utf_canonical_comp[$starter . $utf_chars[$j]])) indvdum@0: { indvdum@0: // Found a composite, replace the starter indvdum@0: $starter = $utf_canonical_comp[$starter . $utf_chars[$j]]; indvdum@0: unset($utf_sort[$cc][$j]); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // No composite, all following characters in that class are blocked indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: while (isset($utf_sort[$cc][++$j])); indvdum@0: } indvdum@0: indvdum@0: // Add the starter to the normalized sequence, followed by non-starters in canonical order indvdum@0: $nf_seq .= $starter; indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: if (!empty($utf_chars)) indvdum@0: { indvdum@0: $nf_seq .= implode('', $utf_chars); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Reset the array and go on indvdum@0: $utf_sort = array(); indvdum@0: $starter = $utf_char; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: while ($k <= $k_max); indvdum@0: indvdum@0: $tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $nf_seq; indvdum@0: $tmp_pos = $pos; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Only a ASCII char can make the program get here indvdum@0: // indvdum@0: // First we skip the current byte with ++$pos, then we quickly skip following ASCII chars with strspn(). indvdum@0: // indvdum@0: // The first two "if"'s here can be removed, with the consequences of being faster on latin text (lots of ASCII) and slower on indvdum@0: // multi-byte text (where the only ASCII chars are spaces and punctuation) indvdum@0: if (++$pos != $len) indvdum@0: { indvdum@0: if ($str[$pos] < "\x80") indvdum@0: { indvdum@0: $pos += strspn($str, UTF8_ASCII_RANGE, ++$pos); indvdum@0: $buffer[++$i & 7] = $str[$pos - 1]; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $buffer[++$i & 7] = $c; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: while ($pos < $len); indvdum@0: indvdum@0: // Now is time to return the string indvdum@0: if ($tmp_pos) indvdum@0: { indvdum@0: // If the $tmp_pos cursor is not at the beggining of the string then at least one character was not in normal form. Replace $str with the fixed version indvdum@0: if ($tmp_pos == $len) indvdum@0: { indvdum@0: // The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str indvdum@0: return $tmp; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // The rightmost chunk of $str has not been appended to $tmp yet indvdum@0: return $tmp . substr($str, $tmp_pos); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // The string was already in normal form indvdum@0: return $str; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Decompose a UTF string indvdum@0: * indvdum@0: * @param string $str UTF string indvdum@0: * @param integer $pos Position of the first UTF char (in bytes) indvdum@0: * @param integer $len Length of the string (in bytes) indvdum@0: * @param array &$decomp_map Decomposition mapping, passed by reference but never modified indvdum@0: * @return string The string, decomposed and sorted canonically indvdum@0: * indvdum@0: * @access private indvdum@0: */ indvdum@0: function decompose($str, $pos, $len, &$decomp_map) indvdum@0: { indvdum@0: global $utf_combining_class; indvdum@0: indvdum@0: // Load some commonly-used tables indvdum@0: if (!isset($utf_combining_class)) indvdum@0: { indvdum@0: global $phpbb_root_path, $phpEx; indvdum@0: include($phpbb_root_path . 'includes/utf/data/utf_normalizer_common.' . $phpEx); indvdum@0: } indvdum@0: indvdum@0: // UTF char length array indvdum@0: $utf_len_mask = array( indvdum@0: // Leading bytes masks indvdum@0: "\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4, indvdum@0: // Trailing bytes masks indvdum@0: "\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0 indvdum@0: ); indvdum@0: indvdum@0: // Some extra checks are triggered on the first byte of a UTF sequence indvdum@0: $extra_check = array( indvdum@0: "\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1, indvdum@0: "\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1, indvdum@0: "\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1 indvdum@0: ); indvdum@0: indvdum@0: // These masks are used to check if a UTF sequence is well formed. Here are the only 3 lengths we acknowledge: indvdum@0: // - 2-byte: 110? ???? 10?? ???? indvdum@0: // - 3-byte: 1110 ???? 10?? ???? 10?? ???? indvdum@0: // - 4-byte: 1111 0??? 10?? ???? 10?? ???? 10?? ???? indvdum@0: // Note that 5- and 6- byte sequences are automatically discarded indvdum@0: $utf_validation_mask = array( indvdum@0: 2 => "\xE0\xC0", indvdum@0: 3 => "\xF0\xC0\xC0", indvdum@0: 4 => "\xF8\xC0\xC0\xC0" indvdum@0: ); indvdum@0: indvdum@0: $utf_validation_check = array( indvdum@0: 2 => "\xC0\x80", indvdum@0: 3 => "\xE0\x80\x80", indvdum@0: 4 => "\xF0\x80\x80\x80" indvdum@0: ); indvdum@0: indvdum@0: $tmp = ''; indvdum@0: $starter_pos = $pos; indvdum@0: $tmp_pos = $last_cc = $sort = $dump = 0; indvdum@0: $utf_sort = array(); indvdum@0: indvdum@0: indvdum@0: // Main loop indvdum@0: do indvdum@0: { indvdum@0: // STEP 0: Capture the current char indvdum@0: indvdum@0: $cur_mask = $str[$pos] & "\xF0"; indvdum@0: if (isset($utf_len_mask[$cur_mask])) indvdum@0: { indvdum@0: if ($utf_len = $utf_len_mask[$cur_mask]) indvdum@0: { indvdum@0: // Multibyte char indvdum@0: $utf_char = substr($str, $pos, $utf_len); indvdum@0: $pos += $utf_len; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // A trailing byte came out of nowhere, we will treat it and all following trailing bytes as if each of them was a Unicode indvdum@0: // replacement char and we will advance the cursor indvdum@0: $spn = strspn($str, UTF8_TRAILING_BYTES, $pos); indvdum@0: indvdum@0: if ($dump) indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: // Dump combiners indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: if ($sort) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: } indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $tmp .= str_repeat(UTF8_REPLACEMENT, $spn); indvdum@0: $dump = $sort = 0; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn); indvdum@0: } indvdum@0: indvdum@0: $pos += $spn; indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: indvdum@0: $utf_sort = array(); indvdum@0: $last_cc = 0; indvdum@0: indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: indvdum@0: // STEP 1: Decide what to do with current char indvdum@0: indvdum@0: // Now, in that order: indvdum@0: // - check if that character is decomposable indvdum@0: // - check if that character is a non-starter indvdum@0: // - check if that character requires extra checks to be performed indvdum@0: if (isset($decomp_map[$utf_char])) indvdum@0: { indvdum@0: // Decompose the char indvdum@0: $_pos = 0; indvdum@0: $_len = strlen($decomp_map[$utf_char]); indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $c = $decomp_map[$utf_char][$_pos]; indvdum@0: $_utf_len =& $utf_len_mask[$c & "\xF0"]; indvdum@0: indvdum@0: if (isset($_utf_len)) indvdum@0: { indvdum@0: $_utf_char = substr($decomp_map[$utf_char], $_pos, $_utf_len); indvdum@0: $_pos += $_utf_len; indvdum@0: indvdum@0: if (isset($utf_combining_class[$_utf_char])) indvdum@0: { indvdum@0: // The character decomposed to a non-starter, buffer it for sorting indvdum@0: $utf_sort[$utf_combining_class[$_utf_char]][] = $_utf_char; indvdum@0: indvdum@0: if ($utf_combining_class[$_utf_char] < $last_cc) indvdum@0: { indvdum@0: // Not canonically ordered, will require sorting indvdum@0: $sort = $dump = 1; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $dump = 1; indvdum@0: $last_cc = $utf_combining_class[$_utf_char]; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // This character decomposition contains a starter, dump the buffer and continue indvdum@0: if ($dump) indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: // Dump combiners indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: if ($sort) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: } indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $tmp .= $_utf_char; indvdum@0: $dump = $sort = 0; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos) . $_utf_char; indvdum@0: } indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: $utf_sort = array(); indvdum@0: $last_cc = 0; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // This character decomposition contains an ASCII char, which is a starter. Dump the buffer and continue indvdum@0: ++$_pos; indvdum@0: indvdum@0: if ($dump) indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: // Dump combiners indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: if ($sort) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: } indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $tmp .= $c; indvdum@0: $dump = $sort = 0; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $pos - $utf_len - $tmp_pos) . $c; indvdum@0: } indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: $utf_sort = array(); indvdum@0: $last_cc = 0; indvdum@0: } indvdum@0: } indvdum@0: while ($_pos < $_len); indvdum@0: } indvdum@0: else if (isset($utf_combining_class[$utf_char])) indvdum@0: { indvdum@0: // Combining character indvdum@0: if ($utf_combining_class[$utf_char] < $last_cc) indvdum@0: { indvdum@0: // Not in canonical order indvdum@0: $sort = $dump = 1; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $last_cc = $utf_combining_class[$utf_char]; indvdum@0: } indvdum@0: indvdum@0: $utf_sort[$utf_combining_class[$utf_char]][] = $utf_char; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Non-decomposable starter, check out if it's a Hangul syllable indvdum@0: if ($utf_char < UTF8_HANGUL_FIRST || $utf_char > UTF8_HANGUL_LAST) indvdum@0: { indvdum@0: // Nope, regular UTF char, check that we have the correct number of trailing bytes indvdum@0: if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len]) indvdum@0: { indvdum@0: // Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char indvdum@0: // has been encoded in a five- or six- byte sequence. indvdum@0: // Move the cursor back to its original position then advance it to the position it should really be at indvdum@0: $pos -= $utf_len; indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: } indvdum@0: indvdum@0: // Add a replacement char then another replacement char for every trailing byte. indvdum@0: // indvdum@0: // @todo I'm not entirely sure that's how we're supposed to mark invalidated byte sequences, check this indvdum@0: $spn = strspn($str, UTF8_TRAILING_BYTES, ++$pos); indvdum@0: $tmp .= str_repeat(UTF8_REPLACEMENT, $spn + 1); indvdum@0: indvdum@0: $dump = $sort = 0; indvdum@0: indvdum@0: $pos += $spn; indvdum@0: $tmp_pos = $pos; indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: if (isset($extra_check[$utf_char[0]])) indvdum@0: { indvdum@0: switch ($utf_char[0]) indvdum@0: { indvdum@0: // Note: 0xED is quite common in Korean indvdum@0: case "\xED": indvdum@0: if ($utf_char >= "\xED\xA0\x80") indvdum@0: { indvdum@0: // Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF) indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: } indvdum@0: indvdum@0: $tmp .= UTF8_REPLACEMENT; indvdum@0: $dump = $sort = 0; indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: // Note: 0xEF is quite common in Japanese indvdum@0: case "\xEF": indvdum@0: if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF") indvdum@0: { indvdum@0: // U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF) indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: } indvdum@0: indvdum@0: $tmp .= UTF8_REPLACEMENT; indvdum@0: $dump = $sort = 0; indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case "\xC0": indvdum@0: case "\xC1": indvdum@0: if ($utf_char <= "\xC1\xBF") indvdum@0: { indvdum@0: // Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: } indvdum@0: indvdum@0: $tmp .= UTF8_REPLACEMENT; indvdum@0: $dump = $sort = 0; indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case "\xE0": indvdum@0: if ($utf_char <= "\xE0\x9F\xBF") indvdum@0: { indvdum@0: // Unicode char U+0000..U+07FF encoded in 3 bytes indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: } indvdum@0: indvdum@0: $tmp .= UTF8_REPLACEMENT; indvdum@0: $dump = $sort = 0; indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case "\xF0": indvdum@0: if ($utf_char <= "\xF0\x8F\xBF\xBF") indvdum@0: { indvdum@0: // Unicode char U+0000..U+FFFF encoded in 4 bytes indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: } indvdum@0: indvdum@0: $tmp .= UTF8_REPLACEMENT; indvdum@0: $dump = $sort = 0; indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: if ($utf_char > UTF8_MAX) indvdum@0: { indvdum@0: // Out of the Unicode range indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: $utf_sort = array(); indvdum@0: } indvdum@0: indvdum@0: $tmp .= UTF8_REPLACEMENT; indvdum@0: $dump = $sort = 0; indvdum@0: indvdum@0: $tmp_pos = $starter_pos = $pos; indvdum@0: continue 2; indvdum@0: } indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Hangul syllable indvdum@0: $idx = (((ord($utf_char[0]) & 0x0F) << 12) | ((ord($utf_char[1]) & 0x3F) << 6) | (ord($utf_char[2]) & 0x3F)) - UNICODE_HANGUL_SBASE; indvdum@0: indvdum@0: // LIndex can only range from 0 to 18, therefore it cannot influence the first two bytes of the L Jamo, which allows us to hardcode them (based on LBase). indvdum@0: // indvdum@0: // The same goes for VIndex, but for TIndex there's a catch: the value of the third byte could exceed 0xBF and we would have to increment the second byte indvdum@0: if ($t_index = $idx % UNICODE_HANGUL_TCOUNT) indvdum@0: { indvdum@0: if ($t_index < 25) indvdum@0: { indvdum@0: $utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x86\x00"; indvdum@0: $utf_char[8] = chr(0xA7 + $t_index); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x87\x00"; indvdum@0: $utf_char[8] = chr(0x67 + $t_index); indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $utf_char = "\xE1\x84\x00\xE1\x85\x00"; indvdum@0: } indvdum@0: indvdum@0: $utf_char[2] = chr(0x80 + (int) ($idx / UNICODE_HANGUL_NCOUNT)); indvdum@0: $utf_char[5] = chr(0xA1 + (int) (($idx % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT)); indvdum@0: indvdum@0: // Just like other decompositions, the resulting Jamos must be dumped to the tmp string indvdum@0: $dump = 1; indvdum@0: } indvdum@0: indvdum@0: // Do we need to dump stuff to the tmp string? indvdum@0: if ($dump) indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: // Dump combiners indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: if ($sort) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: } indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $tmp .= $utf_char; indvdum@0: $dump = $sort = 0; indvdum@0: $tmp_pos = $pos; indvdum@0: } indvdum@0: indvdum@0: $last_cc = 0; indvdum@0: $utf_sort = array(); indvdum@0: $starter_pos = $pos; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // ASCII char, which happens to be a starter (as any other ASCII char) indvdum@0: if ($dump) indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: // Dump combiners indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: if ($sort) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: } indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $tmp .= $str[$pos]; indvdum@0: $dump = $sort = 0; indvdum@0: $tmp_pos = ++$pos; indvdum@0: indvdum@0: $pos += strspn($str, UTF8_ASCII_RANGE, $pos); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $pos += strspn($str, UTF8_ASCII_RANGE, ++$pos); indvdum@0: } indvdum@0: indvdum@0: $last_cc = 0; indvdum@0: $utf_sort = array(); indvdum@0: $starter_pos = $pos; indvdum@0: } indvdum@0: } indvdum@0: while ($pos < $len); indvdum@0: indvdum@0: // Now is time to return the string indvdum@0: if ($dump) indvdum@0: { indvdum@0: $tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos); indvdum@0: indvdum@0: // Dump combiners indvdum@0: if (!empty($utf_sort)) indvdum@0: { indvdum@0: if ($sort) indvdum@0: { indvdum@0: ksort($utf_sort); indvdum@0: } indvdum@0: indvdum@0: foreach ($utf_sort as $utf_chars) indvdum@0: { indvdum@0: $tmp .= implode('', $utf_chars); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return $tmp; indvdum@0: } indvdum@0: else if ($tmp_pos) indvdum@0: { indvdum@0: // If the $tmp_pos cursor was moved then at least one character was not in normal form. Replace $str with the fixed version indvdum@0: if ($tmp_pos == $len) indvdum@0: { indvdum@0: // The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str indvdum@0: return $tmp; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // The rightmost chunk of $str has not been appended to $tmp yet indvdum@0: return $tmp . substr($str, $tmp_pos); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // The string was already in normal form indvdum@0: return $str; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: ?>