justrings.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl,
  4. Copyright (c) 2011 by Jonas Maebe,
  5. members of the Free Pascal development team.
  6. This file implements support routines for UTF-8 strings with FPC
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. { unicodestring is a plain java.lang.String }
  14. {$define FPC_UNICODESTRING_TYPE_DEFINED}
  15. {$define FPC_HAS_DEFAULT_UNICODE_2_ANSI_MOVE}
  16. procedure DefaultUnicode2AnsiMove(source:punicodechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  17. var
  18. localencoder: JNCCharsetEncoder;
  19. inbuf: JNCharBuffer;
  20. outbuf: JNByteBuffer;
  21. begin
  22. localencoder:=widestringmanager.encoder.getForCodePage(cp);
  23. localencoder.reset;
  24. localencoder.onMalformedInput(JNCCodingErrorAction.fREPLACE);
  25. localencoder.onUnmappableCharacter(JNCCodingErrorAction.fREPLACE);
  26. inbuf:=JNCharBuffer.wrap(TJCharArray(source),0,len);
  27. outbuf:=localencoder.encode(inbuf);
  28. setlength(dest,outbuf.limit);
  29. { "The buffer's position will be zero and its limit will *follow* the last
  30. byte written" -> we already have a terminating zero }
  31. outbuf.get(TJByteArray(AnsiStringClass(dest).fdata),0,outbuf.limit);
  32. { already null-terminated because of setlength }
  33. SetCodePage(dest,cp,false);
  34. end;
  35. {$define FPC_HAS_DEFAULT_ANSI_2_UNICODE}
  36. procedure DefaultAnsi2UnicodeMove(source:pchar;cp : TSystemCodePage;var dest:unicodestring;len:SizeInt);
  37. var
  38. localdecoder: JNCCharsetDecoder;
  39. inbuf: JNByteBuffer;
  40. outbuf: JNCharBuffer;
  41. begin
  42. localdecoder:=widestringmanager.decoder.getForCodePage(cp);
  43. localdecoder.reset;
  44. localdecoder.onMalformedInput(JNCCodingErrorAction.fREPLACE);
  45. localdecoder.onUnmappableCharacter(JNCCodingErrorAction.fREPLACE);
  46. inbuf:=JNByteBuffer.wrap(TJByteArray(source),0,len);
  47. outbuf:=localdecoder.decode(inbuf);
  48. dest:=outbuf.toString;
  49. end;
  50. {
  51. This file contains the implementation of the UnicodeString type,
  52. which on the Java platforms is an alias for java.lang.String
  53. }
  54. {$define FPC_HAS_NEW_UNICODESTRING}
  55. Function NewUnicodeString(Len : SizeInt) : JLString;
  56. {
  57. Allocate a new UnicodeString on the heap.
  58. initialize it to zero length and reference count 1.
  59. }
  60. var
  61. data: array of jchar;
  62. begin
  63. setlength(data,len);
  64. result:=JLString.create(data);
  65. end;
  66. { lie, not required }
  67. {$define FPC_HAS_UNICODESTR_DECR_REF}
  68. {$define FPC_HAS_UNICODESTR_INCR_REF}
  69. {$define FPC_HAS_UNICODESTR_TO_SHORTSTR}
  70. procedure fpc_UnicodeStr_To_ShortStr (out res: ShortString;const S2 : UnicodeString); [Public, alias: 'FPC_UNICODESTR_TO_SHORTSTR'];compilerproc;
  71. {
  72. Converts a UnicodeString to a ShortString;
  73. }
  74. Var
  75. Size : SizeInt;
  76. temp : ansistring;
  77. begin
  78. res:='';
  79. Size:=Length(S2);
  80. if Size>0 then
  81. begin
  82. temp:=s2;
  83. res:=temp;
  84. end;
  85. end;
  86. {$define FPC_HAS_SHORTSTR_TO_UNICODESTR}
  87. Function fpc_ShortStr_To_UnicodeStr (Const S2 : ShortString): UnicodeString;compilerproc;
  88. {
  89. Converts a ShortString to a UnicodeString;
  90. }
  91. Var
  92. Size : SizeInt;
  93. begin
  94. result:='';
  95. Size:=Length(S2);
  96. if Size>0 then
  97. widestringmanager.Ansi2UnicodeMoveProc(PChar(ShortstringClass(@S2).fdata),DefaultSystemCodePage,result,Size);
  98. end;
  99. {$define FPC_HAS_UNICODESTR_TO_ANSISTR}
  100. Function fpc_UnicodeStr_To_AnsiStr (const S2 : UnicodeString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): AnsiString; compilerproc;
  101. {
  102. Converts a UnicodeString to an AnsiString
  103. }
  104. Var
  105. Size : SizeInt;
  106. begin
  107. cp:=TranslatePlaceholderCP(cp);
  108. { avoid codepage conversion -- why isn't the result rawbytestring? }
  109. pointer(result):=pointer(AnsistringClass.Create(s2,cp));
  110. end;
  111. {$define FPC_HAS_ANSISTR_TO_UNICODESTR}
  112. Function fpc_AnsiStr_To_UnicodeStr (Const S2 : RawByteString): UnicodeString; compilerproc;
  113. {
  114. Converts an AnsiString to a UnicodeString;
  115. }
  116. Var
  117. Size : SizeInt;
  118. begin
  119. if length(s2)=0 then
  120. result:=''
  121. else
  122. result:=AnsistringClass(S2).toString;
  123. end;
  124. {$define FPC_HAS_UNICODESTR_TO_WIDESTR}
  125. Function fpc_UnicodeStr_To_WideStr (const S2 : UnicodeString): WideString; compilerproc;
  126. begin
  127. result:=s2;
  128. end;
  129. {$define FPC_HAS_WIDESTR_TO_UNICODESTR}
  130. Function fpc_WideStr_To_UnicodeStr (Const S2 : WideString): UnicodeString; compilerproc;
  131. begin
  132. result:=s2;
  133. end;
  134. {$define FPC_HAS_PWIDECHAR_TO_UNICODESTR}
  135. Function fpc_PWideChar_To_UnicodeStr(const p : pwidechar): unicodestring; compilerproc;
  136. var
  137. Size : SizeInt;
  138. begin
  139. result:='';
  140. if p=nil then
  141. exit;
  142. size:=0;
  143. while p[size]<>#0 do
  144. inc(size);
  145. Setlength(result,Size);
  146. if Size>0 then
  147. result:=JLString.Create(TJCharArray(p),0,Size);
  148. end;
  149. { lie, not used by compiler }
  150. {$define FPC_HAS_PUNICODECHAR_TO_SHORTSTR}
  151. {$define FPC_HAS_PWIDECHAR_TO_ANSISTR}
  152. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): ansistring; compilerproc;
  153. begin
  154. result:='';
  155. if (p=nil) or
  156. (p^=#0) then
  157. exit;
  158. cp:=TranslatePlaceholderCP(cp);
  159. pointer(result):=pointer(AnsistringClass.Create(unicodestring(p),cp));
  160. end;
  161. {$define FPC_HAS_PWIDECHAR_TO_SHORTSTR}
  162. procedure fpc_PWideChar_To_ShortStr(out res : shortstring;const p : pwidechar); compilerproc;
  163. begin
  164. res:='';
  165. if (p=nil) or
  166. (p^=#0) then
  167. exit;
  168. res:=unicodestring(p);
  169. end;
  170. { lie, not required for JVM target }
  171. {$define FPC_HAS_UNICODESTR_ASSIGN}
  172. {$define FPC_HAS_UNICODESTR_CONCAT}
  173. procedure fpc_UnicodeStr_Concat (var DestS:Unicodestring;const S1,S2 : UnicodeString); compilerproc;
  174. Var
  175. sb: JLStringBuilder;
  176. begin
  177. { only assign if s1 or s2 is empty }
  178. if (length(S1)=0) then
  179. begin
  180. DestS:=s2;
  181. exit;
  182. end;
  183. if (length(S2)=0) then
  184. begin
  185. DestS:=s1;
  186. exit;
  187. end;
  188. sb:=JLStringBuilder.create(S1);
  189. sb.append(s2);
  190. DestS:=sb.toString;
  191. end;
  192. {$define FPC_HAS_UNICODESTR_CONCAT_MULTI}
  193. procedure fpc_UnicodeStr_Concat_multi (var DestS:Unicodestring;const sarr:array of Unicodestring); compilerproc;
  194. Var
  195. i : Longint;
  196. Size,NewSize : SizeInt;
  197. sb: JLStringBuilder;
  198. begin
  199. { First calculate size of the result so we can allocate a StringBuilder of
  200. the right size }
  201. NewSize:=0;
  202. for i:=low(sarr) to high(sarr) do
  203. inc(Newsize,length(sarr[i]));
  204. sb:=JLStringBuilder.create(NewSize);
  205. for i:=low(sarr) to high(sarr) do
  206. begin
  207. if length(sarr[i])>0 then
  208. sb.append(sarr[i]);
  209. end;
  210. dests:=sb.toString;
  211. end;
  212. {$define FPC_HAS_CHAR_TO_UCHAR}
  213. Function fpc_Char_To_UChar(const c : AnsiChar): UnicodeChar; compilerproc;
  214. var
  215. arr: array[0..0] of ansichar;
  216. w: unicodestring;
  217. begin
  218. arr[0]:=c;
  219. widestringmanager.Ansi2UnicodeMoveProc(pansichar(@arr),DefaultSystemCodePage,w,1);
  220. fpc_Char_To_UChar:=w[1];
  221. end;
  222. {$define FPC_HAS_CHAR_TO_UNICODESTR}
  223. Function fpc_Char_To_UnicodeStr(const c : AnsiChar): UnicodeString; compilerproc;
  224. {
  225. Converts a AnsiChar to a UnicodeString;
  226. }
  227. var
  228. arr: array[0..0] of ansichar;
  229. begin
  230. arr[0]:=c;
  231. widestringmanager.Ansi2UnicodeMoveProc(pansichar(@arr),DefaultSystemCodePage,result,1);
  232. end;
  233. {$define FPC_HAS_UCHAR_TO_CHAR}
  234. Function fpc_UChar_To_Char(const c : UnicodeChar): Char; compilerproc;
  235. {
  236. Converts a UnicodeChar to a Char;
  237. }
  238. var
  239. u: unicodestring;
  240. s: RawByteString;
  241. arr: array[0..0] of unicodechar;
  242. begin
  243. arr[0]:=c;
  244. widestringmanager.Unicode2AnsiMoveProc(punicodechar(@arr), s, DefaultSystemCodePage, 1);
  245. if length(s)=1 then
  246. fpc_UChar_To_Char:= s[1]
  247. else
  248. fpc_UChar_To_Char:='?';
  249. end;
  250. { lie, unused for this target since widechar = unicodechar }
  251. {$define FPC_HAS_UCHAR_TO_UNICODESTR}
  252. Function fpc_UChar_To_UnicodeStr(const c : UnicodeChar): UnicodeString; compilerproc;
  253. {
  254. Converts a UnicodeChar to a UnicodeString;
  255. }
  256. var
  257. arr: array[0..0] of UnicodeChar;
  258. begin
  259. arr[0]:=c;
  260. result:=JLString.create(arr);
  261. end;
  262. {$define FPC_HAS_UCHAR_TO_ANSISTR}
  263. Function fpc_UChar_To_AnsiStr(const c : UnicodeChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): AnsiString; compilerproc;
  264. {
  265. Converts a UnicodeChar to a AnsiString;
  266. }
  267. var
  268. u: unicodestring;
  269. arr: array[0..0] of unicodechar;
  270. begin
  271. arr[0]:=c;
  272. cp:=TranslatePlaceholderCP(cp);
  273. widestringmanager.Unicode2AnsiMoveProc(punicodechar(@arr), RawByteString(fpc_UChar_To_AnsiStr), cp, 1);
  274. end;
  275. {$define FPC_HAS_UCHAR_TO_SHORTSTR}
  276. function fpc_UChar_To_ShortStr(const c : UnicodeChar): shortstring; compilerproc;
  277. {
  278. Converts a UnicodeChar to a AnsiString;
  279. }
  280. var
  281. u: unicodestring;
  282. begin
  283. u:=c;
  284. result:=u;
  285. end;
  286. {$ifndef FPC_HAS_UCHAR_TO_ANSISTR}
  287. {$define FPC_HAS_UCHAR_TO_ANSISTR}
  288. Function fpc_UChar_To_AnsiStr(const c : UnicodeChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): AnsiString; compilerproc;
  289. {
  290. Converts a UnicodeChar to a AnsiString;
  291. }
  292. var
  293. arr: array[0..0] of unicodechar;
  294. {$ifndef FPC_HAS_CPSTRING}
  295. cp : TSystemCodePage;
  296. {$endif FPC_HAS_CPSTRING}
  297. begin
  298. {$ifndef FPC_HAS_CPSTRING}
  299. cp:=DefaultSystemCodePage;
  300. {$endif FPC_HAS_CPSTRING}
  301. cp:=TranslatePlaceholderCP(cp);
  302. arr[0]:=c;
  303. widestringmanager.Unicode2AnsiMoveProc(punicodechar(@arr[0]), fpc_UChar_To_AnsiStr, cp, 1);
  304. end;
  305. {$endif FPC_HAS_UCHAR_TO_ANSISTR}
  306. {$define FPC_HAS_PCHAR_TO_UNICODESTR}
  307. Function fpc_PChar_To_UnicodeStr(const p : pchar): UnicodeString; compilerproc;
  308. var
  309. i, len: longint;
  310. arr: TAnsiCharArray;
  311. begin
  312. arr:=TAnsiCharArray(p);
  313. i:=0;
  314. while arr[i]<>#0 do
  315. inc(i);
  316. if i<>0 then
  317. widestringmanager.Ansi2UnicodeMoveProc(P,DefaultSystemCodePage,fpc_PChar_To_UnicodeStr,i)
  318. else
  319. result:=''
  320. end;
  321. Function real_widechararray_to_unicodestr(const arr: array of widechar; zerobased: boolean): Unicodestring;
  322. var
  323. i : SizeInt;
  324. foundnull : boolean;
  325. begin
  326. if (zerobased) then
  327. begin
  328. foundnull:=false;
  329. for i:=low(arr) to high(arr) do
  330. if arr[i]=#0 then
  331. begin
  332. foundnull:=true;
  333. break;
  334. end;
  335. if not foundnull then
  336. i := high(arr)+1;
  337. end
  338. else
  339. i := high(arr)+1;
  340. result:=JLString.create(arr,0,i);
  341. end;
  342. {$define FPC_HAS_WIDECHARARRAY_TO_UNICODESTR}
  343. Function fpc_WideCharArray_To_UnicodeStr(const arr: array of widechar; zerobased: boolean = true): UnicodeString; compilerproc;
  344. begin
  345. result:=real_widechararray_to_unicodestr(arr,zerobased);
  346. end;
  347. { due to their names, the following procedures should be in wstrings.inc,
  348. however, the compiler generates code using this functions on all platforms }
  349. {$define FPC_HAS_WIDECHARARRAY_TO_SHORTSTR}
  350. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  351. begin
  352. res:=real_widechararray_to_unicodestr(arr,zerobased);
  353. end;
  354. {$define FPC_HAS_WIDECHARARRAY_TO_WIDESTR}
  355. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  356. begin
  357. result:=real_widechararray_to_unicodestr(arr,zerobased);
  358. end;
  359. {$define FPC_HAS_UNICODESTR_TO_CHARARRAY}
  360. procedure fpc_unicodestr_to_chararray(out res: array of AnsiChar; const src: UnicodeString); compilerproc;
  361. var
  362. len: longint;
  363. temp: array of jbyte;
  364. begin
  365. len:=length(src);
  366. { make sure we don't dereference src if it can be nil (JM) }
  367. if len>0 then
  368. begin
  369. temp:=JLString(src).getBytes;
  370. len:=length(temp);
  371. if len>length(res) then
  372. len:=length(res);
  373. JLSystem.ArrayCopy(JLObject(temp),0,JLObject(@res),0,len);
  374. end;
  375. if len<=high(res) then
  376. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  377. end;
  378. function fpc_unicodestr_setchar(const s: UnicodeString; const index: longint; const ch: unicodechar): UnicodeString; compilerproc;
  379. var
  380. sb: JLStringBuilder;
  381. begin
  382. sb:=JLStringBuilder.create(s);
  383. { string indexes are 1-based in Pascal, 0-based in Java }
  384. sb.setCharAt(index-1,ch);
  385. result:=sb.toString();
  386. end;
  387. {$define FPC_HAS_ANSISTR_TO_WIDECHARARRAY}
  388. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: RawByteString); compilerproc;
  389. var
  390. len: SizeInt;
  391. temp: widestring;
  392. begin
  393. len := length(src);
  394. { make sure we don't dereference src if it can be nil (JM) }
  395. if len > 0 then
  396. temp:=src;
  397. len := length(temp);
  398. if len > high(res)+1 then
  399. len := high(res)+1;
  400. JLString(temp).getChars(0,len,res,0);
  401. JUArrays.fill(res,len,high(res),#0);
  402. end;
  403. {$define FPC_HAS_SHORTSTR_TO_WIDECHARARRAY}
  404. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  405. var
  406. len: longint;
  407. temp : unicodestring;
  408. begin
  409. len := length(src);
  410. { temp is initialized with an empty string, so no need to convert src in case
  411. it's also empty}
  412. if len > 0 then
  413. temp:=src;
  414. len := length(temp);
  415. if len > high(res)+1 then
  416. len := high(res)+1;
  417. JLString(temp).getChars(0,len,res,0);
  418. JUArrays.fill(res,len,high(res),#0);
  419. end;
  420. {$define FPC_HAS_UNICODESTR_TO_WIDECHARARRAY}
  421. procedure fpc_unicodestr_to_widechararray(out res: array of widechar; const src: UnicodeString); compilerproc;
  422. var
  423. i, len: SizeInt;
  424. begin
  425. len := length(src);
  426. if len > length(res) then
  427. len := length(res);
  428. JLString(src).getChars(0,len,res,0);
  429. end;
  430. {$define FPC_HAS_UNICODESTR_COMPARE}
  431. Function fpc_UnicodeStr_Compare(const S1,S2 : UnicodeString): SizeInt; compilerproc;
  432. {
  433. Compares 2 UnicodeStrings;
  434. The result is
  435. <0 if S1<S2
  436. 0 if S1=S2
  437. >0 if S1>S2
  438. }
  439. Var
  440. MaxI,Temp : SizeInt;
  441. begin
  442. if JLObject(S1)=JLObject(S2) then
  443. begin
  444. result:=0;
  445. exit;
  446. end;
  447. result:=JLString(S1).compareTo(S2);
  448. end;
  449. {$define FPC_HAS_UNICODESTR_COMPARE_EQUAL}
  450. Function fpc_UnicodeStr_Compare_Equal(const S1,S2 : UnicodeString): SizeInt; compilerproc;
  451. {
  452. Compares 2 UnicodeStrings for equality only;
  453. The result is
  454. 0 if S1=S2
  455. <>0 if S1<>S2
  456. }
  457. Var
  458. MaxI : SizeInt;
  459. begin
  460. result:=ord(not JLString(S1).equals(JLString(S2)));
  461. end;
  462. { lie, not required for this target }
  463. {$define FPC_HAS_UNICODESTR_RANGECHECK}
  464. {$define FPC_HAS_UNICODESTR_SETLENGTH}
  465. Procedure fpc_UnicodeStr_SetLength(Var S : UnicodeString; l : SizeInt);[Public,Alias : 'FPC_UNICODESTR_SETLENGTH']; compilerproc;
  466. {
  467. Sets The length of string S to L.
  468. Makes sure S is unique, and contains enough room.
  469. Returns new val
  470. }
  471. Var
  472. result: UnicodeString;
  473. movelen: SizeInt;
  474. chars: array of widechar;
  475. strlen: SizeInt;
  476. begin
  477. if (l>0) then
  478. begin
  479. if JLObject(S)=nil then
  480. begin
  481. { Need a completely new string...}
  482. result:=NewUnicodeString(l);
  483. end
  484. { no need to create a new string, since Java strings are immutable }
  485. else
  486. begin
  487. strlen:=length(s);
  488. if l=strlen then
  489. result:=s
  490. else if (l<strlen) then
  491. result:=JLString(s).substring(0,l)
  492. else
  493. begin
  494. setlength(chars,l);
  495. JLString(s).getChars(0,strlen,chars,0);
  496. result:=JLString.create(chars,0,l)
  497. end;
  498. end
  499. end
  500. else
  501. begin
  502. result:='';
  503. end;
  504. S:=Result;
  505. end;
  506. {*****************************************************************************
  507. Public functions, In interface.
  508. *****************************************************************************}
  509. {$define FPC_HAS_STRING_LEN_TO_WIDECHAR}
  510. function StringToWideChar(const Src : RawByteString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  511. var
  512. temp: widestring;
  513. Len: SizeInt;
  514. begin
  515. temp:=src;
  516. Len:=Length(temp);
  517. if DestSize<=Len then
  518. Len:=Destsize-1;
  519. JLString(temp).getChars(0,Len,TJCharArray(Dest),0);
  520. Dest[Len]:=#0;
  521. result:=Dest;
  522. end;
  523. {$define FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  524. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : UnicodeString;
  525. begin
  526. result:=JLString.Create(TJCharArray(S),0,len);
  527. end;
  528. {$define FPC_HAS_WIDECHAR_LEN_TO_STRING}
  529. function WideCharLenToString(S : PWideChar;Len : SizeInt) : UnicodeString;
  530. begin
  531. result:=JLString.Create(TJCharArray(S),0,len);
  532. end;
  533. {$define FPC_HAS_UNICODESTR_UNIQUE}
  534. Function fpc_unicodestr_Unique(var S : JLObject): JLObject; compilerproc;
  535. begin
  536. result:=s;
  537. end;
  538. { the publicly accessible uniquestring function is declared as
  539. "external name 'FPC_UNICODESTR_UNIQUE'", which is normally an alias for
  540. the fpc_unicodestr_Unique compiler proc; since one is a function and the
  541. other a procedure that sort of hackery doesn't work for the JVM -> create
  542. a separate procedure for that (since Java strings are immutable, they are
  543. always unique though) }
  544. procedure FPC_UNICODESTR_UNIQUE(var S : UnicodeString);
  545. begin
  546. { do nothing }
  547. end;
  548. {$define FPC_HAS_UNICODESTR_COPY}
  549. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  550. begin
  551. dec(index);
  552. if Index < 0 then
  553. Index := 0;
  554. { Check Size. Accounts for Zero-length S, the double check is needed because
  555. Size can be maxint and will get <0 when adding index }
  556. if (Size>Length(S)) or
  557. (Index+Size>Length(S)) then
  558. Size:=Length(S)-Index;
  559. If Size>0 then
  560. result:=JLString(s).subString(Index,Index+Size)
  561. else
  562. result:='';
  563. end;
  564. {$define FPC_HAS_POS_UNICODESTR_UNICODESTR}
  565. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  566. begin
  567. Pos:=0;
  568. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  569. Pos:=JLString(Source).indexOf(SubStr,Offset-1)+1
  570. end;
  571. { Faster version for a unicodechar alone }
  572. {$define FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  573. Function Pos (c : UnicodeChar; Const s : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  574. begin
  575. Pos:=0;
  576. if (Offset>0) and (Offset<=Length(s)) then
  577. Pos:=JLString(s).indexOf(ord(c),Offset-1)+1;
  578. end;
  579. { Faster version for a char alone. Must be implemented because }
  580. { pos(c: char; const s: shortstring) also exists, so otherwise }
  581. { using pos(char,pchar) will always call the shortstring version }
  582. { (exact match for first argument), also with $h+ (JM) }
  583. {$define FPC_HAS_POS_CHAR_UNICODESTR}
  584. Function Pos (c : AnsiChar; Const s : UnicodeString; Offset: Sizeint = 1) : SizeInt;
  585. var
  586. i: SizeInt;
  587. wc : unicodechar;
  588. begin
  589. wc:=c;
  590. result:=Pos(wc,s,Offset);
  591. end;
  592. {$define FPC_HAS_DELETE_UNICODESTR}
  593. Procedure Delete (Var S : UnicodeString; Index,Size: SizeInt);
  594. Var
  595. LS : SizeInt;
  596. sb: JLStringBuilder;
  597. begin
  598. LS:=Length(S);
  599. if (Index>LS) or (Index<=0) or (Size<=0) then
  600. exit;
  601. { (Size+Index) will overflow if Size=MaxInt. }
  602. if Size>LS-Index then
  603. Size:=LS-Index+1;
  604. if Size<=LS-Index then
  605. begin
  606. Dec(Index);
  607. sb:=JLStringBuilder.Create(s);
  608. sb.delete(index,size);
  609. s:=sb.toString;
  610. end
  611. else
  612. s:=JLString(s).substring(0,index-1);
  613. end;
  614. {$define FPC_HAS_INSERT_UNICODESTR}
  615. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  616. var
  617. Temp : UnicodeString;
  618. LS : SizeInt;
  619. sb : JLStringBuilder;
  620. begin
  621. If Length(Source)=0 then
  622. exit;
  623. if index <= 0 then
  624. index := 1;
  625. Ls:=Length(S);
  626. if index > LS then
  627. index := LS+1;
  628. Dec(Index);
  629. sb:=JLStringBuilder.Create(S);
  630. sb.insert(Index,Source);
  631. S:=sb.toString;
  632. end;
  633. {$define FPC_HAS_UPCASE_UNICODECHAR}
  634. Function UpCase(c:UnicodeChar):UnicodeChar;
  635. begin
  636. result:=JLCharacter.toUpperCase(c);
  637. end;
  638. {$define FPC_HAS_UPCASE_UNICODESTR}
  639. function UpCase(const s : UnicodeString) : UnicodeString;
  640. begin
  641. result:=JLString(s).toUpperCase;
  642. end;
  643. {$define FPC_HAS_LOWERCASE_UNICODECHAR}
  644. Function LowerCase(c:UnicodeChar):UnicodeChar;
  645. begin
  646. result:=JLCharacter.toLowerCase(c);
  647. end;
  648. {$define FPC_HAS_LOWERCASE_UNICODESTR}
  649. function LowerCase(const s : UnicodeString) : UnicodeString;
  650. begin
  651. result:=JLString(s).toLowerCase;
  652. end;
  653. {$define FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  654. Procedure fpc_setstring_unicodestr_pwidechar(Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt); compilerproc;
  655. begin
  656. if assigned(buf) and (Len>0) then
  657. s:=JLString.Create(TJCharArray(Buf),0,Len)
  658. else
  659. s:='';
  660. end;
  661. {$define FPC_HAS_UTF8ENCODE_UNICODESTRING}
  662. function UTF8Encode(const s : UnicodeString) : RawByteString;
  663. var
  664. i : SizeInt;
  665. hs : UTF8String;
  666. chars: array of widechar;
  667. begin
  668. result:='';
  669. if s='' then
  670. exit;
  671. SetLength(hs,length(s)*3);
  672. chars:=JLString(s).toCharArray;
  673. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,pwidechar(chars),length(s));
  674. if i>0 then
  675. begin
  676. SetLength(hs,i-1);
  677. result:=hs;
  678. end;
  679. end;
  680. {$define FPC_HAS_UTF8DECODE_UNICODESTRING}
  681. function UTF8Decode(const s : RawByteString): UnicodeString;
  682. var
  683. i : SizeInt;
  684. chars: array of widechar;
  685. begin
  686. result:='';
  687. if s='' then
  688. exit;
  689. SetLength(chars,length(s)+1);
  690. i:=Utf8ToUnicode(pwidechar(chars),length(s)+1,pchar(s),length(s));
  691. if i>0 then
  692. result:=JLString.Create(chars,0,i-1);
  693. end;
  694. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  695. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  696. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: JLStringBuilder; var index: SizeInt);
  697. begin
  698. { if nc > $ffff, we need two places }
  699. if (index+ord(nc > $ffff)>s.length) then
  700. if (s.length < 10*256) then
  701. s.setLength(s.length+10)
  702. else
  703. s.setlength(s.length+s.length shr 8);
  704. if (nc<$ffff) then
  705. begin
  706. s.setCharAt(index-1,unicodechar(nc));
  707. inc(index);
  708. end
  709. else if (dword(nc)<=$10ffff) then
  710. begin
  711. s.setCharAt(index-1,unicodechar((nc - $10000) shr 10 + $d800));
  712. s.setCharAt(index,unicodechar((nc - $10000) and $3ff + $dc00));
  713. inc(index,2);
  714. end
  715. else
  716. { invalid code point }
  717. begin
  718. s.setCharAt(index-1,'?');
  719. inc(index);
  720. end;
  721. end;
  722. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  723. var
  724. i : SizeInt;
  725. resindex : SizeInt;
  726. tmpres: JLStringBuilder;
  727. begin
  728. { skip terminating #0 }
  729. tmpres:=JLStringBuilder.Create(length(s)-1);
  730. resindex:=1;
  731. for i:=0 to high(s)-1 do
  732. ConcatUTF32ToUnicodeStr(s[i],tmpres,resindex);
  733. { adjust result length (may be too big due to growing }
  734. { for surrogate pairs) }
  735. tmpres.setLength(resindex-1);
  736. result:=tmpres.toString;
  737. end;
  738. procedure UCS4Encode(p: PWideChar; len: sizeint; out res: UCS4String); forward;
  739. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  740. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  741. begin
  742. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  743. end;
  744. {$define FPC_HAS_WIDESTR_TO_UCS4STRING}
  745. function WideStringToUCS4String(const s : WideString) : UCS4String;
  746. begin
  747. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  748. end;
  749. {$define FPC_HAS_UCS4STRING_TO_WIDESTR}
  750. function UCS4StringToWideString(const s : UCS4String) : WideString;
  751. begin
  752. result:=UCS4StringToUnicodeString(s);
  753. end;
  754. function StringElementSize(const S : UnicodeString): Word;
  755. begin
  756. result:=sizeof(unicodechar);
  757. end;
  758. function StringRefCount(const S : UnicodeString): SizeInt;
  759. begin
  760. if assigned(pointer(s)) then
  761. result:=1
  762. else
  763. result:=0;
  764. end;
  765. function StringCodePage(const S : UnicodeString): TSystemCodePage;
  766. begin
  767. if assigned(pointer(s)) then
  768. result:=CP_UTF16BE
  769. else
  770. result:=DefaultUnicodeCodePage;
  771. end;
  772. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  773. Function ToSingleByteFileSystemEncodedFileName(const Str: UnicodeString): RawByteString;
  774. Begin
  775. result:=AnsiString(AnsistringClass.Create(Str,DefaultFileSystemCodePage));
  776. End;
  777. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  778. Function ToSingleByteFileSystemEncodedFileName(const arr: array of widechar): RawByteString;
  779. Begin
  780. result:=AnsiString(AnsistringClass.Create(arr,DefaultFileSystemCodePage));
  781. End;
  782. { helpers for converting between Windows and Java code page identifiers }
  783. {$i jwin2javacharset.inc}
  784. { *************************************************************************** }
  785. { ************************* Collator threadvar ****************************** }
  786. { *************************************************************************** }
  787. function TCollatorThreadVar.InitialValue: JLObject;
  788. begin
  789. { get a copy, since we modify the collator (e.g. setting the strength) }
  790. result:=JTCollator.getInstance.clone
  791. end;
  792. { *************************************************************************** }
  793. { ************************ Helpers for en/decode **************************** }
  794. { *************************************************************************** }
  795. function GetOrInsertNewEnDecoder(hm: JUWeakHashMap; cp: TSystemCodePage; decoder: boolean): JLObject;
  796. var
  797. cs: JNCCharSet;
  798. replacement: array[0..0] of jbyte;
  799. begin
  800. result:=hm.get(JLInteger.valueOf(cp));
  801. if not assigned(result) then
  802. begin
  803. try
  804. cs:=JNCCharSet.forName(win2javacs(cp));
  805. except
  806. { does not exist or not supported, fall back to ASCII like on other
  807. platforms}
  808. cs:=JNCCharset.forName('US-ASCII')
  809. end;
  810. if decoder then
  811. begin
  812. result:=cs.newDecoder;
  813. JNCCharsetDecoder(result).replaceWith('?');
  814. end
  815. else
  816. begin
  817. result:=cs.newEncoder;
  818. replacement[0]:=ord('?');
  819. JNCCharsetEncoder(result).replaceWith(replacement);
  820. end;
  821. { store in weak hashmap for future (possible) reuse }
  822. hm.put(JLInteger.Create(cp),result);
  823. end;
  824. end;
  825. { *************************************************************************** }
  826. { ************************** Decoder threadvar ****************************** }
  827. { *************************************************************************** }
  828. function TCharsetDecoderThreadvar.InitialValue: JLObject;
  829. begin
  830. result:=JUWeakHashMap.Create;
  831. end;
  832. function TCharsetDecoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetDecoder;
  833. var
  834. hm: JUWeakHashMap;
  835. begin
  836. hm:=JUWeakHashMap(get);
  837. result:=JNCCharsetDecoder(GetOrInsertNewEnDecoder(hm,cp,true));
  838. end;
  839. { *************************************************************************** }
  840. { ************************** Encoder threadvar ****************************** }
  841. { *************************************************************************** }
  842. function TCharsetEncoderThreadvar.InitialValue: JLObject;
  843. begin
  844. result:=JUWeakHashMap.Create;
  845. end;
  846. function TCharsetEncoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetEncoder;
  847. var
  848. hm: JUWeakHashMap;
  849. begin
  850. hm:=JUWeakHashMap(get);
  851. result:=JNCCharsetEncoder(GetOrInsertNewEnDecoder(hm,cp,false));
  852. end;
  853. { *************************************************************************** }
  854. { ************************ TUnicodeStringManager **************************** }
  855. { *************************************************************************** }
  856. class constructor TUnicodeStringManager.ClassCreate;
  857. begin
  858. collator:=TCollatorThreadVar.Create;
  859. decoder:=TCharsetDecoderThreadVar.Create;
  860. encoder:=TCharsetEncoderThreadVar.Create;
  861. DefaultSystemCodePage:=javacs2win(JNCCharset.defaultCharset.name);
  862. { unknown/unsupported -> default to ASCII (this will be used to parse
  863. stdin etc, so setting this to utf-8 or so won't help) }
  864. if DefaultSystemCodePage=65535 then
  865. DefaultSystemCodePage:=20127;
  866. DefaultFileSystemCodePage:=DefaultSystemCodePage;
  867. DefaultRTLFileSystemCodePage:=DefaultFileSystemCodePage;
  868. DefaultUnicodeCodePage:=CP_UTF16BE;
  869. end;
  870. procedure TUnicodeStringManager.Wide2AnsiMoveProc(source:pwidechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  871. begin
  872. DefaultUnicode2AnsiMove(source,dest,cp,len);
  873. end;
  874. procedure TUnicodeStringManager.Ansi2WideMoveProc(source:pchar;cp : TSystemCodePage;var dest:widestring;len:SizeInt);
  875. begin
  876. DefaultAnsi2UnicodeMove(source,cp,dest,len);
  877. end;
  878. function TUnicodeStringManager.UpperWideStringProc(const S: WideString): WideString;
  879. begin
  880. result:=upcase(s);
  881. end;
  882. function TUnicodeStringManager.LowerWideStringProc(const S: WideString): WideString;
  883. begin
  884. result:=lowercase(s);
  885. end;
  886. function TUnicodeStringManager.CompareWideStringProc(const s1, s2 : WideString) : PtrInt;
  887. var
  888. localcollator: JTCollator;
  889. begin
  890. localcollator:=JTCollator(collator.get);
  891. localcollator.setStrength(JTCollator.IDENTICAL);
  892. result:=localcollator.compare(s1,s2);
  893. end;
  894. function TUnicodeStringManager.CompareTextWideStringProc(const s1, s2 : WideString): PtrInt;
  895. var
  896. localcollator: JTCollator;
  897. begin
  898. localcollator:=JTCollator(collator.get);
  899. localcollator.setStrength(JTCollator.TERTIARY);
  900. result:=localcollator.compare(s1,s2);
  901. end;
  902. function TUnicodeStringManager.CharLengthPCharProc(const Str: PChar; Index: PtrInt): PtrInt;
  903. var
  904. localdecoder: JNCCharsetDecoder;
  905. begin
  906. localdecoder:=JNCCharsetDecoder(decoder.get);
  907. localdecoder.reset;
  908. localdecoder.onMalformedInput(JNCCodingErrorAction.fREPLACE);
  909. localdecoder.onUnmappableCharacter(JNCCodingErrorAction.fREPLACE);
  910. result:=localdecoder.decode(JNByteBuffer.wrap(TJByteArray(Str),Index,length(Str)-Index)).length;
  911. end;
  912. function TUnicodeStringManager.CodePointLengthProc(const Str: PChar; Index, MaxLookAhead: PtrInt): Ptrint;
  913. var
  914. localdecoder: JNCCharsetDecoder;
  915. inbuf: JNByteBuffer;
  916. outbuf: JNCharBuffer;
  917. coderres: JNCCoderResult;
  918. limit, maxlimit: longint;
  919. begin
  920. localdecoder:=JNCCharsetDecoder(decoder.get);
  921. localdecoder.reset;
  922. localdecoder.onMalformedInput(JNCCodingErrorAction.fREPORT);
  923. localdecoder.onUnmappableCharacter(JNCCodingErrorAction.fREPORT);
  924. localdecoder.reset;
  925. limit:=0;
  926. maxlimit:=min(length(Str)-Index,MaxLookAhead);
  927. { end of pchar? }
  928. if maxlimit=0 then
  929. begin
  930. result:=0;
  931. exit;
  932. end;
  933. inbuf:=JNByteBuffer.wrap(TJByteArray(Str),Index,Index+maxlimit);
  934. { we will get at most 2 output characters (when decoding from UTF-32 to
  935. UTF-16) }
  936. outbuf:=JNCharBuffer.allocate(2);
  937. { keep trying to decode until we managed to decode one character or
  938. reached the limit }
  939. repeat
  940. inc(limit);
  941. inbuf.limit(limit);
  942. coderres:=localdecoder.decode(inbuf,outbuf,true);
  943. until not coderres.isError or
  944. (limit=MaxLookAhead);
  945. if not coderres.isError then
  946. result:=inbuf.limit
  947. else
  948. result:=-1;
  949. end;
  950. function TUnicodeStringManager.UpperAnsiStringProc(const s : ansistring) : ansistring;
  951. begin
  952. result:=UpperWideStringProc(s);
  953. end;
  954. function TUnicodeStringManager.LowerAnsiStringProc(const s : ansistring) : ansistring;
  955. begin
  956. result:=LowerWideStringProc(s);
  957. end;
  958. function TUnicodeStringManager.CompareStrAnsiStringProc(const S1, S2: ansistring): PtrInt;
  959. begin
  960. result:=CompareUnicodeStringProc(S1,S2);
  961. end;
  962. function TUnicodeStringManager.CompareTextAnsiStringProc(const S1, S2: ansistring): PtrInt;
  963. begin
  964. result:=CompareTextUnicodeStringProc(S1,S2);
  965. end;
  966. function TUnicodeStringManager.StrCompAnsiStringProc(S1, S2: PChar): PtrInt;
  967. var
  968. str1,str2: unicodestring;
  969. begin
  970. str1:=JLString.Create(TJCharArray(S1),0,length(S1));
  971. str2:=JLString.Create(TJCharArray(S2),0,length(S2));
  972. result:=CompareUnicodeStringProc(str1,str2);
  973. end;
  974. function TUnicodeStringManager.StrICompAnsiStringProc(S1, S2: PChar): PtrInt;
  975. var
  976. str1,str2: unicodestring;
  977. begin
  978. str1:=JLString.Create(TJCharArray(S1),0,length(S1));
  979. str2:=JLString.Create(TJCharArray(S2),0,length(S2));
  980. result:=CompareTextUnicodeStringProc(str1,str2);
  981. end;
  982. function TUnicodeStringManager.StrLCompAnsiStringProc(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  983. var
  984. str1,str2: unicodestring;
  985. begin
  986. str1:=JLString.Create(TJCharArray(S1),0,min(length(S1),MaxLen));
  987. str2:=JLString.Create(TJCharArray(S2),0,min(length(S2),MaxLen));
  988. result:=CompareUnicodeStringProc(str1,str2);
  989. end;
  990. function TUnicodeStringManager.StrLICompAnsiStringProc(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  991. var
  992. str1,str2: unicodestring;
  993. begin
  994. str1:=JLString.Create(TJCharArray(S1),0,min(length(S1),MaxLen));
  995. str2:=JLString.Create(TJCharArray(S2),0,min(length(S2),MaxLen));
  996. result:=CompareTextUnicodeStringProc(str1,str2);
  997. end;
  998. function TUnicodeStringManager.StrLowerAnsiStringProc(Str: PChar): PChar;
  999. var
  1000. ustr: unicodestring;
  1001. begin
  1002. ustr:=JLString.Create(TJCharArray(Str),0,length(Str));
  1003. result:=PChar(AnsiStringClass(ansistring(LowerWideStringProc(ustr))).fdata);
  1004. end;
  1005. function TUnicodeStringManager.StrUpperAnsiStringProc(Str: PChar): PChar;
  1006. var
  1007. ustr: unicodestring;
  1008. begin
  1009. ustr:=JLString.Create(TJCharArray(Str),0,length(Str));
  1010. result:=PChar(AnsiStringClass(ansistring(UpperWideStringProc(ustr))).fdata);
  1011. end;
  1012. procedure TUnicodeStringManager.Unicode2AnsiMoveProc(source:punicodechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  1013. begin
  1014. DefaultUnicode2AnsiMove(source,dest,cp,len);
  1015. end;
  1016. procedure TUnicodeStringManager.Ansi2UnicodeMoveProc(source:pchar;cp : TSystemCodePage;var dest:unicodestring;len:SizeInt);
  1017. begin
  1018. DefaultAnsi2UnicodeMove(source,cp,dest,len);
  1019. end;
  1020. function TUnicodeStringManager.UpperUnicodeStringProc(const S: UnicodeString): UnicodeString;
  1021. begin
  1022. result:=UpperWideStringProc(S);
  1023. end;
  1024. function TUnicodeStringManager.LowerUnicodeStringProc(const S: UnicodeString): UnicodeString;
  1025. begin
  1026. result:=LowerWideStringProc(S);
  1027. end;
  1028. function TUnicodeStringManager.CompareUnicodeStringProc(const s1, s2 : UnicodeString) : PtrInt;
  1029. begin
  1030. result:=CompareWideStringProc(s1,s2);
  1031. end;
  1032. function TUnicodeStringManager.CompareTextUnicodeStringProc(const s1, s2 : UnicodeString): PtrInt;
  1033. begin
  1034. result:=CompareTextWideStringProc(s1,s2);
  1035. end;
  1036. procedure initunicodestringmanager;
  1037. begin
  1038. widestringmanager:=TUnicodeStringManager.Create;
  1039. end;