jastrings.inc 23 KB

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