2
0

jastrings.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. class function AnsistringClass.internChars(const a: Ansistring): TAnsiCharArray;
  176. begin
  177. if a<>'' then
  178. result:=AnsistringClass(a).fdata
  179. else
  180. { empty pchar: array with one element that is #0 }
  181. setlength(result,1);
  182. end;
  183. {****************************************************************************
  184. Internal functions, not in interface.
  185. ****************************************************************************}
  186. {$ifndef FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  187. {$define FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  188. 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}
  189. begin
  190. JLSystem.arraycopy(JLObject(src),srcindex,JLObject(AnsistringClass(dst).fdata),dstindex,len);
  191. end;
  192. {$endif FPC_HAS_PCHAR_ANSISTR_INTERN_CHARMOVE}
  193. {$ifndef FPC_HAS_PCHAR_PCHAR_INTERN_CHARMOVE}
  194. {$define FPC_HAS_PCHAR_PCHAR_INTERN_CHARMOVE}
  195. 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}
  196. begin
  197. JLSystem.arraycopy(JLObject(src),srcindex,JLObject(dst),dstindex,len);
  198. end;
  199. {$endif FPC_HAS_PCHAR_PCHAR_INTERN_CHARMOVE}
  200. {$ifndef FPC_HAS_SHORTSTR_ANSISTR_INTERN_CHARMOVE}
  201. {$define FPC_HAS_SHORTSTR_ANSISTR_INTERN_CHARMOVE}
  202. 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}
  203. begin
  204. JLSystem.arraycopy(JLObject(ShortStringClass(@src).fdata),srcindex-1,JLObject(AnsistringClass(dst).fdata),dstindex,len);
  205. end;
  206. {$endif FPC_HAS_SHORTSTR_ANSISTR_INTERN_CHARMOVE}
  207. {$define FPC_HAS_NEWANSISTR}
  208. Function NewAnsiString(Len : SizeInt) : Pointer;
  209. {
  210. Allocate a new AnsiString on the heap.
  211. initialize it to zero length and reference count 1.
  212. }
  213. begin
  214. result:=AnsistringClass.Create(len,DefaultSystemCodePage);
  215. end;
  216. { not required }
  217. {$define FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  218. {$define FPC_SYSTEM_HAS_ANSISTR_INCR_REF}
  219. {$define FPC_HAS_ANSISTR_ASSIGN}
  220. {$ifndef FPC_HAS_ANSISTR_CONCAT_COMPLEX}
  221. {$define FPC_HAS_ANSISTR_CONCAT_COMPLEX}
  222. { keeps implicit try..finally block out from primary control flow }
  223. procedure ansistr_concat_complex(var DestS: RawByteString; const S1,S2: RawByteString; cp: TSystemCodePage);
  224. var
  225. U: UnicodeString;
  226. begin
  227. U:=UnicodeString(S1)+UnicodeString(S2);
  228. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(U).toCharArray),DestS,cp,Length(U));
  229. end;
  230. {$endif FPC_HAS_ANSISTR_CONCAT_COMPLEX}
  231. {$define FPC_HAS_ANSISTR_TO_ANSISTR}
  232. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; compilerproc;
  233. {
  234. Converts an AnsiString to an AnsiString taking code pages into care
  235. }
  236. Var
  237. Size : SizeInt;
  238. temp : UnicodeString;
  239. orgcp: TSystemCodePage;
  240. begin
  241. result:='';
  242. Size:=Length(S);
  243. if Size>0 then
  244. begin
  245. if (cp=CP_ACP) then
  246. cp:=DefaultSystemCodePage;
  247. orgcp:=StringCodePage(S);
  248. if (orgcp=CP_ACP) then
  249. orgcp:=DefaultSystemCodePage;
  250. if (orgcp=cp) or (orgcp=CP_NONE) then
  251. begin
  252. result:=RawByteString(AnsistringClass.Create(S,cp));
  253. end
  254. else
  255. begin
  256. temp:=UnicodeString(S);
  257. Size:=Length(temp);
  258. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(temp).toCharArray),result,cp,Size);
  259. end;
  260. end;
  261. end;
  262. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [external name 'fpc_ansistr_to_ansistr'];
  263. {$define FPC_HAS_ANSISTR_CONCAT_MULTI}
  264. procedure fpc_AnsiStr_Concat_multi (var DestS:RawByteString;const sarr:array of RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); compilerproc;
  265. Var
  266. lowstart,i : Longint;
  267. p : pointer;
  268. Size,NewLen,
  269. OldDestLen : SizeInt;
  270. destcopy : RawByteString;
  271. DestCP : TSystemCodePage;
  272. U : UnicodeString;
  273. sameCP : Boolean;
  274. tmpStr : RawByteString;
  275. tmpCP : TSystemCodePage;
  276. begin
  277. if high(sarr)=0 then
  278. begin
  279. DestS:='';
  280. exit;
  281. end;
  282. {$ifdef FPC_HAS_CPSTRING}
  283. if (Pointer(DestS)=nil) then
  284. DestCP:=cp
  285. else
  286. DestCP:=StringCodePage(DestS);
  287. {$else FPC_HAS_CPSTRING}
  288. DestCP:=StringCodePage(DestS);
  289. {$endif FPC_HAS_CPSTRING}
  290. if (DestCP=CP_ACP) then
  291. DestCP:=DefaultSystemCodePage;
  292. sameCP:=true;
  293. lowstart:=low(sarr);
  294. for i:=lowstart to high(sarr) do
  295. begin
  296. tmpCP:=StringCodePage(sarr[i]);
  297. if tmpCP=CP_ACP then
  298. tmpCP:=DefaultSystemCodePage;
  299. if (DestCP<>tmpCp) then
  300. begin
  301. sameCP:=false;
  302. break;
  303. end;
  304. end;
  305. if not sameCP then
  306. begin
  307. U:='';
  308. for i:=lowstart to high(sarr) do begin
  309. tmpCP:=StringCodePage(sarr[i]);
  310. if (tmpCP=CP_ACP) then
  311. begin
  312. tmpStr:=sarr[i];
  313. SetCodePage(tmpStr,DefaultSystemCodePage,False);
  314. U:=U+UnicodeString(tmpStr);
  315. end
  316. else
  317. U:=U+UnicodeString(sarr[i]);
  318. end;
  319. DestS:='';
  320. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(U).toCharArray),DestS,DestCP,Length(U));
  321. exit;
  322. end;
  323. lowstart:=low(sarr);
  324. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  325. inc(lowstart);
  326. { Check for another reuse, then we can't use
  327. the append optimization }
  328. for i:=lowstart to high(sarr) do
  329. begin
  330. if Pointer(DestS)=Pointer(sarr[i]) then
  331. begin
  332. { if DestS is used somewhere in the middle of the expression,
  333. we need to make sure the original string still exists after
  334. we empty/modify DestS -- not necessary on JVM platform, ansistrings
  335. are not explicitly refrence counted there }
  336. lowstart:=low(sarr);
  337. break;
  338. end;
  339. end;
  340. { Start with empty DestS if we start with concatting
  341. the first array element }
  342. if lowstart=low(sarr) then
  343. DestS:='';
  344. OldDestLen:=length(DestS);
  345. { Calculate size of the result so we can do
  346. a single call to SetLength() }
  347. NewLen:=0;
  348. for i:=low(sarr) to high(sarr) do
  349. inc(NewLen,length(sarr[i]));
  350. SetLength(DestS,NewLen);
  351. if (StringCodePage(DestS) <> DestCP) then
  352. SetCodePage(DestS,DestCP,False);
  353. { Concat all strings, except the string we already
  354. copied in DestS }
  355. NewLen:=OldDestLen;
  356. for i:=lowstart to high(sarr) do
  357. begin
  358. p:=pointer(sarr[i]);
  359. if assigned(p) then
  360. begin
  361. Size:=length(ansistring(p));
  362. fpc_pchar_pchar_intern_charmove(pchar(ansistring(p)),0,pchar(DestS),NewLen,Size+1);
  363. inc(NewLen,size);
  364. end;
  365. end;
  366. end;
  367. {$define FPC_HAS_ANSISTR_TO_SHORTSTR}
  368. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  369. {
  370. Converts a AnsiString to a ShortString;
  371. }
  372. Var
  373. Size : SizeInt;
  374. begin
  375. if S2='' then
  376. res:=''
  377. else
  378. begin
  379. Size:=Length(S2);
  380. If Size>high(res) then
  381. Size:=high(res);
  382. if Size>0 then
  383. JLSystem.ArrayCopy(JLObject(AnsistringClass(S2).fdata),0,JLObject(ShortstringClass(@res).fdata),0,Size);
  384. setlength(res,size);
  385. end;
  386. end;
  387. {$define FPC_HAS_PCHAR_TO_ANSISTR}
  388. Function fpc_PChar_To_AnsiStr(const p : PAnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  389. Var
  390. L : SizeInt;
  391. {$ifndef FPC_HAS_CPSTRING}
  392. cp : TSystemCodePage;
  393. {$endif FPC_HAS_CPSTRING}
  394. begin
  395. if (not assigned(p)) or (p[0]=#0) Then
  396. L := 0
  397. else
  398. L:=IndexChar(Arr1jbyte(p),-1,#0);
  399. SetLength(fpc_PChar_To_AnsiStr,L);
  400. if L > 0 then
  401. begin
  402. {$ifdef FPC_HAS_CPSTRING}
  403. if (cp=CP_ACP) then
  404. cp:=DefaultSystemCodePage;
  405. {$else FPC_HAS_CPSTRING}
  406. cp:=DefaultSystemCodePage;
  407. {$endif FPC_HAS_CPSTRING}
  408. fpc_pchar_ansistr_intern_charmove(p,0,fpc_PChar_To_AnsiStr,0,L);
  409. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  410. end;
  411. end;
  412. {$define FPC_HAS_ANSISTR_TO_CHARARRAY}
  413. procedure fpc_ansistr_to_chararray(out res: array of AnsiChar; const src: RawByteString); compilerproc;
  414. var
  415. len: longint;
  416. begin
  417. len:=length(src);
  418. if len>length(res) then
  419. len:=length(res);
  420. { make sure we don't try to access element 1 of the ansistring if it's nil }
  421. if len>0 then
  422. JLSystem.ArrayCopy(JLObject(AnsistringClass(src).fdata),0,JLObject(@res),0,len);
  423. if len<=high(res) then
  424. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  425. end;
  426. function fpc_ansistr_setchar(const s: RawByteString; const index: longint; const ch: ansichar): RawByteString; compilerproc;
  427. var
  428. res: AnsistringClass;
  429. begin
  430. res:=AnsistringClass.Create(s,AnsistringClass(s).fCodePage);
  431. res.fdata[index-1]:=ch;
  432. result:=Ansistring(res);
  433. end;
  434. {$define FPC_HAS_ANSISTR_COMPARE}
  435. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  436. {
  437. Compares 2 AnsiStrings;
  438. The result is
  439. <0 if S1<S2
  440. 0 if S1=S2
  441. >0 if S1>S2
  442. }
  443. Var
  444. MaxI,Temp, i : SizeInt;
  445. cp1,cp2 : TSystemCodePage;
  446. r1,r2 : RawByteString;
  447. begin
  448. if JLObject(S1)=JLObject(S2) then
  449. begin
  450. result:=0;
  451. exit;
  452. end;
  453. if (pointer(S1)=nil) then
  454. begin
  455. result:=-Length(S2);
  456. exit;
  457. end;
  458. if (pointer(S2)=nil) then
  459. begin
  460. result:=Length(S1);
  461. exit;
  462. end;
  463. cp1:=StringCodePage(S1);
  464. cp2:=StringCodePage(S2);
  465. if cp1=cp2 then
  466. begin
  467. Maxi:=Length(S1);
  468. temp:=Length(S2);
  469. If MaxI>Temp then
  470. MaxI:=Temp;
  471. for i:=0 to MaxI-1 do
  472. begin
  473. result:=ord(AnsistringClass(S1).fdata[i])-ord(AnsistringClass(S2).fdata[i]);
  474. if result<>0 then
  475. exit;
  476. end;
  477. result:=Length(S1)-Length(S2);
  478. end
  479. else
  480. begin
  481. r1:=S1;
  482. if (cp1=CP_ACP) then
  483. SetCodePage(r1,DefaultSystemCodePage,false);
  484. r2:=S2;
  485. if (cp2=CP_ACP) then
  486. SetCodePage(r2,DefaultSystemCodePage,false);
  487. //convert them to utf8 then compare
  488. SetCodePage(r1,65001);
  489. SetCodePage(r2,65001);
  490. Result := fpc_AnsiStr_Compare(r1,r2);
  491. end;
  492. end;
  493. {$define FPC_HAS_ANSISTR_COMPARE_EQUAL}
  494. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt; compilerproc;
  495. {
  496. Compares 2 AnsiStrings for equality/inequality only;
  497. The result is
  498. 0 if S1=S2
  499. <>0 if S1<>S2
  500. }
  501. Var
  502. MaxI,Temp : SizeInt;
  503. cp1,cp2 : TSystemCodePage;
  504. r1,r2 : RawByteString;
  505. begin
  506. if JLObject(S1)=JLObject(S2) then
  507. begin
  508. result:=0;
  509. exit;
  510. end;
  511. { don't compare strings if one of them is empty }
  512. if (pointer(S1)=nil) then
  513. begin
  514. result:=-Length(S2);
  515. exit;
  516. end;
  517. if (pointer(S2)=nil) then
  518. begin
  519. result:=Length(S1);
  520. exit;
  521. end;
  522. cp1:=StringCodePage(S1);
  523. cp2:=StringCodePage(S2);
  524. if cp1<>cp2 then
  525. begin
  526. r1:=S1;
  527. if (cp1=CP_ACP) then
  528. SetCodePage(r1,DefaultSystemCodePage,false);
  529. r2:=S2;
  530. if (cp2=CP_ACP) then
  531. SetCodePage(r2,DefaultSystemCodePage,false);
  532. //convert them to utf8 then compare
  533. SetCodePage(r1,65001);
  534. SetCodePage(r2,65001);
  535. end
  536. else
  537. begin
  538. r1:=s1;
  539. r2:=s2;
  540. end;
  541. result:=ord(not JUArrays.equals(TJByteArray(AnsistringClass(r1).fdata),TJByteArray(AnsistringClass(r2).fdata)))
  542. end;
  543. { not required, the JVM does the range checking for us }
  544. {$define FPC_HAS_ANSISTR_CHECKRANGE}
  545. {$define FPC_HAS_ANSISTR_SETLENGTH}
  546. 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;
  547. {
  548. Sets The length of string S to L.
  549. Makes sure S is unique, and contains enough room.
  550. }
  551. var
  552. oldlen: longint;
  553. result: RawByteString;
  554. begin
  555. if (cp=CP_ACP) then
  556. cp:=DefaultSystemCodePage;
  557. { no explicit reference counting possible -> can't reuse S because we don't
  558. know how many references exist to it }
  559. result:=RawByteString(AnsistringClass.Create(l,cp));
  560. oldlen:=length(s);
  561. if l>oldlen then
  562. l:=oldlen;
  563. if l>0 then
  564. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),0,JLObject(AnsistringClass(result).fdata),0,l);
  565. S:=result;
  566. end;
  567. {*****************************************************************************
  568. Public functions, In interface.
  569. *****************************************************************************}
  570. { lie, not needed }
  571. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  572. { can't implement reference counting since no control over what javacc-compiled
  573. code does with ansistrings -> always create a copy }
  574. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  575. procedure FPC_ANSISTR_UNIQUE(var s: AnsiString); inline;
  576. begin
  577. s:=ansistring(AnsistringClass.Create(s,AnsiStringClass(s).fCodePage));
  578. end;
  579. {$define FPC_HAS_ANSISTR_COPY}
  580. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt): RawByteString;compilerproc;
  581. var
  582. res: AnsistringClass;
  583. begin
  584. dec(index);
  585. if Index < 0 then
  586. Index := 0;
  587. { Check Size. Accounts for Zero-length S, the double check is needed because
  588. Size can be maxint and will get <0 when adding index }
  589. if (Size>Length(S)) or
  590. (Index+Size>Length(S)) then
  591. Size:=Length(S)-Index;
  592. If Size>0 then
  593. begin
  594. res:=AnsistringClass.Create;
  595. AnsistringClass(res).fcodepage:=AnsistringClass(S).fcodepage;
  596. { +1 for terminating #0 }
  597. setlength(res.fdata,size+1);
  598. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),index,JLObject(res.fdata),0,size);
  599. result:=ansistring(res);
  600. end;
  601. { default function result is empty string }
  602. end;
  603. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  604. Function Pos(Const Substr : ShortString; Const Source : RawByteString) : SizeInt;
  605. var
  606. i,j,k,MaxLen, SubstrLen : SizeInt;
  607. begin
  608. Pos:=0;
  609. SubstrLen:=Length(SubStr);
  610. if SubstrLen>0 then
  611. begin
  612. MaxLen:=Length(source)-Length(SubStr);
  613. i:=0;
  614. while (i<=MaxLen) do
  615. begin
  616. inc(i);
  617. j:=0;
  618. k:=i-1;
  619. while (j<SubstrLen) and
  620. (ShortStringClass(@SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  621. begin
  622. inc(j);
  623. inc(k);
  624. end;
  625. if (j=SubstrLen) then
  626. begin
  627. Pos:=i;
  628. exit;
  629. end;
  630. end;
  631. end;
  632. end;
  633. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  634. Function Pos(Const Substr : RawByteString; Const Source : RawByteString) : SizeInt;
  635. var
  636. i,j,k,MaxLen, SubstrLen : SizeInt;
  637. begin
  638. Pos:=0;
  639. SubstrLen:=Length(SubStr);
  640. if SubstrLen>0 then
  641. begin
  642. MaxLen:=Length(source)-Length(SubStr);
  643. i:=0;
  644. while (i<=MaxLen) do
  645. begin
  646. inc(i);
  647. j:=0;
  648. k:=i-1;
  649. while (j<SubstrLen) and
  650. (AnsistringClass(SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  651. begin
  652. inc(j);
  653. inc(k);
  654. end;
  655. if (j=SubstrLen) then
  656. begin
  657. Pos:=i;
  658. exit;
  659. end;
  660. end;
  661. end;
  662. end;
  663. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  664. { Faster version for a char alone. Must be implemented because }
  665. { pos(c: char; const s: shortstring) also exists, so otherwise }
  666. { using pos(char,pchar) will always call the shortstring version }
  667. { (exact match for first argument), also with $h+ (JM) }
  668. Function Pos (c : AnsiChar; Const s : RawByteString) : SizeInt;
  669. var
  670. i: SizeInt;
  671. begin
  672. for i:=1 to length(s) do
  673. begin
  674. if AnsistringClass(s).fdata[i-1]=c then
  675. begin
  676. pos:=i;
  677. exit;
  678. end;
  679. end;
  680. pos:=0;
  681. end;
  682. {$define FPC_HAS_ANSISTR_OF_CHAR}
  683. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  684. begin
  685. SetLength(StringOfChar,l);
  686. FillChar(AnsistringClass(result).fdata,l,c);
  687. end;
  688. {$define FPC_HAS_UPCASE_ANSISTR}
  689. function upcase(const s : ansistring) : ansistring;
  690. var
  691. u : unicodestring;
  692. begin
  693. u:=s;
  694. result:=upcase(u);
  695. end;
  696. {$define FPC_HAS_LOWERCASE_ANSISTR}
  697. function lowercase(const s : ansistring) : ansistring;
  698. var
  699. u : unicodestring;
  700. begin
  701. u:=s;
  702. result:=lowercase(u);
  703. end;
  704. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  705. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  706. begin
  707. if assigned(pointer(S)) then
  708. Result:=AnsistringClass(S).fCodePage
  709. else
  710. Result:=DefaultSystemCodePage;
  711. end;
  712. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  713. function StringElementSize(const S: RawByteString): Word; overload;
  714. begin
  715. if assigned(Pointer(S)) then
  716. Result:=AnsistringClass(S).fElementSize
  717. else
  718. Result:=SizeOf(AnsiChar);
  719. end;
  720. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  721. function StringRefCount(const S: RawByteString): SizeInt; overload;
  722. begin
  723. if assigned(Pointer(S)) then
  724. Result:=1
  725. else
  726. Result:=0;
  727. end;
  728. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  729. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  730. begin
  731. if not assigned(Pointer(S)) or (StringCodePage(S)=CodePage) then
  732. exit
  733. else if (AnsistringClass(S).length<>0) and
  734. Convert then
  735. begin
  736. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  737. end
  738. else
  739. begin
  740. AnsistringClass(S).fCodePage:=CodePage;
  741. end;
  742. end;