Debian Codegraph @

src/base/android/castleandroidinternalcwstring.pas

raw
1 {
2 This file is part of the Free Pascal run time library.
3 Copyright (c) 2013 by Yury Sidorov,
4 member of the Free Pascal development team.
5
6 Wide string support for Android
7
8 This file is adapted from the FPC RTL source code, as such
9 the license and copyright information of FPC RTL applies here.
10 That said, the license of FPC RTL happens to be *exactly*
11 the same as used by the "Castle Game Engine": LGPL (version 2.1)
12 with "static linking exception" (with exactly the same wording
13 of the "static linking exception").
14 See the file COPYING.txt, included in this distribution, for details about
15 the copyright of "Castle Game Engine".
16 See http://www.freepascal.org/faq.var#general-license about the copyright
17 of FPC RTL.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 **********************************************************************}
23
24 {$I castleconf.inc}
25 {$inline on}
26 {$implicitexceptions off}
27
28 { Reworked FPC CWString, to delay loading of library until Android
29 activity started, necessary on some Android versions.
30 @exclude Not documented for PasDoc. }
31 unit CastleAndroidInternalCWString;
32
33 { This should be defined only in FPC >= 3.0.2. }
34 {$define FPC_NEW_VERSION_WITH_UNICODE}
35 {$ifdef VER3_0_0}
36 {$undef FPC_NEW_VERSION_WITH_UNICODE}
37 {$endif}
38 {$ifdef VER2_6}
39 {$undef FPC_NEW_VERSION_WITH_UNICODE}
40 {$endif}
41
42 interface
43
44 procedure SetCWidestringManager;
45
46 { Call this once Android activity started to initialize CWString. }
47 procedure InitializeAndroidCWString;
48
49 implementation
50
51 uses dynlibs, CastleAndroidInternalLog;
52
53 type
54 UErrorCode = SizeInt;
55 int32_t = Integer;
56 uint32_t = Cardinal;
57 PUConverter = pointer;
58 PUCollator = pointer;
59 UBool = LongBool;
60
61 var
62 hlibICU: TLibHandle;
63 hlibICUi18n: TLibHandle;
64 ucnv_open: function (converterName: PAnsiChar; var pErrorCode: UErrorCode): PUConverter; cdecl;
65 ucnv_close: procedure (converter: PUConverter); cdecl;
66 ucnv_setSubstChars: procedure (converter: PUConverter; subChars: PAnsiChar; len: byte; var pErrorCode: UErrorCode); cdecl;
67 ucnv_setFallback: procedure (cnv: PUConverter; usesFallback: UBool); cdecl;
68 ucnv_fromUChars: function (cnv: PUConverter; dest: PAnsiChar; destCapacity: int32_t; src: PUnicodeChar; srcLength: int32_t; var pErrorCode: UErrorCode): int32_t; cdecl;
69 ucnv_toUChars: function (cnv: PUConverter; dest: PUnicodeChar; destCapacity: int32_t; src: PAnsiChar; srcLength: int32_t; var pErrorCode: UErrorCode): int32_t; cdecl;
70 u_strToUpper: function (dest: PUnicodeChar; destCapacity: int32_t; src: PUnicodeChar; srcLength: int32_t; locale: PAnsiChar; var pErrorCode: UErrorCode): int32_t; cdecl;
71 u_strToLower: function (dest: PUnicodeChar; destCapacity: int32_t; src: PUnicodeChar; srcLength: int32_t; locale: PAnsiChar; var pErrorCode: UErrorCode): int32_t; cdecl;
72 u_strCompare: function (s1: PUnicodeChar; length1: int32_t; s2: PUnicodeChar; length2: int32_t; codePointOrder: UBool): int32_t; cdecl;
73 u_strCaseCompare: function (s1: PUnicodeChar; length1: int32_t; s2: PUnicodeChar; length2: int32_t; options: uint32_t; var pErrorCode: UErrorCode): int32_t; cdecl;
74
75 ucol_open: function(loc: PAnsiChar; var status: UErrorCode): PUCollator; cdecl;
76 ucol_close: procedure (coll: PUCollator); cdecl;
77 ucol_strcoll: function (coll: PUCollator; source: PUnicodeChar; sourceLength: int32_t; target: PUnicodeChar; targetLength: int32_t): int32_t; cdecl;
78 ucol_setStrength: procedure (coll: PUCollator; strength: int32_t); cdecl;
79 u_errorName: function (code: UErrorCode): PAnsiChar; cdecl;
80
81 threadvar
82 ThreadDataInited: boolean;
83 DefConv, LastConv: PUConverter;
84 LastCP: TSystemCodePage;
85 DefColl: PUCollator;
86
87 function OpenConverter(const name: ansistring): PUConverter;
88 var
89 err: UErrorCode;
90 begin
91 err:=0;
92 Result:=ucnv_open(PAnsiChar(name), err);
93 if Result <> nil then begin
94 ucnv_setSubstChars(Result, '?', 1, err);
95 ucnv_setFallback(Result, True);
96 end;
97 end;
98
99 procedure InitThreadData;
100 var
101 err: UErrorCode;
102 col: PUCollator;
103 begin
104 if (hlibICU = 0) or ThreadDataInited then
105 exit;
106 ThreadDataInited:=True;
107 DefConv:=OpenConverter('utf8');
108 err:=0;
109 col:=ucol_open(nil, err);
110 if col <> nil then
111 ucol_setStrength(col, 2);
112 DefColl:=col;
113 end;
114
115 {$ifdef FPC_NEW_VERSION_WITH_UNICODE}
116
117 function GetConverter(cp: TSystemCodePage): PUConverter;
118 var
119 s: ansistring;
120 begin
121 if hlibICU = 0 then begin
122 Result:=nil;
123 exit;
124 end;
125 InitThreadData;
126 if (cp = CP_UTF8) or (cp = CP_ACP) then
127 Result:=DefConv
128 else begin
129 if cp <> LastCP then begin
130 Str(cp, s);
131 LastConv:=OpenConverter('cp' + s);
132 LastCP:=cp;
133 end;
134 Result:=LastConv;
135 end;
136 end;
137
138 procedure Unicode2AnsiMove(source: PUnicodeChar; var dest: RawByteString; cp: TSystemCodePage; len: SizeInt);
139 var
140 len2: SizeInt;
141 conv: PUConverter;
142 err: UErrorCode;
143 begin
144 if len = 0 then begin
145 dest:='';
146 exit;
147 end;
148 conv:=GetConverter(cp);
149 if (conv = nil) and not ( (cp = CP_UTF8) or (cp = CP_ACP) ) then begin
150 // fallback implementation
151 DefaultUnicode2AnsiMove(source,dest,DefaultSystemCodePage,len);
152 exit;
153 end;
154
155 len2:=len*3;
156 SetLength(dest, len2);
157 err:=0;
158 if conv <> nil then
159 len2:=ucnv_fromUChars(conv, PAnsiChar(dest), len2, source, len, err)
160 else begin
161 // Use UTF-8 conversion from RTL
162 cp:=CP_UTF8;
163 len2:=UnicodeToUtf8(PAnsiChar(dest), len2, source, len) - 1;
164 end;
165 if len2 > Length(dest) then begin
166 SetLength(dest, len2);
167 err:=0;
168 if conv <> nil then
169 len2:=ucnv_fromUChars(conv, PAnsiChar(dest), len2, source, len, err)
170 else
171 len2:=UnicodeToUtf8(PAnsiChar(dest), len2, source, len) - 1;
172 end;
173 if len2 < 0 then
174 len2:=0;
175 SetLength(dest, len2);
176 SetCodePage(dest, cp, False);
177 end;
178
179 procedure Ansi2UnicodeMove(source:pchar;cp : TSystemCodePage;var dest:unicodestring;len:SizeInt);
180 var
181 len2: SizeInt;
182 conv: PUConverter;
183 err: UErrorCode;
184 begin
185 if len = 0 then begin
186 dest:='';
187 exit;
188 end;
189 conv:=GetConverter(cp);
190 if (conv = nil) and not ( (cp = CP_UTF8) or (cp = CP_ACP) ) then begin
191 // fallback implementation
192 DefaultAnsi2UnicodeMove(source,DefaultSystemCodePage,dest,len);
193 exit;
194 end;
195
196 len2:=len;
197 SetLength(dest, len2);
198 err:=0;
199 if conv <> nil then
200 len2:=ucnv_toUChars(conv, PUnicodeChar(dest), len2, source, len, err)
201 else
202 // Use UTF-8 conversion from RTL
203 len2:=Utf8ToUnicode(PUnicodeChar(dest), len2, source, len) - 1;
204 if len2 > Length(dest) then begin
205 SetLength(dest, len2);
206 err:=0;
207 if conv <> nil then
208 len2:=ucnv_toUChars(conv, PUnicodeChar(dest), len2, source, len, err)
209 else
210 len2:=Utf8ToUnicode(PUnicodeChar(dest), len2, source, len) - 1;
211 end;
212 if len2 < 0 then
213 len2:=0;
214 SetLength(dest, len2);
215 end;
216
217 function UpperUnicodeString(const s : UnicodeString) : UnicodeString;
218 var
219 len, len2: SizeInt;
220 err: UErrorCode;
221 begin
222 if hlibICU = 0 then begin
223 // fallback implementation
224 Result:=UnicodeString(UpCase(AnsiString(s)));
225 exit;
226 end;
227 len:=Length(s);
228 SetLength(Result, len);
229 if len = 0 then
230 exit;
231 err:=0;
232 len2:=u_strToUpper(PUnicodeChar(Result), len, PUnicodeChar(s), len, nil, err);
233 if len2 > len then begin
234 SetLength(Result, len2);
235 err:=0;
236 len2:=u_strToUpper(PUnicodeChar(Result), len2, PUnicodeChar(s), len, nil, err);
237 end;
238 SetLength(Result, len2);
239 end;
240
241 function LowerUnicodeString(const s : UnicodeString) : UnicodeString;
242 var
243 len, len2: SizeInt;
244 err: UErrorCode;
245 begin
246 if hlibICU = 0 then begin
247 // fallback implementation
248 Result:=UnicodeString(LowerCase(AnsiString(s)));
249 exit;
250 end;
251 len:=Length(s);
252 SetLength(Result, len);
253 if len = 0 then
254 exit;
255 err:=0;
256 len2:=u_strToLower(PUnicodeChar(Result), len, PUnicodeChar(s), len, nil, err);
257 if len2 > len then begin
258 SetLength(Result, len2);
259 err:=0;
260 len2:=u_strToLower(PUnicodeChar(Result), len2, PUnicodeChar(s), len, nil, err);
261 end;
262 SetLength(Result, len2);
263 end;
264
265 function _CompareStr(const S1, S2: UnicodeString): PtrInt;
266 var
267 count, count1, count2: SizeInt;
268 begin
269 result := 0;
270 Count1 := Length(S1);
271 Count2 := Length(S2);
272 if Count1>Count2 then
273 Count:=Count2
274 else
275 Count:=Count1;
276 result := CompareByte(PUnicodeChar(S1)^, PUnicodeChar(S2)^, Count*SizeOf(UnicodeChar));
277 if result=0 then
278 result:=Count1 - Count2;
279 end;
280
281 function CompareUnicodeString(const s1, s2 : UnicodeString; Options : TCompareOptions) : PtrInt;
282 const
283 U_COMPARE_CODE_POINT_ORDER = $8000;
284 var
285 err: UErrorCode;
286 begin
287 if hlibICU = 0 then begin
288 // fallback implementation
289 Result:=_CompareStr(s1, s2);
290 exit;
291 end;
292 if (coIgnoreCase in Options) then begin
293 err:=0;
294 Result:=u_strCaseCompare(PUnicodeChar(s1), Length(s1), PUnicodeChar(s2), Length(s2), U_COMPARE_CODE_POINT_ORDER, err);
295 end
296 else begin
297 InitThreadData;
298 if DefColl <> nil then
299 Result:=ucol_strcoll(DefColl, PUnicodeChar(s1), Length(s1), PUnicodeChar(s2), Length(s2))
300 else
301 Result:=u_strCompare(PUnicodeChar(s1), Length(s1), PUnicodeChar(s2), Length(s2), True);
302 end;
303 end;
304
305 function UpperAnsiString(const s : AnsiString) : AnsiString;
306 begin
307 Result:=AnsiString(UpperUnicodeString(UnicodeString(s)));
308 end;
309
310 function LowerAnsiString(const s : AnsiString) : AnsiString;
311 begin
312 Result:=AnsiString(LowerUnicodeString(UnicodeString(s)));
313 end;
314
315 function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
316 begin
317 Result:=CompareUnicodeString(UnicodeString(s1), UnicodeString(s2), []);
318 end;
319
320 function StrCompAnsi(s1,s2 : PChar): PtrInt;
321 begin
322 Result:=CompareUnicodeString(UnicodeString(s1), UnicodeString(s2), []);
323 end;
324
325 function AnsiCompareText(const S1, S2: ansistring): PtrInt;
326 begin
327 Result:=CompareUnicodeString(UnicodeString(s1), UnicodeString(s2), [coIgnoreCase]);
328 end;
329
330 function AnsiStrIComp(S1, S2: PChar): PtrInt;
331 begin
332 Result:=CompareUnicodeString(UnicodeString(s1), UnicodeString(s2), [coIgnoreCase]);
333 end;
334
335 function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
336 var
337 as1, as2: ansistring;
338 begin
339 SetString(as1, S1, MaxLen);
340 SetString(as2, S2, MaxLen);
341 Result:=CompareUnicodeString(UnicodeString(as1), UnicodeString(as2), []);
342 end;
343
344 function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
345 var
346 as1, as2: ansistring;
347 begin
348 SetString(as1, S1, MaxLen);
349 SetString(as2, S2, MaxLen);
350 Result:=CompareUnicodeString(UnicodeString(as1), UnicodeString(as2), [coIgnoreCase]);
351 end;
352
353 function AnsiStrLower(Str: PChar): PChar;
354 var
355 s, res: ansistring;
356 begin
357 s:=Str;
358 res:=LowerAnsiString(s);
359 if Length(res) > Length(s) then
360 SetLength(res, Length(s));
361 Move(PAnsiChar(res)^, Str, Length(res) + 1);
362 Result:=Str;
363 end;
364
365 function AnsiStrUpper(Str: PChar): PChar;
366 var
367 s, res: ansistring;
368 begin
369 s:=Str;
370 res:=UpperAnsiString(s);
371 if Length(res) > Length(s) then
372 SetLength(res, Length(s));
373 Move(PAnsiChar(res)^, Str, Length(res) + 1);
374 Result:=Str;
375 end;
376
377 function CodePointLength(const Str: PChar; MaxLookAead: PtrInt): Ptrint;
378 var
379 c: byte;
380 begin
381 // Only UTF-8 encoding is supported
382 c:=byte(Str^);
383 if c = 0 then
384 Result:=0
385 else begin
386 Result:=1;
387 if c < $80 then
388 exit; // 1-byte ASCII char
389 while c and $C0 = $C0 do begin
390 Inc(Result);
391 c:=c shl 1;
392 end;
393 if Result > 6 then
394 Result:=1 // Invalid code point
395 else
396 if Result > MaxLookAead then
397 Result:=-1; // Incomplete code point
398 end;
399 end;
400
401 function GetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
402 begin
403 Result := CP_UTF8; // Android always uses UTF-8
404 end;
405
406 procedure SetStdIOCodePage(var T: Text); inline;
407 begin
408 case TextRec(T).Mode of
409 fmInput:TextRec(T).CodePage:=DefaultSystemCodePage;
410 fmOutput:TextRec(T).CodePage:=DefaultSystemCodePage;
411 end;
412 end;
413
414 procedure SetStdIOCodePages; inline;
415 begin
416 SetStdIOCodePage(Input);
417 SetStdIOCodePage(Output);
418 SetStdIOCodePage(ErrOutput);
419 SetStdIOCodePage(StdOut);
420 SetStdIOCodePage(StdErr);
421 end;
422
423 procedure Ansi2WideMove(source:pchar; cp:TSystemCodePage; var dest:widestring; len:SizeInt);
424 var
425 us: UnicodeString;
426 begin
427 Ansi2UnicodeMove(source,cp,us,len);
428 dest:=us;
429 end;
430
431 function UpperWideString(const s : WideString) : WideString;
432 begin
433 Result:=UpperUnicodeString(s);
434 end;
435
436 function LowerWideString(const s : WideString) : WideString;
437 begin
438 Result:=LowerUnicodeString(s);
439 end;
440
441 function CompareWideString(const s1, s2 : WideString; Options : TCompareOptions) : PtrInt;
442 begin
443 Result:=CompareUnicodeString(s1, s2, Options);
444 end;
445
446 Procedure SetCWideStringManager;
447 Var
448 CWideStringManager : TUnicodeStringManager;
449 begin
450 CWideStringManager:=widestringmanager;
451 With CWideStringManager do
452 begin
453 Wide2AnsiMoveProc:=@Unicode2AnsiMove;
454 Ansi2WideMoveProc:=@Ansi2WideMove;
455 UpperWideStringProc:=@UpperWideString;
456 LowerWideStringProc:=@LowerWideString;
457 CompareWideStringProc:=@CompareWideString;
458
459 UpperAnsiStringProc:=@UpperAnsiString;
460 LowerAnsiStringProc:=@LowerAnsiString;
461 CompareStrAnsiStringProc:=@CompareStrAnsiString;
462 CompareTextAnsiStringProc:=@AnsiCompareText;
463 StrCompAnsiStringProc:=@StrCompAnsi;
464 StrICompAnsiStringProc:=@AnsiStrIComp;
465 StrLCompAnsiStringProc:=@AnsiStrLComp;
466 StrLICompAnsiStringProc:=@AnsiStrLIComp;
467 StrLowerAnsiStringProc:=@AnsiStrLower;
468 StrUpperAnsiStringProc:=@AnsiStrUpper;
469
470 Unicode2AnsiMoveProc:=@Unicode2AnsiMove;
471 Ansi2UnicodeMoveProc:=@Ansi2UnicodeMove;
472 UpperUnicodeStringProc:=@UpperUnicodeString;
473 LowerUnicodeStringProc:=@LowerUnicodeString;
474 CompareUnicodeStringProc:=@CompareUnicodeString;
475
476 GetStandardCodePageProc:=@GetStandardCodePage;
477 CodePointLengthProc:=@CodePointLength;
478 end;
479 SetUnicodeStringManager(CWideStringManager);
480 end;
481
482 {$else}
483
484 function GetConverter(cp: TSystemCodePage): PUConverter;
485 var
486 s: ansistring;
487 begin
488 if hlibICU = 0 then begin
489 Result:=nil;
490 exit;
491 end;
492 InitThreadData;
493 if (cp = DefaultSystemCodePage) or (cp = CP_ACP) then
494 Result:=DefConv
495 else begin
496 if cp <> LastCP then begin
497 Str(cp, s);
498 LastConv:=OpenConverter('cp' + s);
499 LastCP:=cp;
500 end;
501 Result:=LastConv;
502 end;
503 end;
504
505 procedure Unicode2AnsiMove(source: PUnicodeChar; var dest: RawByteString; cp: TSystemCodePage; len: SizeInt);
506 var
507 len2: SizeInt;
508 conv: PUConverter;
509 err: UErrorCode;
510 begin
511 if len = 0 then begin
512 dest:='';
513 exit;
514 end;
515 conv:=GetConverter(cp);
516 if conv = nil then begin
517 DefaultUnicode2AnsiMove(source,dest,DefaultSystemCodePage,len);
518 exit;
519 end;
520
521 len2:=len*3;
522 SetLength(dest, len2);
523 err:=0;
524 len2:=ucnv_fromUChars(conv, PAnsiChar(dest), len2, source, len, err);
525 if len2 > Length(dest) then begin
526 SetLength(dest, len2);
527 err:=0;
528 len2:=ucnv_fromUChars(conv, PAnsiChar(dest), len2, source, len, err);
529 end;
530 SetLength(dest, len2);
531 SetCodePage(dest, cp, False);
532 end;
533
534 procedure Ansi2UnicodeMove(source:pchar;cp : TSystemCodePage;var dest:unicodestring;len:SizeInt);
535 var
536 len2: SizeInt;
537 conv: PUConverter;
538 err: UErrorCode;
539 begin
540 if len = 0 then begin
541 dest:='';
542 exit;
543 end;
544 conv:=GetConverter(cp);
545 if conv = nil then begin
546 DefaultAnsi2UnicodeMove(source,DefaultSystemCodePage,dest,len);
547 exit;
548 end;
549
550 len2:=len;
551 SetLength(dest, len2);
552 err:=0;
553 len2:=ucnv_toUChars(conv, PUnicodeChar(dest), len2, source, len, err);
554 if len2 > Length(dest) then begin
555 SetLength(dest, len2);
556 err:=0;
557 len2:=ucnv_toUChars(conv, PUnicodeChar(dest), len2, source, len, err);
558 end;
559 SetLength(dest, len2);
560 end;
561
562 function UpperUnicodeString(const s : UnicodeString) : UnicodeString;
563 var
564 len, len2: SizeInt;
565 err: UErrorCode;
566 begin
567 if hlibICU = 0 then begin
568 // fallback implementation
569 Result:=UnicodeString(UpCase(AnsiString(s)));
570 exit;
571 end;
572 len:=Length(s);
573 SetLength(Result, len);
574 if len = 0 then
575 exit;
576 err:=0;
577 len2:=u_strToUpper(PUnicodeChar(Result), len, PUnicodeChar(s), len, nil, err);
578 if len2 > len then begin
579 SetLength(Result, len2);
580 err:=0;
581 len2:=u_strToUpper(PUnicodeChar(Result), len2, PUnicodeChar(s), len, nil, err);
582 end;
583 SetLength(Result, len2);
584 end;
585
586 function LowerUnicodeString(const s : UnicodeString) : UnicodeString;
587 var
588 len, len2: SizeInt;
589 err: UErrorCode;
590 begin
591 if hlibICU = 0 then begin
592 // fallback implementation
593 Result:=UnicodeString(LowerCase(AnsiString(s)));
594 exit;
595 end;
596 len:=Length(s);
597 SetLength(Result, len);
598 if len = 0 then
599 exit;
600 err:=0;
601 len2:=u_strToLower(PUnicodeChar(Result), len, PUnicodeChar(s), len, nil, err);
602 if len2 > len then begin
603 SetLength(Result, len2);
604 err:=0;
605 len2:=u_strToLower(PUnicodeChar(Result), len2, PUnicodeChar(s), len, nil, err);
606 end;
607 SetLength(Result, len2);
608 end;
609
610 function _CompareStr(const S1, S2: UnicodeString): PtrInt;
611 var
612 count, count1, count2: SizeInt;
613 begin
614 result := 0;
615 Count1 := Length(S1);
616 Count2 := Length(S2);
617 if Count1>Count2 then
618 Count:=Count2
619 else
620 Count:=Count1;
621 result := CompareByte(PUnicodeChar(S1)^, PUnicodeChar(S2)^, Count*SizeOf(UnicodeChar));
622 if result=0 then
623 result:=Count1 - Count2;
624 end;
625
626 function CompareUnicodeString(const s1, s2 : UnicodeString) : PtrInt;
627 begin
628 if hlibICU = 0 then begin
629 // fallback implementation
630 Result:=_CompareStr(s1, s2);
631 exit;
632 end;
633 InitThreadData;
634 if DefColl <> nil then
635 Result:=ucol_strcoll(DefColl, PUnicodeChar(s1), Length(s1), PUnicodeChar(s2), Length(s2))
636 else
637 Result:=u_strCompare(PUnicodeChar(s1), Length(s1), PUnicodeChar(s2), Length(s2), True);
638 end;
639
640 function CompareTextUnicodeString(const s1, s2 : UnicodeString): PtrInt;
641 const
642 U_COMPARE_CODE_POINT_ORDER = $8000;
643 var
644 err: UErrorCode;
645 begin
646 if hlibICU = 0 then begin
647 // fallback implementation
648 Result:=_CompareStr(UpperUnicodeString(s1), UpperUnicodeString(s2));
649 exit;
650 end;
651 err:=0;
652 Result:=u_strCaseCompare(PUnicodeChar(s1), Length(s1), PUnicodeChar(s2), Length(s2), U_COMPARE_CODE_POINT_ORDER, err);
653 end;
654
655 function UpperAnsiString(const s : AnsiString) : AnsiString;
656 begin
657 Result:=AnsiString(UpperUnicodeString(UnicodeString(s)));
658 end;
659
660 function LowerAnsiString(const s : AnsiString) : AnsiString;
661 begin
662 Result:=AnsiString(LowerUnicodeString(UnicodeString(s)));
663 end;
664
665 function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
666 begin
667 Result:=CompareUnicodeString(UnicodeString(s1), UnicodeString(s2));
668 end;
669
670 function StrCompAnsi(s1,s2 : PChar): PtrInt;
671 begin
672 Result:=CompareUnicodeString(UnicodeString(s1), UnicodeString(s2));
673 end;
674
675 function AnsiCompareText(const S1, S2: ansistring): PtrInt;
676 begin
677 Result:=CompareTextUnicodeString(UnicodeString(s1), UnicodeString(s2));
678 end;
679
680 function AnsiStrIComp(S1, S2: PChar): PtrInt;
681 begin
682 Result:=CompareTextUnicodeString(UnicodeString(s1), UnicodeString(s2));
683 end;
684
685 function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
686 var
687 as1, as2: ansistring;
688 begin
689 SetString(as1, S1, MaxLen);
690 SetString(as2, S2, MaxLen);
691 Result:=CompareUnicodeString(UnicodeString(as1), UnicodeString(as2));
692 end;
693
694 function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
695 var
696 as1, as2: ansistring;
697 begin
698 SetString(as1, S1, MaxLen);
699 SetString(as2, S2, MaxLen);
700 Result:=CompareTextUnicodeString(UnicodeString(as1), UnicodeString(as2));
701 end;
702
703 function AnsiStrLower(Str: PChar): PChar;
704 var
705 s, res: ansistring;
706 begin
707 s:=Str;
708 res:=LowerAnsiString(s);
709 if Length(res) > Length(s) then
710 SetLength(res, Length(s));
711 Move(PAnsiChar(res)^, Str, Length(res) + 1);
712 Result:=Str;
713 end;
714
715 function AnsiStrUpper(Str: PChar): PChar;
716 var
717 s, res: ansistring;
718 begin
719 s:=Str;
720 res:=UpperAnsiString(s);
721 if Length(res) > Length(s) then
722 SetLength(res, Length(s));
723 Move(PAnsiChar(res)^, Str, Length(res) + 1);
724 Result:=Str;
725 end;
726
727 function CodePointLength(const Str: PChar; MaxLookAead: PtrInt): Ptrint;
728 var
729 c: byte;
730 begin
731 // Only UTF-8 encoding is supported
732 c:=byte(Str^);
733 if c = 0 then
734 Result:=0
735 else begin
736 Result:=1;
737 if c < $80 then
738 exit; // 1-byte ASCII char
739 while c and $C0 = $C0 do begin
740 Inc(Result);
741 c:=c shl 1;
742 end;
743 if Result > 6 then
744 Result:=1 // Invalid code point
745 else
746 if Result > MaxLookAead then
747 Result:=-1; // Incomplete code point
748 end;
749 end;
750
751 function GetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
752 begin
753 Result := CP_UTF8; // Android always uses UTF-8
754 end;
755
756 procedure SetStdIOCodePage(var T: Text); {$ifdef SUPPORTS_INLINE} inline; {$endif}
757 begin
758 case TextRec(T).Mode of
759 fmInput:TextRec(T).CodePage:=DefaultSystemCodePage;
760 fmOutput:TextRec(T).CodePage:=DefaultSystemCodePage;
761 end;
762 end;
763
764 procedure SetStdIOCodePages; {$ifdef SUPPORTS_INLINE} inline; {$endif}
765 begin
766 SetStdIOCodePage(Input);
767 SetStdIOCodePage(Output);
768 SetStdIOCodePage(ErrOutput);
769 SetStdIOCodePage(StdOut);
770 SetStdIOCodePage(StdErr);
771 end;
772
773 procedure Ansi2WideMove(source:pchar; cp:TSystemCodePage; var dest:widestring; len:SizeInt);
774 var
775 us: UnicodeString;
776 begin
777 Ansi2UnicodeMove(source,cp,us,len);
778 dest:=us;
779 end;
780
781 function UpperWideString(const s : WideString) : WideString;
782 begin
783 Result:=UpperUnicodeString(s);
784 end;
785
786 function LowerWideString(const s : WideString) : WideString;
787 begin
788 Result:=LowerUnicodeString(s);
789 end;
790
791 function CompareWideString(const s1, s2 : WideString) : PtrInt;
792 begin
793 Result:=CompareUnicodeString(s1, s2);
794 end;
795
796 function CompareTextWideString(const s1, s2 : WideString): PtrInt;
797 begin
798 Result:=CompareTextUnicodeString(s1, s2);
799 end;
800
801 Procedure SetCWideStringManager;
802 Var
803 CWideStringManager : TUnicodeStringManager;
804 begin
805 CWideStringManager:=widestringmanager;
806 With CWideStringManager do
807 begin
808 Wide2AnsiMoveProc:=@Unicode2AnsiMove;
809 Ansi2WideMoveProc:=@Ansi2WideMove;
810 UpperWideStringProc:=@UpperWideString;
811 LowerWideStringProc:=@LowerWideString;
812 CompareWideStringProc:=@CompareWideString;
813 CompareTextWideStringProc:=@CompareTextWideString;
814
815 UpperAnsiStringProc:=@UpperAnsiString;
816 LowerAnsiStringProc:=@LowerAnsiString;
817 CompareStrAnsiStringProc:=@CompareStrAnsiString;
818 CompareTextAnsiStringProc:=@AnsiCompareText;
819 StrCompAnsiStringProc:=@StrCompAnsi;
820 StrICompAnsiStringProc:=@AnsiStrIComp;
821 StrLCompAnsiStringProc:=@AnsiStrLComp;
822 StrLICompAnsiStringProc:=@AnsiStrLIComp;
823 StrLowerAnsiStringProc:=@AnsiStrLower;
824 StrUpperAnsiStringProc:=@AnsiStrUpper;
825
826 Unicode2AnsiMoveProc:=@Unicode2AnsiMove;
827 Ansi2UnicodeMoveProc:=@Ansi2UnicodeMove;
828 UpperUnicodeStringProc:=@UpperUnicodeString;
829 LowerUnicodeStringProc:=@LowerUnicodeString;
830 CompareUnicodeStringProc:=@CompareUnicodeString;
831 CompareTextUnicodeStringProc:=@CompareTextUnicodeString;
832
833 GetStandardCodePageProc:=@GetStandardCodePage;
834 CodePointLengthProc:=@CodePointLength;
835 end;
836 SetUnicodeStringManager(CWideStringManager);
837 end;
838
839 {$endif}
840
841 procedure UnloadICU;
842 begin
843 if hlibICUi18n <> 0 then begin
844 if DefColl <> nil then
845 ucol_close(DefColl);
846 UnloadLibrary(hlibICUi18n);
847 hlibICUi18n:=0;
848 end;
849 if hlibICU <> 0 then begin
850 if DefConv <> nil then
851 ucnv_close(DefConv);
852 if LastConv <> nil then
853 ucnv_close(LastConv);
854 UnloadLibrary(hlibICU);
855 hlibICU:=0;
856 end;
857 end;
858
859 procedure LoadICU;
860 var
861 LibVer: ansistring;
862
863 function _GetProc(const Name: AnsiString; out ProcPtr; hLib: TLibHandle = 0): boolean;
864 var
865 p: pointer;
866 begin
867 if hLib = 0 then
868 hLib:=hlibICU;
869 p:=GetProcedureAddress(hlib, Name + LibVer);
870 if p = nil then begin
871 // unload lib on failure
872 UnloadICU;
873 Result:=False;
874 end
875 else begin
876 pointer(ProcPtr):=p;
877 Result:=True;
878 end;
879 end;
880
881 const
882 ICUver: array [1..9] of ansistring = ('3_8', '4_2', '44', '46', '48', '50', '51', '53', '55');
883 TestProcName = 'ucnv_open';
884
885 var
886 i: int32_t;
887 s: ansistring;
888 begin
889 hlibICU:=LoadLibrary('libicuuc.so');
890 hlibICUi18n:=LoadLibrary('libicui18n.so');
891 if (hlibICU = 0) or (hlibICUi18n = 0) then begin
892 UnloadICU;
893 AndroidLog(alWarn, 'Cannot load libicuuc.so or libicui18n.so. WideString conversion will fail for special UTF-8 characters');
894 exit;
895 end;
896 // Finding ICU version using known versions table
897 for i:=High(ICUver) downto Low(ICUver) do begin
898 s:='_' + ICUver[i];
899 if GetProcedureAddress(hlibICU, TestProcName + s) <> nil then begin
900 LibVer:=s;
901 AndroidLog(alInfo, 'Found libicuuc.so version ' + LibVer);
902 break;
903 end;
904 end;
905
906 if LibVer = '' then begin
907 // Finding unknown ICU version
908 Val(ICUver[High(ICUver)], i);
909 repeat
910 Inc(i);
911 Str(i, s);
912 s:='_' + s;
913 if GetProcedureAddress(hlibICU, TestProcName + s) <> nil then begin
914 LibVer:=s;
915 AndroidLog(alInfo, 'Found libicuuc.so version (by looping) ' + LibVer);
916 break;
917 end;
918 until i >= 100;
919 end;
920
921 if LibVer = '' then begin
922 // Trying versionless name
923 if GetProcedureAddress(hlibICU, TestProcName) = nil then begin
924 // Unable to get ICU version
925 UnloadICU;
926 AndroidLog(alWarn, 'Cannot use libicuuc.so --- no versioned ucnv_open found, and unversioned ucnv_open not available. . WideString conversion will fail for special UTF-8 characters');
927 exit;
928 end;
929 end;
930
931 if not _GetProc('ucnv_open', ucnv_open) then exit;
932 if not _GetProc('ucnv_close', ucnv_close) then exit;
933 if not _GetProc('ucnv_setSubstChars', ucnv_setSubstChars) then exit;
934 if not _GetProc('ucnv_setFallback', ucnv_setFallback) then exit;
935 if not _GetProc('ucnv_fromUChars', ucnv_fromUChars) then exit;
936 if not _GetProc('ucnv_toUChars', ucnv_toUChars) then exit;
937 if not _GetProc('u_strToUpper', u_strToUpper) then exit;
938 if not _GetProc('u_strToLower', u_strToLower) then exit;
939 if not _GetProc('u_strCompare', u_strCompare) then exit;
940 if not _GetProc('u_strCaseCompare', u_strCaseCompare) then exit;
941
942 if not _GetProc('u_errorName', u_errorName) then exit;
943
944 if not _GetProc('ucol_open', ucol_open, hlibICUi18n) then exit;
945 if not _GetProc('ucol_close', ucol_close, hlibICUi18n) then exit;
946 if not _GetProc('ucol_strcoll', ucol_strcoll, hlibICUi18n) then exit;
947 if not _GetProc('ucol_setStrength', ucol_setStrength, hlibICUi18n) then exit;
948 end;
949
950 procedure InitializeAndroidCWString;
951 begin
952 DefaultSystemCodePage:=GetStandardCodePage(scpAnsi);
953 DefaultUnicodeCodePage:=CP_UTF16;
954 LoadICU;
955 SetCWideStringManager;
956 SetStdIOCodePages;
957 end;
958
959 finalization
960 UnloadICU;
961 end.