includes/utf/utf_normalizer.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Fri, 09 Mar 2012 16:14:29 +0400
changeset 13 118ae93ae3fe
parent 0 ceef6d64c21d
permissions -rw-r--r--
Added Art Air Green style
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package utf
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
*/
indvdum@0
    13
if (!defined('IN_PHPBB'))
indvdum@0
    14
{
indvdum@0
    15
	exit;
indvdum@0
    16
}
indvdum@0
    17
indvdum@0
    18
/**
indvdum@0
    19
* Some Unicode characters encoded in UTF-8
indvdum@0
    20
*
indvdum@0
    21
* Preserved for compatibility
indvdum@0
    22
*/
indvdum@0
    23
define('UTF8_REPLACEMENT', "\xEF\xBF\xBD");
indvdum@0
    24
define('UTF8_MAX', "\xF4\x8F\xBF\xBF");
indvdum@0
    25
define('UTF8_FFFE', "\xEF\xBF\xBE");
indvdum@0
    26
define('UTF8_FFFF', "\xEF\xBF\xBF");
indvdum@0
    27
define('UTF8_SURROGATE_FIRST', "\xED\xA0\x80");
indvdum@0
    28
define('UTF8_SURROGATE_LAST', "\xED\xBF\xBF");
indvdum@0
    29
define('UTF8_HANGUL_FIRST', "\xEA\xB0\x80");
indvdum@0
    30
define('UTF8_HANGUL_LAST', "\xED\x9E\xA3");
indvdum@0
    31
indvdum@0
    32
define('UTF8_CJK_FIRST', "\xE4\xB8\x80");
indvdum@0
    33
define('UTF8_CJK_LAST', "\xE9\xBE\xBB");
indvdum@0
    34
define('UTF8_CJK_B_FIRST', "\xF0\xA0\x80\x80");
indvdum@0
    35
define('UTF8_CJK_B_LAST', "\xF0\xAA\x9B\x96");
indvdum@0
    36
indvdum@0
    37
// Unset global variables
indvdum@0
    38
