justrings.inc 34 KB

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