summaryrefslogtreecommitdiff
path: root/rtl/objpas/fpwidestring.pp
blob: 0e633b3e654d27984ba2650e2179e306c7b92042 (plain)
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
unit fpwidestring;

{$mode objfpc}

interface
uses
  unicodedata;

{$i rtldefs.inc}

  function SetActiveCollation(const AName : TCollationName) : Boolean;
  function SetActiveCollation(const ACollation : PUCA_DataBook) : Boolean;
  function GetActiveCollation() : PUCA_DataBook;

var
  DefaultCollationName : TCollationName = '';

implementation
uses
{$ifdef MSWINDOWS}
  Windows,
{$endif MSWINDOWS}
{$ifdef Unix}
  unixcp,
{$endif}
  charset;
  
procedure fpc_rangeerror; [external name 'FPC_RANGEERROR'];
{$ifdef MSWINDOWS}
  function GetACP:UINT; external 'kernel32' name 'GetACP';
{$endif MSWINDOWS}

const
  IgnoreInvalidSequenceFlag = True;
var
  OldManager : TUnicodeStringManager;

{$ifdef FPC_HAS_FEATURE_THREADING}
ThreadVar
{$else FPC_HAS_FEATURE_THREADING}
Var
{$endif FPC_HAS_FEATURE_THREADING}
  current_DefaultSystemCodePage : TSystemCodePage;
  current_Map : punicodemap;
  current_Collation : PUCA_DataBook;

function SetActiveCollation(const ACollation : PUCA_DataBook) : Boolean;
begin
  Result := (ACollation <> nil);
  if Result then
    current_Collation := ACollation;
end;

function SetActiveCollation(const AName : TCollationName) : Boolean;
var
  c : PUCA_DataBook;
begin
  c:=FindCollation(AName);
  Result := (c <> nil);
  if Result then
    Result := SetActiveCollation(c);
end;

function GetActiveCollation() : PUCA_DataBook;
begin
  Result := current_Collation;
end;

{procedure error_CpNotFound(ACodePage:TSystemCodePage);
begin
  System.error(reCodesetConversion);
end;}

procedure InitThread;
var
  c : PUCA_DataBook;
begin
  current_DefaultSystemCodePage:=DefaultSystemCodePage;
  current_Map:=getmap(current_DefaultSystemCodePage);
  c:=nil;
  if (DefaultCollationName<>'') then
    c:=FindCollation(DefaultCollationName);
  if (c=nil) and (GetCollationCount()>0) then
    c:=FindCollation(0);
  current_Collation:=c;
end;

procedure FiniThread;
begin
  current_Map:=nil;
end;

function FindMap(const cp: TSystemCodePage): punicodemap;inline;
begin
  if (cp=DefaultSystemCodePage) then
    begin
      { update current_Map in case the DefaultSystemCodePage has been changed }
      if (current_DefaultSystemCodePage<>DefaultSystemCodePage) or not Assigned(current_Map) then
        begin
          FiniThread;
          InitThread;
        end;
      Result:=current_Map;
    end
  else
    Result:=getmap(cp);
end;

{ 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 }
function UTF8CodePointLength(const Str: PAnsiChar; MaxLookAead: PtrInt): Ptrint;
{... taken from ustrings.inc}
var
  p: PByte;
  TempBYTE: Byte;
  CharLen: SizeUint;
  LookAhead: SizeUInt;
  UC: SizeUInt;