unset($GLOBALS['utf_jamo_index'], $GLOBALS['utf_jamo_type'], $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_combining_class'], $GLOBALS['utf_canonical_comp'], $GLOBALS['utf_canonical_decomp'], $GLOBALS['utf_nfkc_qc'], $GLOBALS['utf_compatibility_decomp']);
indvdum@0
    39
indvdum@0
    40
// NFC_QC and NFKC_QC values
indvdum@0
    41
define('UNICODE_QC_MAYBE', 0);
indvdum@0
    42
define('UNICODE_QC_NO', 1);
indvdum@0
    43
indvdum@0
    44
// Contains all the ASCII characters appearing in UTF-8, sorted by frequency
indvdum@0
    45
define('UTF8_ASCII_RANGE', "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F");
indvdum@0
    46
indvdum@0
    47
// Contains all the tail bytes that can appear in the composition of a UTF-8 char
indvdum@0
    48
define('UTF8_TRAILING_BYTES', "\xA9\xA0\xA8\x80\xAA\x99\xA7\xBB\xAB\x89\x94\x82\xB4\xA2\xAE\x83\xB0\xB9\xB8\x93\xAF\xBC\xB3\x81\xA4\xB2\x9C\xA1\xB5\xBE\xBD\xBA\x98\xAD\xB1\x84\x95\xA6\xB6\x88\x8D\x90\xB7\xBF\x92\x85\xA5\x97\x8C\x86\xA3\x8E\x9F\x8F\x87\x91\x9D\xAC\x9E\x8B\x96\x9B\x8A\x9A");
indvdum@0
    49
indvdum@0
    50
// Constants used by the Hangul [de]composition algorithms
indvdum@0
    51
define('UNICODE_HANGUL_SBASE', 0xAC00);
indvdum@0
    52
define('UNICODE_HANGUL_LBASE', 0x1100);
indvdum@0
    53
define('UNICODE_HANGUL_VBASE', 0x1161);
indvdum@0
    54
define('UNICODE_HANGUL_TBASE', 0x11A7);
indvdum@0
    55
define('UNICODE_HANGUL_SCOUNT', 11172);
indvdum@0
    56
define('UNICODE_HANGUL_LCOUNT', 19);
indvdum@0
    57
define('UNICODE_HANGUL_VCOUNT', 21);
indvdum@0
    58
define('UNICODE_HANGUL_TCOUNT', 28);
indvdum@0
    59
define('UNICODE_HANGUL_NCOUNT', 588);
indvdum@0
    60
define('UNICODE_JAMO_L', 0);
indvdum@0
    61
define('UNICODE_JAMO_V', 1);
indvdum@0
    62
define('UNICODE_JAMO_T', 2);
indvdum@0
    63
indvdum@0
    64
/**
indvdum@0
    65
* Unicode normalization routines
indvdum@0
    66
*
indvdum@0
    67
* @package utf
indvdum@0
    68
*/
indvdum@0
    69
class utf_normalizer
indvdum@0
    70
{
indvdum@0
    71
	/**
indvdum@0
    72
	* Validate, cleanup and normalize a string
indvdum@0
    73
	*
indvdum@0
    74
	* The ultimate convenience function! Clean up invalid UTF-8 sequences,
indvdum@0
    75
	* and convert to Normal Form C, canonical composition.
indvdum@0
    76
	*
indvdum@0
    77
	* @param	string	&$str	The dirty string
indvdum@0
    78
	* @return	string			The same string, all shiny and cleaned-up
indvdum@0
    79
	*/
indvdum@0
    80
	function cleanup(&$str)
indvdum@0
    81
	{
indvdum@0
    82
		// The string below is the list of all autorized characters, sorted by frequency in latin text
indvdum@0
    83
		$pos = strspn($str, "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x0D");
indvdum@0
    84
		$len = strlen($str);
indvdum@0
    85
indvdum@0
    86
		if ($pos == $len)
indvdum@0
    87
		{
indvdum@0
    88
			// ASCII strings with no special chars return immediately
indvdum@0
    89
			return;
indvdum@0
    90
		}
indvdum@0
    91
indvdum@0
    92
		// Note: we do not check for $GLOBALS['utf_canonical_decomp']. It is assumed they are always loaded together
indvdum@0
    93
		if (!isset($GLOBALS['utf_nfc_qc']))
indvdum@0
    94
		{
indvdum@0
    95
			global $phpbb_root_path, $phpEx;
indvdum@0
    96
			include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
indvdum@0
    97
		}
indvdum@0
    98
indvdum@0
    99
		if (!isset($GLOBALS['utf_canonical_decomp']))
indvdum@0
   100
		{
indvdum@0
   101
			global $phpbb_root_path, $phpEx;
indvdum@0
   102
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
indvdum@0
   103
		}
indvdum@0
   104
indvdum@0
   105
		// Replace any byte in the range 0x00..0x1F, except for \r, \n and \t
indvdum@0
   106
		// We replace those characters with a 0xFF byte, which is illegal in UTF-8 and will in turn be replaced with a UTF replacement char
indvdum@0
   107
		$str = strtr(
indvdum@0
   108
			$str,
indvdum@0
   109
			"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
indvdum@0
   110
			"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
indvdum@0
   111
		);
indvdum@0
   112
indvdum@0
   113
		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
indvdum@0
   114
	}
indvdum@0
   115
indvdum@0
   116
	/**
indvdum@0
   117
	* Validate and normalize a UTF string to NFC
indvdum@0
   118
	*
indvdum@0
   119
	* @param	string	&$str	Unchecked UTF string
indvdum@0
   120
	* @return	string			The string, validated and in normal form
indvdum@0
   121
	*/
indvdum@0
   122
	function nfc(&$str)
indvdum@0
   123
	{
indvdum@0
   124
		$pos = strspn($str, UTF8_ASCII_RANGE);
indvdum@0
   125
		$len = strlen($str);
indvdum@0
   126
indvdum@0
   127
		if ($pos == $len)
indvdum@0
   128
		{
indvdum@0
   129
			// ASCII strings return immediately
indvdum@0
   130
			return;
indvdum@0
   131
		}
indvdum@0
   132
indvdum@0
   133
		if (!isset($GLOBALS['utf_nfc_qc']))
indvdum@0
   134
		{
indvdum@0
   135
			global $phpbb_root_path, $phpEx;
indvdum@0
   136
			include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
indvdum@0
   137
		}
indvdum@0
   138
indvdum@0
   139
		if (!isset($GLOBALS['utf_canonical_decomp']))
indvdum@0
   140
		{
indvdum@0
   141
			global $phpbb_root_path, $phpEx;
indvdum@0
   142
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
indvdum@0
   143
		}
indvdum@0
   144
indvdum@0
   145
		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
indvdum@0
   146
	}
indvdum@0
   147
indvdum@0
   148
	/**
indvdum@0
   149
	* Validate and normalize a UTF string to NFKC
indvdum@0
   150
	*
indvdum@0
   151
	* @param	string	&$str	Unchecked UTF string
indvdum@0
   152
	* @return	string			The string, validated and in normal form
indvdum@0
   153
	*/
indvdum@0
   154
	function nfkc(&$str)
indvdum@0
   155
	{
indvdum@0
   156
		$pos = strspn($str, UTF8_ASCII_RANGE);
indvdum@0
   157
		$len = strlen($str);
indvdum@0
   158
indvdum@0
   159
		if ($pos == $len)
indvdum@0
   160
		{
indvdum@0
   161
			// ASCII strings return immediately
indvdum@0
   162
			return;
indvdum@0
   163
		}
indvdum@0
   164
indvdum@0
   165
		if (!isset($GLOBALS['utf_nfkc_qc']))
indvdum@0
   166
		{
indvdum@0
   167
			global $phpbb_root_path, $phpEx;
indvdum@0
   168
			include($phpbb_root_path . 'includes/utf/data/utf_nfkc_qc.' . $phpEx);
indvdum@0
   169
		}
indvdum@0
   170
indvdum@0
   171
		if (!isset($GLOBALS['utf_compatibility_decomp']))
indvdum@0
   172
		{
indvdum@0
   173
			global $phpbb_root_path, $phpEx;
indvdum@0
   174
			include($phpbb_root_path . 'includes/utf/data/utf_compatibility_decomp.' . $phpEx);
indvdum@0
   175
		}
indvdum@0
   176
indvdum@0
   177
		$str = utf_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfkc_qc'], $GLOBALS['utf_compatibility_decomp']);
indvdum@0
   178
	}
indvdum@0
   179
indvdum@0
   180
	/**
indvdum@0
   181
	* Validate and normalize a UTF string to NFD
indvdum@0
   182
	*
indvdum@0
   183
	* @param	string	&$str	Unchecked UTF string
indvdum@0
   184
	* @return	string			The string, validated and in normal form
indvdum@0
   185
	*/
indvdum@0
   186
	function nfd(&$str)
indvdum@0
   187
	{
indvdum@0
   188
		$pos = strspn($str, UTF8_ASCII_RANGE);
indvdum@0
   189
		$len = strlen($str);
indvdum@0
   190
indvdum@0
   191
		if ($pos == $len)
indvdum@0
   192
		{
indvdum@0
   193
			// ASCII strings return immediately
indvdum@0
   194
			return;
indvdum@0
   195
		}
indvdum@0
   196
indvdum@0
   197
		if (!isset($GLOBALS['utf_canonical_decomp']))
indvdum@0
   198
		{
indvdum@0
   199
			global $phpbb_root_path, $phpEx;
indvdum@0
   200
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
indvdum@0
   201
		}
indvdum@0
   202
indvdum@0
   203
		$str = utf_normalizer::decompose($str, $pos, $len, $GLOBALS['utf_canonical_decomp']);
indvdum@0
   204
	}
indvdum@0
   205
indvdum@0
   206
	/**
indvdum@0
   207
	* Validate and normalize a UTF string to NFKD
indvdum@0
   208
	*
indvdum@0
   209
	* @param	string	&$str	Unchecked UTF string
indvdum@0
   210
	* @return	string			The string, validated and in normal form
indvdum@0
   211
	*/
indvdum@0
   212
	function nfkd(&$str)
indvdum@0
   213
	{
indvdum@0
   214
		$pos = strspn($str, UTF8_ASCII_RANGE);
indvdum@0
   215
		$len = strlen($str);
indvdum@0
   216
indvdum@0
   217
		if ($pos == $len)
indvdum@0
   218
		{
indvdum@0
   219
			// ASCII strings return immediately
indvdum@0
   220
			return;
indvdum@0
   221
		}
indvdum@0
   222
indvdum@0
   223
		if (!isset($GLOBALS['utf_compatibility_decomp']))
indvdum@0
   224
		{
indvdum@0
   225
			global $phpbb_root_path, $phpEx;
indvdum@0
   226
			include($phpbb_root_path . 'includes/utf/data/utf_compatibility_decomp.' . $phpEx);
indvdum@0
   227
		}
indvdum@0
   228
indvdum@0
   229
		$str = utf_normalizer::decompose($str, $pos, $len, $GLOBALS['utf_compatibility_decomp']);
indvdum@0
   230
	}
indvdum@0
   231
indvdum@0
   232
indvdum@0
   233
	/**
indvdum@0
   234
	* Recompose a UTF string
indvdum@0
   235
	*
indvdum@0
   236
	* @param	string	$str			Unchecked UTF string
indvdum@0
   237
	* @param	integer	$pos			Position of the first UTF char (in bytes)
indvdum@0
   238
	* @param	integer	$len			Length of the string (in bytes)
indvdum@0
   239
	* @param	array	&$qc			Quick-check array, passed by reference but never modified
indvdum@0
   240
	* @param	array	&$decomp_map	Decomposition mapping, passed by reference but never modified
indvdum@0
   241
	* @return	string					The string, validated and recomposed
indvdum@0
   242
	*
indvdum@0
   243
	* @access	private
indvdum@0
   244
	*/
indvdum@0
   245
	function recompose($str, $pos, $len, &$qc, &$decomp_map)
indvdum@0
   246
	{
indvdum@0
   247
		global $utf_combining_class, $utf_canonical_comp, $utf_jamo_type, $utf_jamo_index;
indvdum@0
   248
indvdum@0
   249
		// Load some commonly-used tables
indvdum@0
   250
		if (!isset($utf_jamo_index, $utf_jamo_type, $utf_combining_class))
indvdum@0
   251
		{
indvdum@0
   252
			global $phpbb_root_path, $phpEx;
indvdum@0
   253
			include($phpbb_root_path . 'includes/utf/data/utf_normalizer_common.' . $phpEx);
indvdum@0
   254
		}
indvdum@0
   255
indvdum@0
   256
		// Load the canonical composition table
indvdum@0
   257
		if (!isset($utf_canonical_comp))
indvdum@0
   258
		{
indvdum@0
   259
			global $phpbb_root_path, $phpEx;
indvdum@0
   260
			include($phpbb_root_path . 'includes/utf/data/utf_canonical_comp.' . $phpEx);
indvdum@0
   261
		}
indvdum@0
   262
indvdum@0
   263
		// Buffer the last ASCII char before the UTF-8 stuff if applicable
indvdum@0
   264
		$tmp = '';
indvdum@0
   265
		$i = $tmp_pos = $last_cc = 0;
indvdum@0
   266
indvdum@0
   267
		$buffer = ($pos) ? array(++$i => $str[$pos - 1]) : array();
indvdum@0
   268
indvdum@0
   269
		// UTF char length array
indvdum@0
   270
		// This array is used to determine the length of a UTF character.
indvdum@0
   271
		// Be $c the result of ($str[$pos] & "\xF0") --where $str is the string we're operating on and $pos
indvdum@0
   272
		// the position of the cursor--, if $utf_len_mask[$c] does not exist, the byte is an ASCII char.
indvdum@0
   273
		// Otherwise, if $utf_len_mask[$c] is greater than 0, we have a the leading byte of a multibyte character
indvdum@0
   274
		// whose length is $utf_len_mask[$c] and if it is equal to 0, the byte is a trailing byte.
indvdum@0
   275
		$utf_len_mask = array(
indvdum@0
   276
			// Leading bytes masks
indvdum@0
   277
			"\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4,
indvdum@0
   278
			// Trailing bytes masks
indvdum@0
   279
			"\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0
indvdum@0
   280
		);
indvdum@0
   281
indvdum@0
   282
		$extra_check = array(
indvdum@0
   283
			"\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1,
indvdum@0
   284
			"\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1,
indvdum@0
   285
			"\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1
indvdum@0
   286
		);
indvdum@0
   287
indvdum@0
   288
		$utf_validation_mask = array(
indvdum@0
   289
			2	=> "\xE0\xC0",
indvdum@0
   290
			3	=> "\xF0\xC0\xC0",
indvdum@0
   291
			4	=> "\xF8\xC0\xC0\xC0"
indvdum@0
   292
		);
indvdum@0
   293
indvdum@0
   294
		$utf_validation_check = array(
indvdum@0
   295
			2	=> "\xC0\x80",
indvdum@0
   296
			3	=> "\xE0\x80\x80",
indvdum@0
   297
			4	=> "\xF0\x80\x80\x80"
indvdum@0
   298
		);
indvdum@0
   299
indvdum@0
   300
		// Main loop
indvdum@0
   301
		do
indvdum@0
   302
		{
indvdum@0
   303
			// STEP 0: Capture the current char and buffer it
indvdum@0
   304
			$c = $str[$pos];
indvdum@0
   305
			$c_mask = $c & "\xF0";
indvdum@0
   306
indvdum@0
   307
			if (isset($utf_len_mask[$c_mask]))
indvdum@0
   308
			{
indvdum@0
   309
				// Byte at $pos is either a leading byte or a missplaced trailing byte
indvdum@0
   310
				if ($utf_len = $utf_len_mask[$c_mask])
indvdum@0
   311
				{
indvdum@0
   312
					// Capture the char
indvdum@0
   313
					$buffer[++$i & 7] = $utf_char = substr($str, $pos, $utf_len);
indvdum@0
   314
indvdum@0
   315
					// Let's find out if a thorough check is needed
indvdum@0
   316
					if (isset($qc[$utf_char]))
indvdum@0
   317
					{
indvdum@0
   318
						// 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
   319
					}
indvdum@0
   320
					else if (isset($utf_combining_class[$utf_char]))
indvdum@0
   321
					{
indvdum@0
   322
						if ($utf_combining_class[$utf_char] < $last_cc)
indvdum@0
   323
						{
indvdum@0
   324
							// A combining character that is NOT canonically ordered
indvdum@0
   325
						}
indvdum@0
   326
						else
indvdum@0
   327
						{
indvdum@0
   328
							// A combining character that IS canonically ordered, skip to the next char
indvdum@0
   329
							$last_cc = $utf_combining_class[$utf_char];
indvdum@0
   330
indvdum@0
   331
							$pos += $utf_len;
indvdum@0
   332
							continue;
indvdum@0
   333
						}
indvdum@0
   334
					}
indvdum@0
   335
					else
indvdum@0
   336
					{
indvdum@0
   337
						// 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
   338
						// 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
   339
						$last_cc = 0;
indvdum@0
   340
indvdum@0
   341
						// Check that we have the correct number of trailing bytes
indvdum@0
   342
						if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len])
indvdum@0
   343
						{
indvdum@0
   344
							// Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char
indvdum@0
   345
							// has been encoded in a five- or six- byte sequence
indvdum@0
   346
							if ($utf_char[0] >= "\xF8")
indvdum@0
   347
							{
indvdum@0
   348
								if ($utf_char[0] < "\xFC")
indvdum@0
   349
								{
indvdum@0
   350
									$trailing_bytes = 4;
indvdum@0
   351
								}
indvdum@0
   352
								else if ($utf_char[0] > "\xFD")
indvdum@0
   353
								{
indvdum@0
   354
									$trailing_bytes = 0;
indvdum@0
   355
								}
indvdum@0
   356
								else
indvdum@0
   357
								{
indvdum@0
   358
									$trailing_bytes = 5;
indvdum@0
   359
								}
indvdum@0
   360
							}
indvdum@0
   361
							else
indvdum@0
   362
							{
indvdum@0
   363
								$trailing_bytes = $utf_len - 1;
indvdum@0
   364
							}
indvdum@0
   365
indvdum@0
   366
							$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
indvdum@0
   367
							$pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes);
indvdum@0
   368
							$tmp_pos = $pos;
indvdum@0
   369
indvdum@0
   370
							continue;
indvdum@0
   371
						}
indvdum@0
   372
indvdum@0
   373
						if (isset($extra_check[$c]))
indvdum@0
   374
						{
indvdum@0
   375
							switch ($c)
indvdum@0
   376
							{
indvdum@0
   377
								// Note: 0xED is quite common in Korean
indvdum@0
   378
								case "\xED":
indvdum@0
   379
									if ($utf_char >= "\xED\xA0\x80")
indvdum@0
   380
									{
indvdum@0
   381
										// Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF)
indvdum@0
   382
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
indvdum@0
   383
										$pos += $utf_len;
indvdum@0
   384
										$tmp_pos = $pos;
indvdum@0
   385
										continue 2;
indvdum@0
   386
									}
indvdum@0
   387
								break;
indvdum@0
   388
indvdum@0
   389
								// Note: 0xEF is quite common in Japanese
indvdum@0
   390
								case "\xEF":
indvdum@0
   391
									if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF")
indvdum@0
   392
									{
indvdum@0
   393
										// U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF)
indvdum@0
   394
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
indvdum@0
   395
										$pos += $utf_len;
indvdum@0
   396
										$tmp_pos = $pos;
indvdum@0
   397
										continue 2;
indvdum@0
   398
									}
indvdum@0
   399
								break;
indvdum@0
   400
indvdum@0
   401
								case "\xC0":
indvdum@0
   402
								case "\xC1":
indvdum@0
   403
									if ($utf_char <= "\xC1\xBF")
indvdum@0
   404
									{
indvdum@0
   405
										// Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char
indvdum@0
   406
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
indvdum@0
   407
										$pos += $utf_len;
indvdum@0
   408
										$tmp_pos = $pos;
indvdum@0
   409
										continue 2;
indvdum@0
   410
									}
indvdum@0
   411
								break;
indvdum@0
   412
indvdum@0
   413
								case "\xE0":
indvdum@0
   414
									if ($utf_char <= "\xE0\x9F\xBF")
indvdum@0
   415
									{
indvdum@0
   416
										// Unicode char U+0000..U+07FF encoded in 3 bytes
indvdum@0
   417
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
indvdum@0
   418
										$pos += $utf_len;
indvdum@0
   419
										$tmp_pos = $pos;
indvdum@0
   420
										continue 2;
indvdum@0
   421
									}
indvdum@0
   422
								break;
indvdum@0
   423
indvdum@0
   424
								case "\xF0":
indvdum@0
   425
									if ($utf_char <= "\xF0\x8F\xBF\xBF")
indvdum@0
   426
									{
indvdum@0
   427
										// Unicode char U+0000..U+FFFF encoded in 4 bytes
indvdum@0
   428
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
indvdum@0
   429
										$pos += $utf_len;
indvdum@0
   430
										$tmp_pos = $pos;
indvdum@0
   431
										continue 2;
indvdum@0
   432
									}
indvdum@0
   433
								break;
indvdum@0
   434
indvdum@0
   435
								default:
indvdum@0
   436
									// Five- and six- byte sequences do not need being checked for here anymore
indvdum@0
   437
									if ($utf_char > UTF8_MAX)
indvdum@0
   438
									{
indvdum@0
   439
										// Out of the Unicode range
indvdum@0
   440
										if ($utf_char[0] < "\xF8")
indvdum@0
   441
										{
indvdum@0
   442
											$trailing_bytes = 3;
indvdum@0
   443
										}
indvdum@0
   444
										else if ($utf_char[0] < "\xFC")
indvdum@0
   445
										{
indvdum@0
   446
											$trailing_bytes = 4;
indvdum@0
   447
										}
indvdum@0
   448
										else if ($utf_char[0] > "\xFD")
indvdum@0
   449
										{
indvdum@0
   450
											$trailing_bytes = 0;
indvdum@0
   451
										}
indvdum@0
   452
										else
indvdum@0
   453
										{
indvdum@0
   454
											$trailing_bytes = 5;
indvdum@0
   455
										}
indvdum@0
   456
indvdum@0
   457
										$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . UTF8_REPLACEMENT;
indvdum@0
   458
										$pos += strspn($str, UTF8_TRAILING_BYTES, ++$pos, $trailing_bytes);
indvdum@0
   459
										$tmp_pos = $pos;
indvdum@0
   460
										continue 2;
indvdum@0
   461
									}
indvdum@0
   462
								break;
indvdum@0
   463
							}
indvdum@0
   464
						}
indvdum@0
   465
indvdum@0
   466
						// The char is a valid starter, move the cursor and go on
indvdum@0
   467
						$pos += $utf_len;
indvdum@0
   468
						continue;
indvdum@0
   469
					}
indvdum@0
   470
				}
indvdum@0
   471
				else
indvdum@0
   472
				{
indvdum@0
   473
					// 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
   474
					// each of them was a Unicode replacement char
indvdum@0
   475
					$spn = strspn($str, UTF8_TRAILING_BYTES, $pos);
indvdum@0
   476
					$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn);
indvdum@0
   477
indvdum@0
   478
					$pos += $spn;
indvdum@0
   479
					$tmp_pos = $pos;
indvdum@0
   480
					continue;
indvdum@0
   481
				}
indvdum@0
   482
indvdum@0
   483
indvdum@0
   484
				// STEP 1: Decompose current char
indvdum@0
   485
indvdum@0
   486
				// We have found a character that is either:
indvdum@0
   487
				//  - in the NFC_QC/NFKC_QC list
indvdum@0
   488
				//  - a non-starter char that is not canonically ordered
indvdum@0
   489
				//
indvdum@0
   490
				// We are going to capture the shortest UTF sequence that satisfies these two conditions:
indvdum@0
   491
				//
indvdum@0
   492
				//  1 - If the sequence does not start at the begginning of the string, it must begin with a starter,
indvdum@0
   493
				// and that starter must not have the NF[K]C_QC property equal to "MAYBE"
indvdum@0
   494
				//
indvdum@0
   495
				//  2 - If the sequence does not end at the end of the string, it must end with a non-starter and be
indvdum@0
   496
				// immediately followed by a starter that is not on the QC list
indvdum@0
   497
				//
indvdum@0
   498
				$utf_seq = array();
indvdum@0
   499
				$last_cc = 0;
indvdum@0
   500
				$lpos = $pos;
indvdum@0
   501
				$pos += $utf_len;
indvdum@0
   502
indvdum@0
   503
				if (isset($decomp_map[$utf_char]))
indvdum@0
   504
				{
indvdum@0
   505
					$_pos = 0;
indvdum@0
   506
					$_len = strlen($decomp_map[$utf_char]);
indvdum@0
   507
indvdum@0
   508
					do
indvdum@0
   509
					{
indvdum@0
   510
						$_utf_len =& $utf_len_mask[$decomp_map[$utf_char][$_pos] & "\xF0"];
indvdum@0
   511
indvdum@0
   512
						if (isset($_utf_len))
indvdum@0
   513
						{
indvdum@0
   514
							$utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
indvdum@0
   515
							$_pos += $_utf_len;
indvdum@0
   516
						}
indvdum@0
   517
						else
indvdum@0
   518
						{
indvdum@0
   519
							$utf_seq[] = $decomp_map[$utf_char][$_pos];
indvdum@0
   520
							++$_pos;
indvdum@0
   521
						}
indvdum@0
   522
					}
indvdum@0
   523
					while ($_pos < $_len);
indvdum@0
   524
				}
indvdum@0
   525
				else
indvdum@0
   526
				{
indvdum@0
   527
					// The char is not decomposable
indvdum@0
   528
					$utf_seq = array($utf_char);
indvdum@0
   529
				}
indvdum@0
   530
indvdum@0
   531
indvdum@0
   532
				// STEP 2: Capture the starter
indvdum@0
   533
indvdum@0
   534
				// Check out the combining class of the first character of the UTF sequence
indvdum@0
   535
				$k = 0;
indvdum@0
   536
				if (isset($utf_combining_class[$utf_seq[0]]) || $qc[$utf_char] == UNICODE_QC_MAYBE)
indvdum@0
   537
				{
indvdum@0
   538
					// Not a starter, inspect previous characters
indvdum@0
   539
					// The last 8 characters are kept in a buffer so that we don't have to capture them everytime.
indvdum@0
   540
					// This is enough for all real-life strings but even if it wasn't, we can capture characters in backward mode,
indvdum@0
   541
					// although it is slower than this method.
indvdum@0
   542
					//
indvdum@0
   543
					// In the following loop, $j starts at the previous buffered character ($i - 1, because current character is
indvdum@0
   544
					// at offset $i) and process them in backward mode until we find a starter.
indvdum@0
   545
					//
indvdum@0
   546
					// $k is the index on each UTF character inside of our UTF sequence. At this time, $utf_seq contains one or more
indvdum@0
   547
					// characters numbered 0 to n. $k starts at 0 and for each char we prepend we pre-decrement it and for numbering
indvdum@0
   548
					$starter_found = 0;
indvdum@0
   549
					$j_min = max(1, $i - 7);
indvdum@0
   550
indvdum@0
   551
					for ($j = $i - 1; $j >= $j_min && $lpos > $tmp_pos; --$j)
indvdum@0
   552
					{
indvdum@0
   553
						$utf_char = $buffer[$j & 7];
indvdum@0
   554
						$lpos -= strlen($utf_char);
indvdum@0
   555
indvdum@0
   556
						if (isset($decomp_map[$utf_char]))
indvdum@0
   557
						{
indvdum@0
   558
							// The char is a composite, decompose for storage
indvdum@0
   559
							$decomp_seq = array();
indvdum@0
   560
							$_pos = 0;
indvdum@0
   561
							$_len = strlen($decomp_map[$utf_char]);
indvdum@0
   562
indvdum@0
   563
							do
indvdum@0
   564
							{
indvdum@0
   565
								$c = $decomp_map[$utf_char][$_pos];
indvdum@0
   566
								$_utf_len =& $utf_len_mask[$c & "\xF0"];
indvdum@0
   567
indvdum@0
   568
								if (isset($_utf_len))
indvdum@0
   569
								{
indvdum@0
   570
									$decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
indvdum@0
   571
									$_pos += $_utf_len;
indvdum@0
   572
								}
indvdum@0
   573
								else
indvdum@0
   574
								{
indvdum@0
   575
									$decomp_seq[] = $c;
indvdum@0
   576
									++$_pos;
indvdum@0
   577
								}
indvdum@0
   578
							}
indvdum@0
   579
							while ($_pos < $_len);
indvdum@0
   580
indvdum@0
   581
							// Prepend the UTF sequence with our decomposed sequence
indvdum@0
   582
							if (isset($decomp_seq[1]))
indvdum@0
   583
							{
indvdum@0
   584
								// The char expanded into several chars
indvdum@0
   585
								$decomp_cnt = sizeof($decomp_seq);
indvdum@0
   586
indvdum@0
   587
								foreach ($decomp_seq as $decomp_i => $decomp_char)
indvdum@0
   588
								{
indvdum@0
   589
									$utf_seq[$k + $decomp_i - $decomp_cnt] = $decomp_char;
indvdum@0
   590
								}
indvdum@0
   591
								$k -= $decomp_cnt;
indvdum@0
   592
							}
indvdum@0
   593
							else
indvdum@0
   594
							{
indvdum@0
   595
								// Decomposed to a single char, easier to prepend
indvdum@0
   596
								$utf_seq[--$k] = $decomp_seq[0];
indvdum@0
   597
							}
indvdum@0
   598
						}
indvdum@0
   599
						else
indvdum@0
   600
						{
indvdum@0
   601
							$utf_seq[--$k] = $utf_char;
indvdum@0
   602
						}
indvdum@0
   603
indvdum@0
   604
						if (!isset($utf_combining_class[$utf_seq[$k]]))
indvdum@0
   605
						{
indvdum@0
   606
							// We have found our starter
indvdum@0
   607
							$starter_found = 1;
indvdum@0
   608
							break;
indvdum@0
   609
						}
indvdum@0
   610
					}
indvdum@0
   611
indvdum@0
   612
					if (!$starter_found && $lpos > $tmp_pos)
indvdum@0
   613
					{
indvdum@0
   614
						// The starter was not found in the buffer, let's rewind some more
indvdum@0
   615
						do
indvdum@0
   616
						{
indvdum@0
   617
							// $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
   618
							$c = $str[--$lpos];
indvdum@0
   619
							$c_mask = $c & "\xF0";
indvdum@0
   620
indvdum@0
   621
							if (isset($utf_len_mask[$c_mask]))
indvdum@0
   622
							{
indvdum@0
   623
								// UTF byte
indvdum@0
   624
								if ($utf_len = $utf_len_mask[$c_mask])
indvdum@0
   625
								{
indvdum@0
   626
									// UTF *leading* byte
indvdum@0
   627
									$utf_char = substr($str, $lpos, $utf_len);
indvdum@0
   628
indvdum@0
   629
									if (isset($decomp_map[$utf_char]))
indvdum@0
   630
									{
indvdum@0
   631
										// Decompose the character
indvdum@0
   632
										$decomp_seq = array();
indvdum@0
   633
										$_pos = 0;
indvdum@0
   634
										$_len = strlen($decomp_map[$utf_char]);
indvdum@0
   635
indvdum@0
   636
										do
indvdum@0
   637
										{
indvdum@0
   638
											$c = $decomp_map[$utf_char][$_pos];
indvdum@0
   639
											$_utf_len =& $utf_len_mask[$c & "\xF0"];
indvdum@0
   640
indvdum@0
   641
											if (isset($_utf_len))
indvdum@0
   642
											{
indvdum@0
   643
												$decomp_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
indvdum@0
   644
												$_pos += $_utf_len;
indvdum@0
   645
											}
indvdum@0
   646
											else
indvdum@0
   647
											{
indvdum@0
   648
												$decomp_seq[] = $c;
indvdum@0
   649
												++$_pos;
indvdum@0
   650
											}
indvdum@0
   651
										}
indvdum@0
   652
										while ($_pos < $_len);
indvdum@0
   653
indvdum@0
   654
										// Prepend the UTF sequence with our decomposed sequence
indvdum@0
   655
										if (isset($decomp_seq[1]))
indvdum@0
   656
										{
indvdum@0
   657
											// The char expanded into several chars
indvdum@0
   658
											$decomp_cnt = sizeof($decomp_seq);
indvdum@0
   659
											foreach ($decomp_seq as $decomp_i => $utf_char)
indvdum@0
   660
											{
indvdum@0
   661
												$utf_seq[$k + $decomp_i - $decomp_cnt] = $utf_char;
indvdum@0
   662
											}
indvdum@0
   663
											$k -= $decomp_cnt;
indvdum@0
   664
										}
indvdum@0
   665
										else
indvdum@0
   666
										{
indvdum@0
   667
											// Decomposed to a single char, easier to prepend
indvdum@0
   668
											$utf_seq[--$k] = $decomp_seq[0];
indvdum@0
   669
										}
indvdum@0
   670
									}
indvdum@0
   671
									else
indvdum@0
   672
									{
indvdum@0
   673
										$utf_seq[--$k] = $utf_char;
indvdum@0
   674
									}
indvdum@0
   675
								}
indvdum@0
   676
							}
indvdum@0
   677
							else
indvdum@0
   678
							{
indvdum@0
   679
								// ASCII char
indvdum@0
   680
								$utf_seq[--$k] = $c;
indvdum@0
   681
							}
indvdum@0
   682
						}
indvdum@0
   683
						while ($lpos > $tmp_pos);
indvdum@0
   684
					}
indvdum@0
   685
				}
indvdum@0
   686
indvdum@0
   687
indvdum@0
   688
				// STEP 3: Capture following combining modifiers
indvdum@0
   689
indvdum@0
   690
				while ($pos < $len)
indvdum@0
   691
				{
indvdum@0
   692
					$c_mask = $str[$pos] & "\xF0";
indvdum@0
   693
indvdum@0
   694
					if (isset($utf_len_mask[$c_mask]))
indvdum@0
   695
					{
indvdum@0
   696
						if ($utf_len = $utf_len_mask[$c_mask])
indvdum@0
   697
						{
indvdum@0
   698
							$utf_char = substr($str, $pos, $utf_len);
indvdum@0
   699
						}
indvdum@0
   700
						else
indvdum@0
   701
						{
indvdum@0
   702
							// A trailing byte came out of nowhere
indvdum@0
   703
							// Trailing bytes are replaced with Unicode replacement chars, we will just ignore it for now, break out of the loop
indvdum@0
   704
							// as if it was a starter (replacement chars ARE starters) and let the next loop replace it
indvdum@0
   705
							break;
indvdum@0
   706
						}
indvdum@0
   707
indvdum@0
   708
						if (isset($utf_combining_class[$utf_char]) || isset($qc[$utf_char]))
indvdum@0
   709
						{
indvdum@0
   710
							// Combining character, add it to the sequence and move the cursor
indvdum@0
   711
							if (isset($decomp_map[$utf_char]))
indvdum@0
   712
							{
indvdum@0
   713
								// Decompose the character
indvdum@0
   714
								$_pos = 0;
indvdum@0
   715
								$_len = strlen($decomp_map[$utf_char]);
indvdum@0
   716
indvdum@0
   717
								do
indvdum@0
   718
								{
indvdum@0
   719
									$c = $decomp_map[$utf_char][$_pos];
indvdum@0
   720
									$_utf_len =& $utf_len_mask[$c & "\xF0"];
indvdum@0
   721
indvdum@0
   722
									if (isset($_utf_len))
indvdum@0
   723
									{
indvdum@0
   724
										$utf_seq[] = substr($decomp_map[$utf_char], $_pos, $_utf_len);
indvdum@0
   725
										$_pos += $_utf_len;
indvdum@0
   726
									}
indvdum@0
   727
									else
indvdum@0
   728
									{
indvdum@0
   729
										$utf_seq[] = $c;
indvdum@0
   730
										++$_pos;
indvdum@0
   731
									}
indvdum@0
   732
								}
indvdum@0
   733
								while ($_pos < $_len);
indvdum@0
   734
							}
indvdum@0
   735
							else
indvdum@0
   736
							{
indvdum@0
   737
								$utf_seq[] = $utf_char;
indvdum@0
   738
							}
indvdum@0
   739
indvdum@0
   740
							$pos += $utf_len;
indvdum@0
   741
						}
indvdum@0
   742
						else
indvdum@0
   743
						{
indvdum@0
   744
							// Combining class 0 and no QC, break out of the loop
indvdum@0
   745
							// Note: we do not know if that character is valid. If it's not, the next iteration will replace it
indvdum@0
   746
							break;
indvdum@0
   747
						}
indvdum@0
   748
					}
indvdum@0
   749
					else
indvdum@0
   750
					{
indvdum@0
   751
						// ASCII chars are starters
indvdum@0
   752
						break;
indvdum@0
   753
					}
indvdum@0
   754
				}
indvdum@0
   755
indvdum@0
   756
indvdum@0
   757
				// STEP 4: Sort and combine
indvdum@0
   758
indvdum@0
   759
				// Here we sort...
indvdum@0
   760
				$k_max = $k + sizeof($utf_seq);
indvdum@0
   761
indvdum@0
   762
				if (!$k && $k_max == 1)
indvdum@0
   763
				{
indvdum@0
   764
					// There is only one char in the UTF sequence, add it then jump to the next iteration of main loop
indvdum@0
   765
						// Note: the two commented lines below can be enabled under PHP5 for a very small performance gain in most cases
indvdum@0
   766
//						if (substr_compare($str, $utf_seq[0], $lpos, $pos - $lpos))
indvdum@0
   767
//						{
indvdum@0
   768
						$tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $utf_seq[0];
indvdum@0
   769
						$tmp_pos = $pos;
indvdum@0
   770
//						}
indvdum@0
   771
indvdum@0
   772
					continue;
indvdum@0
   773
				}
indvdum@0
   774
indvdum@0
   775
				// ...there we combine
indvdum@0
   776
				if (isset($utf_combining_class[$utf_seq[$k]]))
indvdum@0
   777
				{
indvdum@0
   778
					$starter = $nf_seq = '';
indvdum@0
   779
				}
indvdum@0
   780
				else
indvdum@0
   781
				{
indvdum@0
   782
					$starter = $utf_seq[$k++];
indvdum@0
   783
					$nf_seq = '';
indvdum@0
   784
				}
indvdum@0
   785
				$utf_sort = array();
indvdum@0
   786
indvdum@0
   787
				// 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
   788
				// at the end of the string without altering it
indvdum@0
   789
				$utf_seq[] = '';
indvdum@0
   790
indvdum@0
   791
				do
indvdum@0
   792
				{
indvdum@0
   793
					$utf_char = $utf_seq[$k++];
indvdum@0
   794
indvdum@0
   795
					if (isset($utf_combining_class[$utf_char]))
indvdum@0
   796
					{
indvdum@0
   797
						$utf_sort[$utf_combining_class[$utf_char]][] = $utf_char;
indvdum@0
   798
					}
indvdum@0
   799
					else
indvdum@0
   800
					{
indvdum@0
   801
						if (empty($utf_sort))
indvdum@0
   802
						{
indvdum@0
   803
							// No combining characters... check for a composite of the two starters
indvdum@0
   804
							if (isset($utf_canonical_comp[$starter . $utf_char]))
indvdum@0
   805
							{
indvdum@0
   806
								// Good ol' composite character
indvdum@0
   807
								$starter = $utf_canonical_comp[$starter . $utf_char];
indvdum@0
   808
							}
indvdum@0
   809
							else if (isset($utf_jamo_type[$utf_char]))
indvdum@0
   810
							{
indvdum@0
   811
								// Current char is a composable jamo
indvdum@0
   812
								if (isset($utf_jamo_type[$starter]) && $utf_jamo_type[$starter] == UNICODE_JAMO_L && $utf_jamo_type[$utf_char] == UNICODE_JAMO_V)
indvdum@0
   813
								{
indvdum@0
   814
									// 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
   815
									if (isset($utf_jamo_type[$utf_seq[$k]]) && $utf_jamo_type[$utf_seq[$k]] == UNICODE_JAMO_T)
indvdum@0
   816
									{
indvdum@0
   817
										// L+V+T jamos, combine to a LVT Hangul syllable ($k is incremented)
indvdum@0
   818
										$cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char] + $utf_jamo_index[$utf_seq[$k]];
indvdum@0
   819
										++$k;
indvdum@0
   820
									}
indvdum@0
   821
									else
indvdum@0
   822
									{
indvdum@0
   823
										// L+V jamos, combine to a LV Hangul syllable
indvdum@0
   824
										$cp = $utf_jamo_index[$starter] + $utf_jamo_index[$utf_char];
indvdum@0
   825
									}
indvdum@0
   826
indvdum@0
   827
									$starter = chr(0xE0 | ($cp >> 12)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F));
indvdum@0
   828
								}
indvdum@0
   829
								else
indvdum@0
   830
								{
indvdum@0
   831
									// Non-composable jamo, just add it to the sequence
indvdum@0
   832
									$nf_seq .= $starter;
indvdum@0
   833
									$starter = $utf_char;
indvdum@0
   834
								}
indvdum@0
   835
							}
indvdum@0
   836
							else
indvdum@0
   837
							{
indvdum@0
   838
								// No composite, just add the first starter to the sequence then continue with the other one
indvdum@0
   839
								$nf_seq .= $starter;
indvdum@0
   840
								$starter = $utf_char;
indvdum@0
   841
							}
indvdum@0
   842
						}
indvdum@0
   843
						else
indvdum@0
   844
						{
indvdum@0
   845
							ksort($utf_sort);
indvdum@0
   846
indvdum@0
   847
							// For each class of combining characters
indvdum@0
   848
							foreach ($utf_sort as $cc => $utf_chars)
indvdum@0
   849
							{
indvdum@0
   850
								$j = 0;
indvdum@0
   851
indvdum@0
   852
								do
indvdum@0
   853
								{
indvdum@0
   854
									// Look for a composite
indvdum@0
   855
									if (isset($utf_canonical_comp[$starter . $utf_chars[$j]]))
indvdum@0
   856
									{
indvdum@0
   857
										// Found a composite, replace the starter
indvdum@0
   858
										$starter = $utf_canonical_comp[$starter . $utf_chars[$j]];
indvdum@0
   859
										unset($utf_sort[$cc][$j]);
indvdum@0
   860
									}
indvdum@0
   861
									else
indvdum@0
   862
									{
indvdum@0
   863
										// No composite, all following characters in that class are blocked
indvdum@0
   864
										break;
indvdum@0
   865
									}
indvdum@0
   866
								}
indvdum@0
   867
								while (isset($utf_sort[$cc][++$j]));
indvdum@0
   868
							}
indvdum@0
   869
indvdum@0
   870
							// Add the starter to the normalized sequence, followed by non-starters in canonical order
indvdum@0
   871
							$nf_seq .= $starter;
indvdum@0
   872
indvdum@0
   873
							foreach ($utf_sort as $utf_chars)
indvdum@0
   874
							{
indvdum@0
   875
								if (!empty($utf_chars))
indvdum@0
   876
								{
indvdum@0
   877
									$nf_seq .= implode('', $utf_chars);
indvdum@0
   878
								}
indvdum@0
   879
							}
indvdum@0
   880
indvdum@0
   881
							// Reset the array and go on
indvdum@0
   882
							$utf_sort = array();
indvdum@0
   883
							$starter = $utf_char;
indvdum@0
   884
						}
indvdum@0
   885
					}
