WP_Token_Map::contains()publicWP 6.6.0

Indicates if a given word is a lookup key in the map.

Example:

true  === $smilies->contains( ':)' );
false === $smilies->contains( 'simile' );

Method of the class: WP_Token_Map{}

No Hooks.

Return

true|false. Whether there's an entry for the given word in the map.

Usage

$WP_Token_Map = new WP_Token_Map();
$WP_Token_Map->contains( $word, $case_sensitivity ): bool;
$word(string) (required)
Determine if this word is a lookup key in the map.
$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::contains() code WP 6.7.1

public function contains( string $word, string $case_sensitivity = 'case-sensitive' ): bool {
	$ignore_case = 'ascii-case-insensitive' === $case_sensitivity;

	if ( $this->key_length >= strlen( $word ) ) {
		if ( 0 === strlen( $this->small_words ) ) {
			return false;
		}

		$term    = str_pad( $word, $this->key_length + 1, "\x00", STR_PAD_RIGHT );
		$word_at = $ignore_case ? stripos( $this->small_words, $term ) : strpos( $this->small_words, $term );
		if ( false === $word_at ) {
			return false;
		}

		return true;
	}

	$group_key = substr( $word, 0, $this->key_length );
	$group_at  = $ignore_case ? stripos( $this->groups, $group_key ) : strpos( $this->groups, $group_key );
	if ( false === $group_at ) {
		return false;
	}
	$group        = $this->large_words[ $group_at / ( $this->key_length + 1 ) ];
	$group_length = strlen( $group );
	$slug         = substr( $word, $this->key_length );
	$length       = strlen( $slug );
	$at           = 0;

	while ( $at < $group_length ) {
		$token_length   = unpack( 'C', $group[ $at++ ] )[1];
		$token_at       = $at;
		$at            += $token_length;
		$mapping_length = unpack( 'C', $group[ $at++ ] )[1];
		$mapping_at     = $at;

		if ( $token_length === $length && 0 === substr_compare( $group, $slug, $token_at, $token_length, $ignore_case ) ) {
			return true;
		}

		$at = $mapping_at + $mapping_length;
	}

	return false;
}