wppaste
WordPress

WP_Token_Map::read_token( string $text, int $offset = 0, $matched_token_byte_length = null, string $case_sensitivity = 'case-sensitive' ): string|null

Since
6.6.0
Source
wp-includes/class-wp-token-map.php:534
If the text starting at a given offset is a lookup key in the map, return the corresponding transformation from the map, else false.

Description

This function returns the translated string, but accepts an optional parameter $matched_token_byte_length, which communicates how many bytes long the lookup key was, if it found one. This can be used to advance a cursor in calling code if a lookup key was found.

Example:

false === $smilies->read_token( 'Not sure :?.', 0, $token_byte_length );
'πŸ˜•' === $smilies->read_token( 'Not sure :?.', 9, $token_byte_length );
2 === $token_byte_length;

Example:

while ( $at < strlen( $input ) ) {
 $next_at = strpos( $input, ':', $at );
 if ( false === $next_at ) {
 break;
 }

 $smily = $smilies->read_token( $input, $next_at, $token_byte_length );
 if ( false === $next_at ) {
 ++$at;
 continue;
 }

 $prefix = substr( $input, $at, $next_at - $at );
 $at += $token_byte_length;
 $output .= "{$prefix}{$smily}";
}

Compatibility

WordPress
since 6.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstring
String in which to search for a lookup key.
$offsetintoptional
How many bytes into the string where the lookup key ought to start. Default 0.Default: 0
$matched_token_byte_lengthoptional
Default: null
$case_sensitivitystringoptional
Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'.Default: 'case-sensitive'

Return value

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

Performance profile

How much work a call to WP_Token_Map::read_token() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
16–80

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 114.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs Β· 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Token_Map::read_token() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$text_length16none
$text_length20–45strcspn()
!$text_length23->read_small_token()
$text_length && $case_sensitivity === "ascii-case-insensitive"39–49strcspn(), stripos()
$text_length && $case_sensitivity !== "ascii-case-insensitive"42–52strcspn(), ->read_small_token()
$text_length && $case_sensitivity === "ascii-case-insensitive"46–56strcspn(), stripos(), ->read_small_token()
$text_length && $case_sensitivity !== "ascii-case-insensitive" && $group_at !== false && $at76strcspn(), unpack(), unpack(), substr_compare()
$text_length && $case_sensitivity === "ascii-case-insensitive" && $group_at !== false && $at80strcspn(), stripos(), unpack(), unpack(), substr_compare()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11216–8082 fewer instructions than PHP 8.5
8.511416–808
8.411416–80813 fewer instructions than PHP 8.3
8.312716–908
8.212716–908
8.112716–9083 fewer instructions than PHP 7.4
7.413016–928

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses Β· 2

Source code

	public function read_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string {		$ignore_case = 'ascii-case-insensitive' === $case_sensitivity;		$text_length = strlen( $text ); 		// Search for a long word first, if the text is long enough, and if that fails, a short one.		if ( $text_length > $this->key_length ) {			/*			 * Keys cannot contain null bytes, which is taken care of for the full words,			 * but here it’s required to reject group keys with null bytes so that the			 * lookup doesn’t get off track when scanning the group string.			 */			if ( strcspn( $text, "\x00", $offset, $this->key_length ) < $this->key_length ) {				return null;			} 			$group_key = substr( $text, $offset, $this->key_length );			$group_at  = $ignore_case ? stripos( $this->groups, $group_key ) : strpos( $this->groups, $group_key );			if ( false === $group_at ) {				// Perhaps a short word then.				return strlen( $this->small_words ) > 0					? $this->read_small_token( $text, $offset, $matched_token_byte_length, $case_sensitivity )					: null;			} 			$group        = $this->large_words[ $group_at / ( $this->key_length + 1 ) ];			$group_length = strlen( $group );			$at           = 0;			while ( $at < $group_length ) {				$token_length   = unpack( 'C', $group[ $at++ ] )[1];				$token          = substr( $group, $at, $token_length );				$at            += $token_length;				$mapping_length = unpack( 'C', $group[ $at++ ] )[1];				$mapping_at     = $at; 				if ( 0 === substr_compare( $text, $token, $offset + $this->key_length, $token_length, $ignore_case ) ) {					$matched_token_byte_length = $this->key_length + $token_length;					return substr( $group, $mapping_at, $mapping_length );				} 				$at = $mapping_at + $mapping_length;			}		} 		// Perhaps a short word then.		return strlen( $this->small_words ) > 0			? $this->read_small_token( $text, $offset, $matched_token_byte_length, $case_sensitivity )			: null;	}

Changelog

Introduced in 6.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-token-map.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.