cwstring.pp 20 KB

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