justrings.inc 34 KB

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