indvdum@0
   886
				}
indvdum@0
   887
				while ($k <= $k_max);
indvdum@0
   888
indvdum@0
   889
				$tmp .= substr($str, $tmp_pos, $lpos - $tmp_pos) . $nf_seq;
indvdum@0
   890
				$tmp_pos = $pos;
indvdum@0
   891
			}
indvdum@0
   892
			else
indvdum@0
   893
			{
indvdum@0
   894
				// Only a ASCII char can make the program get here
indvdum@0
   895
				//
indvdum@0
   896
				// First we skip the current byte with ++$pos, then we quickly skip following ASCII chars with strspn().
indvdum@0
   897
				//
indvdum@0
   898
				// 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
   899
				// multi-byte text (where the only ASCII chars are spaces and punctuation)
indvdum@0
   900
				if (++$pos != $len)
indvdum@0
   901
				{
indvdum@0
   902
					if ($str[$pos] < "\x80")
indvdum@0
   903
					{
indvdum@0
   904
						$pos += strspn($str, UTF8_ASCII_RANGE, ++$pos);
indvdum@0
   905
						$buffer[++$i & 7] = $str[$pos - 1];
indvdum@0
   906
					}
indvdum@0
   907
					else
indvdum@0
   908
					{
indvdum@0
   909
						$buffer[++$i & 7] = $c;
indvdum@0
   910
					}
indvdum@0
   911
				}
indvdum@0
   912
			}
