2
0

justrings.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. begin
  520. temp:=src;
  521. if Length(temp)<DestSize then
  522. JLString(temp).getChars(0,length(temp),TJCharArray(Dest),0)
  523. else
  524. JLString(temp).getChars(0,DestSize-1,TJCharArray(Dest),0);
  525. Dest[DestSize-1]:=#0;
  526. result:=Dest;
  527. end;
  528. {$define FPC_HAS_UNICODECHAR_LEN_TO_STRING}
  529. function UnicodeCharLenToString(S : PUnicodeChar;Len : SizeInt) : UnicodeString;
  530. begin
  531. result:=JLString.Create(TJCharArray(S),0,len);
  532. end;
  533. {$define FPC_HAS_WIDECHAR_LEN_TO_STRING}
  534. function WideCharLenToString(S : PWideChar;Len : SizeInt) : UnicodeString;
  535. begin
  536. result:=JLString.Create(TJCharArray(S),0,len);
  537. end;
  538. {$define FPC_HAS_UNICODESTR_UNIQUE}
  539. Function fpc_unicodestr_Unique(var S : JLObject): JLObject; compilerproc;
  540. begin
  541. result:=s;
  542. end;
  543. { the publicly accessible uniquestring function is declared as
  544. "external name 'FPC_UNICODESTR_UNIQUE'", which is normally an alias for
  545. the fpc_unicodestr_Unique compiler proc; since one is a function and the
  546. other a procedure that sort of hackery doesn't work for the JVM -> create
  547. a separate procedure for that (since Java strings are immutable, they are
  548. always unique though) }
  549. procedure FPC_UNICODESTR_UNIQUE(var S : UnicodeString);
  550. begin
  551. { do nothing }
  552. end;
  553. {$define FPC_HAS_UNICODESTR_COPY}
  554. Function Fpc_UnicodeStr_Copy (Const S : UnicodeString; Index,Size : SizeInt) : UnicodeString;compilerproc;
  555. begin
  556. dec(index);
  557. if Index < 0 then
  558. Index := 0;
  559. { Check Size. Accounts for Zero-length S, the double check is needed because
  560. Size can be maxint and will get <0 when adding index }
  561. if (Size>Length(S)) or
  562. (Index+Size>Length(S)) then
  563. Size:=Length(S)-Index;
  564. If Size>0 then
  565. result:=JLString(s).subString(Index,Index+Size)
  566. else
  567. result:='';
  568. end;
  569. {$define FPC_HAS_POS_UNICODESTR_UNICODESTR}
  570. Function Pos (Const Substr : UnicodeString; Const Source : UnicodeString) : SizeInt;
  571. begin
  572. Pos:=0;
  573. if Length(SubStr)>0 then
  574. Pos:=JLString(Source).indexOf(SubStr)+1;
  575. end;
  576. { Faster version for a unicodechar alone }
  577. {$define FPC_HAS_POS_UNICODECHAR_UNICODESTR}
  578. Function Pos (c : UnicodeChar; Const s : UnicodeString) : SizeInt;
  579. begin
  580. Pos:=0;
  581. if length(S)>0 then
  582. Pos:=JLString(s).indexOf(ord(c))+1;
  583. end;
  584. { Faster version for a char alone. Must be implemented because }
  585. { pos(c: char; const s: shortstring) also exists, so otherwise }
  586. { using pos(char,pchar) will always call the shortstring version }
  587. { (exact match for first argument), also with $h+ (JM) }
  588. {$define FPC_HAS_POS_CHAR_UNICODESTR}
  589. Function Pos (c : AnsiChar; Const s : UnicodeString) : SizeInt;
  590. var
  591. i: SizeInt;
  592. wc : unicodechar;
  593. begin
  594. wc:=c;
  595. result:=Pos(wc,s);
  596. end;
  597. {$define FPC_HAS_DELETE_UNICODESTR}
  598. Procedure Delete (Var S : UnicodeString; Index,Size: SizeInt);
  599. Var
  600. LS : SizeInt;
  601. sb: JLStringBuilder;
  602. begin
  603. LS:=Length(S);
  604. if (Index>LS) or (Index<=0) or (Size<=0) then
  605. exit;
  606. { (Size+Index) will overflow if Size=MaxInt. }
  607. if Size>LS-Index then
  608. Size:=LS-Index+1;
  609. if Size<=LS-Index then
  610. begin
  611. Dec(Index);
  612. sb:=JLStringBuilder.Create(s);
  613. sb.delete(index,size);
  614. s:=sb.toString;
  615. end
  616. else
  617. s:=JLString(s).substring(0,index-1);
  618. end;
  619. {$define FPC_HAS_INSERT_UNICODESTR}
  620. Procedure Insert (Const Source : UnicodeString; Var S : UnicodeString; Index : SizeInt);
  621. var
  622. Temp : UnicodeString;
  623. LS : SizeInt;
  624. sb : JLStringBuilder;
  625. begin
  626. If Length(Source)=0 then
  627. exit;
  628. if index <= 0 then
  629. index := 1;
  630. Ls:=Length(S);
  631. if index > LS then
  632. index := LS+1;
  633. Dec(Index);
  634. sb:=JLStringBuilder.Create(S);
  635. sb.insert(Index,Source);
  636. S:=sb.toString;
  637. end;
  638. {$define FPC_HAS_UPCASE_UNICODECHAR}
  639. Function UpCase(c:UnicodeChar):UnicodeChar;
  640. begin
  641. result:=JLCharacter.toUpperCase(c);
  642. end;
  643. {$define FPC_HAS_UPCASE_UNICODESTR}
  644. function UpCase(const s : UnicodeString) : UnicodeString;
  645. begin
  646. result:=JLString(s).toUpperCase;
  647. end;
  648. {$define FPC_HAS_LOWERCASE_UNICODECHAR}
  649. Function LowerCase(c:UnicodeChar):UnicodeChar;
  650. begin
  651. result:=JLCharacter.toLowerCase(c);
  652. end;
  653. {$define FPC_HAS_LOWERCASE_UNICODESTR}
  654. function LowerCase(const s : UnicodeString) : UnicodeString;
  655. begin
  656. result:=JLString(s).toLowerCase;
  657. end;
  658. {$define FPC_HAS_SETSTRING_UNICODESTR_PUNICODECHAR}
  659. Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);
  660. begin
  661. if assigned(buf) and (Len>0) then
  662. s:=JLString.Create(TJCharArray(Buf),0,Len)
  663. else
  664. s:='';
  665. end;
  666. {$define FPC_HAS_UTF8ENCODE_UNICODESTRING}
  667. function UTF8Encode(const s : UnicodeString) : RawByteString;
  668. var
  669. i : SizeInt;
  670. hs : UTF8String;
  671. chars: array of widechar;
  672. begin
  673. result:='';
  674. if s='' then
  675. exit;
  676. SetLength(hs,length(s)*3);
  677. chars:=JLString(s).toCharArray;
  678. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,pwidechar(chars),length(s));
  679. if i>0 then
  680. begin
  681. SetLength(hs,i-1);
  682. result:=hs;
  683. end;
  684. end;
  685. {$define FPC_HAS_UTF8DECODE_UNICODESTRING}
  686. function UTF8Decode(const s : RawByteString): UnicodeString;
  687. var
  688. i : SizeInt;
  689. hs : UnicodeString;
  690. chars: array of widechar;
  691. begin
  692. result:='';
  693. if s='' then
  694. exit;
  695. SetLength(chars,length(s));
  696. i:=Utf8ToUnicode(pwidechar(chars),length(hs)+1,pchar(s),length(s));
  697. if i>0 then
  698. begin
  699. result:=JLString.Create(chars,0,i);
  700. end;
  701. end;
  702. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  703. { concatenates an utf-32 char to a unicodestring. S *must* be unique when entering. }
  704. procedure ConcatUTF32ToUnicodeStr(const nc: UCS4Char; var S: JLStringBuilder; var index: SizeInt);
  705. begin
  706. { if nc > $ffff, we need two places }
  707. if (index+ord(nc > $ffff)>s.length) then
  708. if (s.length < 10*256) then
  709. s.setLength(s.length+10)
  710. else
  711. s.setlength(s.length+s.length shr 8);
  712. if (nc<$ffff) then
  713. begin
  714. s.setCharAt(index-1,unicodechar(nc));
  715. inc(index);
  716. end
  717. else if (dword(nc)<=$10ffff) then
  718. begin
  719. s.setCharAt(index-1,unicodechar((nc - $10000) shr 10 + $d800));
  720. s.setCharAt(index,unicodechar((nc - $10000) and $3ff + $dc00));
  721. inc(index,2);
  722. end
  723. else
  724. { invalid code point }
  725. begin
  726. s.setCharAt(index-1,'?');
  727. inc(index);
  728. end;
  729. end;
  730. function UCS4StringToUnicodeString(const s : UCS4String) : UnicodeString;
  731. var
  732. i : SizeInt;
  733. resindex : SizeInt;
  734. tmpres: JLStringBuilder;
  735. begin
  736. { skip terminating #0 }
  737. tmpres:=JLStringBuilder.Create(length(s)-1);
  738. resindex:=1;
  739. for i:=0 to high(s)-1 do
  740. ConcatUTF32ToUnicodeStr(s[i],tmpres,resindex);
  741. { adjust result length (may be too big due to growing }
  742. { for surrogate pairs) }
  743. tmpres.setLength(resindex-1);
  744. result:=tmpres.toString;
  745. end;
  746. procedure UCS4Encode(p: PWideChar; len: sizeint; out res: UCS4String); forward;
  747. {$define FPC_HAS_UCS4STRING_TO_UNICODESTR}
  748. function UnicodeStringToUCS4String(const s : UnicodeString) : UCS4String;
  749. begin
  750. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  751. end;
  752. {$define FPC_HAS_WIDESTR_TO_UCS4STRING}
  753. function WideStringToUCS4String(const s : WideString) : UCS4String;
  754. begin
  755. UCS4Encode(PWideChar(JLString(s).toCharArray),Length(s),result);
  756. end;
  757. {$define FPC_HAS_UCS4STRING_TO_WIDESTR}
  758. function UCS4StringToWideString(const s : UCS4String) : WideString;
  759. begin
  760. result:=UCS4StringToUnicodeString(s);
  761. end;
  762. function StringElementSize(const S : UnicodeString): Word;
  763. begin
  764. result:=sizeof(unicodechar);
  765. end;
  766. function StringRefCount(const S : UnicodeString): SizeInt;
  767. begin
  768. if assigned(pointer(s)) then
  769. result:=1
  770. else
  771. result:=0;
  772. end;
  773. function StringCodePage(const S : UnicodeString): TSystemCodePage;
  774. begin
  775. if assigned(pointer(s)) then
  776. result:=CP_UTF16BE
  777. else
  778. result:=DefaultUnicodeCodePage;
  779. end;
  780. { helpers for converting between Windows and Java code page identifiers }
  781. {$i jwin2javacharset.inc}
  782. { *************************************************************************** }
  783. { ************************* Collator threadvar ****************************** }
  784. { *************************************************************************** }
  785. function TCollatorThreadVar.InitialValue: JLObject;
  786. begin
  787. { get a copy, since we modify the collator (e.g. setting the strength) }
  788. result:=JTCollator.getInstance.clone
  789. end;
  790. { *************************************************************************** }
  791. { ************************ Helpers for en/decode **************************** }
  792. { *************************************************************************** }
  793. function GetOrInsertNewEnDecoder(hm: JUWeakHashMap; cp: TSystemCodePage; decoder: boolean): JLObject;
  794. var
  795. cs: JNCCharSet;
  796. replacement: array[0..0] of jbyte;
  797. begin
  798. result:=hm.get(JLInteger.valueOf(cp));
  799. if not assigned(result) then
  800. begin
  801. try
  802. cs:=JNCCharSet.forName(win2javacs(cp));
  803. except
  804. { does not exist or not supported, fall back to ASCII like on other
  805. platforms}
  806. cs:=JNCCharset.forName('US-ASCII')
  807. end;
  808. if decoder then
  809. begin
  810. result:=cs.newDecoder;
  811. JNCCharsetDecoder(result).replaceWith('?');
  812. end
  813. else
  814. begin
  815. result:=cs.newEncoder;
  816. replacement[0]:=ord('?');
  817. JNCCharsetEncoder(result).replaceWith(replacement);
  818. end;
  819. { store in weak hashmap for future (possible) reuse }
  820. hm.put(JLInteger.Create(cp),result);
  821. end;
  822. end;
  823. { *************************************************************************** }
  824. { ************************** Decoder threadvar ****************************** }
  825. { *************************************************************************** }
  826. function TCharsetDecoderThreadvar.InitialValue: JLObject;
  827. begin
  828. result:=JUWeakHashMap.Create;
  829. end;
  830. function TCharsetDecoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetDecoder;
  831. var
  832. hm: JUWeakHashMap;
  833. begin
  834. hm:=JUWeakHashMap(get);
  835. result:=JNCCharsetDecoder(GetOrInsertNewEnDecoder(hm,cp,true));
  836. end;
  837. { *************************************************************************** }
  838. { ************************** Encoder threadvar ****************************** }
  839. { *************************************************************************** }
  840. function TCharsetEncoderThreadvar.InitialValue: JLObject;
  841. begin
  842. result:=JUWeakHashMap.Create;
  843. end;
  844. function TCharsetEncoderThreadvar.getForCodePage(cp: TSystemCodePage): JNCCharsetEncoder;
  845. var
  846. hm: JUWeakHashMap;
  847. begin
  848. hm:=JUWeakHashMap(get);
  849. result:=JNCCharsetEncoder(GetOrInsertNewEnDecoder(hm,cp,false));
  850. end;
  851. { *************************************************************************** }
  852. { ************************ TUnicodeStringManager **************************** }
  853. { *************************************************************************** }
  854. class constructor TUnicodeStringManager.ClassCreate;
  855. begin
  856. collator:=TCollatorThreadVar.Create;
  857. decoder:=TCharsetDecoderThreadVar.Create;
  858. encoder:=TCharsetEncoderThreadVar.Create;
  859. DefaultSystemCodePage:=javacs2win(JNCCharset.defaultCharset.name);
  860. { unknown/unsupported -> default to ASCII (this will be used to parse
  861. stdin etc, so setting this to utf-8 or so won't help) }
  862. if DefaultSystemCodePage=65535 then
  863. DefaultSystemCodePage:=20127;
  864. DefaultUnicodeCodePage:=CP_UTF16BE;
  865. end;
  866. procedure TUnicodeStringManager.Wide2AnsiMoveProc(source:pwidechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  867. begin
  868. DefaultUnicode2AnsiMove(source,dest,cp,len);
  869. end;
  870. procedure TUnicodeStringManager.Ansi2WideMoveProc(source:pchar;cp : TSystemCodePage;var dest:widestring;len:SizeInt);
  871. begin
  872. DefaultAnsi2UnicodeMove(source,cp,dest,len);
  873. end;
  874. function TUnicodeStringManager.UpperWideStringProc(const S: WideString): WideString;
  875. begin
  876. result:=upcase(s);
  877. end;
  878. function TUnicodeStringManager.LowerWideStringProc(const S: WideString): WideString;
  879. begin
  880. result:=lowercase(s);
  881. end;
  882. function TUnicodeStringManager.CompareWideStringProc(const s1, s2 : WideString) : PtrInt;
  883. var
  884. localcollator: JTCollator;
  885. begin
  886. localcollator:=JTCollator(collator.get);
  887. localcollator.setStrength(JTCollator.IDENTICAL);
  888. result:=localcollator.compare(s1,s2);
  889. end;
  890. function TUnicodeStringManager.CompareTextWideStringProc(const s1, s2 : WideString): PtrInt;
  891. var
  892. localcollator: JTCollator;
  893. begin
  894. localcollator:=JTCollator(collator.get);
  895. localcollator.setStrength(JTCollator.TERTIARY);
  896. result:=localcollator.compare(s1,s2);
  897. end;
  898. function TUnicodeStringManager.CharLengthPCharProc(const Str: PChar; Index: PtrInt): PtrInt;
  899. var
  900. localdecoder: JNCCharsetDecoder;
  901. begin
  902. localdecoder:=JNCCharsetDecoder(decoder.get);
  903. localdecoder.reset;
  904. localdecoder.onMalformedInput(JNCCodingErrorAction.fREPLACE);
  905. localdecoder.onUnmappableCharacter(JNCCodingErrorAction.fREPLACE);
  906. result:=localdecoder.decode(JNByteBuffer.wrap(TJByteArray(Str),Index,length(Str)-Index)).length;
  907. end;
  908. function TUnicodeStringManager.CodePointLengthProc(const Str: PChar; Index, MaxLookAhead: PtrInt): Ptrint;
  909. var
  910. localdecoder: JNCCharsetDecoder;
  911. inbuf: JNByteBuffer;
  912. outbuf: JNCharBuffer;
  913. coderres: JNCCoderResult;
  914. limit, maxlimit: longint;
  915. begin
  916. localdecoder:=JNCCharsetDecoder(decoder.get);
  917. localdecoder.reset;
  918. localdecoder.onMalformedInput(JNCCodingErrorAction.fREPORT);
  919. localdecoder.onUnmappableCharacter(JNCCodingErrorAction.fREPORT);
  920. localdecoder.reset;
  921. limit:=0;
  922. maxlimit:=min(length(Str)-Index,MaxLookAhead);
  923. { end of pchar? }
  924. if maxlimit=0 then
  925. begin
  926. result:=0;
  927. exit;
  928. end;
  929. inbuf:=JNByteBuffer.wrap(TJByteArray(Str),Index,Index+maxlimit);
  930. { we will get at most 2 output characters (when decoding from UTF-32 to
  931. UTF-16) }
  932. outbuf:=JNCharBuffer.allocate(2);
  933. { keep trying to decode until we managed to decode one character or
  934. reached the limit }
  935. repeat
  936. inc(limit);
  937. inbuf.limit(limit);
  938. coderres:=localdecoder.decode(inbuf,outbuf,true);
  939. until not coderres.isError or
  940. (limit=MaxLookAhead);
  941. if not coderres.isError then
  942. result:=inbuf.limit
  943. else
  944. result:=-1;
  945. end;
  946. function TUnicodeStringManager.UpperAnsiStringProc(const s : ansistring) : ansistring;
  947. begin
  948. result:=UpperWideStringProc(s);
  949. end;
  950. function TUnicodeStringManager.LowerAnsiStringProc(const s : ansistring) : ansistring;
  951. begin
  952. result:=LowerWideStringProc(s);
  953. end;
  954. function TUnicodeStringManager.CompareStrAnsiStringProc(const S1, S2: ansistring): PtrInt;
  955. begin
  956. result:=CompareUnicodeStringProc(S1,S2);
  957. end;
  958. function TUnicodeStringManager.CompareTextAnsiStringProc(const S1, S2: ansistring): PtrInt;
  959. begin
  960. result:=CompareTextUnicodeStringProc(S1,S2);
  961. end;
  962. function TUnicodeStringManager.StrCompAnsiStringProc(S1, S2: PChar): PtrInt;
  963. var
  964. str1,str2: unicodestring;
  965. begin
  966. str1:=JLString.Create(TJCharArray(S1),0,length(S1));
  967. str2:=JLString.Create(TJCharArray(S2),0,length(S2));
  968. result:=CompareUnicodeStringProc(str1,str2);
  969. end;
  970. function TUnicodeStringManager.StrICompAnsiStringProc(S1, S2: PChar): PtrInt;
  971. var
  972. str1,str2: unicodestring;
  973. begin
  974. str1:=JLString.Create(TJCharArray(S1),0,length(S1));
  975. str2:=JLString.Create(TJCharArray(S2),0,length(S2));
  976. result:=CompareTextUnicodeStringProc(str1,str2);
  977. end;
  978. function TUnicodeStringManager.StrLCompAnsiStringProc(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  979. var
  980. str1,str2: unicodestring;
  981. begin
  982. str1:=JLString.Create(TJCharArray(S1),0,min(length(S1),MaxLen));
  983. str2:=JLString.Create(TJCharArray(S2),0,min(length(S2),MaxLen));
  984. result:=CompareUnicodeStringProc(str1,str2);
  985. end;
  986. function TUnicodeStringManager.StrLICompAnsiStringProc(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  987. var
  988. str1,str2: unicodestring;
  989. begin
  990. str1:=JLString.Create(TJCharArray(S1),0,min(length(S1),MaxLen));
  991. str2:=JLString.Create(TJCharArray(S2),0,min(length(S2),MaxLen));
  992. result:=CompareTextUnicodeStringProc(str1,str2);
  993. end;
  994. function TUnicodeStringManager.StrLowerAnsiStringProc(Str: PChar): PChar;
  995. var
  996. ustr: unicodestring;
  997. begin
  998. ustr:=JLString.Create(TJCharArray(Str),0,length(Str));
  999. result:=PChar(AnsiStringClass(ansistring(LowerWideStringProc(ustr))).fdata);
  1000. end;
  1001. function TUnicodeStringManager.StrUpperAnsiStringProc(Str: PChar): PChar;
  1002. var
  1003. ustr: unicodestring;
  1004. begin
  1005. ustr:=JLString.Create(TJCharArray(Str),0,length(Str));
  1006. result:=PChar(AnsiStringClass(ansistring(UpperWideStringProc(ustr))).fdata);
  1007. end;
  1008. procedure TUnicodeStringManager.Unicode2AnsiMoveProc(source:punicodechar;var dest:RawByteString;cp : TSystemCodePage;len:SizeInt);
  1009. begin
  1010. DefaultUnicode2AnsiMove(source,dest,cp,len);
  1011. end;
  1012. procedure TUnicodeStringManager.Ansi2UnicodeMoveProc(source:pchar;cp : TSystemCodePage;var dest:unicodestring;len:SizeInt);
  1013. begin
  1014. DefaultAnsi2UnicodeMove(source,cp,dest,len);
  1015. end;
  1016. function TUnicodeStringManager.UpperUnicodeStringProc(const S: UnicodeString): UnicodeString;
  1017. begin
  1018. result:=UpperWideStringProc(S);
  1019. end;
  1020. function TUnicodeStringManager.LowerUnicodeStringProc(const S: UnicodeString): UnicodeString;
  1021. begin
  1022. result:=LowerWideStringProc(S);
  1023. end;
  1024. function TUnicodeStringManager.CompareUnicodeStringProc(const s1, s2 : UnicodeString) : PtrInt;
  1025. begin
  1026. result:=CompareWideStringProc(s1,s2);
  1027. end;
  1028. function TUnicodeStringManager.CompareTextUnicodeStringProc(const s1, s2 : UnicodeString): PtrInt;
  1029. begin
  1030. result:=CompareTextWideStringProc(s1,s2);
  1031. end;
  1032. procedure initunicodestringmanager;
  1033. begin
  1034. widestringmanager:=TUnicodeStringManager.Create;
  1035. end;