justrings.inc 30 KB

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