begin
  if (Str=nil) then
    exit(0);
  p:=PByte(Str);
  if (p^=0) then
    exit(0);

  p:=PByte(Str);
  if (p^ and $80) = 0 then //One character US-ASCII,
    exit(1);
  TempByte:=p^;
  CharLen:=0;
  while (TempByte and $80)<>0 do
    begin
      TempByte:=(TempByte shl 1) and $FE;
      Inc(CharLen);
    end;
  //Test for the "CharLen" conforms UTF-8 string
  //This means the 10xxxxxx pattern.
  if SizeUInt(CharLen-1)>MaxLookAead then //Insuficient chars in string to decode UTF-8 array
    exit(-1);
  for LookAhead := 1 to CharLen-1 do
    begin
      if ((p[LookAhead] and $80)<>$80) or
         ((p[LookAhead] and $40)<>$00)
      then
        begin
          //Invalid UTF-8 sequence, fallback.
          exit(-1);
        end;
    end;

  Result:=CharLen;
  case CharLen of
    1:  begin
          //Not valid UTF-8 sequence
          Result:=-1;
        end;
    2:  begin
          //Two bytes UTF, convert it
          UC:=(p^ and $1F) shl 6;
          UC:=UC or (p[1] and $3F);
          if UC <= $7F then
            begin
              //Invalid UTF sequence.
              Result:=-1;
            end;
        end;
    3:  begin
          //Three bytes, convert it to unicode
          UC:= (p^ and $0F) shl 12;
          UC:= UC or ((p[1] and $3F) shl 6);
          UC:= UC or ((p[2] and $3F));
          if (UC <= $7FF) or (UC >= $FFFE) or ((UC >= $D800) and (UC <= $DFFF)) then
            begin
              //Invalid UTF-8 sequence
              Result:=-1;
            End;
        end;
    4:  begin
          //Four bytes, convert it to two unicode characters
          UC:= (p^ and $07) shl 18;
          UC:= UC or ((p[1] and $3F) shl 12);
          UC:= UC or ((p[2] and $3F) shl 6);
          UC:= UC or ((p[3] and $3F));
          if (UC < $10000) or (UC > $10FFFF) then
            begin
              Result:=-1;
            end
        end;
    5,6,7:  begin
              //Invalid UTF8 to unicode conversion,
              //mask it as invalid UNICODE too.
              Result:=-1;
            end;
  end;
end;

{ 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 }
function CodePointLength(const Str: PAnsiChar; MaxLookAead: PtrInt): PtrInt;
var
  p : PByte;
begin
  if (current_DefaultSystemCodePage=CP_UTF8) then
    exit(UTF8CodePointLength(Str,MaxLookAead));

  if (Str=nil) then
    exit(0);
  p:=PByte(Str);
  if (p^=0) then
    exit(0);
  if (current_Map=nil) then
    exit(1);

  if (p^>current_Map^.lastchar) then
    exit(-1);
  case current_Map^.map[p^].flag of
    umf_undefined : Result:=-1;
    umf_leadbyte  :
      begin
        if (MaxLookAead>0) then
          Result:=2
        else
          Result:=-1;
      end;
    else
      Result:=1;
  end;
end;

