Iterate over string android codepoints
Under Linux and BSD you can create a filename with both encodings. For example the filename 'ä.txt' can be encoded in Unicode with two different sequences (#$C3#$A4 and 'a'#$CC#$88). The file functions of the FileUtil unit also take care of macOS specific behaviour: macOS normalizes filenames. It is not specific to any encoding but Unicode in general.
This is not automatically handled by the RTL. one of the string contains decomposed characters, while the other uses the direct codes for the same letter. procedure IterateUTF8Codepoints ( const AnUTF8String : string ) var p : PChar unicode : Cardinal CPLen : integer begin p := PChar ( AnUTF8String ) repeat unicode := UTF8CodepointToUnicode ( p, CPLen ) writeln ( 'Unicode=', unicode ) inc ( p, CPLen ) until ( CPLen = 0 ) or ( unicode = 0 ) end Decomposed charactersĭue to the ambiguity of Unicode, compare functions and Pos() might show unexpected behavior when e.g. Searching for a valid UTF-8 string with Pos will always return a valid UTF-8 byte position:
ITERATE OVER STRING ANDROID CODEPOINTS CODE
For UTF-16 there is plenty of sloppy code which assumes codepoints to be fixed width. Code that deals with codepoints must always be done right with UTF-8 because multi-byte codepoints are common. (D800 range signals first surrogate, DC00 range signals second part of surrogate) Note that similar integrity features are also exists in UTF-16. This allows using the old fast string functions like Pos() and Copy() in many situations. A byte at a certain position in a multi-byte sequence can never be confused with the other bytes.You can always find the start of a multi-byte codepoint even if you jumped to a random byte position.
The integrity of multi-byte data can be verified from the number of '1'-bits at the beginning of each byte.However that backwards compatibility does not extend to code, since code has to be recrafted to avoid mangling utf8 strings. ASCII is also used in markup language tags and other metadata which gives UTF-8 an advantage with any language. It is backwards compatible with ASCII and produces compact data for western languages.The design of UTF-8 has some benefits over other encodings :