| 1 |
{ |
| 2 |
Copyright 2014-2024 Michalis Kamburelis, |
| 3 |
parts based on LazUTF8 unit copyright by Lazarus developers. |
| 4 |
Parts of this source code are based on Lazarus LazUTF8 source code, |
| 5 |
but no worries --- Lazarus license is exactly the same as Castle Game Engine :) |
| 6 |
|
| 7 |
This file is part of "Castle Game Engine". |
| 8 |
|
| 9 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 10 |
included in this distribution, for details about the copyright. |
| 11 |
|
| 12 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
|
| 16 |
---------------------------------------------------------------------------- |
| 17 |
} |
| 18 |
|
| 19 |
{ Unicode utilities. } |
| 20 |
unit CastleUnicode; |
| 21 |
|
| 22 |
{$I castleconf.inc} |
| 23 |
|
| 24 |
interface |
| 25 |
|
| 26 |
uses CastleUtils, CastleStringUtils; |
| 27 |
|
| 28 |
type |
| 29 |
TUnicodeChar = Cardinal; |
| 30 |
|
| 31 |
TUnicodeCharList = class(TCardinalList) |
| 32 |
public |
| 33 |
{ Add a single Unicode character. |
| 34 |
Doesn't add duplicates (contrary to ancestor Add). } |
| 35 |
procedure Add(const C: TUnicodeChar); reintroduce; overload; |
| 36 |
|
| 37 |
{ Add all characters from SampleText. |
| 38 |
Useful to fill TUnicodeCharList |
| 39 |
when you have a sample text of international letters. |
| 40 |
Doesn't add duplicates. } |
| 41 |
procedure Add(const SampleText: string); overload; |
| 42 |
|
| 43 |
{ Add all characters from given set. Try e.g. SimpleAsciiCharacters. |
| 44 |
Doesn't add duplicates. } |
| 45 |
procedure Add(const Characters: TSetOfChars); overload; |
| 46 |
|
| 47 |
{ Express all characters inside as one string. |
| 48 |
|
| 49 |
The resulting String follows the encoding conventions used throughout CGE, |
| 50 |
which means it will be UTF-8 with FPC (AnsiString) |
| 51 |
or UTF-16 with Delphi (UnicodeString). |
| 52 |
See https://castle-engine.io/coding_conventions#strings_unicode . } |
| 53 |
function ToString: String; override; |
| 54 |
end; |
| 55 |
|
| 56 |
{ Length of the string, in Unicode characters. |
| 57 |
|
| 58 |
This is like standard Pascal @code(Length), but safe for Unicode, and working with |
| 59 |
both FPC and Delphi default String (see https://castle-engine.io/coding_conventions#strings_unicode ). |
| 60 |
|
| 61 |
This works taking into account that: |
| 62 |
@unorderedList( |
| 63 |
@item(with FPC, we expect String = AnsiString and holding UTF-8 data,) |
| 64 |
@item(with Delphi we expect String = UnicodeString and holding UTF-16 data.) |
| 65 |
) |
| 66 |
See https://castle-engine.io/coding_conventions#strings_unicode . } |
| 67 |
function StringLength(const S: String): Integer; |
| 68 |
|
| 69 |
{ Copy a number of Unicode characters from given string, from given position. |
| 70 |
|
| 71 |
This is like standard Pascal @code(Copy), but safe for Unicode, and working with |
| 72 |
both FPC and Delphi default String (see https://castle-engine.io/coding_conventions#strings_unicode ). |
| 73 |
|
| 74 |
StartIndex is 1-based, i.e. the first Unicode character in String has index 1, |
| 75 |
last Unicode character has index StringLength(S). |
| 76 |
|
| 77 |
In case CountToCopy, it is guaranteed to only copy the maximum possible |
| 78 |
characters, without causing any memory overruns. |
| 79 |
|
| 80 |
Note that it doesn't try to deal with strings that may end abruptly |
| 81 |
in the middle of a Unicode character (that may span multiple Pascal Char |
| 82 |
(AnsiChar or WideChar) values, possible both in case |
| 83 |
of UTF-8 in AnsiString and UTF-16 in UnicodeString). |
| 84 |
The results of such abrupt ending are undefined: this routine may copy the partial |
| 85 |
(unfinished) Unicode character, or it may reject the unfinished partial character altogether. |
| 86 |
|
| 87 |
This works taking into account that: |
| 88 |
@unorderedList( |
| 89 |
@item(with FPC, we expect String = AnsiString and holding UTF-8 data,) |
| 90 |
@item(with Delphi we expect String = UnicodeString and holding UTF-16 data.) |
| 91 |
) |
| 92 |
See https://castle-engine.io/coding_conventions#strings_unicode . } |
| 93 |
function StringCopy(const S: String; const StartIndex, CountToCopy: Integer): String; |
| 94 |
|
| 95 |
{ Copy all characters from given string, from given position. |
| 96 |
StartIndex is 1-based, i.e. the first Unicode character in String has index 1, |
| 97 |
last Unicode character has index StringLength(S). |
| 98 |
|
| 99 |
This is like @link(SEnding), but safe for Unicode, and working with |
| 100 |
both FPC and Delphi default String (see https://castle-engine.io/coding_conventions#strings_unicode ). } |
| 101 |
function StringEnding(const S: String; const StartIndex: Integer): String; |
| 102 |
|
| 103 |
(* TODO: |
| 104 |
|
| 105 |
{ Get Unicode char code at given position in the String. |
| 106 |
|
| 107 |
This works taking into account that: |
| 108 |
@unorderedList( |
| 109 |
@item(with FPC, we expect String = AnsiString and holding UTF-8 data,) |
| 110 |
@item(with Delphi we expect String = UnicodeString and holding UTF-16 data.) |
| 111 |
) |
| 112 |
See https://castle-engine.io/coding_conventions#strings_unicode . } |
| 113 |
function StringUnicodeChar(const S: String; const Index: Integer): TUnicodeChar; |
| 114 |
*) |
| 115 |
|
| 116 |
{ Express single Unicode character code as a String that you can write. } |
| 117 |
function UnicodeCharToString(const C: TUnicodeChar): String; |
| 118 |
|
| 119 |
{ Like UnicodeCharToString, but in case C is not a printable character |
| 120 |
(like ASCII control characters with code < 32), |
| 121 |
show it as '#' + character number. |
| 122 |
|
| 123 |
Use this only for debugging, or to display error messages, |
| 124 |
because the output is not 100% unambiguous: if the original string |
| 125 |
contains a sequence like #xxx, we make no attempt to "quoute" this sequence. |
| 126 |
This the output is ambiguous, both for human and machine processing. |
| 127 |
It is just "useful enough" for some cases of debugging output. |
| 128 |
|
| 129 |
To have unambiguous output, use StringWithHtmlEntities. |
| 130 |
This uses HTML entity encoding and takes care to also quote special '&'. |
| 131 |
StringWithHtmlEntities it converts also characters above 128, like Polish and Chinese, |
| 132 |
to numbers -- it is up to your needs whether this is more readable or not, |
| 133 |
depends on how do you output this in practice. } |
| 134 |
function UnicodeCharToReadableString(const C: TUnicodeChar): String; |
| 135 |
|
| 136 |
{ Convert all special Unicode characters in the given string to HTML entities. |
| 137 |
This is a helpful routine to visualize a string with any Unicode characters |
| 138 |
using simple ASCII. |
| 139 |
|
| 140 |
"Special" Unicode characters is "anything outside of safe ASCII range, |
| 141 |
which is between space and ASCII code 128". |
| 142 |
The resulting string contains these special characters encoded |
| 143 |
as HTML entities that show the Unicode code point in hex. |
| 144 |
Like @code(&#xNNNN;) (see https://en.wikipedia.org/wiki/Unicode_and_HTML ). |
| 145 |
Converts also ampersand @code(&) to @code(&) to prevent ambiguities. |
| 146 |
|
| 147 |
Tip: You can check Unicode codes by going to e.g. https://codepoints.net/U+F3 |
| 148 |
for @code(ó). Just edit this URL in the WWW browser address bar. |
| 149 |
} |
| 150 |
function StringWithHtmlEntities(const S: String): String; |
| 151 |
|
| 152 |
type |
| 153 |
{ Iterate over String that contains Unicode characters suitable |
| 154 |
for both FPC (with default String = AnsiString) |
| 155 |
and Delphi (with default String = UnicodeString). |
| 156 |
|
| 157 |
This should be used for all iteration over strings in Castle Game Engine. |
| 158 |
It abstracts away various details about UTF-8 processing (in case of FPC) |
| 159 |
and UTF-16 processing (in case of Delphi). |
| 160 |
See https://castle-engine.io/coding_conventions#strings_unicode . |
| 161 |
|
| 162 |
The typical usage looks like this: |
| 163 |
|
| 164 |
@longCode(# |
| 165 |
var |
| 166 |
Iter: TCastleStringIterator; |
| 167 |
begin |
| 168 |
Iter.Start('my_string'); |
| 169 |
while Iter.GetNext do |
| 170 |
begin |
| 171 |
// do something with Iter.Current now |
| 172 |
WritelnLog('Got Unicode character: %d, %s', [ |
| 173 |
Iter.Current, |
| 174 |
UnicodeCharToString(Iter.Current) |
| 175 |
]); |
| 176 |
end; |
| 177 |
end; |
| 178 |
#) |
| 179 |
|
| 180 |
It is allowed to use @link(Start) again, with the same iterator |
| 181 |
(regardless if it finished or not), to start processing a new (or the same) |
| 182 |
String. } |
| 183 |
TCastleStringIterator = record |
| 184 |
strict private |
| 185 |
FCurrent: TUnicodeChar; |
| 186 |
{$ifdef FPC} |
| 187 |
TextPtr: PChar; |
| 188 |
{$else} |
| 189 |
TextCopy: String; |
| 190 |
TextIndex: Integer; |
| 191 |
TextLength: Integer; |
| 192 |
{$endif} |
| 193 |
public |
| 194 |
{ Start processing the given String. |
| 195 |
Must be called before accessing @link(Current) or @link(GetNext). } |
| 196 |
procedure Start(const S: String); |
| 197 |
|
| 198 |
{ Call this in a loop. |
| 199 |
Must be called (and return @true) before accessing @link(Current). } |
| 200 |
function GetNext: Boolean; |
| 201 |
|
| 202 |
{ Is next character available or not. |
| 203 |
This returns exactly the same thing as @code(not GetNext) would return, |
| 204 |
but this doesn't change the @link(Current) and doesn't advance to next |
| 205 |
character. } |
| 206 |
// Implemented, but not really useful now. |
| 207 |
//function EndOfText: Boolean; |
| 208 |
|
| 209 |
{ After @link(GetNext) was called, and returned @true, |
| 210 |
read this to get the current Unicode character. |
| 211 |
Do not use it after @link(GetNext) returned @false. } |
| 212 |
property Current: TUnicodeChar read FCurrent; |
| 213 |
end; |
| 214 |
|
| 215 |
implementation |
| 216 |
|
| 217 |
uses SysUtils{$ifndef FPC}, Character{$endif}, CastleLog; |
| 218 |
|
| 219 |
{ TUnicodeCharList ----------------------------------------------------------- } |
| 220 |
|
| 221 |
procedure TUnicodeCharList.Add(const C: TUnicodeChar); |
| 222 |
begin |
| 223 |
if IndexOf(C) = -1 then |
| 224 |
inherited Add(C); |
| 225 |
end; |
| 226 |
|
| 227 |
procedure TUnicodeCharList.Add(const SampleText: string); |
| 228 |
var |
| 229 |
Iter: TCastleStringIterator; |
| 230 |
begin |
| 231 |
Iter.Start(SampleText); |
| 232 |
while Iter.GetNext do |
| 233 |
Add(Iter.Current); |
| 234 |
end; |
| 235 |
|
| 236 |
procedure TUnicodeCharList.Add(const Characters: TSetOfChars); |
| 237 |
var |
| 238 |
C: char; |
| 239 |
begin |
| 240 |
for C in Characters do |
| 241 |
Add(Ord(C)); |
| 242 |
end; |
| 243 |
|
| 244 |
function TUnicodeCharList.ToString: String; |
| 245 |
var |
| 246 |
C: TUnicodeChar; |
| 247 |
begin |
| 248 |
Result := ''; |
| 249 |
for C in Self do |
| 250 |
Result := Result + UnicodeCharToString(C); |
| 251 |
end; |
| 252 |
|
| 253 |
{ global --------------------------------------------------------------------- } |
| 254 |
{$ifdef FPC} |
| 255 |
|
| 256 |
function UTF8CharacterLength(p: PChar): Integer; |
| 257 |
begin |
| 258 |
if p<>nil then begin |
| 259 |
if ord(p^)<$C0 { binary 11000000 } then begin |
| 260 |
// regular single byte character (#0 is a character, this is pascal ;) |
| 261 |
Result:=1; |
| 262 |
end |
| 263 |
else begin |
| 264 |
// multi byte |
| 265 |
if ((ord(p^) and $E0 { binary 11100000 }) = $C0 { binary 11000000 }) then begin |
| 266 |
// could be 2 byte character |
| 267 |
if (ord(p[1]) and $C0 { binary 11000000 }) = $80 { binary 10000000 } then |
| 268 |
Result:=2 |
| 269 |
else |
| 270 |
Result:=1; |
| 271 |
end |
| 272 |
else if ((ord(p^) and $F0 { binary 11110000 }) = $E0 { binary 11100000 }) then begin |
| 273 |
// could be 3 byte character |
| 274 |
if ((ord(p[1]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) |
| 275 |
and ((ord(p[2]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) then |
| 276 |
Result:=3 |
| 277 |
else |
| 278 |
Result:=1; |
| 279 |
end |
| 280 |
else if ((ord(p^) and $F8 { binary 11111000 }) = $F0 { binary 11110000 }) then begin |
| 281 |
// could be 4 byte character |
| 282 |
if ((ord(p[1]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) |
| 283 |
and ((ord(p[2]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) |
| 284 |
and ((ord(p[3]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) then |
| 285 |
Result:=4 |
| 286 |
else |
| 287 |
Result:=1; |
| 288 |
end |
| 289 |
else |
| 290 |
Result:=1; |
| 291 |
end; |
| 292 |
end else |
| 293 |
Result:=0; |
| 294 |
end; |
| 295 |
|
| 296 |
function UTF8Length(const s: string): PtrInt; forward; overload; |
| 297 |
function UTF8Length(p: PChar; ByteCount: PtrInt): PtrInt; forward; overload; |
| 298 |
|
| 299 |
function UTF8Length(const s: string): PtrInt; |
| 300 |
begin |
| 301 |
Result:=UTF8Length(PChar(s),length(s)); |
| 302 |
end; |
| 303 |
|
| 304 |
function UTF8Length(p: PChar; ByteCount: PtrInt): PtrInt; |
| 305 |
var |
| 306 |
CharLen: Integer; |
| 307 |
begin |
| 308 |
Result:=0; |
| 309 |
while (ByteCount>0) do begin |
| 310 |
inc(Result); |
| 311 |
CharLen := UTF8CharacterLength(p); |
| 312 |
inc(p,CharLen); |
| 313 |
dec(ByteCount,CharLen); |
| 314 |
end; |
| 315 |
end; |
| 316 |
|
| 317 |
{ Len is the length in bytes of UTF8Str |
| 318 |
CharIndex is the position of the desired char (starting at 0), in chars |
| 319 |
} |
| 320 |
function UTF8CharStart(UTF8Str: PChar; Len, CharIndex: PtrInt): PChar; |
| 321 |
var |
| 322 |
CharLen: Integer; |
| 323 |
begin |
| 324 |
Result:=UTF8Str; |
| 325 |
if Result<>nil then begin |
| 326 |
while (CharIndex>0) and (Len>0) do begin |
| 327 |
CharLen := UTF8CharacterLength(Result); |
| 328 |
dec(Len,CharLen); |
| 329 |
dec(CharIndex); |
| 330 |
inc(Result,CharLen); |
| 331 |
end; |
| 332 |
if (CharIndex<>0) or (Len<0) then |
| 333 |
Result:=nil; |
| 334 |
end; |
| 335 |
end; |
| 336 |
|
| 337 |
function UTF8Copy(const s: string; StartCharIndex, CharCount: PtrInt): string; |
| 338 |
// returns substring |
| 339 |
var |
| 340 |
StartBytePos: PChar; |
| 341 |
EndBytePos: PChar; |
| 342 |
MaxBytes: PtrInt; |
| 343 |
begin |
| 344 |
StartBytePos:=UTF8CharStart(PChar(s),length(s),StartCharIndex-1); |
| 345 |
if StartBytePos=nil then |
| 346 |
Result:='' |
| 347 |
else begin |
| 348 |
MaxBytes:=PtrInt(PChar(s)+length(s)-StartBytePos); |
| 349 |
EndBytePos:=UTF8CharStart(StartBytePos,MaxBytes,CharCount); |
| 350 |
if EndBytePos=nil then |
| 351 |
Result:=copy(s,StartBytePos-PChar(s)+1,MaxBytes) |
| 352 |
else |
| 353 |
Result:=copy(s,StartBytePos-PChar(s)+1,EndBytePos-StartBytePos); |
| 354 |
end; |
| 355 |
end; |
| 356 |
|
| 357 |
function UTF8CharacterToUnicode(p: PChar; out CharLen: integer): Cardinal; |
| 358 |
{ if p=nil then CharLen=0 otherwise CharLen>0 |
| 359 |
If there is an encoding error the Result is undefined. |
| 360 |
Use UTF8FixBroken to fix UTF-8 encoding. |
| 361 |
It does not check if the codepoint is defined in the Unicode tables. |
| 362 |
} |
| 363 |
begin |
| 364 |
if p<>nil then begin |
| 365 |
if ord(p^)<$C0 { binary 11000000 } then begin |
| 366 |
// regular single byte character (#0 is a normal char, this is pascal ;) |
| 367 |
Result:=ord(p^); |
| 368 |
CharLen:=1; |
| 369 |
end |
| 370 |
else if ((ord(p^) and $E0 { binary 11100000 }) = $C0 { binary 11000000 }) then begin |
| 371 |
// starts with %110 => could be double byte character |
| 372 |
if (ord(p[1]) and $C0 { binary 11000000 }) = $80 { binary 10000000 } then begin |
| 373 |
CharLen:=2; |
| 374 |
Result:=((ord(p^) and $1F { binary 00011111 }) shl 6) |
| 375 |
or (ord(p[1]) and $3F { binary 00111111 }); |
| 376 |
if Result<(1 shl 7) then begin |
| 377 |
// wrong encoded, could be an XSS attack |
| 378 |
Result:=0; |
| 379 |
end; |
| 380 |
end else begin |
| 381 |
Result:=ord(p^); |
| 382 |
CharLen:=1; |
| 383 |
end; |
| 384 |
end |
| 385 |
else if ((ord(p^) and $F0 { binary 11110000 }) = $E0 { binary 11100000 }) then begin |
| 386 |
// starts with %1110 => could be triple byte character |
| 387 |
if ((ord(p[1]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) |
| 388 |
and ((ord(p[2]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) then begin |
| 389 |
CharLen:=3; |
| 390 |
Result:=((ord(p^) and $1F { binary 00011111 }) shl 12) |
| 391 |
or ((ord(p[1]) and $3F { binary 00111111 }) shl 6) |
| 392 |
or (ord(p[2]) and $3F { binary 00111111 }); |
| 393 |
if Result<(1 shl 11) then begin |
| 394 |
// wrong encoded, could be an XSS attack |
| 395 |
Result:=0; |
| 396 |
end; |
| 397 |
end else begin |
| 398 |
Result:=ord(p^); |
| 399 |
CharLen:=1; |
| 400 |
end; |
| 401 |
end |
| 402 |
else if ((ord(p^) and $F8 { binary 11111000 }) = $F0 { binary 11110000 }) then begin |
| 403 |
// starts with %11110 => could be 4 byte character |
| 404 |
if ((ord(p[1]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) |
| 405 |
and ((ord(p[2]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) |
| 406 |
and ((ord(p[3]) and $C0 { binary 11000000 }) = $80 { binary 10000000 }) then begin |
| 407 |
CharLen:=4; |
| 408 |
Result:=((ord(p^) and $0F { binary 00001111 }) shl 18) |
| 409 |
or ((ord(p[1]) and $3F { binary 00111111 }) shl 12) |
| 410 |
or ((ord(p[2]) and $3F { binary 00111111 }) shl 6) |
| 411 |
or (ord(p[3]) and $3F { binary 00111111 }); |
| 412 |
if Result<(1 shl 16) then begin |
| 413 |
// wrong encoded, could be an XSS attack |
| 414 |
Result:=0; |
| 415 |
end; |
| 416 |
end else begin |
| 417 |
Result:=ord(p^); |
| 418 |
CharLen:=1; |
| 419 |
end; |
| 420 |
end |
| 421 |
else begin |
| 422 |
// invalid character |
| 423 |
Result:=ord(p^); |
| 424 |
CharLen:=1; |
| 425 |
end; |
| 426 |
end else begin |
| 427 |
Result:=0; |
| 428 |
CharLen:=0; |
| 429 |
end; |
| 430 |
end; |
| 431 |
|
| 432 |
function UnicodeToUTF8Inline(CodePoint: TUnicodeChar; Buf: PChar): integer; |
| 433 |
begin |
| 434 |
case CodePoint of |
| 435 |
0..$7f: |
| 436 |
begin |
| 437 |
Result:=1; |
| 438 |
Buf[0]:=char(byte(CodePoint)); |
| 439 |
end; |
| 440 |
$80..$7ff: |
| 441 |
begin |
| 442 |
Result:=2; |
| 443 |
Buf[0]:=char(byte($c0 or (CodePoint shr 6))); |
| 444 |
Buf[1]:=char(byte($80 or (CodePoint and $3f))); |
| 445 |
end; |
| 446 |
$800..$ffff: |
| 447 |
begin |
| 448 |
Result:=3; |
| 449 |
Buf[0]:=char(byte($e0 or (CodePoint shr 12))); |
| 450 |
Buf[1]:=char(byte((CodePoint shr 6) and $3f) or $80); |
| 451 |
Buf[2]:=char(byte(CodePoint and $3f) or $80); |
| 452 |
end; |
| 453 |
$10000..$10ffff: |
| 454 |
begin |
| 455 |
Result:=4; |
| 456 |
Buf[0]:=char(byte($f0 or (CodePoint shr 18))); |
| 457 |
Buf[1]:=char(byte((CodePoint shr 12) and $3f) or $80); |
| 458 |
Buf[2]:=char(byte((CodePoint shr 6) and $3f) or $80); |
| 459 |
Buf[3]:=char(byte(CodePoint and $3f) or $80); |
| 460 |
end; |
| 461 |
else |
| 462 |
Result:=0; |
| 463 |
end; |
| 464 |
end; |
| 465 |
|
| 466 |
function UnicodeToUTF8(CodePoint: TUnicodeChar): string; |
| 467 |
var |
| 468 |
Buf: array[0..6] of Char; |
| 469 |
Len: Integer; |
| 470 |
begin |
| 471 |
Len:=UnicodeToUTF8Inline(CodePoint, @Buf[0]); |
| 472 |
Buf[Len]:=#0; |
| 473 |
Result := StrPas(@Buf[0]); |
| 474 |
end; |
| 475 |
|
| 476 |
{$else FPC} |
| 477 |
|
| 478 |
{ About IsSurrogate usage below: |
| 479 |
|
| 480 |
IsSurrogate detects either leading or trailing surrogate. |
| 481 |
And in valid UTF-16 string, you either have: |
| 482 |
|
| 483 |
- 1 code unit (16 bit char) == 1 Unicode code point |
| 484 |
|
| 485 |
And then this 16-bit character is not surrogate. |
| 486 |
|
| 487 |
- 2 code unit (2x 16 bit char) == 1 Unicode code point |
| 488 |
|
| 489 |
And then both 16-bit characters are surrogate (they both will have |
| 490 |
IsSurrogate true). |
| 491 |
1st is high, leading. |
| 492 |
2nd is low, trailing. |
| 493 |
|
| 494 |
Note that Delphi also has IsHighSurrogate and IsLowSurrogate. |
| 495 |
|
| 496 |
See https://stackoverflow.com/questions/52584308/convert-unicode-surrogate-pair-to-literal-string : |
| 497 |
|
| 498 |
""" |
| 499 |
In Unicode, you have code points. These are 21 bits long. |
| 500 |
Your character 𝐀, Mathematical Bold Capital A, has a code point of U+1D400. |
| 501 |
|
| 502 |
In Unicode encodings, you have code units. |
| 503 |
These are the natural unit of the encoding: 8-bit for UTF-8, 16-bit for UTF-16, and so on. |
| 504 |
One or more code units encode a single code point. |
| 505 |
|
| 506 |
In UTF-16, two code units that form a single code point are called a surrogate pair. |
| 507 |
Surrogate pairs are used to encode any code point greater than 16 bits, i.e. U+10000 and up. |
| 508 |
""" |
| 509 |
|
| 510 |
https://datacadamia.com/data/type/text/surrogate |
| 511 |
|
| 512 |
""" |
| 513 |
A surrogate pair: |
| 514 |
|
| 515 |
is composed of a two pair of code point (H and L) 1) (with a value in a special range) |
| 516 |
represents a unicode code point known as S with a value above 0xFFFF |
| 517 |
""" |
| 518 |
} |
| 519 |
|
| 520 |
{ Length of string assuming it is a standard Delphi String |
| 521 |
(that is, UnicodeString holding UTF-16). } |
| 522 |
function UnicodeStringLength(const Text: String): Integer; |
| 523 |
var |
| 524 |
I: Integer; |
| 525 |
begin |
| 526 |
Result := 0; |
| 527 |
I := 1; |
| 528 |
while I <= Length(Text) do |
| 529 |
begin |
| 530 |
if IsSurrogate(Text, I) then |
| 531 |
Inc(I); |
| 532 |
Inc(Result); |
| 533 |
Inc(I); |
| 534 |
end; |
| 535 |
end; |
| 536 |
|
| 537 |
function UnicodeStringNextChar(const Text: String; const Index: Integer; out NextCharIndex: Integer): TUnicodeChar; |
| 538 |
begin |
| 539 |
NextCharIndex := Index + 1; |
| 540 |
|
| 541 |
if IsLowSurrogate(Text, Index) then // skip trailing surrogate |
| 542 |
Inc(NextCharIndex); |
| 543 |
|
| 544 |
Result := ConvertToUtf32(Text, Index); |
| 545 |
end; |
| 546 |
|
| 547 |
{ Copy a number of given Unicode characters from given string. |
| 548 |
Assumes it is a standard Delphi String (that is, UnicodeString holding UTF-16). } |
| 549 |
function UnicodeStringCopy(const S: string; StartCharIndex, CharCount: PtrInt): String; |
| 550 |
var |
| 551 |
I: Integer; |
| 552 |
AddedChars: Integer; |
| 553 |
begin |
| 554 |
I := StartCharIndex; |
| 555 |
AddedChars := 0; |
| 556 |
while (I <= Length(S)) and (AddedChars < CharCount) do |
| 557 |
begin |
| 558 |
if IsSurrogate(S, I) then |
| 559 |
begin |
| 560 |
Result := Result + S[I]; |
| 561 |
Inc(I); |
| 562 |
end; |
| 563 |
|
| 564 |
if I > Length(S) then |
| 565 |
begin |
| 566 |
WritelnWarning('StringCopy', 'String ends in the middle of a Unicode character, cannot copy it'); |
| 567 |
Break; |
| 568 |
end; |
| 569 |
|
| 570 |
Result := Result + S[I]; |
| 571 |
Inc(AddedChars); |
| 572 |
Inc(I); |
| 573 |
end; |
| 574 |
end; |
| 575 |
|
| 576 |
{$endif FPC} |
| 577 |
|
| 578 |
function StringWithHtmlEntities(const S: String): String; |
| 579 |
var |
| 580 |
Iter: TCastleStringIterator; |
| 581 |
C: TUnicodeChar; |
| 582 |
begin |
| 583 |
Iter.Start(S); |
| 584 |
Result := ''; |
| 585 |
while Iter.GetNext do |
| 586 |
begin |
| 587 |
C := Iter.Current; |
| 588 |
if (C < Ord(' ')) or (C >= 128) then |
| 589 |
Result := Result + '&#x' + IntToHex(C, 1) + ';' |
| 590 |
else |
| 591 |
if C = Ord('&') then |
| 592 |
Result := Result + '&' |
| 593 |
else |
| 594 |
Result := Result + Chr(C); |
| 595 |
end; |
| 596 |
end; |
| 597 |
|
| 598 |
function StringLength(const S: String): Integer; |
| 599 |
begin |
| 600 |
Result := {$ifdef FPC} UTF8Length {$else} UnicodeStringLength {$endif} (S); |
| 601 |
end; |
| 602 |
|
| 603 |
function StringCopy(const S: String; const StartIndex, CountToCopy: Integer): String; |
| 604 |
begin |
| 605 |
Result := {$ifdef FPC} UTF8Copy {$else} UnicodeStringCopy {$endif} (S, StartIndex, CountToCopy); |
| 606 |
end; |
| 607 |
|
| 608 |
function StringEnding(const S: String; const StartIndex: Integer): String; |
| 609 |
begin |
| 610 |
Result := {$ifdef FPC} UTF8Copy {$else} UnicodeStringCopy {$endif} (S, StartIndex, MaxInt); |
| 611 |
end; |
| 612 |
|
| 613 |
function UnicodeCharToString(const C: TUnicodeChar): String; |
| 614 |
begin |
| 615 |
Result := {$ifdef FPC} UnicodeToUTF8 {$else}ConvertFromUtf32 {$endif} (C); |
| 616 |
end; |
| 617 |
|
| 618 |
function UnicodeCharToReadableString(const C: TUnicodeChar): String; |
| 619 |
begin |
| 620 |
if C < Ord(' ') then |
| 621 |
Result := '#' + IntToStr(C) |
| 622 |
else |
| 623 |
Result := UnicodeCharToString(C); |
| 624 |
end; |
| 625 |
|
| 626 |
(* |
| 627 |
function StringUnicodeChar(const S: String; const Index: Integer): TUnicodeChar; |
| 628 |
begin |
| 629 |
Result := {$ifdef FPC} xxx {$else} xxx {$endif} (S, Index); |
| 630 |
end; |
| 631 |
*) |
| 632 |
{$ifdef FPC} |
| 633 |
|
| 634 |
procedure TCastleStringIterator.Start(const S: String); |
| 635 |
begin |
| 636 |
TextPtr := PChar(S); |
| 637 |
end; |
| 638 |
|
| 639 |
function TCastleStringIterator.GetNext: Boolean; |
| 640 |
var |
| 641 |
CharLen: Integer; |
| 642 |
begin |
| 643 |
FCurrent := UTF8CharacterToUnicode(TextPtr, CharLen); |
| 644 |
Result := (FCurrent > 0) and (CharLen > 0); |
| 645 |
if Result then |
| 646 |
Inc(TextPtr, CharLen); |
| 647 |
end; |
| 648 |
|
| 649 |
(* Implemented, but not really useful now. |
| 650 |
function TCastleStringIterator.EndOfText: Boolean; |
| 651 |
var |
| 652 |
CharLen: Integer; |
| 653 |
IgnoreChar: TUnicodeChar; |
| 654 |
begin |
| 655 |
// deliberately expressed to be obvious it is "not" + the same logic as GetNext |
| 656 |
IgnoreChar := UTF8CharacterToUnicode(TextPtr, CharLen); |
| 657 |
Result := (IgnoreChar > 0) and (CharLen > 0); |
| 658 |
end; |
| 659 |
*) |
| 660 |
|
| 661 |
{$else} |
| 662 |
|
| 663 |
procedure TCastleStringIterator.Start(const S: String); |
| 664 |
begin |
| 665 |
TextIndex := 1; |
| 666 |
TextCopy := S; |
| 667 |
TextLength := Length(S); |
| 668 |
end; |
| 669 |
|
| 670 |
function TCastleStringIterator.GetNext: Boolean; |
| 671 |
var |
| 672 |
NextTextIndex: Integer; |
| 673 |
begin |
| 674 |
Result := TextIndex <= TextLength; |
| 675 |
if Result then |
| 676 |
begin |
| 677 |
FCurrent := UnicodeStringNextChar(TextCopy, TextIndex, NextTextIndex); |
| 678 |
TextIndex := NextTextIndex; |
| 679 |
end; |
| 680 |
end; |
| 681 |
|
| 682 |
(* Implemented, but not really useful now. |
| 683 |
function TCastleStringIterator.EndOfText: Boolean; |
| 684 |
begin |
| 685 |
// deliberately expressed to be obvious it is "not" + the same logic as GetNext |
| 686 |
Result := not (TextIndex <= TextLength); |
| 687 |
end; |
| 688 |
*) |
| 689 |
|
| 690 |
{$endif} |
| 691 |
|
| 692 |
end. |