procedure Unicode2AnsiMove(source:punicodechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
var
  locSource : punicodechar;
  locMap : punicodemap;
  destBuffer : PAnsiChar;
  destLen,actualLen, i : SizeInt;
  blockLen : SizeInt;
begin
  if (len=0) then
    begin
      SetLength(dest,0);
      exit;
    end;

  if (cp=CP_UTF8) then
    begin
      destLen:=UnicodeToUtf8(nil,High(SizeUInt),source,len);
      SetLength(dest,destLen);
      UnicodeToUtf8(@dest[1],destLen,source,len);
      SetCodePage(dest,cp,False);
      exit;
    end;
  if (cp=CP_UTF16) then
    begin
      destLen:=len*SizeOf(UnicodeChar);
      SetLength(dest,destLen);
      Move(source^,dest[1],destLen);
      SetCodePage(dest,cp,False);
      exit;
    end;

  locMap:=FindMap(cp);
  if (locMap=nil) then
    begin
      DefaultUnicode2AnsiMove(source,dest,DefaultSystemCodePage,len);
      exit;
    end;
    
  destLen:=3*len;
  SetLength(dest,destLen);
  destBuffer:=@dest[1];
  actualLen:=0;
  locSource:=source;
  for i:=1 to len do
    begin
      blockLen:=getascii(tunicodechar(locSource^),locMap,destBuffer,(destLen-actualLen));
      if (blockLen<0) then
        begin
          destLen:=destLen + 3*(1+len-i);
          SetLength(dest,destLen);
          destBuffer:=@dest[1];
          blockLen:=getascii(tunicodechar(locSource^),locMap,destBuffer,(destLen-actualLen));
        end;
      Inc(destBuffer,blockLen);
      actualLen:=actualLen+blockLen;
      Inc(locSource);
    end;
  if (actualLen<>Length(dest)) then
    SetLength(dest,actualLen);
  if (Length(dest)>0) then
    SetCodePage(dest,cp,False);
end;

procedure Ansi2UnicodeMove(source:PAnsiChar; cp:TSystemCodePage; var dest:UnicodeString; len:SizeInt);
var
  locMap : punicodemap;
  destLen : SizeInt;
begin
  if (len<=0) then
    begin
      SetLength(dest,0);
      exit;
    end;

  if (cp=CP_UTF8) then
    begin
      destLen:=Utf8ToUnicode(nil,source,len);
      if destLen > 0 then
        SetLength(dest,destLen-1)
      else
        SetLength(dest,0);
      Utf8ToUnicode(@dest[1],source,len);
      exit;
    end;
  if (cp=CP_UTF16) then
    begin
      //what if (len mod 2) > 0 ?
      destLen:=len div SizeOf(UnicodeChar);
      SetLength(dest,destLen);
      Move(source^,dest[1],(destLen*SizeOf(UnicodeChar)));
      exit;
    end;

  locMap:=FindMap(cp);
  if (locMap=nil) then
    begin
      DefaultAnsi2UnicodeMove(source,DefaultSystemCodePage,dest,len);
      exit;
    end;

  destLen:=getunicode(source,len,locMap,nil);   
  SetLength(dest,destLen);
  getunicode(source,len,locMap,tunicodestring(@dest[1]));   
end;

{$ifdef MSWINDOWS}
procedure Ansi2WideMove(source:PAnsiChar; cp:TSystemCodePage; var dest:WideString; len:SizeInt);
var
  locMap : punicodemap;
  destLen : SizeInt;
begin
  if (len<=0) then
    begin
      SetLength(dest,0);
      exit;
    end;

  locMap:=FindMap(cp);
  if (locMap=nil) then
    begin
      DefaultAnsi2WideMove(source,DefaultSystemCodePage,dest,len);
      exit;
    end;

  destLen:=getunicode(source,len,locMap,nil);   
  SetLength(dest,destLen);
  getunicode(source,len,locMap,tunicodestring(@dest[1])); 
end;
{$endif MSWINDOWS}

function UpperUnicodeString(const S: UnicodeString): UnicodeString;
begin
  if (UnicodeToUpper(S,IgnoreInvalidSequenceFlag,Result) <> 0) then
    system.error(reRangeError);
end;

function UpperWideString(const S: WideString): WideString;
var
  u : UnicodeString;
begin
  u:=s;
  Result:=UpperUnicodeString(u);
end;

function LowerUnicodeString(const S: UnicodeString): UnicodeString;
begin
  if (UnicodeToLower(S,IgnoreInvalidSequenceFlag,Result) <> 0) then
    system.error(reRangeError);
end;

function LowerWideString(const S: WideString): WideString;
var
  u : UnicodeString;
begin
  u:=s;
  Result:=LowerUnicodeString(u);
end;


function CompareUnicodeStringUCA(p1,p2:PUnicodeChar; l1,l2:PtrInt) : PtrInt;inline;
begin
  Result := IncrementalCompareString(p1,l1,p2,l2,current_Collation);
end;

function CompareUnicodeString(p1,p2:PUnicodeChar; l1,l2:PtrInt) : PtrInt;inline;
begin
  if (Pointer(p1)=Pointer(p2)) then
    exit(0);
  if (l1=0) then
    exit(-l2);
  if (l2=0) then
    exit(l1);
  Result := CompareUnicodeStringUCA(p1,p2,l1,l2);
end;

function CompareUnicodeString(const s1, s2 : UnicodeString;Options : TCompareOptions) : PtrInt;
Var
  us1,us2 : UnicodeString;
begin
  if (current_Collation=nil) then
    exit(OldManager.CompareUnicodeStringProc(s1,s2,Options));
  if (coIgnoreCase in Options) then  
    begin
    us1:=UpperUnicodeString(s1);
    us2:=UpperUnicodeString(s2);
    end
  else
    begin
    us1:=S1;
    us2:=S2;
    end;  
  Result:=CompareUnicodeString(
            PUnicodeChar(Pointer(s1)),
            PUnicodeChar(Pointer(s2)),
            Length(s1),Length(s2)
          );
end;

function CompareWideString(const s1, s2 : WideString; Options : TCompareOptions) : PtrInt;

Var
  us1,us2 : WideString;
   
begin
  if (current_Collation=nil) then
    exit(OldManager.CompareWideStringProc(s1,s2,Options));
  if (coIgnoreCase in Options) then  
    begin
    us1:=UpperWideString(s1);
    us2:=UpperWideString(s2);
    end
  else
    begin
    us1:=S1;
    us2:=S2;
    end;  
  Result:=CompareUnicodeString(
            PUnicodeChar(Pointer(us1)),
            PUnicodeChar(Pointer(us2)),
            Length(us1),Length(us2)
          );
end;

function CompareTextUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
begin
  Result:=CompareUnicodeString(s1,s2,[coIgnoreCase]);
end;

function CompareTextWideString(const s1, s2 : WideString) : PtrInt;
begin
  Result:=CompareWideString(s1,s2,[coIgnoreCase]);
end;

procedure EnsureAnsiLen(var S: AnsiString; const len: SizeInt); inline;
begin
  if (len>length(s)) then
    if (length(s) < 10*256) then
      setlength(s,length(s)+10)
    else
      setlength(s,length(s)+length(s) shr 8);
end;

procedure ConcatCharToAnsiStr(const c: AnsiChar; var S: AnsiString; var index: SizeInt);
begin
  EnsureAnsiLen(s,index);
  pansichar(@s[index])^:=c;
  inc(index);
end;

function UpperAnsiString(const s : ansistring) : ansistring;
var
  p        : PAnsiChar;
  i, slen,
  resindex : SizeInt;
  mblen    : SizeInt;
  us,usl   : UnicodeString;
  locMap   : punicodemap;
  ulen,k,
  aalen,ai : SizeInt;
  aa       : array[0..8] of AnsiChar;
begin
  if (Length(s)=0) then
    exit('');
  if (DefaultSystemCodePage=CP_UTF8) then 
    begin
      //convert to UnicodeString,uppercase,convert back to utf8
      ulen:=Utf8ToUnicode(nil,@s[1],Length(s));
      if ulen>0 then
        SetLength(us,ulen-1);
      Utf8ToUnicode(@us[1],@s[1],Length(s));
      us:=UpperUnicodeString(us);
      
      ulen:=Length(us);
      slen:=UnicodeToUtf8(nil,0,@us[1],ulen);
      SetLength(Result,slen);
      UnicodeToUtf8(@Result[1],slen,@us[1],ulen);
      exit;    
    end;

  locMap:=FindMap(DefaultSystemCodePage);
  if (locMap=nil) then
    exit(System.UpCase(s));

  SetLength(us,2);
  p:=@s[1];
  slen:=length(s);
  SetLength(result,slen+10);
  i:=1;
  resindex:=1;
  while (i<=slen) do
    begin
      mblen:=CodePointLength(p,slen-i);
      if (mblen<=0) then
        begin
          ConcatCharToAnsiStr(p^,result,resindex);
          mblen:=1;
        end
      else
        begin
          SetLength(us,2);
          ulen:=getunicode(p,mblen,locMap,@us[1]);
          if (Length(us)<>ulen) then
            SetLength(us,ulen);
          usl:=UpperUnicodeString(us);
          for k:=1 to Length(usl) do
            begin
              aalen:=getascii(tunicodechar(us[k]),locMap,@aa[Low(aa)],Length(aa));
              for ai:=0 to aalen-1 do
                ConcatCharToAnsiStr(aa[ai],result,resindex);
            end;
        end;
      Inc(p,mblen);
    end;
  SetLength(result,resindex-1);
end;

function LowerAnsiString(const s : ansistring) : ansistring;
var
  p        : PAnsiChar;
  i, slen,
  resindex : SizeInt;
  mblen    : SizeInt;
  us,usl   : UnicodeString;
  locMap   : punicodemap;
  ulen,k,
  aalen,ai : SizeInt;
  aa       : array[0..8] of AnsiChar;
begin
  if (Length(s)=0) then
    exit('');
  if (DefaultSystemCodePage=CP_UTF8) then 
    begin
      //convert to UnicodeString,lowercase,convert back to utf8
      ulen:=Utf8ToUnicode(nil,@s[1],Length(s));
      if ulen>0 then
        SetLength(us,ulen-1);
      Utf8ToUnicode(@us[1],@s[1],Length(s));
      us:=LowerUnicodeString(us);
      
      ulen:=Length(us);
      slen:=UnicodeToUtf8(nil,0,@us[1],ulen);
      SetLength(Result,slen);
      UnicodeToUtf8(@Result[1],slen,@us[1],ulen);
      exit;    
    end;
  locMap:=FindMap(DefaultSystemCodePage);
  if (locMap=nil) then
    exit(System.LowerCase(s));

  SetLength(us,2);
  p:=@s[1];
  slen:=length(s);
  SetLength(result,slen+10);
  i:=1;
  resindex:=1;
  while (i<=slen) do
    begin
      mblen:=CodePointLength(p,slen-i);
      if (mblen<=0) then
        begin
          ConcatCharToAnsiStr(p^,result,resindex);
          mblen:=1;
        end
      else
        begin
          SetLength(us,2);
          ulen:=getunicode(p,mblen,locMap,@us[1]);
          if (Length(us)<>ulen) then
            SetLength(us,ulen);
          usl:=LowerUnicodeString(us);
          for k:=1 to Length(usl) do
            begin
              aalen:=getascii(tunicodechar(us[k]),locMap,@aa[Low(aa)],Length(aa));
              for ai:=0 to aalen-1 do
                ConcatCharToAnsiStr(aa[ai],result,resindex);
            end;
        end;
      Inc(p,mblen);
    end;
  SetLength(result,resindex-1);
end;

procedure ansi2pchar(const s: ansistring; const orgp: pansichar; out p: pansichar);
var
  newlen: sizeint;
begin
  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: PAnsiChar): PAnsiChar;
