2
0

justrings.inc 35 KB

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