justrings.inc 33 KB

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