File: iosxwstr.pp

package info (click to toggle)
fpc 3.0.0%2Bdfsg-11%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 273,420 kB
  • ctags: 106,632
  • sloc: pascal: 2,840,574; xml: 152,225; ansic: 9,635; asm: 8,297; java: 5,346; sh: 3,991; yacc: 3,745; php: 3,281; makefile: 2,635; lex: 2,538; sql: 267; cpp: 145; perl: 134; sed: 132; csh: 34; tcl: 7
file content (665 lines) | stat: -rw-r--r-- 21,135 bytes parent folder | download | duplicates (5)
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
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2015 by Jonas Maebe,
    member of the Free Pascal development team.

    CoreFoundation-based wide string support

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 **********************************************************************}

{$mode objfpc}
{$implicitexceptions off}

unit iosxwstr;

interface

{$linkframework CoreFoundation}

  procedure SetCFWidestringManager;

implementation

  uses
    unixcp,
    { for access to libiconv-based routines }
    cwstring,
    MacTypes,
    CFBase,CFString,CFStringEncodingExt,CFLocale;

  procedure fpc_rangeerror; [external name 'FPC_RANGEERROR'];

  var
    CWStringWideStringManager: TUnicodeStringManager;

  procedure InitThread;
    begin
      { we don't need anything special, but since we may use cwstring itself,
        call through to it }
      CWStringWideStringManager.ThreadInitProc;
    end;


  procedure FiniThread;
    begin
      { we don't need anything special, but since we may use cwstring itself,
        call through to it }
      CWStringWideStringManager.ThreadFiniProc;
    end;


  function get_cfencoding_for_cp(cp: TSystemCodePage): CFStringEncoding;
    var
      defscp: TSystemCodePage;
    begin
      { translate placeholder code pages }
      if (cp=CP_ACP) or
         (cp=CP_OEMCP) then
        cp:=DefaultSystemCodePage;
      result:=CFStringConvertWindowsCodepageToEncoding(cp);
    end;


  procedure GetAnsiDataFromCFString(str: CFstringRef; cfcp: CFStringEncoding; estimated_length: SizeInt; var dest: RawByteString);
    var
      range: CFRange;
      encodedlen,convertedchars: CFIndex;
      strlen: SizeInt;
    begin
      { first rough estimate for the length }
      setlength(dest,estimated_length);
      { try to convert }
      range.location:=0;
      strlen:=CFStringGetLength(str);
      range.length:=strlen;
      convertedchars:=CFStringGetBytes(str,range,cfcp,ByteParameter('?'),false,UInt8Ptr(dest),estimated_length,encodedlen);
      { failed -> bail out }
      if convertedchars<0 then
        begin
          CFRelease(str);
          runerror(231);
        end
      { if partially succesful, recreate with the required len }
      else if convertedchars<strlen then
        begin
          setlength(dest,encodedlen);
          { try again }
          convertedchars:=CFStringGetBytes(str,range,cfcp,ByteParameter('?'),false,UInt8Ptr(dest),encodedlen,encodedlen);
          { failed again ? }
          if convertedchars<>strlen then
            begin
              CFRelease(str);
              runerror(231);
            end;
        end;
      { truncate }
      setlength(dest,encodedlen);
    end;


  function CFStringCreateFromAnsiData(data: pchar; len: SizeInt; cp: TSystemCodePage): CFStringRef;
    var
      strlen,encodedlen: CFIndex;
      range: CFRange;
      cfcp: CFStringEncoding;
    begin
      result:=nil;
      { get source cf codepage }
      cfcp:=get_cfencoding_for_cp(cp);
      { unsupported encoding -> try libiconv instead }
      if cfcp=kCFStringEncodingInvalidId then
        exit;
      { make a cfstring from the original data }
      result:=CFStringCreateWithBytesNoCopy(nil,UnivPtr(data),len,cfcp,false,kCFAllocatorNull);
    end;


  function CFStringCreateFromAnsiDataOptionallyViaUnicodeString(data: pchar; len: SizeInt; cp: TSystemCodePage; out wtemp: UnicodeString): CFStringRef;
    begin
      result:=CFStringCreateFromAnsiData(data,len,cp);
      { failed -> translate via libiconv and then create using the unicodestring
        characters; since we use the no-copy constructor for performance
        reasons, the unicodestring has to survive this routine }
      if not assigned(result) then
        begin
          CWStringWideStringManager.Ansi2UnicodeMoveProc(data,cp,wtemp,len);
          result:=CFStringCreateWithCharactersNoCopy(nil,UniCharPtr(wtemp),len,kCFAllocatorNull);
        end;
    end;


  function CFStringCreateFromWideData(data: pwidechar; len: SizeInt): CFStringRef; inline;
    begin
      { make a cfstring from the utf-16 data }
      result:=CFStringCreateWithCharactersNoCopy(nil,UniCharPtr(data),len,kCFAllocatorNull);
    end;


  function CFStringCreateFromWideDataOptionallyViaUUTF8String(data: pwidechar; len: SizeInt; out temp: RawByteString): CFStringRef;
    begin
      result:=CFStringCreateFromWideData(data,len);
      { failed -> translate to UTF-8 via libiconv to filter out any
        potentially invalid characters and then create using the unicodestring
        characters; since we use the no-copy constructor for performance
        reasons, the unicodestring has to survive this routine }
      if not assigned(result) then
        begin
          CWStringWideStringManager.Unicode2AnsiMoveProc(data,temp,CP_UTF8,len);
          result:=CFStringCreateWithBytesNoCopy(nil,UnivPtr(temp),length(temp),kCFStringEncodingUTF8,false,kCFAllocatorNull);
          if not assigned(result) then
            runerror(231)
        end;
    end;


  procedure Wide2AnsiMove(source:pwidechar; var dest:RawByteString; cp:TSystemCodePage; len:SizeInt);
    var
      str: CFStringRef;
      strlen,estimatedlen: CFIndex;
      cfcp: CFStringEncoding;
    begin
      str:=nil;
      { get target cf codepage }
      cfcp:=get_cfencoding_for_cp(cp);
      { unsupported encoding -> default move }
      if cfcp<>kCFStringEncodingInvalidId then
        { make a cfstring from the utf-16 data }
        str:=CFStringCreateFromWideData(source,len);
      { You cannot create a CFString for a sequence that contains an error :/
        We want to replace the error positions with '?' -> fall back to libiconv
      }
      if not assigned(str) then
        begin
          CWStringWideStringManager.Unicode2AnsiMoveProc(source,dest,cp,len);
          exit;
        end;

      GetAnsiDataFromCFString(str,cfcp,len*3,dest);
      { set code page }
      SetCodePage(dest,cp,false);
      { release cfstring }
      CFRelease(str);
    end;


  procedure Ansi2WideMove(source:pchar; cp:TSystemCodePage; var dest:widestring; len:SizeInt);
    var
      str: CFStringRef;
      strlen,encodedlen: CFIndex;
      range: CFRange;
      cfcp: CFStringEncoding;
    begin
      str:=CFStringCreateFromAnsiData(source,len,cp);
      { You cannot create a CFString for a sequence that contains an error :/
        We want to replace the error positions with '?' -> fall back to libiconv
      }
      if not assigned(str) then
        begin
          CWStringWideStringManager.Ansi2UnicodeMoveProc(source,cp,dest,len);
          exit;
        end;

      { convert }
      range.location:=0;
      strlen:=CFStringGetLength(str);
      range.length:=strlen;
      setlength(dest,strlen);
      CFStringGetCharacters(str,range,UniCharPtr(dest));
      { release cfstring }
      CFRelease(str);
    end;


  function LowerWideString(const s : WideString) : WideString;
    var
      str: CFStringRef;
      mstr: CFMutableStringRef;
      range: CFRange;
      encodedlen: CFIndex;
      locale: CFLocaleRef;
      temp: RawByteString;
    begin
      { empty string -> exit }
      if s='' then
        begin
          result:='';
          exit;
        end;
      { create cfstring from the string data }
      str:=CFStringCreateFromWideDataOptionallyViaUUTF8String(pwidechar(s),length(s),temp);
      { convert to mutable cfstring }
      mstr:=CFStringCreateMutableCopy(nil,0,str);
      { lowercase }
      locale:=CFLocaleCopyCurrent;
      CFStringLowercase(mstr,locale);
      CFRelease(locale);
      { extract the data again }
      range.location:=0;
      range.length:=CFStringGetLength(CFStringRef(mstr));
      setlength(result,range.length);
      CFStringGetCharacters(mstr,range,UniCharPtr(result));
      CFRelease(mstr);
      CFRelease(str);
    end;


  function UpperWideString(const s : WideString) : WideString;
  var
    str: CFStringRef;
    mstr: CFMutableStringRef;
    range: CFRange;
    encodedlen: CFIndex;
    locale: CFLocaleRef;
    temp: RawByteString;
  begin
    { empty string -> exit }
    if s='' then
      begin
        result:='';
        exit;
      end;
    { create cfstring from the string data }
    str:=CFStringCreateFromWideDataOptionallyViaUUTF8String(pwidechar(s),length(s),temp);
    { convert to mutable cfstring }
    mstr:=CFStringCreateMutableCopy(nil,0,str);
    { lowercase }
    locale:=CFLocaleCopyCurrent;
    CFStringUppercase(mstr,locale);
    CFRelease(locale);
    { extract the data again }
    range.location:=0;
    range.length:=CFStringGetLength(CFStringRef(mstr));
    setlength(result,range.length);
    CFStringGetCharacters(mstr,range,UniCharPtr(result));
    CFRelease(mstr);
    CFRelease(str);
  end;


  function UpperLowerAnsiString(const s: AnsiString; upper: boolean): AnsiString;
    var
      str: CFStringRef;
      mstr: CFMutableStringRef;
      cfcp: CFStringEncoding;
      locale: CFLocaleRef;
      wtemp: UnicodeString;
      range: CFRange;
    begin
      if s='' then
        begin
          result:='';
          exit
        end;
      str:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(pchar(s),length(s),StringCodePage(s),wtemp);
      { unsupported encoding for either CF or iconv -> return original string }
      if not assigned(str) then
        begin
          result:=s;
          exit;
        end;
      { convert to mutable cfstring }
      mstr:=CFStringCreateMutableCopy(nil,0,str);
      CFRelease(str);
      { upper/lowercase }
      locale:=CFLocaleCopyCurrent;
      if upper then
        CFStringUppercase(mstr,locale)
      else
        CFStringLowercase(mstr,locale);
      CFRelease(locale);
      { convert back to ansistring }
      cfcp:=get_cfencoding_for_cp(StringCodePage(s));
      if cfcp<>kCFStringEncodingInvalidId then
        begin
          GetAnsiDataFromCFString(CFStringRef(mstr),cfcp,length(s),RawByteString(result));
          SetCodePage(RawByteString(result),StringCodePage(s),false);
        end
      else
        begin
          { unsupported encoding -> use libiconv instead via UTF-16
            intermediate }
          range.location:=0;
          range.length:=CFStringGetLength(mstr);
          SetLength(wtemp,range.length);
          CFStringGetCharacters(mstr,range,UniCharPtr(wtemp));
          CWStringWideStringManager.Wide2AnsiMoveProc(pwidechar(wtemp),RawByteString(result),StringCodePage(s),range.length);
        end;
      CFRelease(mstr);
    end;


  function LowerAnsiString(const s: AnsiString): AnsiString;
    begin
      result:=UpperLowerAnsiString(s,false);
    end;


  function UpperAnsiString(const s: AnsiString): AnsiString;
    begin
      result:=UpperLowerAnsiString(s,true);
    end;


  function CompareCFStrings(const s1, s2: CFStringRef; case_insensitive: boolean): longint;
    var
      flags: CFStringCompareFlags;
    begin
      flags:=0;
      if case_insensitive then
        flags:=flags or kCFCompareCaseInsensitive;
      result:=CFStringCompare(s1,s2,flags)
    end;


  function CompareWideString(const s1, s2 : WideString) : PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      temp1, temp2: RawByteString;
    begin
      cfstr1:=CFStringCreateFromWideDataOptionallyViaUUTF8String(pwidechar(s1),length(s1),temp1);
      cfstr2:=CFStringCreateFromWideDataOptionallyViaUUTF8String(pwidechar(s2),length(s2),temp2);
      result:=CompareCFStrings(cfstr1,cfstr2,false);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;

  function CompareTextWideString(const s1, s2 : WideString): PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      temp1, temp2: RawByteString;
    begin
      cfstr1:=CFStringCreateFromWideDataOptionallyViaUUTF8String(pwidechar(s1),length(s1),temp1);
      cfstr2:=CFStringCreateFromWideDataOptionallyViaUUTF8String(pwidechar(s2),length(s2),temp2);
      result:=CompareCFStrings(cfstr1,cfstr2,true);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;


  function InternalCodePointLength(const Str: PChar; cfcp: CFStringEncoding; maxlookahead: ptrint): PtrInt;
    var
      cfstr: CFStringRef;
    begin
      result:=0;
      { try creating a string with the first 1, 2, ... bytes until we find a
        valid one }
      while (str[result]<>#0) and
            (result<maxlookahead) do
        begin
          inc(result);
          cfstr:=CFStringCreateWithBytesNoCopy(nil,UnivPtr(Str),result,cfcp,false,kCFAllocatorNull);
          if assigned(cfstr) then
            begin
              CFRelease(cfstr);
              exit;
            end;
        end;
      result:=-1;
    end;


  function CharLengthPChar(const Str: PChar): PtrInt;
    var
      cfstr: CFStringRef;
      cfcp: CFStringEncoding;
      s: PChar;
      tmplen: PtrInt;
    begin
      result:=0;
      if str[0]=#0 then
        exit;
      cfcp:=get_cfencoding_for_cp(DefaultSystemCodePage);
      if cfcp=kCFStringEncodingInvalidId then
        begin
          { or -1? }
          result:=strlen(Str);
          exit
        end;
      s:=str;
      repeat
        tmplen:=InternalCodePointLength(s,cfcp,8);
        { invalid -> skip }
        if tmplen=-1 then
          tmplen:=1;
        inc(s,tmplen);
        inc(result);
      until s[0]=#0;
    end;


  function CodePointLength(const Str: PChar; maxlookahead: ptrint): PtrInt;
    var
      cfstr: CFStringRef;
      cfcp: CFStringEncoding;
    begin
      result:=0;
      if str[0]=#0 then
        exit;
      cfcp:=get_cfencoding_for_cp(DefaultSystemCodePage);
      if cfcp=kCFStringEncodingInvalidId then
        begin
          { if we would return -1, then the caller would keep trying with
            longer and longer sequences, but that wouldn't change anything }
          result:=1;
          exit
        end;
      result:=InternalCodePointLength(str,cfcp,maxlookahead);
    end;


  function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      wtemp1, wtemp2: UnicodeString;
    begin
      cfstr1:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(pchar(s1),length(s1),StringCodePage(s1),wtemp1);
      cfstr2:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(pchar(s2),length(s2),StringCodePage(s2),wtemp2);
      result:=CompareCFStrings(cfstr1,cfstr2,false);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;


  function StrCompAnsi(s1,s2 : PChar): PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      wtemp1, wtemp2: UnicodeString;
    begin
      cfstr1:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s1,strlen(s1),DefaultSystemCodePage,wtemp1);
      cfstr2:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s2,strlen(s2),DefaultSystemCodePage,wtemp2);
      result:=CompareCFStrings(cfstr1,cfstr2,false);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;


  function AnsiCompareText(const S1, S2: ansistring): PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      wtemp1, wtemp2: UnicodeString;
    begin
      cfstr1:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(pchar(s1),length(s1),DefaultSystemCodePage,wtemp1);
      cfstr2:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(pchar(s2),length(s2),DefaultSystemCodePage,wtemp2);
      result:=CompareCFStrings(cfstr1,cfstr2,true);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;


  function AnsiStrIComp(S1, S2: PChar): PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      wtemp1, wtemp2: UnicodeString;
    begin
      cfstr1:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s1,strlen(s1),DefaultSystemCodePage,wtemp1);
      cfstr2:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s2,strlen(s2),DefaultSystemCodePage,wtemp2);
      result:=CompareCFStrings(cfstr1,cfstr2,true);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;


  function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      wtemp1, wtemp2: UnicodeString;
    begin
      cfstr1:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s1,MaxLen,StringCodePage(s1),wtemp1);
      cfstr2:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s2,MaxLen,StringCodePage(s2),wtemp2);
      result:=CompareCFStrings(cfstr1,cfstr2,false);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;


  function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
    var
      cfstr1, cfstr2: CFStringRef;
      wtemp1, wtemp2: UnicodeString;
    begin
      cfstr1:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s1,MaxLen,StringCodePage(s1),wtemp1);
      cfstr2:=CFStringCreateFromAnsiDataOptionallyViaUnicodeString(s2,MaxLen,StringCodePage(s2),wtemp2);
      result:=CompareCFStrings(cfstr1,cfstr2,true);
      CFRelease(cfstr1);
      CFRelease(cfstr2);
    end;


  procedure ansi2pchar(const s: ansistring; const orgp: pchar; out p: pchar);
    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: PChar): PChar;
    var
      temp: ansistring;
    begin
      temp:=loweransistring(str);
      ansi2pchar(temp,str,result);
    end;


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


  function GetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
    var
      langinfo: pchar;
    begin
      { don't use CFStringGetSystemEncoding, that one returns MacRoman on e.g.
        an English system, which is definitely not what we want. Since there are
        no "ansi" interfaces on OS X and all APIs support all characters, always
        use UTF-8. Additionally,  Darwin always uses UTF-8 for file system
        operations }
      result:=CP_UTF8;
    end;


  procedure SetStdIOCodePage(var T: Text); inline;
    begin
      case TextRec(T).Mode of
        fmInput:TextRec(T).CodePage:=GetStandardCodePage(scpConsoleInput);
        fmOutput:TextRec(T).CodePage:=GetStandardCodePage(scpConsoleOutput);
      end;
    end;


  procedure SetStdIOCodePages; inline;
    begin
      SetStdIOCodePage(Input);
      SetStdIOCodePage(Output);
      SetStdIOCodePage(ErrOutput);
      SetStdIOCodePage(StdOut);
      SetStdIOCodePage(StdErr);
    end;


  procedure SetCFWideStringManager;
    var
      CFWideStringManager : TUnicodeStringManager;
    begin
      GetUnicodeStringManager(CWStringWideStringManager);
      CFWideStringManager:=CWStringWideStringManager;
      with CFWideStringManager do
        begin
          Wide2AnsiMoveProc:=@Wide2AnsiMove;
          Ansi2WideMoveProc:=@Ansi2WideMove;

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

          CompareWideStringProc:=@CompareWideString;
          CompareTextWideStringProc:=@CompareTextWideString;

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

          UpperAnsiStringProc:=@UpperAnsiString;
          LowerAnsiStringProc:=@LowerAnsiString;
          CompareStrAnsiStringProc:=@CompareStrAnsiString;
          CompareTextAnsiStringProc:=@AnsiCompareText;
          StrCompAnsiStringProc:=@StrCompAnsi;
          StrICompAnsiStringProc:=@AnsiStrIComp;
          StrLCompAnsiStringProc:=@AnsiStrLComp;
          StrLICompAnsiStringProc:=@AnsiStrLIComp;
          StrLowerAnsiStringProc:=@AnsiStrLower;
          StrUpperAnsiStringProc:=@AnsiStrUpper;
          ThreadInitProc:=@InitThread;
          ThreadFiniProc:=@FiniThread;
          { Unicode }
          Unicode2AnsiMoveProc:=@Wide2AnsiMove;
          Ansi2UnicodeMoveProc:=@Ansi2WideMove;
          UpperUnicodeStringProc:=@UpperWideString;
          LowerUnicodeStringProc:=@LowerWideString;
          CompareUnicodeStringProc:=@CompareWideString;
          CompareTextUnicodeStringProc:=@CompareTextWideString;
          { CodePage }
          GetStandardCodePageProc:=@GetStandardCodePage;
        end;
      SetUnicodeStringManager(CFWideStringManager);
    end;


initialization
  SetCFWideStringManager;

  { set the DefaultSystemCodePage }
  DefaultSystemCodePage:=GetStandardCodePage(scpAnsi);
  DefaultFileSystemCodePage:=GetStandardCodePage(scpFileSystemSingleByte);
  DefaultRTLFileSystemCodePage:=DefaultFileSystemCodePage;

  SetStdIOCodePages;

  { don't call init, we don't need to do anything and the cwstring routine we
    call through has already been called from the init code of cwstring itself
  InitThread;
  }
finalization
  { don't call for the same reason as not calling FiniThread
  FiniThread;
  }
  { restore previous widestring manager so that subsequent calls
    into the widestring manager won't trigger the finalized functionality }
  SetWideStringManager(CWStringWideStringManager);
end.