justrings.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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) : SizeInt;
  566. begin
  567. Pos:=0;
  568. if Length(SubStr)>0 then
  569. Pos:=JLString(Source).indexOf(SubStr)+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) : SizeInt;
  574. begin
  575. Pos:=0;
  576. if length(S)>0 then
  577. Pos:=JLString(s).indexOf(ord(c))+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) : SizeInt;
  585. var
  586. i: SizeInt;
  587. wc : unicodechar;
  588. begin
  589. wc:=c;
  590. result:=Pos(wc,s);
  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. hs : UnicodeString;
  685. chars: array of widechar;
  686. begin
  687. result:='';
  688. if s='' then
  689. exit;
  690. SetLength(chars,length(s));
  691. i:=Utf8ToUnicode(pwidechar(chars),length(hs)+1,pchar(s),length(s));
  692. if i>0 then
  693. begin
  694. result:=JLString.Create(chars,0,i);
  695. end;
  696. end;
  697. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  698. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  699. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: JLStringBuilder; var index: SizeInt);
  700. begin
  701. { if nc > $ffff, we need two places }
  702. if (index+ord(nc > $ffff)>s.length) then
  703. if (s.length < 10*256) then
  704. s.setLength(s.length+10)
  705. else
  706. s.setlength(s.length+s.length shr 8);
  707. if (nc<$ffff) then
  708. begin
  709. s.setCharAt(index-1,unicodechar(nc));
  710. inc(index);
  711. end
  712. else if (dword(nc)<=$10ffff) then
  713. begin
  714. s.setCharAt(index-1,unicodechar((nc - $10000) shr 10 + $d800));
  715. s.setCharAt(index,unicodechar((nc - $10000) and $3ff + $dc00));
  716. inc(index,2);
  717. end
  718. else
  719. { invalid code point }
  720. begin
  721. s.setCharAt(index-1,'?');
  722. inc(index);
  723. end;
  724. end;
  725. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  726. var
  727. i : SizeInt;
  728. resindex : SizeInt;
  729. tmpres: JLStringBuilder;
  730. begin
  731. { skip terminating #0 }
  732. tmpres:=JLStringBuilder.Create(length(s)-1);
  733. resindex:=1;
  734. for i:=0 to high(s)-1 do
  735. ConcatUTF32ToUnicodeStr(s[i],tmpres,resindex);
  736. { adjust result length (may be too big due to growing }
  737. { for surrogate pairs) }
  738. tmpres.setLength(resindex-1);
  739. result:=tmpres.toString;
  740. end;
  741. procedure UCS4Encode(p: PWideChar; len: sizeint; out res: UCS4String); forward;
  742. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  743. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  744. begin
  745. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  746. end;
  747. {$define FPC_HAS_WIDESTR_TO_UCS4STRING}
  748. function WideStringToUCS4String(const s : WideString) : UCS4String;
  749. begin
  750. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  751. end;
  752. {$define FPC_HAS_UCS4STRING_TO_WIDESTR}
  753. function UCS4StringToWideString(const s : UCS4String) : WideString;
  754. begin
  755. result:=UCS4StringToUnicodeString(s);
  756. end;
  757. function StringElementSize(const S : UnicodeString): Word;
  758. begin
  759. result:=sizeof(unicodechar);
  760. end;
  761. function StringRefCount(const S : UnicodeString): SizeInt;
  762. begin
  763. if assigned(pointer(s)) then
  764. result:=1
  765. else
  766. result:=0;
  767. end;
  768. function StringCodePage(const S : UnicodeString): TSystemCodePage;
  769. begin
  770. if assigned(pointer(s)) then
  771. result:=CP_UTF16BE
  772. else
  773. result:=DefaultUnicodeCodePage;
  774. end;
  775. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODESTRING}
  776. Function ToSingleByteFileSystemEncodedFileName(const Str: UnicodeString): RawByteString;
  777. Begin
  778. result:=AnsiString(AnsistringClass.Create(Str,DefaultFileSystemCodePage));
  779. End;
  780. {$define FPC_HAS_TOSINGLEBYTEFILESYSTEMENCODEDFILENAME_UNICODECHARARRAY}
  781. Function ToSingleByteFileSystemEncodedFileName(const arr: array of widechar): RawByteString;
  782. Begin
  783. result:=AnsiString(AnsistringClass.Create(arr,DefaultFileSystemCodePage));
  784. End;
  785. { helpers for converting between Windows and Java code page identifiers }
  786. {$i jwin2javacharset.inc}
  787. { *************************************************************************** }
  788. { ************************* Collator threadvar ****************************** }
  789. { *************************************************************************** }
  790. function TCollatorThreadVar.InitialValue: JLObject;
  791. begin
  792. { get a copy, since we modify the collator (e.g. setting the strength) }
  793. result:=JTCollator.getInstance.clone
  794. end;
  795. { *************************************************************************** }
  796. { ************************ Helpers for en/decode **************************** }
  797. { *************************************************************************** }
  798. function GetOrInsertNewEnDecoder(hm: JUWeakHashMap; cp: TSystemCodePage; decoder: boolean): JLObject;
  799. var
  800. cs: JNCCharSet;
  801. replacement: array[0..0] of jbyte;
  802. begin
  803. result:=hm.get(JLInteger.valueOf(cp));
  804. if not assigned(result) then
  805. begin
  806. try
  807. cs:=JNCCharSet.forName(win2javacs(cp));
  808. except
  809. { does not exist or not supported, fall back to ASCII like on other
  810. platforms}
  811. cs:=JNCCharset.forName('US-ASCII')
  812. end;
  813. if decoder then
  814. begin
  815. result:=cs.newDecoder;
  816. JNCCharsetDecoder(result).replaceWith('?');
  817. end
  818. else
  819. begin
  820. result:=cs.newEncoder;
  821. replacement[0]:=ord('?');
  822. JNCCharsetEncoder(result).replaceWith(replacement);
  823. end;
  824. { store in weak hashmap for future (possible) reuse }
  825. hm.put(JLInteger.Create(cp),result);
  826. end;
  827. end;
  828. { *************************************************************************** }
  829. { ************************** Decoder threadvar ****************************** }
  830. { *************************************************************************** }
  831. function TCharsetDecoderThreadvar.InitialValue: JLObject;
  832. begin
  833. result:=JUWeakHashMap.Create;
  834. end;
  835. function TCharsetDecoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetDecoder;
  836. var
  837. hm: JUWeakHashMap;
  838. begin
  839. hm:=JUWeakHashMap(get);
  840. result:=JNCCharsetDecoder(GetOrInsertNewEnDecoder(hm,cp,true));
  841. end;
  842. { *************************************************************************** }
  843. { ************************** Encoder threadvar ****************************** }
  844. { *************************************************************************** }
  845. function TCharsetEncoderThreadvar.InitialValue: JLObject;
  846. begin
  847. result:=JUWeakHashMap.Create;
  848. end;
  849. function TCharsetEncoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetEncoder;
  850. var
  851. hm: JUWeakHashMap;
  852. begin
  853. hm:=JUWeakHashMap(get);
  854. result:=JNCCharsetEncoder(GetOrInsertNewEnDecoder(hm,cp,false));
  855. end;
  856. { *************************************************************************** }
  857. { ************************ TUnicodeStringManager **************************** }
  858. { *************************************************************************** }
  859. class constructor TUnicodeStringManager.ClassCreate;
  860. begin
  861. collator:=TCollatorThreadVar.Create;
  862. decoder:=TCharsetDecoderThreadVar.Create;
  863. encoder:=TCharsetEncoderThreadVar.Create;
  864. DefaultSystemCodePage:=javacs2win(JNCCharset.defaultCharset.name);
  865. { unknown/unsupported -> default to ASCII (this will be used to parse
  866. stdin etc, so setting this to utf-8 or so won't help) }
  867. if DefaultSystemCodePage=65535 then
  868. DefaultSystemCodePage:=20127;
  869. DefaultFileSystemCodePage:=DefaultSystemCodePage;
  870. DefaultRTLFileSystemCodePage:=DefaultFileSystemCodePage;
  871. DefaultUnicodeCodePage:=CP_UTF16BE;
  872. end;
  873. procedure TUnicodeStringManager.Wide2AnsiMoveProc(source:pwidechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  874. begin
  875. DefaultUnicode2AnsiMove(source,dest,cp,len);
  876. end;
  877. procedure TUnicodeStringManager.Ansi2WideMoveProc(source:pchar;cp : TSystemCodePage;var dest:widestring;len:SizeInt);
  878. begin
  879. DefaultAnsi2UnicodeMove(source,cp,dest,len);
  880. end;
  881. function TUnicodeStringManager.UpperWideStringProc(const S: WideString): WideString;
  882. begin
  883. result:=upcase(s);
  884. end;
  885. function TUnicodeStringManager.LowerWideStringProc(const S: WideString): WideString;
  886. begin
  887. result:=lowercase(s);
  888. end;
  889. function TUnicodeStringManager.CompareWideStringProc(const s1, s2 : WideString) : PtrInt;
  890. var
  891. localcollator: JTCollator;
  892. begin
  893. localcollator:=JTCollator(collator.get);
  894. localcollator.setStrength(JTCollator.IDENTICAL);
  895. result:=localcollator.compare(s1,s2);
  896. end;
  897. function TUnicodeStringManager.CompareTextWideStringProc(const s1, s2 : WideString): PtrInt;
  898. var
  899. localcollator: JTCollator;
  900. begin
  901. localcollator:=JTCollator(collator.get);
  902. localcollator.setStrength(JTCollator.TERTIARY);
  903. result:=localcollator.compare(s1,s2);
  904. end;
  905. function TUnicodeStringManager.CharLengthPCharProc(const Str: PChar; Index: PtrInt): PtrInt;
  906. var
  907. localdecoder: JNCCharsetDecoder;
  908. begin
  909. localdecoder:=JNCCharsetDecoder(decoder.get);
  910. localdecoder.reset;
  911. localdecoder.onMalformedInput(JNCCodingErrorAction.fREPLACE);
  912. localdecoder.onUnmappableCharacter(JNCCodingErrorAction.fREPLACE);
  913. result:=localdecoder.decode(JNByteBuffer.wrap(TJByteArray(Str),Index,length(Str)-Index)).length;
  914. end;
  915. function TUnicodeStringManager.CodePointLengthProc(const Str: PChar; Index, MaxLookAhead: PtrInt): Ptrint;
  916. var
  917. localdecoder: JNCCharsetDecoder;
  918. inbuf: JNByteBuffer;
  919. outbuf: JNCharBuffer;
  920. coderres: JNCCoderResult;
  921. limit, maxlimit: longint;
  922. begin
  923. localdecoder:=JNCCharsetDecoder(decoder.get);
  924. localdecoder.reset;
  925. localdecoder.onMalformedInput(JNCCodingErrorAction.fREPORT);
  926. localdecoder.onUnmappableCharacter(JNCCodingErrorAction.fREPORT);
  927. localdecoder.reset;
  928. limit:=0;
  929. maxlimit:=min(length(Str)-Index,MaxLookAhead);
  930. { end of pchar? }
  931. if maxlimit=0 then
  932. begin
  933. result:=0;
  934. exit;
  935. end;
  936. inbuf:=JNByteBuffer.wrap(TJByteArray(Str),Index,Index+maxlimit);
  937. { we will get at most 2 output characters (when decoding from UTF-32 to
  938. UTF-16) }
  939. outbuf:=JNCharBuffer.allocate(2);
  940. { keep trying to decode until we managed to decode one character or
  941. reached the limit }
  942. repeat
  943. inc(limit);
  944. inbuf.limit(limit);
  945. coderres:=localdecoder.decode(inbuf,outbuf,true);
  946. until not coderres.isError or
  947. (limit=MaxLookAhead);
  948. if not coderres.isError then
  949. result:=inbuf.limit
  950. else
  951. result:=-1;
  952. end;
  953. function TUnicodeStringManager.UpperAnsiStringProc(const s : ansistring) : ansistring;
  954. begin
  955. result:=UpperWideStringProc(s);
  956. end;
  957. function TUnicodeStringManager.LowerAnsiStringProc(const s : ansistring) : ansistring;
  958. begin
  959. result:=LowerWideStringProc(s);
  960. end;
  961. function TUnicodeStringManager.CompareStrAnsiStringProc(const S1, S2: ansistring): PtrInt;
  962. begin
  963. result:=CompareUnicodeStringProc(S1,S2);
  964. end;
  965. function TUnicodeStringManager.CompareTextAnsiStringProc(const S1, S2: ansistring): PtrInt;
  966. begin
  967. result:=CompareTextUnicodeStringProc(S1,S2);
  968. end;
  969. function TUnicodeStringManager.StrCompAnsiStringProc(S1, S2: PChar): PtrInt;
  970. var
  971. str1,str2: unicodestring;
  972. begin
  973. str1:=JLString.Create(TJCharArray(S1),0,length(S1));
  974. str2:=JLString.Create(TJCharArray(S2),0,length(S2));
  975. result:=CompareUnicodeStringProc(str1,str2);
  976. end;
  977. function TUnicodeStringManager.StrICompAnsiStringProc(S1, S2: PChar): PtrInt;
  978. var
  979. str1,str2: unicodestring;
  980. begin
  981. str1:=JLString.Create(TJCharArray(S1),0,length(S1));
  982. str2:=JLString.Create(TJCharArray(S2),0,length(S2));
  983. result:=CompareTextUnicodeStringProc(str1,str2);
  984. end;
  985. function TUnicodeStringManager.StrLCompAnsiStringProc(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  986. var
  987. str1,str2: unicodestring;
  988. begin
  989. str1:=JLString.Create(TJCharArray(S1),0,min(length(S1),MaxLen));
  990. str2:=JLString.Create(TJCharArray(S2),0,min(length(S2),MaxLen));
  991. result:=CompareUnicodeStringProc(str1,str2);
  992. end;
  993. function TUnicodeStringManager.StrLICompAnsiStringProc(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  994. var
  995. str1,str2: unicodestring;
  996. begin
  997. str1:=JLString.Create(TJCharArray(S1),0,min(length(S1),MaxLen));
  998. str2:=JLString.Create(TJCharArray(S2),0,min(length(S2),MaxLen));
  999. result:=CompareTextUnicodeStringProc(str1,str2);
  1000. end;
  1001. function TUnicodeStringManager.StrLowerAnsiStringProc(Str: PChar): PChar;
  1002. var
  1003. ustr: unicodestring;
  1004. begin
  1005. ustr:=JLString.Create(TJCharArray(Str),0,length(Str));
  1006. result:=PChar(AnsiStringClass(ansistring(LowerWideStringProc(ustr))).fdata);
  1007. end;
  1008. function TUnicodeStringManager.StrUpperAnsiStringProc(Str: PChar): PChar;
  1009. var
  1010. ustr: unicodestring;
  1011. begin
  1012. ustr:=JLString.Create(TJCharArray(Str),0,length(Str));
  1013. result:=PChar(AnsiStringClass(ansistring(UpperWideStringProc(ustr))).fdata);
  1014. end;
  1015. procedure TUnicodeStringManager.Unicode2AnsiMoveProc(source:punicodechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  1016. begin
  1017. DefaultUnicode2AnsiMove(source,dest,cp,len);
  1018. end;
  1019. procedure TUnicodeStringManager.Ansi2UnicodeMoveProc(source:pchar;cp : TSystemCodePage;var dest:unicodestring;len:SizeInt);
  1020. begin
  1021. DefaultAnsi2UnicodeMove(source,cp,dest,len);
  1022. end;
  1023. function TUnicodeStringManager.UpperUnicodeStringProc(const S: UnicodeString): UnicodeString;
  1024. begin
  1025. result:=UpperWideStringProc(S);
  1026. end;
  1027. function TUnicodeStringManager.LowerUnicodeStringProc(const S: UnicodeString): UnicodeString;
  1028. begin
  1029. result:=LowerWideStringProc(S);
  1030. end;
  1031. function TUnicodeStringManager.CompareUnicodeStringProc(const s1, s2 : UnicodeString) : PtrInt;
  1032. begin
  1033. result:=CompareWideStringProc(s1,s2);
  1034. end;
  1035. function TUnicodeStringManager.CompareTextUnicodeStringProc(const s1, s2 : UnicodeString): PtrInt;
  1036. begin
  1037. result:=CompareTextWideStringProc(s1,s2);
  1038. end;
  1039. procedure initunicodestringmanager;
  1040. begin
  1041. widestringmanager:=TUnicodeStringManager.Create;
  1042. end;