var
  temp: ansistring;
begin
  temp:=LowerAnsiString(str);
  ansi2pchar(temp,str,result);
end;

function AnsiStrUpper(Str: PAnsiChar): PAnsiChar;
var
  temp: ansistring;
begin
  temp:=UpperAnsiString(str);
  ansi2pchar(temp,str,result);
end;

function CharLengthPChar(const Str: PAnsiChar): PtrInt;
var
  len:PtrInt;
  nextlen: ptrint;
  s: PAnsiChar;
begin
  Result:=0;
  if (Str=nil) or (Byte(Str^)=0) then
    exit;
  s:=str;
  len:=strlen(s);
  repeat
    nextlen:=CodePointLength(s,len);
    { skip invalid/incomplete sequences }
    if (nextlen<0) then
      nextlen:=1;
    Inc(result,nextlen);
    Inc(s,nextlen);
    Dec(len,nextlen);
  until (nextlen=0);
end;

function InternalCompareStrAnsiString(
  const S1, S2     : PAnsiChar;
  const Len1, Len2 : PtrUInt
) : PtrInt;inline;
var
  a, b : UnicodeString;
begin
  a := '';
  Ansi2UnicodeMove(S1,DefaultSystemCodePage,a,Len1);
  b := '';
  Ansi2UnicodeMove(S2,DefaultSystemCodePage,b,Len2);
  Result := CompareUnicodeString(a,b,[]);
