jastrings.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. member of the Free Pascal development team.
  5. This file implements AnsiStrings for FPC
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { This will release some functions for special shortstring support }
  13. { define EXTRAANSISHORT}
  14. constructor AnsistringClass.Create(len: longint; cp: TSystemCodePage);
  15. begin
  16. fElementSize:=1;
  17. { +1 for terminating #0 }
  18. setlength(fdata,len+1);
  19. fCodePage:=cp;
  20. end;
  21. constructor AnsistringClass.Create(const arr: array of ansichar; length: longint; cp: TSystemCodePage);
  22. begin
  23. fElementSize:=1;
  24. fCodePage:=cp;
  25. { make explicit copy so that changing the array afterwards doesn't change
  26. the string }
  27. if length=0 then
  28. begin
  29. { terminating #0 }
  30. setlength(fdata,1);
  31. exit;
  32. end;
  33. setlength(fdata,length+1);
  34. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(fdata),0,length);
  35. // last char is already #0 because of setlength
  36. end;
  37. constructor AnsistringClass.Create(const arr: array of unicodechar; cp: TSystemCodePage);
  38. var
  39. temp: RawByteString;
  40. begin
  41. fElementSize:=1;
  42. fCodePage:=cp;
  43. if high(arr)=-1 then
  44. begin
  45. { terminating #0 }
  46. setlength(fdata,1);
  47. exit;
  48. end;
  49. widestringmanager.Unicode2AnsiMoveProc(punicodechar(@arr),temp,cp,system.length(arr));
  50. fdata:=AnsistringClass(temp).fdata;
  51. // last char is already #0 because of Unicode2AnsiMoveProc()
  52. end;
  53. constructor AnsistringClass.Create(const u: unicodestring; cp: TSystemCodePage);
  54. var
  55. temp: RawByteString;
  56. begin
  57. fElementSize:=1;
  58. fCodePage:=cp;
  59. if system.length(u)=0 then
  60. begin
  61. { terminating #0 }
  62. setlength(fdata,1);
  63. exit;
  64. end;
  65. widestringmanager.Unicode2AnsiMoveProc(punicodechar(JLString(u).toCharArray),temp,cp,system.length(u));
  66. fdata:=AnsistringClass(temp).fdata;
  67. // last char is already #0 because of Unicode2AnsiMoveProc()
  68. end;
  69. constructor AnsistringClass.Create(const a: RawByteString; cp: TSystemCodePage);
  70. begin
  71. Create(AnsistringClass(a).fdata,system.length(AnsistringClass(a).fdata)-1,cp);
  72. end;
  73. constructor AnsistringClass.Create(const s: shortstring; cp: TSystemCodePage);
  74. begin
  75. Create(ShortstringClass(@s).fdata,system.length(s),cp);
  76. end;
  77. constructor AnsistringClass.Create(ch: ansichar; cp: TSystemCodePage);
  78. var
  79. arr: array[0..0] of ansichar;
  80. begin
  81. fElementSize:=1;
  82. fCodePage:=cp;
  83. setlength(fdata,2);
  84. fdata[0]:=ch;
  85. // last char is already #0 because of setlength
  86. end;
  87. constructor AnsistringClass.Create(ch: unicodechar; cp: TSystemCodePage);
  88. var
  89. temp: RawByteString;
  90. arr: array[0..0] of unicodechar;
  91. begin
  92. fElementSize:=1;
  93. fCodePage:=cp;
  94. arr[0]:=ch;
  95. widestringmanager.Unicode2AnsiMoveProc(punicodechar(@arr),temp,cp,system.length(arr));
  96. fdata:=AnsistringClass(temp).fdata;
  97. end;
  98. class function AnsistringClass.CreateFromLiteralStringBytes(const u: unicodestring; cp: TSystemCodePage): RawByteString;
  99. var
  100. res: AnsistringClass;
  101. i: longint;
  102. begin
  103. { used to construct constant ansistrings from Java string constants }
  104. res:=AnsistringClass.Create(system.length(u),cp);
  105. for i:=1 to system.length(u) do
  106. res.fdata[i-1]:=ansichar(ord(u[i]));
  107. result:=ansistring(res);
  108. end;
  109. function AnsistringClass.charAt(index: jint): ansichar;
  110. begin
  111. { index is already decreased by one, because same calling code is used for
  112. JLString.charAt() }
  113. result:=fdata[index];
  114. end;
  115. function AnsistringClass.toUnicodeString: unicodestring;
  116. begin
  117. widestringmanager.Ansi2UnicodeMoveProc(pchar(fdata),fCodePage,result,system.length(fdata)-1);
  118. end;
  119. function AnsistringClass.toShortstring(maxlen: byte): shortstring;
  120. begin
  121. ShortstringClass(@result).copyFromAnsiCharArray(fData,maxlen);
  122. end;
  123. function AnsistringClass.toString: JLString;
  124. begin
  125. result:=JLString(toUnicodeString);
  126. end;
  127. (*
  128. function AnsistringClass.concat(const a: ansistring): ansistring;
  129. var
  130. newdata: array of ansichar;
  131. addlen: sizeint;
  132. begin
  133. addlen:=length(a);
  134. thislen:=this.length;
  135. setlength(newdata,addlen+thislen);
  136. if thislen>0 then
  137. JLSystem.ArrayCopy(JLObject(fdata),0,JLObject(newdata),0,thislen);
  138. if addlen>0 then
  139. JLSystem.ArrayCopy(JLObject(AnsistringClass(a).fdata),0,JLObject(newdata),thislen,addlen);
  140. end;
  141. procedure AnsistringClass.concatmultiple(const arr: array of ansistring): ansistring;
  142. Var
  143. i : longint;
  144. size, newsize : sizeint;
  145. curlen, addlen : sizeint
  146. newdata: array of ansichar;
  147. begin
  148. { First calculate size of the result so we can allocate an array of
  149. the right size }
  150. NewSize:=0;
  151. for i:=low(arr) to high(arr) do
  152. inc(newsize,length(arr[i]));
  153. setlength(newdata,newsize);
  154. curlen
  155. for i:=low(arr) to high(arr) do
  156. begin
  157. if length(arr[i])>0 then
  158. sb.append(arr[i]);
  159. end;
  160. DestS:=sb.toString;
  161. end;
  162. *)
  163. function AnsiStringClass.length: jint;
  164. begin
  165. result:=system.length(fdata)-1;
  166. end;
  167. function AnsistringClass.codePage: TSystemCodePage;
  168. begin
  169. result:=fCodePage;
  170. end;
  171. function AnsistringClass.elementSize: Word;
  172. begin
  173. result:=fElementSize;
  174. end;
  175. {****************************************************************************
  176. Internal functions, not in interface.
  177. ****************************************************************************}
  178. {$ifndef FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  179. {$define FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  180. procedure fpc_pchar_ansistr_intern_charmove(const src: pchar; const srcindex: sizeint; var dst: ansistring; const dstindex, len: sizeint); {$ifdef FPC_HAS_CPSTRING}rtlproc;{$endif} {$ifdef SYSTEMINLINE}inline;{$endif}
  181. begin
  182. JLSystem.arraycopy(JLObject(src),srcindex,JLObject(AnsistringClass(dst).fdata),dstindex,len);
  183. end;
  184. {$endif FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  185. {$ifndef FPC_HAS_PCHAR_PCHAR_INTERN_CHARMOVE}
  186. {$define FPC_HAS_PCHAR_PCHAR_INTERN_CHARMOVE}
  187. procedure fpc_pchar_pchar_intern_charmove(const src: pchar; const srcindex: sizeint; const dst: pchar; const dstindex, len: sizeint); {$ifdef FPC_HAS_CPSTRING}rtlproc;{$endif} {$ifdef SYSTEMINLINE}inline;{$endif}
  188. begin
  189. JLSystem.arraycopy(JLObject(src),srcindex,JLObject(dst),dstindex,len);
  190. end;
  191. {$endif FPC_HAS_PCHAR_PCHAR_INTERN_CHARMOVE}
  192. {$ifndef FPC_HAS_SHORTSTR_ANSISTR_INTERN_CHARMOVE}
  193. {$define FPC_HAS_SHORTSTR_ANSISTR_INTERN_CHARMOVE}
  194. procedure fpc_shortstr_ansistr_intern_charmove(const src: shortstring; const srcindex: sizeint; var dst: rawbytestring; const dstindex, len: sizeint); {$ifdef FPC_HAS_CPSTRING}rtlproc;{$endif} {$ifdef SYSTEMINLINE}inline;{$endif}
  195. begin
  196. JLSystem.arraycopy(JLObject(ShortStringClass(@src).fdata),srcindex-1,JLObject(AnsistringClass(dst).fdata),dstindex,len);
  197. end;
  198. {$endif FPC_HAS_SHORTSTR_ANSISTR_INTERN_CHARMOVE}
  199. {$define FPC_HAS_NEWANSISTR}
  200. Function NewAnsiString(Len : SizeInt) : Pointer;
  201. {
  202. Allocate a new AnsiString on the heap.
  203. initialize it to zero length and reference count 1.
  204. }
  205. begin
  206. result:=AnsistringClass.Create(len,DefaultSystemCodePage);
  207. end;
  208. { not required }
  209. {$define FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  210. {$define FPC_SYSTEM_HAS_ANSISTR_INCR_REF}
  211. {$define FPC_HAS_ANSISTR_ASSIGN}
  212. {$ifndef FPC_HAS_ANSISTR_CONCAT_COMPLEX}
  213. {$define FPC_HAS_ANSISTR_CONCAT_COMPLEX}
  214. { keeps implicit try..finally block out from primary control flow }
  215. procedure ansistr_concat_complex(var DestS: RawByteString; const S1,S2: RawByteString; cp: TSystemCodePage);
  216. var
  217. U: UnicodeString;
  218. begin
  219. U:=UnicodeString(S1)+UnicodeString(S2);
  220. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(U).toCharArray),DestS,cp,Length(U));
  221. end;
  222. {$endif FPC_HAS_ANSISTR_CONCAT_COMPLEX}
  223. {$define FPC_HAS_ANSISTR_TO_ANSISTR}
  224. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; compilerproc;
  225. {
  226. Converts an AnsiString to an AnsiString taking code pages into care
  227. }
  228. Var
  229. Size : SizeInt;
  230. temp : UnicodeString;
  231. orgcp: TSystemCodePage;
  232. begin
  233. result:='';
  234. Size:=Length(S);
  235. if Size>0 then
  236. begin
  237. if (cp=CP_ACP) then
  238. cp:=DefaultSystemCodePage;
  239. orgcp:=StringCodePage(S);
  240. if (orgcp=CP_ACP) then
  241. orgcp:=DefaultSystemCodePage;
  242. if (orgcp=cp) or (orgcp=CP_NONE) then
  243. begin
  244. result:=RawByteString(AnsistringClass.Create(S,cp));
  245. end
  246. else
  247. begin
  248. temp:=S;
  249. Size:=Length(temp);
  250. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(temp).toCharArray),result,cp,Size);
  251. end;
  252. end;
  253. end;
  254. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [external name 'fpc_ansistr_to_ansistr'];
  255. {$define FPC_HAS_ANSISTR_CONCAT_MULTI}
  256. procedure fpc_AnsiStr_Concat_multi (var DestS:RawByteString;const sarr:array of RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); compilerproc;
  257. Var
  258. lowstart,i : Longint;
  259. p : pointer;
  260. Size,NewLen,
  261. OldDestLen : SizeInt;
  262. destcopy : RawByteString;
  263. DestCP : TSystemCodePage;
  264. U : UnicodeString;
  265. sameCP : Boolean;
  266. tmpStr : RawByteString;
  267. tmpCP : TSystemCodePage;
  268. begin
  269. if high(sarr)=0 then
  270. begin
  271. DestS:='';
  272. exit;
  273. end;
  274. {$ifdef FPC_HAS_CPSTRING}
  275. if (Pointer(DestS)=nil) then
  276. DestCP:=cp
  277. else
  278. DestCP:=StringCodePage(DestS);
  279. {$else FPC_HAS_CPSTRING}
  280. DestCP:=StringCodePage(DestS);
  281. {$endif FPC_HAS_CPSTRING}
  282. if (DestCP=CP_ACP) then
  283. DestCP:=DefaultSystemCodePage;
  284. sameCP:=true;
  285. lowstart:=low(sarr);
  286. for i:=lowstart to high(sarr) do
  287. begin
  288. tmpCP:=StringCodePage(sarr[i]);
  289. if tmpCP=CP_ACP then
  290. tmpCP:=DefaultSystemCodePage;
  291. if (DestCP<>tmpCp) then
  292. begin
  293. sameCP:=false;
  294. break;
  295. end;
  296. end;
  297. if not sameCP then
  298. begin
  299. U:='';
  300. for i:=lowstart to high(sarr) do begin
  301. tmpCP:=StringCodePage(sarr[i]);
  302. if (tmpCP=CP_ACP) then
  303. begin
  304. tmpStr:=sarr[i];
  305. SetCodePage(tmpStr,DefaultSystemCodePage,False);
  306. U:=U+UnicodeString(tmpStr);
  307. end
  308. else
  309. U:=U+UnicodeString(sarr[i]);
  310. end;
  311. DestS:='';
  312. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(U).toCharArray),DestS,DestCP,Length(U));
  313. exit;
  314. end;
  315. lowstart:=low(sarr);
  316. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  317. inc(lowstart);
  318. { Check for another reuse, then we can't use
  319. the append optimization }
  320. for i:=lowstart to high(sarr) do
  321. begin
  322. if Pointer(DestS)=Pointer(sarr[i]) then
  323. begin
  324. { if DestS is used somewhere in the middle of the expression,
  325. we need to make sure the original string still exists after
  326. we empty/modify DestS -- not necessary on JVM platform, ansistrings
  327. are not explicitly refrence counted there }
  328. lowstart:=low(sarr);
  329. break;
  330. end;
  331. end;
  332. { Start with empty DestS if we start with concatting
  333. the first array element }
  334. if lowstart=low(sarr) then
  335. DestS:='';
  336. OldDestLen:=length(DestS);
  337. { Calculate size of the result so we can do
  338. a single call to SetLength() }
  339. NewLen:=0;
  340. for i:=low(sarr) to high(sarr) do
  341. inc(NewLen,length(sarr[i]));
  342. SetLength(DestS,NewLen);
  343. if (StringCodePage(DestS) <> DestCP) then
  344. SetCodePage(DestS,DestCP,False);
  345. { Concat all strings, except the string we already
  346. copied in DestS }
  347. NewLen:=OldDestLen;
  348. for i:=lowstart to high(sarr) do
  349. begin
  350. p:=pointer(sarr[i]);
  351. if assigned(p) then
  352. begin
  353. Size:=length(ansistring(p));
  354. fpc_pchar_pchar_intern_charmove(pchar(ansistring(p)),0,pchar(DestS),NewLen,Size+1);
  355. inc(NewLen,size);
  356. end;
  357. end;
  358. end;
  359. {$define FPC_HAS_ANSISTR_TO_SHORTSTR}
  360. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  361. {
  362. Converts a AnsiString to a ShortString;
  363. }
  364. Var
  365. Size : SizeInt;
  366. begin
  367. if S2='' then
  368. res:=''
  369. else
  370. begin
  371. Size:=Length(S2);
  372. If Size>high(res) then
  373. Size:=high(res);
  374. if Size>0 then
  375. JLSystem.ArrayCopy(JLObject(AnsistringClass(S2).fdata),0,JLObject(ShortstringClass(@res).fdata),0,Size);
  376. setlength(res,size);
  377. end;
  378. end;
  379. {$define FPC_HAS_PCHAR_TO_ANSISTR}
  380. Function fpc_PChar_To_AnsiStr(const p : PAnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  381. Var
  382. L : SizeInt;
  383. {$ifndef FPC_HAS_CPSTRING}
  384. cp : TSystemCodePage;
  385. {$endif FPC_HAS_CPSTRING}
  386. begin
  387. if (not assigned(p)) or (p[0]=#0) Then
  388. L := 0
  389. else
  390. L:=IndexChar(Arr1jbyte(p),-1,#0);
  391. SetLength(fpc_PChar_To_AnsiStr,L);
  392. if L > 0 then
  393. begin
  394. {$ifdef FPC_HAS_CPSTRING}
  395. if (cp=CP_ACP) then
  396. cp:=DefaultSystemCodePage;
  397. {$else FPC_HAS_CPSTRING}
  398. cp:=DefaultSystemCodePage;
  399. {$endif FPC_HAS_CPSTRING}
  400. fpc_pchar_ansistr_intern_charmove(p,0,fpc_PChar_To_AnsiStr,0,L);
  401. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  402. end;
  403. end;
  404. {$define FPC_HAS_ANSISTR_TO_CHARARRAY}
  405. procedure fpc_ansistr_to_chararray(out res: array of AnsiChar; const src: RawByteString); compilerproc;
  406. var
  407. len: longint;
  408. begin
  409. len:=length(src);
  410. if len>length(res) then
  411. len:=length(res);
  412. { make sure we don't try to access element 1 of the ansistring if it's nil }
  413. if len>0 then
  414. JLSystem.ArrayCopy(JLObject(AnsistringClass(src).fdata),0,JLObject(@res),0,len);
  415. if len<=high(res) then
  416. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  417. end;
  418. function fpc_ansistr_setchar(const s: RawByteString; const index: longint; const ch: ansichar): RawByteString; compilerproc;
  419. var
  420. res: AnsistringClass;
  421. begin
  422. res:=AnsistringClass.Create(s,AnsistringClass(s).fCodePage);
  423. res.fdata[index-1]:=ch;
  424. result:=Ansistring(res);
  425. end;
  426. {$define FPC_HAS_ANSISTR_COMPARE}
  427. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  428. {
  429. Compares 2 AnsiStrings;
  430. The result is
  431. <0 if S1<S2
  432. 0 if S1=S2
  433. >0 if S1>S2
  434. }
  435. Var
  436. MaxI,Temp, i : SizeInt;
  437. cp1,cp2 : TSystemCodePage;
  438. r1,r2 : RawByteString;
  439. begin
  440. if JLObject(S1)=JLObject(S2) then
  441. begin
  442. result:=0;
  443. exit;
  444. end;
  445. if (pointer(S1)=nil) then
  446. begin
  447. result:=-Length(S2);
  448. exit;
  449. end;
  450. if (pointer(S2)=nil) then
  451. begin
  452. result:=Length(S1);
  453. exit;
  454. end;
  455. cp1:=StringCodePage(S1);
  456. cp2:=StringCodePage(S2);
  457. if cp1=cp2 then
  458. begin
  459. Maxi:=Length(S1);
  460. temp:=Length(S2);
  461. If MaxI>Temp then
  462. MaxI:=Temp;
  463. for i:=0 to MaxI-1 do
  464. begin
  465. result:=ord(AnsistringClass(S1).fdata[i])-ord(AnsistringClass(S2).fdata[i]);
  466. if result<>0 then
  467. exit;
  468. end;
  469. result:=Length(S1)-Length(S2);
  470. end
  471. else
  472. begin
  473. r1:=S1;
  474. if (cp1=CP_ACP) then
  475. SetCodePage(r1,DefaultSystemCodePage,false);
  476. r2:=S2;
  477. if (cp2=CP_ACP) then
  478. SetCodePage(r2,DefaultSystemCodePage,false);
  479. //convert them to utf8 then compare
  480. SetCodePage(r1,65001);
  481. SetCodePage(r2,65001);
  482. Result := fpc_AnsiStr_Compare(r1,r2);
  483. end;
  484. end;
  485. {$define FPC_HAS_ANSISTR_COMPARE_EQUAL}
  486. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt; compilerproc;
  487. {
  488. Compares 2 AnsiStrings for equality/inequality only;
  489. The result is
  490. 0 if S1=S2
  491. <>0 if S1<>S2
  492. }
  493. Var
  494. MaxI,Temp : SizeInt;
  495. cp1,cp2 : TSystemCodePage;
  496. r1,r2 : RawByteString;
  497. begin
  498. if JLObject(S1)=JLObject(S2) then
  499. begin
  500. result:=0;
  501. exit;
  502. end;
  503. { don't compare strings if one of them is empty }
  504. if (pointer(S1)=nil) then
  505. begin
  506. result:=-Length(S2);
  507. exit;
  508. end;
  509. if (pointer(S2)=nil) then
  510. begin
  511. result:=Length(S1);
  512. exit;
  513. end;
  514. cp1:=StringCodePage(S1);
  515. cp2:=StringCodePage(S2);
  516. if cp1<>cp2 then
  517. begin
  518. r1:=S1;
  519. if (cp1=CP_ACP) then
  520. SetCodePage(r1,DefaultSystemCodePage,false);
  521. r2:=S2;
  522. if (cp2=CP_ACP) then
  523. SetCodePage(r2,DefaultSystemCodePage,false);
  524. //convert them to utf8 then compare
  525. SetCodePage(r1,65001);
  526. SetCodePage(r2,65001);
  527. end
  528. else
  529. begin
  530. r1:=s1;
  531. r2:=s2;
  532. end;
  533. result:=ord(not JUArrays.equals(TJByteArray(AnsistringClass(r1).fdata),TJByteArray(AnsistringClass(r2).fdata)))
  534. end;
  535. { not required, the JVM does the range checking for us }
  536. {$define FPC_HAS_ANSISTR_CHECKRANGE}
  537. {$define FPC_HAS_ANSISTR_SETLENGTH}
  538. Procedure fpc_AnsiStr_SetLength (Var S : RawByteString; l : SizeInt{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  539. {
  540. Sets The length of string S to L.
  541. Makes sure S is unique, and contains enough room.
  542. }
  543. var
  544. oldlen: longint;
  545. result: RawByteString;
  546. begin
  547. if (cp=CP_ACP) then
  548. cp:=DefaultSystemCodePage;
  549. { no explicit reference counting possible -> can't reuse S because we don't
  550. know how many references exist to it }
  551. result:=RawByteString(AnsistringClass.Create(l,cp));
  552. oldlen:=length(s);
  553. if l>oldlen then
  554. l:=oldlen;
  555. if l>0 then
  556. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),0,JLObject(AnsistringClass(result).fdata),0,l);
  557. S:=result;
  558. end;
  559. {*****************************************************************************
  560. Public functions, In interface.
  561. *****************************************************************************}
  562. { lie, not needed }
  563. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  564. { can't implement reference counting since no control over what javacc-compiled
  565. code does with ansistrings -> always create a copy }
  566. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  567. procedure FPC_ANSISTR_UNIQUE(var s: AnsiString); inline;
  568. begin
  569. s:=ansistring(AnsistringClass.Create(s,AnsiStringClass(s).fCodePage));
  570. end;
  571. {$define FPC_HAS_ANSISTR_COPY}
  572. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt): RawByteString;compilerproc;
  573. var
  574. res: AnsistringClass;
  575. begin
  576. dec(index);
  577. if Index < 0 then
  578. Index := 0;
  579. { Check Size. Accounts for Zero-length S, the double check is needed because
  580. Size can be maxint and will get <0 when adding index }
  581. if (Size>Length(S)) or
  582. (Index+Size>Length(S)) then
  583. Size:=Length(S)-Index;
  584. If Size>0 then
  585. begin
  586. res:=AnsistringClass.Create;
  587. AnsistringClass(res).fcodepage:=AnsistringClass(S).fcodepage;
  588. { +1 for terminating #0 }
  589. setlength(res.fdata,size+1);
  590. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),index,JLObject(res.fdata),0,size);
  591. result:=ansistring(res);
  592. end;
  593. { default function result is empty string }
  594. end;
  595. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  596. Function Pos(Const Substr : ShortString; Const Source : RawByteString) : SizeInt;
  597. var
  598. i,j,k,MaxLen, SubstrLen : SizeInt;
  599. begin
  600. Pos:=0;
  601. SubstrLen:=Length(SubStr);
  602. if SubstrLen>0 then
  603. begin
  604. MaxLen:=Length(source)-Length(SubStr);
  605. i:=0;
  606. while (i<=MaxLen) do
  607. begin
  608. inc(i);
  609. j:=0;
  610. k:=i-1;
  611. while (j<SubstrLen) and
  612. (ShortStringClass(@SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  613. begin
  614. inc(j);
  615. inc(k);
  616. end;
  617. if (j=SubstrLen) then
  618. begin
  619. Pos:=i;
  620. exit;
  621. end;
  622. end;
  623. end;
  624. end;
  625. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  626. Function Pos(Const Substr : RawByteString; Const Source : RawByteString) : SizeInt;
  627. var
  628. i,j,k,MaxLen, SubstrLen : SizeInt;
  629. begin
  630. Pos:=0;
  631. SubstrLen:=Length(SubStr);
  632. if SubstrLen>0 then
  633. begin
  634. MaxLen:=Length(source)-Length(SubStr);
  635. i:=0;
  636. while (i<=MaxLen) do
  637. begin
  638. inc(i);
  639. j:=0;
  640. k:=i-1;
  641. while (j<SubstrLen) and
  642. (AnsistringClass(SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  643. begin
  644. inc(j);
  645. inc(k);
  646. end;
  647. if (j=SubstrLen) then
  648. begin
  649. Pos:=i;
  650. exit;
  651. end;
  652. end;
  653. end;
  654. end;
  655. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  656. { Faster version for a char alone. Must be implemented because }
  657. { pos(c: char; const s: shortstring) also exists, so otherwise }
  658. { using pos(char,pchar) will always call the shortstring version }
  659. { (exact match for first argument), also with $h+ (JM) }
  660. Function Pos (c : AnsiChar; Const s : RawByteString) : SizeInt;
  661. var
  662. i: SizeInt;
  663. begin
  664. for i:=1 to length(s) do
  665. begin
  666. if AnsistringClass(s).fdata[i-1]=c then
  667. begin
  668. pos:=i;
  669. exit;
  670. end;
  671. end;
  672. pos:=0;
  673. end;
  674. {$define FPC_HAS_ANSISTR_OF_CHAR}
  675. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  676. begin
  677. SetLength(StringOfChar,l);
  678. FillChar(AnsistringClass(result).fdata,l,c);
  679. end;
  680. {$define FPC_HAS_UPCASE_ANSISTR}
  681. function upcase(const s : ansistring) : ansistring;
  682. var
  683. u : unicodestring;
  684. begin
  685. u:=s;
  686. result:=upcase(u);
  687. end;
  688. {$define FPC_HAS_LOWERCASE_ANSISTR}
  689. function lowercase(const s : ansistring) : ansistring;
  690. var
  691. u : unicodestring;
  692. begin
  693. u:=s;
  694. result:=lowercase(u);
  695. end;
  696. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  697. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  698. begin
  699. if assigned(pointer(S)) then
  700. Result:=AnsistringClass(S).fCodePage
  701. else
  702. Result:=DefaultSystemCodePage;
  703. end;
  704. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  705. function StringElementSize(const S: RawByteString): Word; overload;
  706. begin
  707. if assigned(Pointer(S)) then
  708. Result:=AnsistringClass(S).fElementSize
  709. else
  710. Result:=SizeOf(AnsiChar);
  711. end;
  712. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  713. function StringRefCount(const S: RawByteString): SizeInt; overload;
  714. begin
  715. if assigned(Pointer(S)) then
  716. Result:=1
  717. else
  718. Result:=0;
  719. end;
  720. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  721. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  722. begin
  723. if not assigned(Pointer(S)) or (StringCodePage(S)=CodePage) then
  724. exit
  725. else if (AnsistringClass(S).length<>0) and
  726. Convert then
  727. begin
  728. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  729. end
  730. else
  731. begin
  732. AnsistringClass(S).fCodePage:=CodePage;
  733. end;
  734. end;