278 * @param int $key_length Determines the group key length. Leave at the default value |
278 * @param int $key_length Determines the group key length. Leave at the default value |
279 * of 2 unless there's an empirical reason to change it. |
279 * of 2 unless there's an empirical reason to change it. |
280 * |
280 * |
281 * @return WP_Token_Map|null Token map, unless unable to create it. |
281 * @return WP_Token_Map|null Token map, unless unable to create it. |
282 */ |
282 */ |
283 public static function from_array( $mappings, $key_length = 2 ) { |
283 public static function from_array( array $mappings, int $key_length = 2 ): ?WP_Token_Map { |
284 $map = new WP_Token_Map(); |
284 $map = new WP_Token_Map(); |
285 $map->key_length = $key_length; |
285 $map->key_length = $key_length; |
286 |
286 |
287 // Start by grouping words. |
287 // Start by grouping words. |
288 |
288 |
326 */ |
326 */ |
327 usort( $shorts, 'WP_Token_Map::longest_first_then_alphabetical' ); |
327 usort( $shorts, 'WP_Token_Map::longest_first_then_alphabetical' ); |
328 foreach ( $groups as $group_key => $group ) { |
328 foreach ( $groups as $group_key => $group ) { |
329 usort( |
329 usort( |
330 $groups[ $group_key ], |
330 $groups[ $group_key ], |
331 static function ( $a, $b ) { |
331 static function ( array $a, array $b ): int { |
332 return self::longest_first_then_alphabetical( $a[0], $b[0] ); |
332 return self::longest_first_then_alphabetical( $a[0], $b[0] ); |
333 } |
333 } |
334 ); |
334 ); |
335 } |
335 } |
336 |
336 |
383 * @type array $small_mappings Small word mappings. |
383 * @type array $small_mappings Small word mappings. |
384 * } |
384 * } |
385 * |
385 * |
386 * @return WP_Token_Map Map with precomputed data loaded. |
386 * @return WP_Token_Map Map with precomputed data loaded. |
387 */ |
387 */ |
388 public static function from_precomputed_table( $state ) { |
388 public static function from_precomputed_table( $state ): ?WP_Token_Map { |
389 $has_necessary_state = isset( |
389 $has_necessary_state = isset( |
390 $state['storage_version'], |
390 $state['storage_version'], |
391 $state['key_length'], |
391 $state['key_length'], |
392 $state['groups'], |
392 $state['groups'], |
393 $state['large_words'], |
393 $state['large_words'], |
437 * |
437 * |
438 * @param string $word Determine if this word is a lookup key in the map. |
438 * @param string $word Determine if this word is a lookup key in the map. |
439 * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. |
439 * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. |
440 * @return bool Whether there's an entry for the given word in the map. |
440 * @return bool Whether there's an entry for the given word in the map. |
441 */ |
441 */ |
442 public function contains( $word, $case_sensitivity = 'case-sensitive' ) { |
442 public function contains( string $word, string $case_sensitivity = 'case-sensitive' ): bool { |
443 $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; |
443 $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; |
444 |
444 |
445 if ( $this->key_length >= strlen( $word ) ) { |
445 if ( $this->key_length >= strlen( $word ) ) { |
446 if ( 0 === strlen( $this->small_words ) ) { |
446 if ( 0 === strlen( $this->small_words ) ) { |
447 return false; |
447 return false; |
518 * $output .= "{$prefix}{$smily}"; |
518 * $output .= "{$prefix}{$smily}"; |
519 * } |
519 * } |
520 * |
520 * |
521 * @since 6.6.0 |
521 * @since 6.6.0 |
522 * |
522 * |
523 * @param string $text String in which to search for a lookup key. |
523 * @param string $text String in which to search for a lookup key. |
524 * @param int $offset Optional. How many bytes into the string where the lookup key ought to start. Default 0. |
524 * @param int $offset Optional. How many bytes into the string where the lookup key ought to start. Default 0. |
525 * @param ?int &$matched_token_byte_length Optional. Holds byte-length of found token matched, otherwise not set. Default null. |
525 * @param int|null &$matched_token_byte_length Optional. Holds byte-length of found token matched, otherwise not set. Default null. |
526 * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. |
526 * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. |
|
527 * |
527 * @return string|null Mapped value of lookup key if found, otherwise `null`. |
528 * @return string|null Mapped value of lookup key if found, otherwise `null`. |
528 */ |
529 */ |
529 public function read_token( $text, $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ) { |
530 public function read_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string { |
530 $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; |
531 $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; |
531 $text_length = strlen( $text ); |
532 $text_length = strlen( $text ); |
532 |
533 |
533 // Search for a long word first, if the text is long enough, and if that fails, a short one. |
534 // Search for a long word first, if the text is long enough, and if that fails, a short one. |
534 if ( $text_length > $this->key_length ) { |
535 if ( $text_length > $this->key_length ) { |
568 } |
569 } |
569 |
570 |
570 /** |
571 /** |
571 * Finds a match for a short word at the index. |
572 * Finds a match for a short word at the index. |
572 * |
573 * |
573 * @since 6.6.0. |
574 * @since 6.6.0 |
574 * |
575 * |
575 * @param string $text String in which to search for a lookup key. |
576 * @param string $text String in which to search for a lookup key. |
576 * @param int $offset Optional. How many bytes into the string where the lookup key ought to start. Default 0. |
577 * @param int $offset Optional. How many bytes into the string where the lookup key ought to start. Default 0. |
577 * @param ?int &$matched_token_byte_length Optional. Holds byte-length of found lookup key if matched, otherwise not set. Default null. |
578 * @param int|null &$matched_token_byte_length Optional. Holds byte-length of found lookup key if matched, otherwise not set. Default null. |
578 * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. |
579 * @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'. |
|
580 * |
579 * @return string|null Mapped value of lookup key if found, otherwise `null`. |
581 * @return string|null Mapped value of lookup key if found, otherwise `null`. |
580 */ |
582 */ |
581 private function read_small_token( $text, $offset, &$matched_token_byte_length, $case_sensitivity = 'case-sensitive' ) { |
583 private function read_small_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string { |
582 $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; |
584 $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; |
583 $small_length = strlen( $this->small_words ); |
585 $small_length = strlen( $this->small_words ); |
584 $search_text = substr( $text, $offset, $this->key_length ); |
586 $search_text = substr( $text, $offset, $this->key_length ); |
585 if ( $ignore_case ) { |
587 if ( $ignore_case ) { |
586 $search_text = strtoupper( $search_text ); |
588 $search_text = strtoupper( $search_text ); |
693 * @since 6.6.0 |
695 * @since 6.6.0 |
694 * |
696 * |
695 * @param string $indent Optional. Use this string for indentation, or rely on the default horizontal tab character. Default "\t". |
697 * @param string $indent Optional. Use this string for indentation, or rely on the default horizontal tab character. Default "\t". |
696 * @return string Value which can be pasted into a PHP source file for quick loading of table. |
698 * @return string Value which can be pasted into a PHP source file for quick loading of table. |
697 */ |
699 */ |
698 public function precomputed_php_source_table( $indent = "\t" ) { |
700 public function precomputed_php_source_table( string $indent = "\t" ): string { |
699 $i1 = $indent; |
701 $i1 = $indent; |
700 $i2 = $i1 . $indent; |
702 $i2 = $i1 . $indent; |
701 $i3 = $i2 . $indent; |
703 $i3 = $i2 . $indent; |
702 |
704 |
703 $class_version = self::STORAGE_VERSION; |
705 $class_version = self::STORAGE_VERSION; |
798 * |
800 * |
799 * @param string $a First string to compare. |
801 * @param string $a First string to compare. |
800 * @param string $b Second string to compare. |
802 * @param string $b Second string to compare. |
801 * @return int -1 or lower if `$a` is less than `$b`; 1 or greater if `$a` is greater than `$b`, and 0 if they are equal. |
803 * @return int -1 or lower if `$a` is less than `$b`; 1 or greater if `$a` is greater than `$b`, and 0 if they are equal. |
802 */ |
804 */ |
803 private static function longest_first_then_alphabetical( $a, $b ) { |
805 private static function longest_first_then_alphabetical( string $a, string $b ): int { |
804 if ( $a === $b ) { |
806 if ( $a === $b ) { |
805 return 0; |
807 return 0; |
806 } |
808 } |
807 |
809 |
808 $length_a = strlen( $a ); |
810 $length_a = strlen( $a ); |