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. 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. Len: SizeInt;
  518. begin
  519. temp:=src;
  520. Len:=Length(temp);
  521. if DestSize<=Len then
  522. Len:=Destsize-1;
  523. JLString(temp).getChars(0,Len,TJCharArray(Dest),0);
  524. Dest[Len]:=#0;
  525. result:=Dest;
  526. end;
  527. {$define FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  528. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : UnicodeString;
  529. begin
  530. result:=JLString.Create(TJCharArray(S),0,len);
  531. end;
  532. {$define FPC_HAS_WIDECHAR_LEN_TO_STRING}
  533. function WideCharLenToString(S : PWideChar;Len : SizeInt) : UnicodeString;
  534. begin
  535. result:=JLString.Create(TJCharArray(S),0,len);
  536. end;
  537. {$define FPC_HAS_UNICODESTR_UNIQUE}
  538. Function fpc_unicodestr_Unique(var S : JLObject): JLObject; compilerproc;
  539. begin
  540. result:=s;
  541. end;
  542. { the publicly accessible uniquestring function is declared as
  543. "external name 'FPC_UNICODESTR_UNIQUE'", which is normally an alias for
  544. the fpc_unicodestr_Unique compiler proc; since one is a function and the
  545. other a procedure that sort of hackery doesn't work for the JVM -> create
  546. a separate procedure for that (since Java strings are immutable, they are
  547. always unique though) }
  548. procedure FPC_UNICODESTR_UNIQUE(var S : UnicodeString);
  549. begin
  550. { do nothing }
  551. end;
  552. {$define FPC_HAS_UNICODESTR_COPY}
  553. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  554. begin
  555. dec(index);
  556. if Index < 0 then
  557. Index := 0;
  558. { Check Size. Accounts for Zero-length S, the double check is needed because
  559. Size can be maxint and will get <0 when adding index }
  560. if (Size>Length(S)) or
  561. (Index+Size>Length(S)) then
  562. Size:=Length(S)-Index;
  563. If Size>0 then
  564. result:=JLString(s).subString(Index,Index+Size)
  565. else
  566. result:='';
  567. end;
  568. {$define FPC_HAS_POS_UNICODESTR_UNICODESTR}
  569. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString) : SizeInt;
  570. begin
  571. Pos:=0;
  572. if Length(SubStr)>0 then
  573. Pos:=JLString(Source).indexOf(SubStr)+1;
  574. end;
  575. { Faster version for a unicodechar alone }
  576. {$define FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  577. Function Pos (c : UnicodeChar; Const s : UnicodeString) : SizeInt;
  578. begin
  579. Pos:=0;
  580. if length(S)>0 then
  581. Pos:=JLString(s).indexOf(ord(c))+1;
  582. end;
  583. { Faster version for a char alone. Must be implemented because }
  584. { pos(c: char; const s: shortstring) also exists, so otherwise }
  585. { using pos(char,pchar) will always call the shortstring version }
  586. { (exact match for first argument), also with $h+ (JM) }
  587. {$define FPC_HAS_POS_CHAR_UNICODESTR}
  588. Function Pos (c : AnsiChar; Const s : UnicodeString) : SizeInt;
  589. var
  590. i: SizeInt;
  591. wc : unicodechar;
  592. begin
  593. wc:=c;
  594. result:=Pos(wc,s);
  595. end;
  596. {$define FPC_HAS_DELETE_UNICODESTR}
  597. Procedure Delete (Var S : UnicodeString; Index,Size: SizeInt);
  598. Var
  599. LS : SizeInt;
  600. sb: JLStringBuilder;
  601. begin
  602. LS:=Length(S);
  603. if (Index>LS) or (Index<=0) or (Size<=0) then
  604. exit;
  605. { (Size+Index) will overflow if Size=MaxInt. }
  606. if Size>LS-Index then
  607. Size:=LS-Index+1;
  608. if Size<=LS-Index then
  609. begin
  610. Dec(Index);
  611. sb:=JLStringBuilder.Create(s);
  612. sb.delete(index,size);
  613. s:=sb.toString;
  614. end
  615. else
  616. s:=JLString(s).substring(0,index-1);
  617. end;
  618. {$define FPC_HAS_INSERT_UNICODESTR}
  619. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  620. var
  621. Temp : UnicodeString;
  622. LS : SizeInt;
  623. sb : JLStringBuilder;
  624. begin
  625. If Length(Source)=0 then
  626. exit;
  627. if index <= 0 then
  628. index := 1;
  629. Ls:=Length(S);
  630. if index > LS then
  631. index := LS+1;
  632. Dec(Index);
  633. sb:=JLStringBuilder.Create(S);
  634. sb.insert(Index,Source);
  635. S:=sb.toString;
  636. end;
  637. {$define FPC_HAS_UPCASE_UNICODECHAR}
  638. Function UpCase(c:UnicodeChar):UnicodeChar;
  639. begin
  640. result:=JLCharacter.toUpperCase(c);
  641. end;
  642. {$define FPC_HAS_UPCASE_UNICODESTR}
  643. function UpCase(const s : UnicodeString) : UnicodeString;
  644. begin
  645. result:=JLString(s).toUpperCase;
  646. end;
  647. {$define FPC_HAS_LOWERCASE_UNICODECHAR}
  648. Function LowerCase(c:UnicodeChar):UnicodeChar;
  649. begin
  650. result:=JLCharacter.toLowerCase(c);
  651. end;
  652. {$define FPC_HAS_LOWERCASE_UNICODESTR}
  653. function LowerCase(const s : UnicodeString) : UnicodeString;
  654. begin
  655. result:=JLString(s).toLowerCase;
  656. end;
  657. {$define FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  658. Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);
  659. begin
  660. if assigned(buf) and (Len>0) then
  661. s:=JLString.Create(TJCharArray(Buf),0,Len)
  662. else
  663. s:='';
  664. end;
  665. {$define FPC_HAS_UTF8ENCODE_UNICODESTRING}
  666. function UTF8Encode(const s : UnicodeString) : RawByteString;
  667. var
  668. i : SizeInt;
  669. hs : UTF8String;
  670. chars: array of widechar;
  671. begin
  672. result:='';
  673. if s='' then
  674. exit;
  675. SetLength(hs,length(s)*3);
  676. chars:=JLString(s).toCharArray;
  677. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,pwidechar(chars),length(s));
  678. if i>0 then
  679. begin
  680. SetLength(hs,i-1);
  681. result:=hs;
  682. end;
  683. end;
  684. {$define FPC_HAS_UTF8DECODE_UNICODESTRING}
  685. function UTF8Decode(const s : RawByteString): UnicodeString;
  686. var
  687. i : SizeInt;
  688. hs : UnicodeString;
  689. chars: array of widechar;
  690. begin
  691. result:='';
  692. if s='' then
  693. exit;
  694. SetLength(chars,length(s));
  695. i:=Utf8ToUnicode(pwidechar(chars),length(hs)+1,pchar(s),length(s));
  696. if i>0 then
  697. begin
  698. result:=JLString.Create(chars,0,i);
  699. end;
  700. end;
  701. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  702. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  703. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: JLStringBuilder; var index: SizeInt);
  704. begin
  705. { if nc > $ffff, we need two places }
  706. if (index+ord(nc > $ffff)>s.length) then
  707. if (s.length < 10*256) then
  708. s.setLength(s.length+10)
  709. else
  710. s.setlength(s.length+s.length shr 8);
  711. if (nc<$ffff) then
  712. begin
  713. s.setCharAt(index-1,unicodechar(nc));
  714. inc(index);
  715. end
  716. else if (dword(nc)<=$10ffff) then
  717. begin
  718. s.setCharAt(index-1,unicodechar((nc - $10000) shr 10 + $d800));
  719. s.setCharAt(index,unicodechar((nc - $10000) and $3ff + $dc00));
  720. inc(index,2);
  721. end
  722. else
  723. { invalid code point }
  724. begin
  725. s.setCharAt(index-1,'?');
  726. inc(index);
  727. end;
  728. end;
  729. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  730. var
  731. i : SizeInt;
  732. resindex : SizeInt;
  733. tmpres: JLStringBuilder;
  734. begin
  735. { skip terminating #0 }
  736. tmpres:=JLStringBuilder.Create(length(s)-1);
  737. resindex:=1;
  738. for i:=0 to high(s)-1 do
  739. ConcatUTF32ToUnicodeStr(s[i],tmpres,resindex);
  740. { adjust result length (may be too big due to growing }
  741. { for surrogate pairs) }
  742. tmpres.setLength(resindex-1);
  743. result:=tmpres.toString;
  744. end;
  745. procedure UCS4Encode(p: PWideChar; len: sizeint; out res: UCS4String); forward;
  746. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  747. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  748. begin
  749. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  750. end;
  751. {$define FPC_HAS_WIDESTR_TO_UCS4STRING}
  752. function WideStringToUCS4String(const s : WideString) : UCS4String;
  753. begin
  754. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  755. end;
  756. {$define FPC_HAS_UCS4STRING_TO_WIDESTR}
  757. function UCS4StringToWideString(const s : UCS4String) : WideString;
  758. begin
  759. result:=UCS4StringToUnicodeString(s);
  760. end;
  761. function StringElementSize(const S : UnicodeString): Word;
  762. begin
  763. result:=sizeof(unicodechar);
  764. end;
  765. function StringRefCount(const S : UnicodeString): SizeInt;
  766. begin
  767. if assigned(pointer(s)) then
  768. result:=1
  769. else
  770. result:=0;
  771. end;
  772. function StringCodePage(const S : UnicodeString): TSystemCodePage;
  773. begin
  774. if assigned(pointer(s)) then
  775. result:=CP_UTF16BE
  776. else
  777. result:=DefaultUnicodeCodePage;
  778. end;
  779. { helpers for converting between Windows and Java code page identifiers }
  780. {$i jwin2javacharset.inc}
  781. { *************************************************************************** }
  782. { ************************* Collator threadvar ****************************** }
  783. { *************************************************************************** }
  784. function TCollatorThreadVar.InitialValue: JLObject;
  785. begin
  786. { get a copy, since we modify the collator (e.g. setting the strength) }
  787. result:=JTCollator.getInstance.clone
  788. end;
  789. { *************************************************************************** }
  790. { ************************ Helpers for en/decode **************************** }
  791. { *************************************************************************** }
  792. function GetOrInsertNewEnDecoder(hm: JUWeakHashMap; cp: TSystemCodePage; decoder: boolean): JLObject;
  793. var
  794. cs: JNCCharSet;
  795. replacement: array[0..0] of jbyte;
  796. begin
  797. result:=hm.get(JLInteger.valueOf(cp));
  798. if not assigned(result) then
  799. begin
  800. try
  801. cs:=JNCCharSet.forName(win2javacs(cp));
  802. except
  803. { does not exist or not supported, fall back to ASCII like on other
  804. platforms}
  805. cs:=JNCCharset.forName('US-ASCII')
  806. end;
  807. if decoder then
  808. begin
  809. result:=cs.newDecoder;
  810. JNCCharsetDecoder(result).replaceWith('?');
  811. end
  812. else
  813. begin
  814. result:=cs.newEncoder;
  815. replacement[0]:=ord('?');
  816. JNCCharsetEncoder(result).replaceWith(replacement);
  817. end;
  818. { store in weak hashmap for future (possible) reuse }
  819. hm.put(JLInteger.Create(cp),result);
  820. end;
  821. end;
  822. { *************************************************************************** }
  823. { ************************** Decoder threadvar ****************************** }
  824. { *************************************************************************** }
  825. function TCharsetDecoderThreadvar.InitialValue: JLObject;
  826. begin
  827. result:=JUWeakHashMap.Create;
  828. end;
  829. function TCharsetDecoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetDecoder;
  830. var
  831. hm: JUWeakHashMap;
  832. begin
  833. hm:=JUWeakHashMap(get);
  834. result:=JNCCharsetDecoder(GetOrInsertNewEnDecoder(hm,cp,true));
  835. end;
  836. { *************************************************************************** }
  837. { ************************** Encoder threadvar ****************************** }
  838. { *************************************************************************** }
  839. function TCharsetEncoderThreadvar.InitialValue: JLObject;
  840. begin
  841. result:=JUWeakHashMap.Create;
  842. end;
  843. function TCharsetEncoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetEncoder;
  844. var
  845. hm: JUWeakHashMap;
  846. begin
  847. hm:=JUWeakHashMap(get);
  848. result:=JNCCharsetEncoder(GetOrInsertNewEnDecoder(hm,cp,false));
  849. end;
  850. { *************************************************************************** }
  851. { ************************ TUnicodeStringManager **************************** }
  852. { *************************************************************************** }
  853. class constructor TUnicodeStringManager.ClassCreate;
  854. begin
  855. collator:=TCollatorThreadVar.Create;
  856. decoder:=TCharsetDecoderThreadVar.Create;
  857. encoder:=TCharsetEncoderThreadVar.Create;
  858. DefaultSystemCodePage:=javacs2win(JNCCharset.defaultCharset.name);
  859. { unknown/unsupported -> default to ASCII (this will be used to parse
  860. stdin etc, so setting this to utf-8 or so won't help) }
  861. if DefaultSystemCodePage=65535 then
  862. DefaultSystemCodePage:=20127;
  863. DefaultFileSystemCodePage:=DefaultSystemCodePage;
  864. DefaultRTLFileSystemCodePage:=DefaultFileSystemCodePage;
  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;