cwstring.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. if PtrInt(iconv_wide2ansi)=-1 then
  160. begin
  161. DefaultUnicode2AnsiMove(source,dest,len);
  162. exit;
  163. end;
  164. mynil:=nil;
  165. my0:=0;
  166. { rought estimation }
  167. setlength(dest,len*3);
  168. outlength:=len*3;
  169. srclen:=len*2;
  170. srcpos:=source;
  171. destpos:=pchar(dest);
  172. outleft:=outlength;
  173. while iconv(iconv_wide2ansi,ppchar(@srcpos),@srclen,@destpos,@outleft)=size_t(-1) do
  174. begin
  175. err:=fpgetCerrno;
  176. case err of
  177. { last character is incomplete sequence }
  178. ESysEINVAL,
  179. { incomplete sequence in the middle }
  180. ESysEILSEQ:
  181. begin
  182. { skip and set to '?' }
  183. inc(srcpos);
  184. dec(srclen,2);
  185. destpos^:='?';
  186. inc(destpos);
  187. dec(outleft);
  188. { reset }
  189. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  190. if err=ESysEINVAL then
  191. break;
  192. end;
  193. ESysE2BIG:
  194. begin
  195. outoffset:=destpos-pchar(dest);
  196. { extend }
  197. setlength(dest,outlength+len*3);
  198. inc(outleft,len*3);
  199. inc(outlength,len*3);
  200. { string could have been moved }
  201. destpos:=pchar(dest)+outoffset;
  202. end;
  203. else
  204. runerror(231);
  205. end;
  206. end;
  207. // truncate string
  208. setlength(dest,length(dest)-outleft);
  209. end;
  210. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  211. var
  212. outlength,
  213. outoffset,
  214. outleft : size_t;
  215. srcpos,
  216. destpos: pchar;
  217. mynil : pchar;
  218. my0 : size_t;
  219. err: cint;
  220. begin
  221. if PtrInt(iconv_ansi2wide)=-1 then
  222. begin
  223. DefaultAnsi2UnicodeMove(source,dest,len);
  224. exit;
  225. end;
  226. mynil:=nil;
  227. my0:=0;
  228. // extra space
  229. outlength:=len+1;
  230. setlength(dest,outlength);
  231. srcpos:=source;
  232. destpos:=pchar(dest);
  233. outleft:=outlength*2;
  234. while iconv(iconv_ansi2wide,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  235. begin
  236. err:=fpgetCerrno;
  237. case err of
  238. ESysEINVAL,
  239. ESysEILSEQ:
  240. begin
  241. { skip and set to '?' }
  242. inc(srcpos);
  243. dec(len);
  244. pwidechar(destpos)^:='?';
  245. inc(destpos,2);
  246. dec(outleft,2);
  247. { reset }
  248. iconv(iconv_ansi2wide,@mynil,@my0,@mynil,@my0);
  249. if err=ESysEINVAL then
  250. break;
  251. end;
  252. ESysE2BIG:
  253. begin
  254. outoffset:=destpos-pchar(dest);
  255. { extend }
  256. setlength(dest,outlength+len);
  257. inc(outleft,len*2);
  258. inc(outlength,len);
  259. { string could have been moved }
  260. destpos:=pchar(dest)+outoffset;
  261. end;
  262. else
  263. runerror(231);
  264. end;
  265. end;
  266. // truncate string
  267. setlength(dest,length(dest)-outleft div 2);
  268. end;
  269. function LowerWideString(const s : WideString) : WideString;
  270. var
  271. i : SizeInt;
  272. begin
  273. SetLength(result,length(s));
  274. for i:=0 to length(s)-1 do
  275. pwidechar(result)[i]:=WideChar(towlower(wint_t(s[i+1])));
  276. end;
  277. function UpperWideString(const s : WideString) : WideString;
  278. var
  279. i : SizeInt;
  280. begin
  281. SetLength(result,length(s));
  282. for i:=0 to length(s)-1 do
  283. pwidechar(result)[i]:=WideChar(towupper(wint_t(s[i+1])));
  284. end;
  285. procedure EnsureAnsiLen(var S: AnsiString; const len: SizeInt); inline;
  286. begin
  287. if (len>length(s)) then
  288. if (length(s) < 10*256) then
  289. setlength(s,length(s)+10)
  290. else
  291. setlength(s,length(s)+length(s) shr 8);
  292. end;
  293. procedure ConcatCharToAnsiStr(const c: char; var S: AnsiString; var index: SizeInt);
  294. begin
  295. EnsureAnsiLen(s,index);
  296. pchar(@s[index])^:=c;
  297. inc(index);
  298. end;
  299. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  300. {$ifndef beos}
  301. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt; var mbstate: mbstate_t);
  302. {$else not beos}
  303. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt);
  304. {$endif beos}
  305. var
  306. p : pchar;
  307. mblen : size_t;
  308. begin
  309. { we know that s is unique -> avoid uniquestring calls}
  310. p:=@s[index];
  311. if (nc<=127) then
  312. ConcatCharToAnsiStr(char(nc),s,index)
  313. else
  314. begin
  315. EnsureAnsiLen(s,index+MB_CUR_MAX);
  316. {$ifndef beos}
  317. mblen:=wcrtomb(p,wchar_t(nc),@mbstate);
  318. {$else not beos}
  319. mblen:=wctomb(p,wchar_t(nc));
  320. {$endif not beos}
  321. if (mblen<>size_t(-1)) then
  322. inc(index,mblen)
  323. else
  324. begin
  325. { invalid wide char }
  326. p^:='?';
  327. inc(index);
  328. end;
  329. end;
  330. end;
  331. function LowerAnsiString(const s : AnsiString) : AnsiString;
  332. var
  333. i, slen,
  334. resindex : SizeInt;
  335. mblen : size_t;
  336. {$ifndef beos}
  337. ombstate,
  338. nmbstate : mbstate_t;
  339. {$endif beos}
  340. wc : wchar_t;
  341. begin
  342. {$ifndef beos}
  343. fillchar(ombstate,sizeof(ombstate),0);
  344. fillchar(nmbstate,sizeof(nmbstate),0);
  345. {$endif beos}
  346. slen:=length(s);
  347. SetLength(result,slen+10);
  348. i:=1;
  349. resindex:=1;
  350. while (i<=slen) do
  351. begin
  352. if (s[i]<=#127) then
  353. begin
  354. wc:=wchar_t(s[i]);
  355. mblen:= 1;
  356. end
  357. else
  358. {$ifndef beos}
  359. mblen:=mbrtowc(@wc, pchar(@s[i]), slen-i+1, @ombstate);
  360. {$else not beos}
  361. mblen:=mbtowc(@wc, pchar(@s[i]), slen-i+1);
  362. {$endif not beos}
  363. case mblen of
  364. size_t(-2):
  365. begin
  366. { partial invalid character, copy literally }
  367. while (i<=slen) do
  368. begin
  369. ConcatCharToAnsiStr(s[i],result,resindex);
  370. inc(i);
  371. end;
  372. end;
  373. size_t(-1), 0:
  374. begin
  375. { invalid or null character }
  376. ConcatCharToAnsiStr(s[i],result,resindex);
  377. inc(i);
  378. end;
  379. else
  380. begin
  381. { a valid sequence }
  382. { even if mblen = 1, the lowercase version may have a }
  383. { different length }
  384. { We can't do anything special if wchar_t is 16 bit... }
  385. {$ifndef beos}
  386. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex,nmbstate);
  387. {$else not beos}
  388. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex);
  389. {$endif not beos}
  390. inc(i,mblen);
  391. end;
  392. end;
  393. end;
  394. SetLength(result,resindex-1);
  395. end;
  396. function UpperAnsiString(const s : AnsiString) : AnsiString;
  397. var
  398. i, slen,
  399. resindex : SizeInt;
  400. mblen : size_t;
  401. {$ifndef beos}
  402. ombstate,
  403. nmbstate : mbstate_t;
  404. {$endif beos}
  405. wc : wchar_t;
  406. begin
  407. {$ifndef beos}
  408. fillchar(ombstate,sizeof(ombstate),0);
  409. fillchar(nmbstate,sizeof(nmbstate),0);
  410. {$endif beos}
  411. slen:=length(s);
  412. SetLength(result,slen+10);
  413. i:=1;
  414. resindex:=1;
  415. while (i<=slen) do
  416. begin
  417. if (s[i]<=#127) then
  418. begin
  419. wc:=wchar_t(s[i]);
  420. mblen:= 1;
  421. end
  422. else
  423. {$ifndef beos}
  424. mblen:=mbrtowc(@wc, pchar(@s[i]), slen-i+1, @ombstate);
  425. {$else not beos}
  426. mblen:=mbtowc(@wc, pchar(@s[i]), slen-i+1);
  427. {$endif beos}
  428. case mblen of
  429. size_t(-2):
  430. begin
  431. { partial invalid character, copy literally }
  432. while (i<=slen) do
  433. begin
  434. ConcatCharToAnsiStr(s[i],result,resindex);
  435. inc(i);
  436. end;
  437. end;
  438. size_t(-1), 0:
  439. begin
  440. { invalid or null character }
  441. ConcatCharToAnsiStr(s[i],result,resindex);
  442. inc(i);
  443. end;
  444. else
  445. begin
  446. { a valid sequence }
  447. { even if mblen = 1, the uppercase version may have a }
  448. { different length }
  449. { We can't do anything special if wchar_t is 16 bit... }
  450. {$ifndef beos}
  451. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex,nmbstate);
  452. {$else not beos}
  453. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex);
  454. {$endif not beos}
  455. inc(i,mblen);
  456. end;
  457. end;
  458. end;
  459. SetLength(result,resindex-1);
  460. end;
  461. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; external name 'FPC_UTF16TOUTF32';
  462. function WideStringToUCS4StringNoNulls(const s : WideString) : UCS4String;
  463. var
  464. i, slen,
  465. destindex : SizeInt;
  466. len : longint;
  467. uch : UCS4Char;
  468. begin
  469. slen:=length(s);
  470. setlength(result,slen+1);
  471. i:=1;
  472. destindex:=0;
  473. while (i<=slen) do
  474. begin
  475. uch:=utf16toutf32(s,i,len);
  476. if (uch=UCS4Char(0)) then
  477. uch:=UCS4Char(32);
  478. result[destindex]:=uch;
  479. inc(destindex);
  480. inc(i,len);
  481. end;
  482. result[destindex]:=UCS4Char(0);
  483. { destindex <= slen }
  484. setlength(result,destindex+1);
  485. end;
  486. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  487. var
  488. hs1,hs2 : UCS4String;
  489. begin
  490. { wcscoll interprets null chars as end-of-string -> filter out }
  491. hs1:=WideStringToUCS4StringNoNulls(s1);
  492. hs2:=WideStringToUCS4StringNoNulls(s2);
  493. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  494. end;
  495. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  496. begin
  497. result:=CompareWideString(UpperWideString(s1),UpperWideString(s2));
  498. end;
  499. function CharLengthPChar(const Str: PChar): PtrInt;
  500. var
  501. nextlen: ptrint;
  502. s: pchar;
  503. {$ifndef beos}
  504. mbstate: mbstate_t;
  505. {$endif not beos}
  506. begin
  507. result:=0;
  508. s:=str;
  509. repeat
  510. {$ifdef beos}
  511. nextlen:=ptrint(mblen(str,MB_CUR_MAX));
  512. {$else beos}
  513. nextlen:=ptrint(mbrlen(str,MB_CUR_MAX,@mbstate));
  514. {$endif beos}
  515. { skip invalid/incomplete sequences }
  516. if (nextlen<0) then
  517. nextlen:=1;
  518. inc(result,nextlen);
  519. inc(s,nextlen);
  520. until (nextlen=0);
  521. end;
  522. function StrCompAnsiIntern(s1,s2 : PChar; len1, len2: PtrInt; canmodifys1, canmodifys2: boolean): PtrInt;
  523. var
  524. a,b: pchar;
  525. i: PtrInt;
  526. begin
  527. if not(canmodifys1) then
  528. getmem(a,len1+1)
  529. else
  530. a:=s1;
  531. for i:=0 to len1-1 do
  532. if s1[i]<>#0 then
  533. a[i]:=s1[i]
  534. else
  535. a[i]:=#32;
  536. a[len1]:=#0;
  537. if not(canmodifys2) then
  538. getmem(b,len2+1)
  539. else
  540. b:=s2;
  541. for i:=0 to len2-1 do
  542. if s2[i]<>#0 then
  543. b[i]:=s2[i]
  544. else
  545. b[i]:=#32;
  546. b[len2]:=#0;
  547. result:=strcoll(a,b);
  548. if not(canmodifys1) then
  549. freemem(a);
  550. if not(canmodifys2) then
  551. freemem(b);
  552. end;
  553. function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
  554. begin
  555. result:=StrCompAnsiIntern(pchar(s1),pchar(s2),length(s1),length(s2),false,false);
  556. end;
  557. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  558. begin
  559. result:=strcoll(s1,s2);
  560. end;
  561. function AnsiCompareText(const S1, S2: ansistring): PtrInt;
  562. var
  563. a, b: AnsiString;
  564. begin
  565. a:=UpperAnsistring(s1);
  566. b:=UpperAnsistring(s2);
  567. result:=StrCompAnsiIntern(pchar(a),pchar(b),length(a),length(b),true,true);
  568. end;
  569. function AnsiStrIComp(S1, S2: PChar): PtrInt;
  570. begin
  571. result:=AnsiCompareText(ansistring(s1),ansistring(s2));
  572. end;
  573. function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  574. var
  575. a, b: pchar;
  576. begin
  577. if (maxlen=0) then
  578. exit(0);
  579. if (s1[maxlen]<>#0) then
  580. begin
  581. getmem(a,maxlen+1);
  582. move(s1^,a^,maxlen);
  583. a[maxlen]:=#0;
  584. end
  585. else
  586. a:=s1;
  587. if (s2[maxlen]<>#0) then
  588. begin
  589. getmem(b,maxlen+1);
  590. move(s2^,b^,maxlen);
  591. b[maxlen]:=#0;
  592. end
  593. else
  594. b:=s2;
  595. result:=StrCompAnsiIntern(a,b,maxlen,maxlen,a<>s1,b<>s2);
  596. if (a<>s1) then
  597. freemem(a);
  598. if (b<>s2) then
  599. freemem(b);
  600. end;
  601. function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  602. var
  603. a, b: ansistring;
  604. begin
  605. if (maxlen=0) then
  606. exit(0);
  607. setlength(a,maxlen);
  608. move(s1^,a[1],maxlen);
  609. setlength(b,maxlen);
  610. move(s2^,b[1],maxlen);
  611. result:=AnsiCompareText(a,b);
  612. end;
  613. procedure ansi2pchar(const s: ansistring; const orgp: pchar; out p: pchar);
  614. var
  615. newlen: sizeint;
  616. begin
  617. newlen:=length(s);
  618. if newlen>strlen(orgp) then
  619. fpc_rangeerror;
  620. p:=orgp;
  621. if (newlen>0) then
  622. move(s[1],p[0],newlen);
  623. p[newlen]:=#0;
  624. end;
  625. function AnsiStrLower(Str: PChar): PChar;
  626. var
  627. temp: ansistring;
  628. begin
  629. temp:=loweransistring(str);
  630. ansi2pchar(temp,str,result);
  631. end;
  632. function AnsiStrUpper(Str: PChar): PChar;
  633. var
  634. temp: ansistring;
  635. begin
  636. temp:=upperansistring(str);
  637. ansi2pchar(temp,str,result);
  638. end;
  639. procedure InitThread;
  640. begin
  641. {$if not(defined(darwin) and defined(arm))}
  642. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding2);
  643. iconv_ansi2wide:=iconv_open(unicode_encoding2,nl_langinfo(CODESET));
  644. {$else}
  645. { Unix locale settings are ignored on iPhoneOS }
  646. iconv_wide2ansi:=iconv_open('UTF-8',unicode_encoding2);
  647. iconv_ansi2wide:=iconv_open(unicode_encoding2,'UTF-8');
  648. {$endif}
  649. end;
  650. procedure FiniThread;
  651. begin
  652. if (iconv_wide2ansi <> iconv_t(-1)) then
  653. iconv_close(iconv_wide2ansi);
  654. if (iconv_ansi2wide <> iconv_t(-1)) then
  655. iconv_close(iconv_ansi2wide);
  656. end;
  657. Procedure SetCWideStringManager;
  658. Var
  659. CWideStringManager : TUnicodeStringManager;
  660. begin
  661. CWideStringManager:=widestringmanager;
  662. With CWideStringManager do
  663. begin
  664. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  665. Ansi2WideMoveProc:=@Ansi2WideMove;
  666. UpperWideStringProc:=@UpperWideString;
  667. LowerWideStringProc:=@LowerWideString;
  668. CompareWideStringProc:=@CompareWideString;
  669. CompareTextWideStringProc:=@CompareTextWideString;
  670. CharLengthPCharProc:=@CharLengthPChar;
  671. UpperAnsiStringProc:=@UpperAnsiString;
  672. LowerAnsiStringProc:=@LowerAnsiString;
  673. CompareStrAnsiStringProc:=@CompareStrAnsiString;
  674. CompareTextAnsiStringProc:=@AnsiCompareText;
  675. StrCompAnsiStringProc:=@StrCompAnsi;
  676. StrICompAnsiStringProc:=@AnsiStrIComp;
  677. StrLCompAnsiStringProc:=@AnsiStrLComp;
  678. StrLICompAnsiStringProc:=@AnsiStrLIComp;
  679. StrLowerAnsiStringProc:=@AnsiStrLower;
  680. StrUpperAnsiStringProc:=@AnsiStrUpper;
  681. ThreadInitProc:=@InitThread;
  682. ThreadFiniProc:=@FiniThread;
  683. {$ifndef VER2_2}
  684. { Unicode }
  685. Unicode2AnsiMoveProc:=@Wide2AnsiMove;
  686. Ansi2UnicodeMoveProc:=@Ansi2WideMove;
  687. UpperUnicodeStringProc:=@UpperWideString;
  688. LowerUnicodeStringProc:=@LowerWideString;
  689. {$endif VER2_2}
  690. end;
  691. SetUnicodeStringManager(CWideStringManager);
  692. end;
  693. initialization
  694. SetCWideStringManager;
  695. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  696. { with the information from the environment variables according to POSIX }
  697. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  698. setlocale(LC_ALL,'');
  699. { init conversion tables for main program }
  700. InitThread;
  701. finalization
  702. { fini conversion tables for main program }
  703. FiniThread;
  704. end.