justrings.inc 33 KB

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