justrings.inc 34 KB

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