WP_Token_Map::read_small_token()privateWP 6.6.0

Finds a match for a short word at the index.

Method of the class: WP_Token_Map{}

No Hooks.

Return

String|null. Mapped value of lookup key if found, otherwise null.

Usage

// private - for code of main (parent) class only
$result = $this->read_small_token( $text, $offset, $matched_token_byte_length, $case_sensitivity ): ?string;
$text(string) (required)
String in which to search for a lookup key.
$offset(int)
How many bytes into the string where the lookup key ought to start.
$matched_token_byte_length (passed by reference — &)
-
Default: null
$case_sensitivity(string)
Pass 'ascii-case-insensitive' to ignore ASCII case when matching.
Default: 'case-sensitive'

Changelog

Since 6.6.0 Introduced.

WP_Token_Map::read_small_token() code WP 6.8

private function read_small_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string {
	$ignore_case  = 'ascii-case-insensitive' === $case_sensitivity;
	$small_length = strlen( $this->small_words );
	$search_text  = substr( $text, $offset, $this->key_length );
	if ( $ignore_case ) {
		$search_text = strtoupper( $search_text );
	}
	$starting_char = $search_text[0];

	$at = 0;
	while ( $at < $small_length ) {
		if (
			$starting_char !== $this->small_words[ $at ] &&
			( ! $ignore_case || strtoupper( $this->small_words[ $at ] ) !== $starting_char )
		) {
			$at += $this->key_length + 1;
			continue;
		}

		for ( $adjust = 1; $adjust < $this->key_length; $adjust++ ) {
			if ( "\x00" === $this->small_words[ $at + $adjust ] ) {
				$matched_token_byte_length = $adjust;
				return $this->small_mappings[ $at / ( $this->key_length + 1 ) ];
			}

			if (
				$search_text[ $adjust ] !== $this->small_words[ $at + $adjust ] &&
				( ! $ignore_case || strtoupper( $this->small_words[ $at + $adjust ] !== $search_text[ $adjust ] ) )
			) {
				$at += $this->key_length + 1;
				continue 2;
			}
		}

		$matched_token_byte_length = $adjust;
		return $this->small_mappings[ $at / ( $this->key_length + 1 ) ];
	}

	return null;
}