indvdum@0
   913
		}
indvdum@0
   914
		while ($pos < $len);
indvdum@0
   915
indvdum@0
   916
		// Now is time to return the string
indvdum@0
   917
		if ($tmp_pos)
indvdum@0
   918
		{
indvdum@0
   919
			// 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
   920
			if ($tmp_pos == $len)
indvdum@0
   921
			{
indvdum@0
   922
				// The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str
indvdum@0
   923
				return $tmp;
indvdum@0
   924
			}
indvdum@0
   925
			else
indvdum@0
   926
			{
indvdum@0
   927
				// The rightmost chunk of $str has not been appended to $tmp yet
indvdum@0
   928
				return $tmp . substr($str, $tmp_pos);
indvdum@0
   929
			}
indvdum@0
   930
		}
indvdum@0
   931
indvdum@0
   932
		// The string was already in normal form
indvdum@0
   933
		return $str;
indvdum@0
   934
	}
indvdum@0
   935
indvdum@0
   936
	/**
indvdum@0
   937
	* Decompose a UTF string
indvdum@0
   938
	*
indvdum@0
   939
	* @param	string	$str			UTF string
indvdum@0
   940
	* @param	integer	$pos			Position of the first UTF char (in bytes)
indvdum@0
   941
	* @param	integer	$len			Length of the string (in bytes)
indvdum@0
   942
	* @param	array	&$decomp_map	Decomposition mapping, passed by reference but never modified
indvdum@0
   943
	* @return	string					The string, decomposed and sorted canonically
indvdum@0
   944
	*
indvdum@0
   945
	* @access	private
indvdum@0
   946
	*/
