wppaste
WordPress

_wp_utf8_codepoint_span( string $text, int $byte_offset, int $max_code_points, ?int $found_code_points = 0 ): int

Since
6.9.0
Source
wp-includes/compat-utf8.php:381
Given a starting offset within a string and a maximum number of code points, return how many bytes are occupied by the span of characters.

Description

Invalid spans of bytes count as a single code point according to the maximal subpart rule. This function is a fallback method for calling strlen( mb_substr( substr( $text, $at ), 0, $max_code_points ) ).

Compatibility

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

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstring
Count bytes of span in this text.
$byte_offsetint
Start counting at this byte offset.
$max_code_pointsint
Stop counting after this many code points have been seen, or at the end of the string.
$found_code_points?intoptional
Will be set to number of found code points in span, as this might be smaller than the maximum count if the string is not long enough.Default: 0

Return value

int
Number of bytes spanned by the code points.

Performance profile

How much work a call to _wp_utf8_codepoint_span() 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
14–16

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always14–16none

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 33 instructions, 14–16 executed per call, 4 branches. The work does not change between versions.

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 · 1

  • _wp_scan_utf8()Finds spans of valid and invalid UTF-8 bytes in a given string.

Used by · 1

Source code

function _wp_utf8_codepoint_span( string $text, int $byte_offset, int $max_code_points, ?int &$found_code_points = 0 ): int {	$was_at            = $byte_offset;	$invalid_length    = 0;	$end               = strlen( $text );	$found_code_points = 0; 	while ( $byte_offset < $end && $found_code_points < $max_code_points ) {		$needed      = $max_code_points - $found_code_points;		$chunk_count = _wp_scan_utf8( $text, $byte_offset, $invalid_length, null, $needed ); 		$found_code_points += $chunk_count; 		// Invalid spans only convey one code point count regardless of how long they are.		if ( 0 !== $invalid_length && $found_code_points < $max_code_points ) {			++$found_code_points;			$byte_offset += $invalid_length;		}	} 	return $byte_offset - $was_at;}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/compat-utf8.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.