1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
{
*****************************************************************************
This file is part of LazUtils.
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
A widestring manager written in Pascal
and optimized for DefaultSystemCodePage CP_UTF8.
}
unit PasWString;
{$mode objfpc}
{$inline on}
{$i lazutils_defines.inc}
//{$define PASWSTRING_VERBOSE}
//{.$define PASWSTRING_SUPPORT_NONUTF8_ANSISTRING} disabled by default because
// non utf-8 ansistring is rare in UNIXes and lconvencoding makes the executable big
// sanity checks for defines
//{$IF FPC_FULLVERSION >= 30000}
{$IFnDEF NO_CP_RTL}
{$IFDEF UTF8_RTL}
{$IFDEF PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
{$error UTF8 or not UTF8}
{$ENDIF}
{$ENDIF}
{$DEFINE DisablePasWString}
{$ENDIF}
interface
uses
SysUtils, LazUTF8
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}, lconvencoding{$endif}
;
{$IFnDEF DisablePasWString}
procedure SetPasWidestringManager;
{$ENDIF}
implementation
{$IFnDEF DisablePasWString}
procedure fpc_rangeerror; [external name 'FPC_RANGEERROR'];
function IsASCII(const s: string): boolean; inline;
var
i: Integer;
begin
for i:=1 to length(s) do if ord(s[i])>127 then exit(false);
Result:=true;
end;
// len comes in widechars, not bytes
procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('Wide2AnsiMove START');{$endif}
dest := UTF16ToUTF8(Source,len);
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
// And correct to the real Ansi encoding
dest := ConvertEncoding(dest, EncodingUTF8, GetDefaultTextEncoding());
{$endif}
end;
procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
var
ansistr: ansistring;
{$endif}
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('Ansi2WideMove START');{$endif}
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
// Copy the originating string taking into account the specified length
SetLength(ansistr, len);
System.Move(source^, ansistr[1], len);
// Convert to UTF-8
ansistr := ConvertEncoding(ansistr, GetDefaultTextEncoding(), EncodingUTF8);
// Now convert it, using UTF-8 -> UTF-16
dest := UTF8ToUTF16(ansistr);
{$else}
dest := UTF8ToUTF16(source,len);
{$endif}
end;
function LowerWideString(const s : WideString) : WideString;
var
str: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('LowerWideString START');{$endif}
str := UTF16ToUTF8(PWideChar(s),length(s));
str := UTF8LowerCase(str);
Result := UTF8ToUTF16(str);
end;
function UpperWideString(const s : WideString) : WideString;
var
str: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('UpperWideString START');{$endif}
str := UTF16ToUTF8(PWideChar(s),length(s));
str := UTF8UpperCase(str);
Result := UTF8ToUTF16(str);
end;
procedure EnsureAnsiLen(var S: AnsiString; const len: SizeInt); inline;
var
l: SizeUInt;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('EnsureAnsiLen START');{$endif}
l:=length(s);
if (len>l) then
if (l < 128) then
setlength(s,l+8)
else
setlength(s,l+l shr 8);
end;
procedure ConcatCharToAnsiStr(const c: char; var S: AnsiString; var index: SizeInt);
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('ConcatCharToAnsiStr START');{$endif}
EnsureAnsiLen(s,index);
pchar(@s[index])^:=c;
inc(index);
end;
function LowerAnsiString(const s : AnsiString) : AnsiString;
var
Str: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('LowerAnsiString START');{$endif}
Str := SysToUTF8(s);
Str := UTF8LowerCase(Str);
Result := UTF8ToSys(Str);
end;
function UpperAnsiString(const s : AnsiString) : AnsiString;
var
Str: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('UpperAnsiString START');{$endif}
Str := SysToUTF8(s);
Str := UTF8UpperCase(Str);
Result := UTF8ToSys(Str);
end;
// Just do a simple byte comparison
// A more complex analysis would require normalization
function WideCompareStr(const s1, s2 : WideString) : PtrInt;
var
count, count1, count2: integer;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('WideCompareStr START');{$endif}
result := 0;
Count1 := Length(S1);
Count2 := Length(S2);
if Count1>Count2 then
Count:=Count2
else
Count:=Count1;
result := SysUtils.CompareMemRange(Pointer(S1),Pointer(S2), Count*2);
if result=0 then
result:=Count1-Count2;
end;
function WideCompareText(const s1, s2 : WideString): PtrInt;
var
a, b: String;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('WideCompareText START');{$endif}
a := UTF16ToUTF8(PWideChar(s1),length(s1));
a := UTF8LowerCase(a);
b := UTF16ToUTF8(PWideChar(s2),length(s2));
b := UTF8LowerCase(b);
result := UTF8CompareStr(a,b);
end;
function CharLengthPChar(const Str: PChar): PtrInt;
// return the number of codepoints (including invalid codepoints)
var
p: PChar;
l: Integer;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('CharLengthPChar START');{$endif}
p:=Str;
if p=nil then exit(0);
while p^<>#0 do begin
l:=UTF8CodepointSize(p);
inc(Result);
inc(p,l);
end;
end;
function CodePointLengthPChar(const p: PChar; MaxLookAhead: PtrInt): Ptrint;
{ return value:
-1 if incomplete or invalid code point
0 if NULL character,
> 0 if that's the length in bytes of the code point }
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('CodePointLengthPChar START');{$endif}
if (p=nil) then exit(0);
if (MaxLookAhead<0) then exit(-1);
if ord(p^)<%10000000 then begin
// regular single byte character
if p^=#0 then
exit(0)
else
exit(1);
end;
if ord(p^)<%11000000 then begin
// invalid single byte character
exit(-1);
end;
if (MaxLookAhead=0) then exit(-1);
if ((ord(p^) and %11100000) = %11000000) then begin
// should be 2 byte character
if (ord(p[1]) and %11000000) = %10000000 then
exit(2)
else
exit(-1);
end;
if (MaxLookAhead=1) then exit(-1);
if ((ord(p^) and %11110000) = %11100000) then begin
// should be 3 byte character
if ((ord(p[1]) and %11000000) = %10000000)
and ((ord(p[2]) and %11000000) = %10000000) then
exit(3)
else
exit(-1);
end;
if (MaxLookAhead=2) then exit(-1);
if ((ord(p^) and %11111000) = %11110000) then begin
// should be 4 byte character
if ((ord(p[1]) and %11000000) = %10000000)
and ((ord(p[2]) and %11000000) = %10000000)
and ((ord(p[3]) and %11000000) = %10000000) then
exit(4)
else
exit(-1);
end;
exit(-1);
end;
function AnsiCompareStr(const s1, s2: ansistring): PtrInt;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('AnsiCompareStr START');{$endif}
Result := SysUtils.CompareStr(s1, s2);
end;
// Similar to AnsiCompareStr, but with PChar
function StrCompAnsi(s1,s2 : PChar): PtrInt;
var
Count1: SizeInt;
Count2: SizeInt;
Count: SizeInt;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('StrCompAnsi START');{$endif}
result := 0;
Count1:=StrLen(s1);
Count2:=StrLen(s2);
if Count1>Count2 then
Count:=Count2
else
Count:=Count1;
result := CompareMemRange(s1, s2, Count);
if result=0 then
result:=Count1-Count2;
end;
function AnsiCompareText(const S1, S2: ansistring): PtrInt;
var
str1, str2: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('AnsiCompareText START');{$endif}
str1 := SysToUTF8(S1);
str2 := SysToUTF8(S2);
Result := UTF8CompareText(str1, str2);
end;
function AnsiStrIComp(S1, S2: PChar): PtrInt;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('AnsiStrIComp START');{$endif}
Result := AnsiCompareText(StrPas(s1),StrPas(s2));
end;
function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
var
a, b: pchar;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('AnsiStrLComp START');{$endif}
Result := 0;
if (maxlen=0) then
exit(0);
if (s1[maxlen]<>#0) then
begin
getmem(a,maxlen+1);
move(s1^,a^,maxlen);
a[maxlen]:=#0;
end
else
a:=s1;
if (s2[maxlen]<>#0) then
begin
getmem(b,maxlen+1);
move(s2^,b^,maxlen);
b[maxlen]:=#0;
end
else
b:=s2;
result:=StrCompAnsi(a,b);
if (a<>s1) then
freemem(a);
if (b<>s2) then
freemem(b);
end;
function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
var
a, b: ansistring;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('AnsiStrLIComp START');{$endif}
if (maxlen=0) then
exit(0);
setlength(a,maxlen);
move(s1^,a[1],maxlen);
setlength(b,maxlen);
move(s2^,b[1],maxlen);
result:=AnsiCompareText(a,b);
end;
procedure ansi2pchar(const s: ansistring; const orgp: pchar; out p: pchar);
var
newlen: sizeint;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('ansi2pchar START');{$endif}
newlen:=length(s);
if newlen>strlen(orgp) then
fpc_rangeerror;
p:=orgp;
if (newlen>0) then
move(s[1],p[0],newlen);
p[newlen]:=#0;
end;
function AnsiStrLower(Str: PChar): PChar;
var
temp: ansistring;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('AnsiStrLower START');{$endif}
temp:=loweransistring(str);
ansi2pchar(temp,str,result);
end;
function AnsiStrUpper(Str: PChar): PChar;
var
temp: ansistring;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('AnsiStrUpper START');{$endif}
temp:=upperansistring(str);
ansi2pchar(temp,str,result);
end;
procedure InitThread;
begin
end;
procedure FiniThread;
begin
end;
{ Unicode }
procedure Unicode2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('Unicode2AnsiMove START');{$endif}
dest := UTF16ToUTF8(source,len);
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
// And correct to the real Ansi encoding
dest := ConvertEncoding(dest, EncodingUTF8, GetDefaultTextEncoding());
{$endif}
end;
procedure Ansi2UnicodeMove(source:pchar;var dest:UnicodeString;len:SizeInt);
{$IFDEF PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
var
ansistr: ansistring;
{$ENDIF}
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('Ansi2UnicodeMove START');{$endif}
{$IFDEF PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
if NeedRTLAnsi then begin
// Copy the originating string taking into account the specified length
SetLength(ansistr, len);
System.Move(source^, ansistr[1], len);
// Convert to UTF-8
ansistr := ConvertEncoding(ansistr, GetDefaultTextEncoding(), EncodingUTF8);
// Now convert it, using UTF-8 -> UTF-16
dest := UTF8ToUTF16(ansistr);
end else begin
dest := UTF8ToUTF16(source,len);
end;
{$ELSE}
dest := UTF8ToUTF16(source,len);
{$ENDIF}
end;
function UpperUnicodeString(const s : UnicodeString) : UnicodeString;
var
str: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('UpperUnicodeString START');{$endif}
str := UTF16ToUTF8(s);
str := UTF8UpperCase(str);
Result := UTF8ToUTF16(str);
end;
function LowerUnicodeString(const s : UnicodeString) : UnicodeString;
var
str: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('LowerUnicodeString START');{$endif}
str := UTF16ToUTF8(s);
str := UTF8LowerCase(str);
Result := UTF8ToUTF16(str);
end;
// Just do a simple byte comparison
// A more complex analysis would require normalization
function PasUnicodeCompareStr(const s1, s2 : unicodestring) : PtrInt;
var
count, count1, count2: integer;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('PasUnicodeCompareStr START');{$endif}
result := 0;
Count1 := Length(S1);
Count2 := Length(S2);
if Count1>Count2 then
Count:=Count2
else
Count:=Count1;
result := SysUtils.CompareMemRange(Pointer(S1),Pointer(S2), Count*2);
if result=0 then
result:=Count1-Count2;
end;
function PasUnicodeCompareText(const s1, s2 : unicodestring): PtrInt;
var
a, b: string;
begin
{$ifdef PASWSTRING_VERBOSE}WriteLn('PasUnicodeCompareText START');{$endif}
a := UTF16ToUTF8(s1);
a := UTF8LowerCase(a);
b := UTF16ToUTF8(s2);
b := UTF8LowerCase(b);
result := UTF8CompareText(a,b);
end;
Procedure SetPasWideStringManager;
Var
PasWideStringManager : TUnicodeStringManager;
begin
PasWideStringManager:=widestringmanager;
PasWideStringManager.Wide2AnsiMoveProc:=@Wide2AnsiMove;
PasWideStringManager.Ansi2WideMoveProc:=@Ansi2WideMove;
// UpperUTF8 : procedure(p:PUTF8String);
PasWideStringManager.UpperWideStringProc:=@UpperWideString;
// UpperUCS4 : procedure(p:PUCS4Char);
// LowerUTF8 : procedure(p:PUTF8String);
PasWideStringManager.LowerWideStringProc:=@LowerWideString;
// LowerUCS4 : procedure(p:PUCS4Char);
{
CompUTF8 : function(p1,p2:PUTF8String) : shortint;
CompUCS2 : function(p1,p2:PUCS2Char) : shortint;
CompUCS4 : function(p1,p2:PUC42Char) : shortint;
}
PasWideStringManager.CompareWideStringProc:=@WideCompareStr;
PasWideStringManager.CompareTextWideStringProc:=@WideCompareText;
{ return value: number of code points in the string. Whenever an invalid
code point is encountered, all characters part of this invalid code point
are considered to form one "character" and the next character is
considered to be the start of a new (possibly also invalid) code point }
PasWideStringManager.CharLengthPCharProc:=@CharLengthPChar;
PasWideStringManager.CodePointLengthProc:=@CodePointLengthPChar;
{ Ansi }
PasWideStringManager.UpperAnsiStringProc:=@UpperAnsiString;
PasWideStringManager.LowerAnsiStringProc:=@LowerAnsiString;
PasWideStringManager.CompareStrAnsiStringProc:=@AnsiCompareStr;
PasWideStringManager.CompareTextAnsiStringProc:=@AnsiCompareText;
PasWideStringManager.StrCompAnsiStringProc:=@StrCompAnsi;
PasWideStringManager.StrICompAnsiStringProc:=@AnsiStrIComp;
PasWideStringManager.StrLCompAnsiStringProc:=@AnsiStrLComp;
PasWideStringManager.StrLICompAnsiStringProc:=@AnsiStrLIComp;
PasWideStringManager.StrLowerAnsiStringProc:=@AnsiStrLower;
PasWideStringManager.StrUpperAnsiStringProc:=@AnsiStrUpper;
PasWideStringManager.ThreadInitProc:=@InitThread;
PasWideStringManager.ThreadFiniProc:=@FiniThread;
{ Unicode }
PasWideStringManager.Unicode2AnsiMoveProc:=@Unicode2AnsiMove;
PasWideStringManager.Ansi2UnicodeMoveProc:=@Ansi2UnicodeMove;
PasWideStringManager.UpperUnicodeStringProc:=@UpperUnicodeString;
PasWideStringManager.LowerUnicodeStringProc:=@LowerUnicodeString;
PasWideStringManager.CompareUnicodeStringProc:=@PasUnicodeCompareStr;
PasWideStringManager.CompareTextUnicodeStringProc:=@PasUnicodeCompareText;
SetUnicodeStringManager(PasWideStringManager);
end;
initialization
SetPasWideStringManager;
{$ENDIF}
end.
|