indvdum@0
   947
	function decompose($str, $pos, $len, &$decomp_map)
indvdum@0
   948
	{
indvdum@0
   949
		global $utf_combining_class;
indvdum@0
   950
indvdum@0
   951
		// Load some commonly-used tables
indvdum@0
   952
		if (!isset($utf_combining_class))
indvdum@0
   953
		{
indvdum@0
   954
			global $phpbb_root_path, $phpEx;
indvdum@0
   955
			include($phpbb_root_path . 'includes/utf/data/utf_normalizer_common.' . $phpEx);
indvdum@0
   956
		}
indvdum@0
   957
indvdum@0
   958
		// UTF char length array
indvdum@0
   959
		$utf_len_mask = array(
indvdum@0
   960
			// Leading bytes masks
indvdum@0
   961
			"\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4,
indvdum@0
   962
			// Trailing bytes masks
indvdum@0
   963
			"\x80" => 0, "\x90" => 0, "\xA0" => 0, "\xB0" => 0
indvdum@0
   964
		);
indvdum@0
   965
indvdum@0
   966
		// Some extra checks are triggered on the first byte of a UTF sequence
indvdum@0
   967
		$extra_check = array(
indvdum@0
   968
			"\xED" => 1, "\xEF" => 1, "\xC0" => 1, "\xC1" => 1, "\xE0" => 1, "\xF0" => 1,
indvdum@0
   969
			"\xF4" => 1, "\xF5" => 1, "\xF6" => 1, "\xF7" => 1, "\xF8" => 1, "\xF9" => 1,
indvdum@0
   970
			"\xFA" => 1, "\xFB" => 1, "\xFC" => 1, "\xFD" => 1, "\xFE" => 1, "\xFF" => 1
indvdum@0
   971
		);
indvdum@0
   972
indvdum@0
   973
		// These masks are used to check if a UTF sequence is well formed. Here are the only 3 lengths we acknowledge:
indvdum@0
   974
		//   - 2-byte: 110? ???? 10?? ????
indvdum@0
   975
		//   - 3-byte: 1110 ???? 10?? ???? 10?? ????
indvdum@0
   976
		//   - 4-byte: 1111 0??? 10?? ???? 10?? ???? 10?? ????
indvdum@0
   977
		// Note that 5- and 6- byte sequences are automatically discarded
indvdum@0
   978
		$utf_validation_mask = array(
indvdum@0
   979
			2	=> "\xE0\xC0",
indvdum@0
   980
			3	=> "\xF0\xC0\xC0",
indvdum@0
   981
			4	=> "\xF8\xC0\xC0\xC0"
indvdum@0
   982
		);
indvdum@0
   983
indvdum@0
   984
		$utf_validation_check = array(
indvdum@0
   985
			2	=> "\xC0\x80",
indvdum@0
   986
			3	=> "\xE0\x80\x80",
indvdum@0
   987
			4	=> "\xF0\x80\x80\x80"
indvdum@0
   988
		);
indvdum@0
   989
indvdum@0
   990
		$tmp = '';
indvdum@0
   991
		$starter_pos = $pos;
indvdum@0
   992
		$tmp_pos = $last_cc = $sort = $dump = 0;
indvdum@0
   993
		$utf_sort = array();
indvdum@0
   994
indvdum@0
   995
indvdum@0
   996
		// Main loop
indvdum@0
   997
		do
indvdum@0
   998
		{
indvdum@0
   999
			// STEP 0: Capture the current char
indvdum@0
  1000
indvdum@0
  1001
			$cur_mask = $str[$pos] & "\xF0";
indvdum@0
  1002
			if (isset($utf_len_mask[$cur_mask]))
indvdum@0
  1003
			{
indvdum@0
  1004
				if ($utf_len = $utf_len_mask[$cur_mask])
indvdum@0
  1005
				{
indvdum@0
  1006
					// Multibyte char
indvdum@0
  1007
					$utf_char = substr($str, $pos, $utf_len);
indvdum@0
  1008
					$pos += $utf_len;
indvdum@0
  1009
				}
indvdum@0
  1010
				else
indvdum@0
  1011
				{
indvdum@0
  1012
					// 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
  1013
					// replacement char and we will advance the cursor
indvdum@0
  1014
					$spn = strspn($str, UTF8_TRAILING_BYTES, $pos);
indvdum@0
  1015
indvdum@0
  1016
					if ($dump)
indvdum@0
  1017
					{
indvdum@0
  1018
						$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1019
indvdum@0
  1020
						// Dump combiners
indvdum@0
  1021
						if (!empty($utf_sort))
indvdum@0
  1022
						{
indvdum@0
  1023
							if ($sort)
indvdum@0
  1024
							{
indvdum@0
  1025
								ksort($utf_sort);
indvdum@0
  1026
							}
indvdum@0
  1027
indvdum@0
  1028
							foreach ($utf_sort as $utf_chars)
indvdum@0
  1029
							{
indvdum@0
  1030
								$tmp .= implode('', $utf_chars);
indvdum@0
  1031
							}
indvdum@0
  1032
						}
indvdum@0
  1033
indvdum@0
  1034
						$tmp .= str_repeat(UTF8_REPLACEMENT, $spn);
indvdum@0
  1035
						$dump = $sort = 0;
indvdum@0
  1036
					}
indvdum@0
  1037
					else
indvdum@0
  1038
					{
indvdum@0
  1039
						$tmp .= substr($str, $tmp_pos, $pos - $tmp_pos) . str_repeat(UTF8_REPLACEMENT, $spn);
indvdum@0
  1040
					}
indvdum@0
  1041
indvdum@0
  1042
					$pos += $spn;
indvdum@0
  1043
					$tmp_pos = $starter_pos = $pos;
indvdum@0
  1044
indvdum@0
  1045
					$utf_sort = array();
indvdum@0
  1046
					$last_cc = 0;
indvdum@0
  1047
indvdum@0
  1048
					continue;
indvdum@0
  1049
				}
indvdum@0
  1050
indvdum@0
  1051
indvdum@0
  1052
				// STEP 1: Decide what to do with current char
indvdum@0
  1053
indvdum@0
  1054
				// Now, in that order:
indvdum@0
  1055
				//  - check if that character is decomposable
indvdum@0
  1056
				//  - check if that character is a non-starter
indvdum@0
  1057
				//  - check if that character requires extra checks to be performed
indvdum@0
  1058
				if (isset($decomp_map[$utf_char]))
indvdum@0
  1059
				{
indvdum@0
  1060
					// Decompose the char
indvdum@0
  1061
					$_pos = 0;
indvdum@0
  1062
					$_len = strlen($decomp_map[$utf_char]);
indvdum@0
  1063
indvdum@0
  1064
					do
indvdum@0
  1065
					{
indvdum@0
  1066
						$c = $decomp_map[$utf_char][$_pos];
indvdum@0
  1067
						$_utf_len =& $utf_len_mask[$c & "\xF0"];
indvdum@0
  1068
indvdum@0
  1069
						if (isset($_utf_len))
indvdum@0
  1070
						{
indvdum@0
  1071
							$_utf_char = substr($decomp_map[$utf_char], $_pos, $_utf_len);
indvdum@0
  1072
							$_pos += $_utf_len;
indvdum@0
  1073
indvdum@0
  1074
							if (isset($utf_combining_class[$_utf_char]))
indvdum@0
  1075
							{
indvdum@0
  1076
								// The character decomposed to a non-starter, buffer it for sorting
indvdum@0
  1077
								$utf_sort[$utf_combining_class[$_utf_char]][] = $_utf_char;
indvdum@0
  1078
indvdum@0
  1079
								if ($utf_combining_class[$_utf_char] < $last_cc)
indvdum@0
  1080
								{
indvdum@0
  1081
									// Not canonically ordered, will require sorting
indvdum@0
  1082
									$sort = $dump = 1;
indvdum@0
  1083
								}
indvdum@0
  1084
								else
indvdum@0
  1085
								{
indvdum@0
  1086
									$dump = 1;
indvdum@0
  1087
									$last_cc = $utf_combining_class[$_utf_char];
indvdum@0
  1088
								}
indvdum@0
  1089
							}
indvdum@0
  1090
							else
indvdum@0
  1091
							{
indvdum@0
  1092
								// This character decomposition contains a starter, dump the buffer and continue
indvdum@0
  1093
								if ($dump)
indvdum@0
  1094
								{
indvdum@0
  1095
									$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1096
indvdum@0
  1097
									// Dump combiners
indvdum@0
  1098
									if (!empty($utf_sort))
indvdum@0
  1099
									{
indvdum@0
  1100
										if ($sort)
indvdum@0
  1101
										{
indvdum@0
  1102
											ksort($utf_sort);
indvdum@0
  1103
										}
indvdum@0
  1104
indvdum@0
  1105
										foreach ($utf_sort as $utf_chars)
indvdum@0
  1106
										{
indvdum@0
  1107
											$tmp .= implode('', $utf_chars);
indvdum@0
  1108
										}
indvdum@0
  1109
									}
indvdum@0
  1110
indvdum@0
  1111
									$tmp .= $_utf_char;
indvdum@0
  1112
									$dump = $sort = 0;
indvdum@0
  1113
								}
indvdum@0
  1114
								else
indvdum@0
  1115
								{
indvdum@0
  1116
									$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos) . $_utf_char;
indvdum@0
  1117
								}
indvdum@0
  1118
indvdum@0
  1119
								$tmp_pos = $starter_pos = $pos;
indvdum@0
  1120
								$utf_sort = array();
indvdum@0
  1121
								$last_cc = 0;
indvdum@0
  1122
							}
indvdum@0
  1123
						}
indvdum@0
  1124
						else
indvdum@0
  1125
						{
indvdum@0
  1126
							// This character decomposition contains an ASCII char, which is a starter. Dump the buffer and continue
indvdum@0
  1127
							++$_pos;
indvdum@0
  1128
indvdum@0
  1129
							if ($dump)
indvdum@0
  1130
							{
indvdum@0
  1131
								$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1132
indvdum@0
  1133
								// Dump combiners
indvdum@0
  1134
								if (!empty($utf_sort))
indvdum@0
  1135
								{
indvdum@0
  1136
									if ($sort)
indvdum@0
  1137
									{
indvdum@0
  1138
										ksort($utf_sort);
indvdum@0
  1139
									}
indvdum@0
  1140
indvdum@0
  1141
									foreach ($utf_sort as $utf_chars)
indvdum@0
  1142
									{
indvdum@0
  1143
										$tmp .= implode('', $utf_chars);
indvdum@0
  1144
									}
indvdum@0
  1145
								}
indvdum@0
  1146
indvdum@0
  1147
								$tmp .= $c;
indvdum@0
  1148
								$dump = $sort = 0;
indvdum@0
  1149
							}
indvdum@0
  1150
							else
indvdum@0
  1151
							{
indvdum@0
  1152
								$tmp .= substr($str, $tmp_pos, $pos - $utf_len - $tmp_pos) . $c;
indvdum@0
  1153
							}
indvdum@0
  1154
indvdum@0
  1155
							$tmp_pos = $starter_pos = $pos;
indvdum@0
  1156
							$utf_sort = array();
indvdum@0
  1157
							$last_cc = 0;
indvdum@0
  1158
						}
indvdum@0
  1159
					}
indvdum@0
  1160
					while ($_pos < $_len);
indvdum@0
  1161
				}
indvdum@0
  1162
				else if (isset($utf_combining_class[$utf_char]))
indvdum@0
  1163
				{
indvdum@0
  1164
					// Combining character
indvdum@0
  1165
					if ($utf_combining_class[$utf_char] < $last_cc)
indvdum@0
  1166
					{
indvdum@0
  1167
						// Not in canonical order
indvdum@0
  1168
						$sort = $dump = 1;
indvdum@0
  1169
					}
indvdum@0
  1170
					else
indvdum@0
  1171
					{
indvdum@0
  1172
						$last_cc = $utf_combining_class[$utf_char];
indvdum@0
  1173
					}
indvdum@0
  1174
indvdum@0
  1175
					$utf_sort[$utf_combining_class[$utf_char]][] = $utf_char;
indvdum@0
  1176
				}
indvdum@0
  1177
				else
indvdum@0
  1178
				{
indvdum@0
  1179
					// Non-decomposable starter, check out if it's a Hangul syllable
indvdum@0
  1180
					if ($utf_char < UTF8_HANGUL_FIRST || $utf_char > UTF8_HANGUL_LAST)
indvdum@0
  1181
					{
indvdum@0
  1182
						// Nope, regular UTF char, check that we have the correct number of trailing bytes
indvdum@0
  1183
						if (($utf_char & $utf_validation_mask[$utf_len]) != $utf_validation_check[$utf_len])
indvdum@0
  1184
						{
indvdum@0
  1185
							// Current char isn't well-formed or legal: either one or several trailing bytes are missing, or the Unicode char
indvdum@0
  1186
							// has been encoded in a five- or six- byte sequence.
indvdum@0
  1187
							// Move the cursor back to its original position then advance it to the position it should really be at
indvdum@0
  1188
							$pos -= $utf_len;
indvdum@0
  1189
							$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1190
indvdum@0
  1191
							if (!empty($utf_sort))
indvdum@0
  1192
							{
indvdum@0
  1193
								ksort($utf_sort);
indvdum@0
  1194
indvdum@0
  1195
								foreach ($utf_sort as $utf_chars)
indvdum@0
  1196
								{
indvdum@0
  1197
									$tmp .= implode('', $utf_chars);
indvdum@0
  1198
								}
indvdum@0
  1199
								$utf_sort = array();
indvdum@0
  1200
							}
indvdum@0
  1201
indvdum@0
  1202
							// Add a replacement char then another replacement char for every trailing byte.
indvdum@0
  1203
							//
indvdum@0
  1204
							// @todo I'm not entirely sure that's how we're supposed to mark invalidated byte sequences, check this
indvdum@0
  1205
							$spn = strspn($str, UTF8_TRAILING_BYTES, ++$pos);
indvdum@0
  1206
							$tmp .= str_repeat(UTF8_REPLACEMENT, $spn + 1);
indvdum@0
  1207
indvdum@0
  1208
							$dump = $sort = 0;
indvdum@0
  1209
indvdum@0
  1210
							$pos += $spn;
indvdum@0
  1211
							$tmp_pos = $pos;
indvdum@0
  1212
							continue;
indvdum@0
  1213
						}
indvdum@0
  1214
indvdum@0
  1215
						if (isset($extra_check[$utf_char[0]]))
indvdum@0
  1216
						{
indvdum@0
  1217
							switch ($utf_char[0])
indvdum@0
  1218
							{
indvdum@0
  1219
								// Note: 0xED is quite common in Korean
indvdum@0
  1220
								case "\xED":
indvdum@0
  1221
									if ($utf_char >= "\xED\xA0\x80")
indvdum@0
  1222
									{
indvdum@0
  1223
										// Surrogates (U+D800..U+DFFF) are not allowed in UTF-8 (UTF sequence 0xEDA080..0xEDBFBF)
indvdum@0
  1224
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1225
indvdum@0
  1226
										if (!empty($utf_sort))
indvdum@0
  1227
										{
indvdum@0
  1228
											ksort($utf_sort);
indvdum@0
  1229
indvdum@0
  1230
											foreach ($utf_sort as $utf_chars)
indvdum@0
  1231
											{
indvdum@0
  1232
												$tmp .= implode('', $utf_chars);
indvdum@0
  1233
											}
indvdum@0
  1234
											$utf_sort = array();
indvdum@0
  1235
										}
indvdum@0
  1236
indvdum@0
  1237
										$tmp .= UTF8_REPLACEMENT;
indvdum@0
  1238
										$dump = $sort = 0;
indvdum@0
  1239
indvdum@0
  1240
										$tmp_pos = $starter_pos = $pos;
indvdum@0
  1241
										continue 2;
indvdum@0
  1242
									}
indvdum@0
  1243
								break;
indvdum@0
  1244
indvdum@0
  1245
								// Note: 0xEF is quite common in Japanese
indvdum@0
  1246
								case "\xEF":
indvdum@0
  1247
									if ($utf_char == "\xEF\xBF\xBE" || $utf_char == "\xEF\xBF\xBF")
indvdum@0
  1248
									{
indvdum@0
  1249
										// U+FFFE and U+FFFF are explicitly disallowed (UTF sequence 0xEFBFBE..0xEFBFBF)
indvdum@0
  1250
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1251
indvdum@0
  1252
										if (!empty($utf_sort))
indvdum@0
  1253
										{
indvdum@0
  1254
											ksort($utf_sort);
indvdum@0
  1255
indvdum@0
  1256
											foreach ($utf_sort as $utf_chars)
indvdum@0
  1257
											{
indvdum@0
  1258
												$tmp .= implode('', $utf_chars);
indvdum@0
  1259
											}
indvdum@0
  1260
											$utf_sort = array();
indvdum@0
  1261
										}
indvdum@0
  1262
indvdum@0
  1263
										$tmp .= UTF8_REPLACEMENT;
indvdum@0
  1264
										$dump = $sort = 0;
indvdum@0
  1265
indvdum@0
  1266
										$tmp_pos = $starter_pos = $pos;
indvdum@0
  1267
										continue 2;
indvdum@0
  1268
									}
indvdum@0
  1269
								break;
indvdum@0
  1270
indvdum@0
  1271
								case "\xC0":
indvdum@0
  1272
								case "\xC1":
indvdum@0
  1273
									if ($utf_char <= "\xC1\xBF")
indvdum@0
  1274
									{
indvdum@0
  1275
										// Overlong sequence: Unicode char U+0000..U+007F encoded as a double-byte UTF char
indvdum@0
  1276
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1277
indvdum@0
  1278
										if (!empty($utf_sort))
indvdum@0
  1279
										{
indvdum@0
  1280
											ksort($utf_sort);
indvdum@0
  1281
indvdum@0
  1282
											foreach ($utf_sort as $utf_chars)
indvdum@0
  1283
											{
indvdum@0
  1284
												$tmp .= implode('', $utf_chars);
indvdum@0
  1285
											}
indvdum@0
  1286
											$utf_sort = array();
indvdum@0
  1287
										}
indvdum@0
  1288
indvdum@0
  1289
										$tmp .= UTF8_REPLACEMENT;
indvdum@0
  1290
										$dump = $sort = 0;
indvdum@0
  1291
indvdum@0
  1292
										$tmp_pos = $starter_pos = $pos;
indvdum@0
  1293
										continue 2;
indvdum@0
  1294
									}
indvdum@0
  1295
								break;
indvdum@0
  1296
indvdum@0
  1297
								case "\xE0":
indvdum@0
  1298
									if ($utf_char <= "\xE0\x9F\xBF")
indvdum@0
  1299
									{
indvdum@0
  1300
										// Unicode char U+0000..U+07FF encoded in 3 bytes
indvdum@0
  1301
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1302
indvdum@0
  1303
										if (!empty($utf_sort))
indvdum@0
  1304
										{
indvdum@0
  1305
											ksort($utf_sort);
indvdum@0
  1306
indvdum@0
  1307
											foreach ($utf_sort as $utf_chars)
indvdum@0
  1308
											{
indvdum@0
  1309
												$tmp .= implode('', $utf_chars);
indvdum@0
  1310
											}
indvdum@0
  1311
											$utf_sort = array();
indvdum@0
  1312
										}
indvdum@0
  1313
indvdum@0
  1314
										$tmp .= UTF8_REPLACEMENT;
indvdum@0
  1315
										$dump = $sort = 0;
indvdum@0
  1316
indvdum@0
  1317
										$tmp_pos = $starter_pos = $pos;
indvdum@0
  1318
										continue 2;
indvdum@0
  1319
									}
indvdum@0
  1320
								break;
indvdum@0
  1321
indvdum@0
  1322
								case "\xF0":
indvdum@0
  1323
									if ($utf_char <= "\xF0\x8F\xBF\xBF")
indvdum@0
  1324
									{
indvdum@0
  1325
										// Unicode char U+0000..U+FFFF encoded in 4 bytes
indvdum@0
  1326
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1327
indvdum@0
  1328
										if (!empty($utf_sort))
indvdum@0
  1329
										{
indvdum@0
  1330
											ksort($utf_sort);
indvdum@0
  1331
indvdum@0
  1332
											foreach ($utf_sort as $utf_chars)
indvdum@0
  1333
											{
indvdum@0
  1334
												$tmp .= implode('', $utf_chars);
indvdum@0
  1335
											}
indvdum@0
  1336
											$utf_sort = array();
indvdum@0
  1337
										}
indvdum@0
  1338
indvdum@0
  1339
										$tmp .= UTF8_REPLACEMENT;
indvdum@0
  1340
										$dump = $sort = 0;
indvdum@0
  1341
indvdum@0
  1342
										$tmp_pos = $starter_pos = $pos;
indvdum@0
  1343
										continue 2;
indvdum@0
  1344
									}
indvdum@0
  1345
								break;
indvdum@0
  1346
indvdum@0
  1347
								default:
indvdum@0
  1348
									if ($utf_char > UTF8_MAX)
indvdum@0
  1349
									{
indvdum@0
  1350
										// Out of the Unicode range
indvdum@0
  1351
										$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1352
indvdum@0
  1353
										if (!empty($utf_sort))
indvdum@0
  1354
										{
indvdum@0
  1355
											ksort($utf_sort);
indvdum@0
  1356
indvdum@0
  1357
											foreach ($utf_sort as $utf_chars)
indvdum@0
  1358
											{
indvdum@0
  1359
												$tmp .= implode('', $utf_chars);
indvdum@0
  1360
											}
indvdum@0
  1361
											$utf_sort = array();
indvdum@0
  1362
										}
indvdum@0
  1363
indvdum@0
  1364
										$tmp .= UTF8_REPLACEMENT;
indvdum@0
  1365
										$dump = $sort = 0;
indvdum@0
  1366
indvdum@0
  1367
										$tmp_pos = $starter_pos = $pos;
indvdum@0
  1368
										continue 2;
indvdum@0
  1369
									}
indvdum@0
  1370
								break;
indvdum@0
  1371
							}
indvdum@0
  1372
						}
indvdum@0
  1373
					}
indvdum@0
  1374
					else
indvdum@0
  1375
					{
indvdum@0
  1376
						// Hangul syllable
indvdum@0
  1377
						$idx = (((ord($utf_char[0]) & 0x0F) << 12) | ((ord($utf_char[1]) & 0x3F) << 6) | (ord($utf_char[2]) & 0x3F)) - UNICODE_HANGUL_SBASE;
indvdum@0
  1378
indvdum@0
  1379
						// 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
  1380
						//
indvdum@0
  1381
						// 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
  1382
						if ($t_index = $idx % UNICODE_HANGUL_TCOUNT)
indvdum@0
  1383
						{
indvdum@0
  1384
							if ($t_index < 25)
indvdum@0
  1385
							{
indvdum@0
  1386
								$utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x86\x00";
indvdum@0
  1387
								$utf_char[8] = chr(0xA7 + $t_index);
indvdum@0
  1388
							}
indvdum@0
  1389
							else
indvdum@0
  1390
							{
indvdum@0
  1391
								$utf_char = "\xE1\x84\x00\xE1\x85\x00\xE1\x87\x00";
indvdum@0
  1392
								$utf_char[8] = chr(0x67 + $t_index);
indvdum@0
  1393
							}
indvdum@0
  1394
						}
indvdum@0
  1395
						else
indvdum@0
  1396
						{
indvdum@0
  1397
							$utf_char = "\xE1\x84\x00\xE1\x85\x00";
indvdum@0
  1398
						}
indvdum@0
  1399
indvdum@0
  1400
						$utf_char[2] = chr(0x80 + (int) ($idx / UNICODE_HANGUL_NCOUNT));
indvdum@0
  1401
						$utf_char[5] = chr(0xA1 + (int) (($idx % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT));
indvdum@0
  1402
indvdum@0
  1403
						// Just like other decompositions, the resulting Jamos must be dumped to the tmp string
indvdum@0
  1404
						$dump = 1;
indvdum@0
  1405
					}
indvdum@0
  1406
indvdum@0
  1407
					// Do we need to dump stuff to the tmp string?
indvdum@0
  1408
					if ($dump)
indvdum@0
  1409
					{
indvdum@0
  1410
						$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1411
indvdum@0
  1412
						// Dump combiners
indvdum@0
  1413
						if (!empty($utf_sort))
indvdum@0
  1414
						{
indvdum@0
  1415
							if ($sort)
indvdum@0
  1416
							{
indvdum@0
  1417
								ksort($utf_sort);
indvdum@0
  1418
							}
indvdum@0
  1419
indvdum@0
  1420
							foreach ($utf_sort as $utf_chars)
indvdum@0
  1421
							{
indvdum@0
  1422
								$tmp .= implode('', $utf_chars);
indvdum@0
  1423
							}
indvdum@0
  1424
						}
indvdum@0
  1425
indvdum@0
  1426
						$tmp .= $utf_char;
indvdum@0
  1427
						$dump = $sort = 0;
indvdum@0
  1428
						$tmp_pos = $pos;
indvdum@0
  1429
					}
indvdum@0
  1430
indvdum@0
  1431
					$last_cc = 0;
indvdum@0
  1432
					$utf_sort = array();
indvdum@0
  1433
					$starter_pos = $pos;
indvdum@0
  1434
				}
indvdum@0
  1435
			}
indvdum@0
  1436
			else
indvdum@0
  1437
			{
indvdum@0
  1438
				// ASCII char, which happens to be a starter (as any other ASCII char)
indvdum@0
  1439
				if ($dump)
indvdum@0
  1440
				{
indvdum@0
  1441
					$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1442
indvdum@0
  1443
					// Dump combiners
indvdum@0
  1444
					if (!empty($utf_sort))
indvdum@0
  1445
					{
indvdum@0
  1446
						if ($sort)
indvdum@0
  1447
						{
indvdum@0
  1448
							ksort($utf_sort);
indvdum@0
  1449
						}
indvdum@0
  1450
indvdum@0
  1451
						foreach ($utf_sort as $utf_chars)
indvdum@0
  1452
						{
indvdum@0
  1453
							$tmp .= implode('', $utf_chars);
indvdum@0
  1454
						}
indvdum@0
  1455
					}
indvdum@0
  1456
indvdum@0
  1457
					$tmp .= $str[$pos];
indvdum@0
  1458
					$dump = $sort = 0;
indvdum@0
  1459
					$tmp_pos = ++$pos;
indvdum@0
  1460
indvdum@0
  1461
					$pos += strspn($str, UTF8_ASCII_RANGE, $pos);
indvdum@0
  1462
				}
indvdum@0
  1463
				else
indvdum@0
  1464
				{
indvdum@0
  1465
					$pos += strspn($str, UTF8_ASCII_RANGE, ++$pos);
indvdum@0
  1466
				}
indvdum@0
  1467
indvdum@0
  1468
				$last_cc = 0;
indvdum@0
  1469
				$utf_sort = array();
indvdum@0
  1470
				$starter_pos = $pos;
indvdum@0
  1471
			}
indvdum@0
  1472
		}
indvdum@0
  1473
		while ($pos < $len);
indvdum@0
  1474
indvdum@0
  1475
		// Now is time to return the string
indvdum@0
  1476
		if ($dump)
indvdum@0
  1477
		{
indvdum@0
  1478
			$tmp .= substr($str, $tmp_pos, $starter_pos - $tmp_pos);
indvdum@0
  1479
indvdum@0
  1480
			// Dump combiners
indvdum@0
  1481
			if (!empty($utf_sort))
indvdum@0
  1482
			{
indvdum@0
  1483
				if ($sort)
indvdum@0
  1484
				{
indvdum@0
  1485
					ksort($utf_sort);
indvdum@0
  1486
				}
indvdum@0
  1487
indvdum@0
  1488
				foreach ($utf_sort as $utf_chars)
indvdum@0
  1489
				{
indvdum@0
  1490
					$tmp .= implode('', $utf_chars);
indvdum@0
  1491
				}
indvdum@0
  1492
			}
indvdum@0
  1493
indvdum@0
  1494
			return $tmp;
indvdum@0
  1495
		}
indvdum@0
  1496
		else if ($tmp_pos)
indvdum@0
  1497
		{
indvdum@0
  1498
			// 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
  1499
			if ($tmp_pos == $len)
indvdum@0
  1500
			{
indvdum@0
  1501
				// The $tmp_pos cursor is at the end of $str, therefore $tmp holds the whole $str
indvdum@0
  1502
				return $tmp;
indvdum@0
  1503
			}
indvdum@0
  1504
			else
indvdum@0
  1505
			{
indvdum@0
  1506
				// The rightmost chunk of $str has not been appended to $tmp yet
indvdum@0
  1507
				return $tmp . substr($str, $tmp_pos);
indvdum@0
  1508
			}
indvdum@0
  1509
		}
indvdum@0
  1510
indvdum@0
  1511
		// The string was already in normal form
indvdum@0
  1512
		return $str;
indvdum@0
  1513
	}
indvdum@0
  1514
}
indvdum@0
  1515
indvdum@0
  1516
?>