Changeset 50842 for trunk/src/wp-includes/Requests/IDNAEncoder.php
- Timestamp:
- 05/11/2021 07:40:41 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/IDNAEncoder.php
r46586 r50842 142 142 $strlen = strlen($input); 143 143 144 // phpcs:ignore Generic.CodeAnalysis.JumbledIncrementer -- This is a deliberate choice. 144 145 for ($position = 0; $position < $strlen; $position++) { 145 146 $value = ord($input[$position]); … … 148 149 if ((~$value & 0x80) === 0x80) { 149 150 $character = $value; 150 $length = 1;151 $length = 1; 151 152 $remaining = 0; 152 153 } … … 154 155 elseif (($value & 0xE0) === 0xC0) { 155 156 $character = ($value & 0x1F) << 6; 156 $length = 2;157 $length = 2; 157 158 $remaining = 1; 158 159 } … … 160 161 elseif (($value & 0xF0) === 0xE0) { 161 162 $character = ($value & 0x0F) << 12; 162 $length = 3;163 $length = 3; 163 164 $remaining = 2; 164 165 } … … 166 167 elseif (($value & 0xF8) === 0xF0) { 167 168 $character = ($value & 0x07) << 18; 168 $length = 4;169 $length = 4; 169 170 $remaining = 3; 170 171 } … … 186 187 } 187 188 188 $character |= ($value & 0x3F) << (--$remaining * 6); 189 --$remaining; 190 $character |= ($value & 0x3F) << ($remaining * 6); 189 191 } 190 192 $position--; 191 193 } 192 194 193 if ( 194 // Non-shortest form sequences are invalid 195 $length > 1 && $character <= 0x7F 195 if (// Non-shortest form sequences are invalid 196 $length > 1 && $character <= 0x7F 196 197 || $length > 2 && $character <= 0x7FF 197 198 || $length > 3 && $character <= 0xFFFF … … 202 203 || ( 203 204 // Everything else not in ucschar 204 205 $character > 0xD7FF && $character < 0xF900 205 206 || $character < 0x20 206 207 || $character > 0x7E && $character < 0xA0 … … 228 229 public static function punycode_encode($input) { 229 230 $output = ''; 230 #let n = initial_n231 // let n = initial_n 231 232 $n = self::BOOTSTRAP_INITIAL_N; 232 #let delta = 0233 // let delta = 0 233 234 $delta = 0; 234 #let bias = initial_bias235 // let bias = initial_bias 235 236 $bias = self::BOOTSTRAP_INITIAL_BIAS; 236 # let h = b = the number of basic code points in the input 237 $h = $b = 0; // see loop 238 # copy them to the output in order 237 // let h = b = the number of basic code points in the input 238 $h = 0; 239 $b = 0; // see loop 240 // copy them to the output in order 239 241 $codepoints = self::utf8_to_codepoints($input); 240 $extended = array();242 $extended = array(); 241 243 242 244 foreach ($codepoints as $char) { … … 261 263 sort($extended); 262 264 $b = $h; 263 #[copy them] followed by a delimiter if b > 0265 // [copy them] followed by a delimiter if b > 0 264 266 if (strlen($output) > 0) { 265 267 $output .= '-'; 266 268 } 267 # {if the input contains a non-basic code point < n then fail} 268 # while h < length(input) do begin 269 while ($h < count($codepoints)) { 270 # let m = the minimum code point >= n in the input 269 // {if the input contains a non-basic code point < n then fail} 270 // while h < length(input) do begin 271 $codepointcount = count($codepoints); 272 while ($h < $codepointcount) { 273 // let m = the minimum code point >= n in the input 271 274 $m = array_shift($extended); 272 275 //printf('next code point to insert is %s' . PHP_EOL, dechex($m)); 273 #let delta = delta + (m - n) * (h + 1), fail on overflow276 // let delta = delta + (m - n) * (h + 1), fail on overflow 274 277 $delta += ($m - $n) * ($h + 1); 275 #let n = m278 // let n = m 276 279 $n = $m; 277 #for each code point c in the input (in order) do begin278 for ($num = 0; $num < count($codepoints); $num++) {280 // for each code point c in the input (in order) do begin 281 for ($num = 0; $num < $codepointcount; $num++) { 279 282 $c = $codepoints[$num]; 280 #if c < n then increment delta, fail on overflow283 // if c < n then increment delta, fail on overflow 281 284 if ($c < $n) { 282 285 $delta++; 283 286 } 284 #if c == n then begin287 // if c == n then begin 285 288 elseif ($c === $n) { 286 #let q = delta289 // let q = delta 287 290 $q = $delta; 288 #for k = base to infinity in steps of base do begin291 // for k = base to infinity in steps of base do begin 289 292 for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) { 290 #let t = tmin if k <= bias {+ tmin}, or291 #tmax if k >= bias + tmax, or k - bias otherwise293 // let t = tmin if k <= bias {+ tmin}, or 294 // tmax if k >= bias + tmax, or k - bias otherwise 292 295 if ($k <= ($bias + self::BOOTSTRAP_TMIN)) { 293 296 $t = self::BOOTSTRAP_TMIN; … … 299 302 $t = $k - $bias; 300 303 } 301 #if q < t then break304 // if q < t then break 302 305 if ($q < $t) { 303 306 break; 304 307 } 305 #output the code point for digit t + ((q - t) mod (base - t))306 $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t));308 // output the code point for digit t + ((q - t) mod (base - t)) 309 $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t)); 307 310 $output .= self::digit_to_char($digit); 308 #let q = (q - t) div (base - t)311 // let q = (q - t) div (base - t) 309 312 $q = floor(($q - $t) / (self::BOOTSTRAP_BASE - $t)); 310 # end 311 } 312 # output the code point for digit q 313 } // end 314 // output the code point for digit q 313 315 $output .= self::digit_to_char($q); 314 #let bias = adapt(delta, h + 1, test h equals b?)316 // let bias = adapt(delta, h + 1, test h equals b?) 315 317 $bias = self::adapt($delta, $h + 1, $h === $b); 316 #let delta = 0318 // let delta = 0 317 319 $delta = 0; 318 #increment h320 // increment h 319 321 $h++; 320 # end 321 } 322 # end 323 } 324 # increment delta and n 322 } // end 323 } // end 324 // increment delta and n 325 325 $delta++; 326 326 $n++; 327 # end 328 } 327 } // end 329 328 330 329 return $output; … … 359 358 * @param bool $firsttime 360 359 * @return int New bias 360 * 361 * function adapt(delta,numpoints,firsttime): 361 362 */ 362 363 protected static function adapt($delta, $numpoints, $firsttime) { 363 # function adapt(delta,numpoints,firsttime): 364 # if firsttime then let delta = delta div damp 364 // if firsttime then let delta = delta div damp 365 365 if ($firsttime) { 366 366 $delta = floor($delta / self::BOOTSTRAP_DAMP); 367 367 } 368 #else let delta = delta div 2368 // else let delta = delta div 2 369 369 else { 370 370 $delta = floor($delta / 2); 371 371 } 372 #let delta = delta + (delta div numpoints)372 // let delta = delta + (delta div numpoints) 373 373 $delta += floor($delta / $numpoints); 374 #let k = 0374 // let k = 0 375 375 $k = 0; 376 #while delta > ((base - tmin) * tmax) div 2 do begin376 // while delta > ((base - tmin) * tmax) div 2 do begin 377 377 $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2); 378 378 while ($delta > $max) { 379 #let delta = delta div (base - tmin)379 // let delta = delta div (base - tmin) 380 380 $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN)); 381 #let k = k + base381 // let k = k + base 382 382 $k += self::BOOTSTRAP_BASE; 383 # end 384 } 385 # return k + (((base - tmin + 1) * delta) div (delta + skew)) 383 } // end 384 // return k + (((base - tmin + 1) * delta) div (delta + skew)) 386 385 return $k + floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN + 1) * $delta) / ($delta + self::BOOTSTRAP_SKEW)); 387 386 }
Note: See TracChangeset
for help on using the changeset viewer.