cwstring.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005 by Florian Klaempfl,
  4. member of the Free Pascal development team.
  5. libc based wide string support
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. {$inline on}
  14. unit cwstring;
  15. interface
  16. procedure SetCWidestringManager;
  17. implementation
  18. {$linklib c}
  19. {$if not defined(linux) and not defined(solaris)} // Linux (and maybe glibc platforms in general), have iconv in glibc.
  20. {$if defined(haiku)}
  21. {$linklib textencoding}
  22. {$else}
  23. {$linklib iconv}
  24. {$endif}
  25. {$define useiconv}
  26. {$endif linux}
  27. Uses
  28. BaseUnix,
  29. ctypes,
  30. unix,
  31. unixtype,
  32. initc;
  33. Const
  34. {$ifndef useiconv}
  35. libiconvname='c'; // is in libc under Linux.
  36. {$else}
  37. {$ifdef haiku}
  38. libiconvname='textencoding'; // is in libtextencoding under Haiku
  39. {$else}
  40. libiconvname='iconv';
  41. {$endif}
  42. {$endif}
  43. { helper functions from libc }
  44. function towlower(__wc:wint_t):wint_t;cdecl;external clib name 'towlower';
  45. function towupper(__wc:wint_t):wint_t;cdecl;external clib name 'towupper';
  46. function wcscoll (__s1:pwchar_t; __s2:pwchar_t):cint;cdecl;external clib name 'wcscoll';
  47. function strcoll (__s1:pchar; __s2:pchar):cint;cdecl;external clib name 'strcoll';
  48. function setlocale(category: cint; locale: pchar): pchar; cdecl; external clib name 'setlocale';
  49. {$ifndef beos}
  50. function mbrtowc(pwc: pwchar_t; const s: pchar; n: size_t; ps: pmbstate_t): size_t; cdecl; external clib name 'mbrtowc';
  51. function wcrtomb(s: pchar; wc: wchar_t; ps: pmbstate_t): size_t; cdecl; external clib name 'wcrtomb';
  52. function mbrlen(const s: pchar; n: size_t; ps: pmbstate_t): size_t; cdecl; external clib name 'mbrlen';
  53. {$else beos}
  54. function mbtowc(pwc: pwchar_t; const s: pchar; n: size_t): size_t; cdecl; external clib name 'mbtowc';
  55. function wctomb(s: pchar; wc: wchar_t): size_t; cdecl; external clib name 'wctomb';
  56. function mblen(const s: pchar; n: size_t): size_t; cdecl; external clib name 'mblen';
  57. {$endif beos}
  58. const
  59. {$ifdef linux}
  60. __LC_CTYPE = 0;
  61. LC_ALL = 6;
  62. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  63. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  64. CODESET = _NL_CTYPE_CODESET_NAME;
  65. {$else linux}
  66. {$ifdef darwin}
  67. CODESET = 0;
  68. LC_ALL = 0;
  69. {$else darwin}
  70. {$ifdef FreeBSD} // actually FreeBSD5. internationalisation is afaik not default on 4.
  71. __LC_CTYPE = 0;
  72. LC_ALL = 0;
  73. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  74. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  75. CODESET = 0; // _NL_CTYPE_CODESET_NAME;
  76. {$else freebsd}
  77. {$ifdef solaris}
  78. CODESET=49;
  79. LC_ALL = 6;
  80. {$else solaris}
  81. {$ifdef beos}
  82. {$warning check correct value for BeOS}
  83. CODESET=49;
  84. {$ifdef haiku}
  85. LC_ALL = 0; // Checked for Haiku
  86. {$else}
  87. LC_ALL = 6; // Checked for BeOS
  88. {$endif}
  89. ESysEILSEQ = EILSEQ;
  90. {$else}
  91. {$error lookup the value of CODESET in /usr/include/langinfo.h, and the value of LC_ALL in /usr/include/locale.h for your OS }
  92. // and while doing it, check if iconv is in libc, and if the symbols are prefixed with iconv_ or libiconv_
  93. {$endif beos}
  94. {$endif solaris}
  95. {$endif FreeBSD}
  96. {$endif darwin}
  97. {$endif linux}
  98. { unicode encoding name }
  99. {$ifdef FPC_LITTLE_ENDIAN}
  100. unicode_encoding2 = 'UTF-16LE';
  101. unicode_encoding4 = 'UCS-4LE';
  102. {$else FPC_LITTLE_ENDIAN}
  103. unicode_encoding2 = 'UTF-16BE';
  104. unicode_encoding4 = 'UCS-4BE';
  105. {$endif FPC_LITTLE_ENDIAN}
  106. { en_US.UTF-8 needs maximally 6 chars, UCS-4/UTF-32 needs 4 }
  107. { -> 10 should be enough? Should actually use MB_CUR_MAX, but }
  108. { that's a libc macro mapped to internal functions/variables }
  109. { and thus not a stable external API on systems where libc }
  110. { breaks backwards compatibility every now and then }
  111. MB_CUR_MAX = 10;
  112. type
  113. piconv_t = ^iconv_t;
  114. iconv_t = pointer;
  115. nl_item = cint;
  116. {$ifndef beos}
  117. function nl_langinfo(__item:nl_item):pchar;cdecl;external libiconvname name 'nl_langinfo';
  118. {$endif}
  119. {$if (not defined(bsd) and not defined(beos)) or (defined(darwin) and not defined(cpupowerpc32))}
  120. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'iconv_open';
  121. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'iconv';
  122. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'iconv_close';
  123. {$else}
  124. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'libiconv_open';
  125. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'libiconv';
  126. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'libiconv_close';
  127. {$endif}
  128. procedure fpc_rangeerror; [external name 'FPC_RANGEERROR'];
  129. threadvar
  130. iconv_ansi2wide,
  131. iconv_wide2ansi : iconv_t;
  132. {$ifdef beos}
  133. function nl_langinfo(__item:nl_item):pchar;
  134. begin
  135. {$warning TODO BeOS nl_langinfo or more uptodate port of iconv...}
  136. // Now implement the minimum required to correctly initialize WideString support
  137. case __item of
  138. CODESET : Result := 'UTF-8'; // BeOS use UTF-8
  139. else
  140. begin
  141. Assert(False, 'nl_langinfo was called with an unknown nl_item value');
  142. Result := '';
  143. end;
  144. end;
  145. end;
  146. {$endif}
  147. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  148. var
  149. outlength,
  150. outoffset,
  151. srclen,
  152. outleft : size_t;
  153. srcpos : pwidechar;
  154. destpos: pchar;
  155. mynil : pchar;
  156. my0 : size_t;
  157. err: cint;
  158. begin
  159. mynil:=nil;
  160. my0:=0;
  161. { rought estimation }
  162. setlength(dest,len*3);
  163. outlength:=len*3;
  164. srclen:=len*2;
  165. srcpos:=source;
  166. destpos:=pchar(dest);
  167. outleft:=outlength;
  168. while iconv(iconv_wide2ansi,ppchar(@srcpos),@srclen,@destpos,@outleft)=size_t(-1) do
  169. begin
  170. err:=fpgetCerrno;
  171. case err of
  172. { last character is incomplete sequence }
  173. ESysEINVAL,
  174. { incomplete sequence in the middle }
  175. ESysEILSEQ:
  176. begin
  177. { skip and set to '?' }
  178. inc(srcpos);
  179. dec(srclen,2);
  180. destpos^:='?';
  181. inc(destpos);
  182. dec(outleft);
  183. { reset }
  184. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  185. if err=ESysEINVAL then
  186. break;
  187. end;
  188. ESysE2BIG:
  189. begin
  190. outoffset:=destpos-pchar(dest);
  191. { extend }
  192. setlength(dest,outlength+len*3);
  193. inc(outleft,len*3);
  194. inc(outlength,len*3);
  195. { string could have been moved }
  196. destpos:=pchar(dest)+outoffset;
  197. end;
  198. else
  199. runerror(231);
  200. end;
  201. end;
  202. // truncate string
  203. setlength(dest,length(dest)-outleft);
  204. end;
  205. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  206. var
  207. outlength,
  208. outoffset,
  209. outleft : size_t;
  210. srcpos,
  211. destpos: pchar;
  212. mynil : pchar;
  213. my0 : size_t;
  214. err: cint;
  215. begin
  216. mynil:=nil;
  217. my0:=0;
  218. // extra space
  219. outlength:=len+1;
  220. setlength(dest,outlength);
  221. srcpos:=source;
  222. destpos:=pchar(dest);
  223. outleft:=outlength*2;
  224. while iconv(iconv_ansi2wide,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  225. begin
  226. err:=fpgetCerrno;
  227. case err of
  228. ESysEINVAL,
  229. ESysEILSEQ:
  230. begin
  231. { skip and set to '?' }
  232. inc(srcpos);
  233. dec(len);
  234. pwidechar(destpos)^:='?';
  235. inc(destpos,2);
  236. dec(outleft,2);
  237. { reset }
  238. iconv(iconv_ansi2wide,@mynil,@my0,@mynil,@my0);
  239. if err=ESysEINVAL then
  240. break;
  241. end;
  242. ESysE2BIG:
  243. begin
  244. outoffset:=destpos-pchar(dest);
  245. { extend }
  246. setlength(dest,outlength+len);
  247. inc(outleft,len*2);
  248. inc(outlength,len);
  249. { string could have been moved }
  250. destpos:=pchar(dest)+outoffset;
  251. end;
  252. else
  253. runerror(231);
  254. end;
  255. end;
  256. // truncate string
  257. setlength(dest,length(dest)-outleft div 2);
  258. end;
  259. function LowerWideString(const s : WideString) : WideString;
  260. var
  261. i : SizeInt;
  262. begin
  263. SetLength(result,length(s));
  264. for i:=0 to length(s)-1 do
  265. pwidechar(result)[i]:=WideChar(towlower(wint_t(s[i+1])));
  266. end;
  267. function UpperWideString(const s : WideString) : WideString;
  268. var
  269. i : SizeInt;
  270. begin
  271. SetLength(result,length(s));
  272. for i:=0 to length(s)-1 do
  273. pwidechar(result)[i]:=WideChar(towupper(wint_t(s[i+1])));
  274. end;
  275. procedure EnsureAnsiLen(var S: AnsiString; const len: SizeInt); inline;
  276. begin
  277. if (len>length(s)) then
  278. if (length(s) < 10*256) then
  279. setlength(s,length(s)+10)
  280. else
  281. setlength(s,length(s)+length(s) shr 8);
  282. end;
  283. procedure ConcatCharToAnsiStr(const c: char; var S: AnsiString; var index: SizeInt);
  284. begin
  285. EnsureAnsiLen(s,index);
  286. pchar(@s[index])^:=c;
  287. inc(index);
  288. end;
  289. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  290. {$ifndef beos}
  291. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt; var mbstate: mbstate_t);
  292. {$else not beos}
  293. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt);
  294. {$endif beos}
  295. var
  296. p : pchar;
  297. mblen : size_t;
  298. begin
  299. { we know that s is unique -> avoid uniquestring calls}
  300. p:=@s[index];
  301. if (nc<=127) then
  302. ConcatCharToAnsiStr(char(nc),s,index)
  303. else
  304. begin
  305. EnsureAnsiLen(s,index+MB_CUR_MAX);
  306. {$ifndef beos}
  307. mblen:=wcrtomb(p,wchar_t(nc),@mbstate);
  308. {$else not beos}
  309. mblen:=wctomb(p,wchar_t(nc));
  310. {$endif not beos}
  311. if (mblen<>size_t(-1)) then
  312. inc(index,mblen)
  313. else
  314. begin
  315. { invalid wide char }
  316. p^:='?';
  317. inc(index);
  318. end;
  319. end;
  320. end;
  321. function LowerAnsiString(const s : AnsiString) : AnsiString;
  322. var
  323. i, slen,
  324. resindex : SizeInt;
  325. mblen : size_t;
  326. {$ifndef beos}
  327. ombstate,
  328. nmbstate : mbstate_t;
  329. {$endif beos}
  330. wc : wchar_t;
  331. begin
  332. {$ifndef beos}
  333. fillchar(ombstate,sizeof(ombstate),0);
  334. fillchar(nmbstate,sizeof(nmbstate),0);
  335. {$endif beos}
  336. slen:=length(s);
  337. SetLength(result,slen+10);
  338. i:=1;
  339. resindex:=1;
  340. while (i<=slen) do
  341. begin
  342. if (s[i]<=#127) then
  343. begin
  344. wc:=wchar_t(s[i]);
  345. mblen:= 1;
  346. end
  347. else
  348. {$ifndef beos}
  349. mblen:=mbrtowc(@wc, pchar(@s[i]), slen-i+1, @ombstate);
  350. {$else not beos}
  351. mblen:=mbtowc(@wc, pchar(@s[i]), slen-i+1);
  352. {$endif not beos}
  353. case mblen of
  354. size_t(-2):
  355. begin
  356. { partial invalid character, copy literally }
  357. while (i<=slen) do
  358. begin
  359. ConcatCharToAnsiStr(s[i],result,resindex);
  360. inc(i);
  361. end;
  362. end;
  363. size_t(-1), 0:
  364. begin
  365. { invalid or null character }
  366. ConcatCharToAnsiStr(s[i],result,resindex);
  367. inc(i);
  368. end;
  369. else
  370. begin
  371. { a valid sequence }
  372. { even if mblen = 1, the lowercase version may have a }
  373. { different length }
  374. { We can't do anything special if wchar_t is 16 bit... }
  375. {$ifndef beos}
  376. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex,nmbstate);
  377. {$else not beos}
  378. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex);
  379. {$endif not beos}
  380. inc(i,mblen);
  381. end;
  382. end;
  383. end;
  384. SetLength(result,resindex-1);
  385. end;
  386. function UpperAnsiString(const s : AnsiString) : AnsiString;
  387. var
  388. i, slen,
  389. resindex : SizeInt;
  390. mblen : size_t;
  391. {$ifndef beos}
  392. ombstate,
  393. nmbstate : mbstate_t;
  394. {$endif beos}
  395. wc : wchar_t;
  396. begin
  397. {$ifndef beos}
  398. fillchar(ombstate,sizeof(ombstate),0);
  399. fillchar(nmbstate,sizeof(nmbstate),0);
  400. {$endif beos}
  401. slen:=length(s);
  402. SetLength(result,slen+10);
  403. i:=1;
  404. resindex:=1;
  405. while (i<=slen) do
  406. begin
  407. if (s[i]<=#127) then
  408. begin
  409. wc:=wchar_t(s[i]);
  410. mblen:= 1;
  411. end
  412. else
  413. {$ifndef beos}
  414. mblen:=mbrtowc(@wc, pchar(@s[i]), slen-i+1, @ombstate);
  415. {$else not beos}
  416. mblen:=mbtowc(@wc, pchar(@s[i]), slen-i+1);
  417. {$endif beos}
  418. case mblen of
  419. size_t(-2):
  420. begin
  421. { partial invalid character, copy literally }
  422. while (i<=slen) do
  423. begin
  424. ConcatCharToAnsiStr(s[i],result,resindex);
  425. inc(i);
  426. end;
  427. end;
  428. size_t(-1), 0:
  429. begin
  430. { invalid or null character }
  431. ConcatCharToAnsiStr(s[i],result,resindex);
  432. inc(i);
  433. end;
  434. else
  435. begin
  436. { a valid sequence }
  437. { even if mblen = 1, the uppercase version may have a }
  438. { different length }
  439. { We can't do anything special if wchar_t is 16 bit... }
  440. {$ifndef beos}
  441. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex,nmbstate);
  442. {$else not beos}
  443. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex);
  444. {$endif not beos}
  445. inc(i,mblen);
  446. end;
  447. end;
  448. end;
  449. SetLength(result,resindex-1);
  450. end;
  451. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; external name 'FPC_UTF16TOUTF32';
  452. function WideStringToUCS4StringNoNulls(const s : WideString) : UCS4String;
  453. var
  454. i, slen,
  455. destindex : SizeInt;
  456. len : longint;
  457. uch : UCS4Char;
  458. begin
  459. slen:=length(s);
  460. setlength(result,slen+1);
  461. i:=1;
  462. destindex:=0;
  463. while (i<=slen) do
  464. begin
  465. uch:=utf16toutf32(s,i,len);
  466. if (uch=UCS4Char(0)) then
  467. uch:=UCS4Char(32);
  468. result[destindex]:=uch;
  469. inc(destindex);
  470. inc(i,len);
  471. end;
  472. result[destindex]:=UCS4Char(0);
  473. { destindex <= slen }
  474. setlength(result,destindex+1);
  475. end;
  476. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  477. var
  478. hs1,hs2 : UCS4String;
  479. begin
  480. { wcscoll interprets null chars as end-of-string -> filter out }
  481. hs1:=WideStringToUCS4StringNoNulls(s1);
  482. hs2:=WideStringToUCS4StringNoNulls(s2);
  483. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  484. end;
  485. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  486. begin
  487. result:=CompareWideString(UpperWideString(s1),UpperWideString(s2));
  488. end;
  489. function CharLengthPChar(const Str: PChar): PtrInt;
  490. var
  491. nextlen: ptrint;
  492. s: pchar;
  493. {$ifndef beos}
  494. mbstate: mbstate_t;
  495. {$endif not beos}
  496. begin
  497. result:=0;
  498. s:=str;
  499. repeat
  500. {$ifdef beos}
  501. nextlen:=ptrint(mblen(str,MB_CUR_MAX));
  502. {$else beos}
  503. nextlen:=ptrint(mbrlen(str,MB_CUR_MAX,@mbstate));
  504. {$endif beos}
  505. { skip invalid/incomplete sequences }
  506. if (nextlen<0) then
  507. nextlen:=1;
  508. inc(result,nextlen);
  509. inc(s,nextlen);
  510. until (nextlen=0);
  511. end;
  512. function StrCompAnsiIntern(s1,s2 : PChar; len1, len2: PtrInt; canmodifys1, canmodifys2: boolean): PtrInt;
  513. var
  514. a,b: pchar;
  515. i: PtrInt;
  516. begin
  517. if not(canmodifys1) then
  518. getmem(a,len1+1)
  519. else
  520. a:=s1;
  521. for i:=0 to len1-1 do
  522. if s1[i]<>#0 then
  523. a[i]:=s1[i]
  524. else
  525. a[i]:=#32;
  526. a[len1]:=#0;
  527. if not(canmodifys2) then
  528. getmem(b,len2+1)
  529. else
  530. b:=s2;
  531. for i:=0 to len2-1 do
  532. if s2[i]<>#0 then
  533. b[i]:=s2[i]
  534. else
  535. b[i]:=#32;
  536. b[len2]:=#0;
  537. result:=strcoll(a,b);
  538. if not(canmodifys1) then
  539. freemem(a);
  540. if not(canmodifys2) then
  541. freemem(b);
  542. end;
  543. function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
  544. begin
  545. result:=StrCompAnsiIntern(pchar(s1),pchar(s2),length(s1),length(s2),false,false);
  546. end;
  547. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  548. begin
  549. result:=strcoll(s1,s2);
  550. end;
  551. function AnsiCompareText(const S1, S2: ansistring): PtrInt;
  552. var
  553. a, b: AnsiString;
  554. begin
  555. a:=UpperAnsistring(s1);
  556. b:=UpperAnsistring(s2);
  557. result:=StrCompAnsiIntern(pchar(a),pchar(b),length(a),length(b),true,true);
  558. end;
  559. function AnsiStrIComp(S1, S2: PChar): PtrInt;
  560. begin
  561. result:=AnsiCompareText(ansistring(s1),ansistring(s2));
  562. end;
  563. function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  564. var
  565. a, b: pchar;
  566. begin
  567. if (maxlen=0) then
  568. exit(0);
  569. if (s1[maxlen]<>#0) then
  570. begin
  571. getmem(a,maxlen+1);
  572. move(s1^,a^,maxlen);
  573. a[maxlen]:=#0;
  574. end
  575. else
  576. a:=s1;
  577. if (s2[maxlen]<>#0) then
  578. begin
  579. getmem(b,maxlen+1);
  580. move(s2^,b^,maxlen);
  581. b[maxlen]:=#0;
  582. end
  583. else
  584. b:=s2;
  585. result:=StrCompAnsiIntern(a,b,maxlen,maxlen,a<>s1,b<>s2);
  586. if (a<>s1) then
  587. freemem(a);
  588. if (b<>s2) then
  589. freemem(b);
  590. end;
  591. function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  592. var
  593. a, b: ansistring;
  594. begin
  595. if (maxlen=0) then
  596. exit(0);
  597. setlength(a,maxlen);
  598. move(s1^,a[1],maxlen);
  599. setlength(b,maxlen);
  600. move(s2^,b[1],maxlen);
  601. result:=AnsiCompareText(a,b);
  602. end;
  603. procedure ansi2pchar(const s: ansistring; const orgp: pchar; out p: pchar);
  604. var
  605. newlen: sizeint;
  606. begin
  607. newlen:=length(s);
  608. if newlen>strlen(orgp) then
  609. fpc_rangeerror;
  610. p:=orgp;
  611. if (newlen>0) then
  612. move(s[1],p[0],newlen);
  613. p[newlen]:=#0;
  614. end;
  615. function AnsiStrLower(Str: PChar): PChar;
  616. var
  617. temp: ansistring;
  618. begin
  619. temp:=loweransistring(str);
  620. ansi2pchar(temp,str,result);
  621. end;
  622. function AnsiStrUpper(Str: PChar): PChar;
  623. var
  624. temp: ansistring;
  625. begin
  626. temp:=upperansistring(str);
  627. ansi2pchar(temp,str,result);
  628. end;
  629. procedure InitThread;
  630. begin
  631. {$if not(defined(darwin) and defined(arm))}
  632. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding2);
  633. iconv_ansi2wide:=iconv_open(unicode_encoding2,nl_langinfo(CODESET));
  634. {$else}
  635. { Unix locale settings are ignored on iPhoneOS }
  636. iconv_wide2ansi:=iconv_open('UTF-8',unicode_encoding2);
  637. iconv_ansi2wide:=iconv_open(unicode_encoding2,'UTF-8');
  638. {$endif}
  639. end;
  640. procedure FiniThread;
  641. begin
  642. if (iconv_wide2ansi <> iconv_t(-1)) then
  643. iconv_close(iconv_wide2ansi);
  644. if (iconv_ansi2wide <> iconv_t(-1)) then
  645. iconv_close(iconv_ansi2wide);
  646. end;
  647. Procedure SetCWideStringManager;
  648. Var
  649. CWideStringManager : TUnicodeStringManager;
  650. begin
  651. CWideStringManager:=widestringmanager;
  652. With CWideStringManager do
  653. begin
  654. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  655. Ansi2WideMoveProc:=@Ansi2WideMove;
  656. UpperWideStringProc:=@UpperWideString;
  657. LowerWideStringProc:=@LowerWideString;
  658. CompareWideStringProc:=@CompareWideString;
  659. CompareTextWideStringProc:=@CompareTextWideString;
  660. CharLengthPCharProc:=@CharLengthPChar;
  661. UpperAnsiStringProc:=@UpperAnsiString;
  662. LowerAnsiStringProc:=@LowerAnsiString;
  663. CompareStrAnsiStringProc:=@CompareStrAnsiString;
  664. CompareTextAnsiStringProc:=@AnsiCompareText;
  665. StrCompAnsiStringProc:=@StrCompAnsi;
  666. StrICompAnsiStringProc:=@AnsiStrIComp;
  667. StrLCompAnsiStringProc:=@AnsiStrLComp;
  668. StrLICompAnsiStringProc:=@AnsiStrLIComp;
  669. StrLowerAnsiStringProc:=@AnsiStrLower;
  670. StrUpperAnsiStringProc:=@AnsiStrUpper;
  671. ThreadInitProc:=@InitThread;
  672. ThreadFiniProc:=@FiniThread;
  673. {$ifndef VER2_2}
  674. { Unicode }
  675. Unicode2AnsiMoveProc:=@Wide2AnsiMove;
  676. Ansi2UnicodeMoveProc:=@Ansi2WideMove;
  677. UpperUnicodeStringProc:=@UpperWideString;
  678. LowerUnicodeStringProc:=@LowerWideString;
  679. {$endif VER2_2}
  680. end;
  681. SetUnicodeStringManager(CWideStringManager);
  682. end;
  683. initialization
  684. SetCWideStringManager;
  685. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  686. { with the information from the environment variables according to POSIX }
  687. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  688. setlocale(LC_ALL,'');
  689. { init conversion tables for main program }
  690. InitThread;
  691. finalization
  692. { fini conversion tables for main program }
  693. FiniThread;
  694. end.