Sign in Join
FPDoc Onlineexperiment

UTF8LengthFast function

In unit LazUTF8

Fast version of UTF8Length.

Declaration

pascal
function UTF8LengthFast(const s: string): PtrInt

Arguments

s

String with UTF-8-encoded values.

Function result

Number of codepoints in the string.

Description

UTF8LengthFast is an overloaded PtrInt function used to get the length of a UTF-8-encoded string in codepoints. UTF8LengthFast is the fast version of UTF8Length. It does not call the UTF8CodepointSize function. The UTF-8-encoded string data is assumed to be valid. The native data size for the CPU is used to process blocks of UTF-8-encoded data. For a 64-bit CPU, this means that 8 bytes are read and processed at once.

The overloaded variants allow the UTF-8-encoded data to be specified as either a String type, or a null-terminated PChar type. Internally, the String-based variant casts its data to a PChar type and calls the overloaded variant.

UTF8LengthFast is a Free Pascal implementation of the C routine provided by Colin Percival:

Even faster UTF-8 character counting

See also

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.