cwstring.pp 22 KB

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