end;

function StrLCompAnsiString(S1, S2: PAnsiChar; MaxLen: PtrUInt): PtrInt;
begin
  if (current_Collation=nil) then
    exit(OldManager.StrLCompAnsiStringProc(s1,s2,MaxLen));
  if (MaxLen=0) then
    exit(0);
  Result := InternalCompareStrAnsiString(S1,S2,MaxLen,MaxLen);
end;

function CompareStrAnsiString(const S1, S2: ansistring): PtrInt;
var
  l1, l2 : PtrInt;
begin
  if (current_Collation=nil) then
    exit(OldManager.CompareStrAnsiStringProc(s1,s2));
  if (Pointer(S1)=Pointer(S2)) then
    exit(0);
  l1:=Length(S1);
  l2:=Length(S2);
  if (l1=0) then begin
    if (l2=0) then
      exit(0);
    exit(-l2);
  end;
  if (l2=0) then
    exit(-l1);
  Result := InternalCompareStrAnsiString(@S1[1],@S2[2],l1,l2);
end;

function CompareTextAnsiString(const S1, S2: ansistring): PtrInt;
var
  a,b : ansistring;
begin
  a:=UpperAnsistring(s1);
  b:=UpperAnsistring(s2);
  Result:=CompareStrAnsiString(a,b);
end;

function StrCompAnsiString(S1, S2: PChar): PtrInt;
var
  l1,l2 : PtrInt;
begin
  if (current_Collation=nil) then
    exit(OldManager.StrCompAnsiStringProc(s1,s2));
  l1:=strlen(S1);
  l2:=strlen(S2);
  Result := InternalCompareStrAnsiString(S1,S2,l1,l2);
end;

function StrLICompAnsiString(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
var
  a, b: ansistring;
begin
  if (MaxLen=0) then
    exit(0);
  SetLength(a,MaxLen);
  Move(s1^,a[1],MaxLen);
  SetLength(b,MaxLen);
  Move(s2^,b[1],MaxLen);
  Result:=CompareTextAnsiString(a,b);
end;

function StrICompAnsiString(S1, S2: PChar): PtrInt;
begin
  Result:=CompareTextAnsiString(ansistring(s1),ansistring(s2));
end;

function StrLowerAnsiString(Str: PChar): PChar;
var
  temp: ansistring;
begin
  temp:=LowerAnsiString(str);
  ansi2pchar(temp,str,result);
end;

function StrUpperAnsiString(Str: PChar): PChar;
var
  temp: ansistring;
begin
  temp:=UpperAnsiString(str);
  ansi2pchar(temp,str,result);
end;

//------------------------------------------------------------------------------
procedure SetPascalWideStringManager();
var
  locWideStringManager : TUnicodeStringManager;
begin
  OldManager := widestringmanager;
  locWideStringManager:=widestringmanager;
  With locWideStringManager do
    begin
      Wide2AnsiMoveProc:=@Unicode2AnsiMove;
{$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
      Ansi2WideMoveProc:=@Ansi2WideMove;

      UpperWideStringProc:=@UpperWideString;
      LowerWideStringProc:=@LowerWideString;

      CompareWideStringProc:=@CompareWideString;
{$else FPC_WIDESTRING_EQUAL_UNICODESTRING}
      Ansi2WideMoveProc:=@Ansi2UnicodeMove;

      UpperWideStringProc:=@UpperUnicodeString;
      LowerWideStringProc:=@LowerUnicodeString;

      CompareWideStringProc:=@CompareUnicodeString;
{$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}

      CharLengthPCharProc:=@CharLengthPChar;
      CodePointLengthProc:=@CodePointLength;

      UpperAnsiStringProc:=@UpperAnsiString;
      LowerAnsiStringProc:=@LowerAnsiString;
      CompareStrAnsiStringProc:=@CompareStrAnsiString;
      CompareTextAnsiStringProc:=@CompareTextAnsiString;
      StrCompAnsiStringProc:=@StrCompAnsiString;
      StrICompAnsiStringProc:=@StrICompAnsiString;
      StrLCompAnsiStringProc:=@StrLCompAnsiString;
      StrLICompAnsiStringProc:=@StrLICompAnsiString;
      StrLowerAnsiStringProc:=@StrLowerAnsiString;
      StrUpperAnsiStringProc:=@StrUpperAnsiString;
      ThreadInitProc:=@InitThread;
      ThreadFiniProc:=@FiniThread;
      { Unicode }
      Unicode2AnsiMoveProc:=@Unicode2AnsiMove;
      Ansi2UnicodeMoveProc:=@Ansi2UnicodeMove;
      UpperUnicodeStringProc:=@UpperUnicodeString;
      LowerUnicodeStringProc:=@LowerUnicodeString;
      CompareUnicodeStringProc:=@CompareUnicodeString;
    end;
  SetUnicodeStringManager(locWideStringManager);

  DefaultUnicodeCodePage:=CP_UTF16;
{$ifdef MSWINDOWS}
  DefaultSystemCodePage:=GetACP();
{$ELSE MSWINDOWS}
 {$ifdef UNIX}
  DefaultSystemCodePage:=GetSystemCodepage;
  if (DefaultSystemCodePage = CP_NONE) then
    DefaultSystemCodePage:=CP_UTF8;
  {$ifdef FPCRTL_FILESYSTEM_UTF8}
  DefaultFileSystemCodePage:=CP_UTF8;
  {$else}
  DefaultFileSystemCodePage:=DefaultSystemCodepage;
  {$endif}
  DefaultRTLFileSystemCodePage:=DefaultFileSystemCodePage;
 {$ELSE UNIX}
  if Assigned (WideStringManager.GetStandardCodePageProc) then
   DefaultSystemCodePage := WideStringManager.GetStandardCodePageProc (scpAnsi)
  else
   DefaultSystemCodePage := CP_NONE;
  DefaultFileSystemCodePage := DefaultSystemCodePage;
  DefaultRTLFileSystemCodePage := DefaultSystemCodePage;
 {$endif UNIX}
{$endif MSWINDOWS}
end;


initialization
  current_Collation := nil;
  SetPascalWideStringManager();
  InitThread();

finalization
  FiniThread();

end.