jastrings.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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: ansistring; 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. DestCP:=TranslatePlaceholderCP(DestCP);
  303. sameCP:=true;
  304. lowstart:=low(sarr);
  305. { skip empty strings }
  306. while (lowstart<=high(sarr)) and
  307. (sarr[lowstart]='') do
  308. inc(lowstart);
  309. tmpCP:=TranslatePlaceholderCP(StringCodePage(sarr[lowstart]));
  310. for i:=lowstart+1 to high(sarr) do
  311. begin
  312. { ignore the code page of empty strings, it will always be
  313. DefaultSystemCodePage but it doesn't matter for the outcome }
  314. if (sarr[i]<>'') and
  315. (tmpCP<>TranslatePlaceholderCP(StringCodePage(sarr[i]))) then
  316. begin
  317. sameCP:=false;
  318. break;
  319. end;
  320. end;
  321. if not sameCP then
  322. begin
  323. U:='';
  324. for i:=lowstart to high(sarr) do
  325. if sarr[i]<>'' then
  326. U:=U+UnicodeString(sarr[i]);
  327. DestS:='';
  328. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(U).toCharArray),DestS,DestCP,Length(U));
  329. exit;
  330. end;
  331. {$ifdef FPC_HAS_CPSTRING}
  332. { if the result is rawbytestring and all strings have the same code page,
  333. keep that code page }
  334. if cp=CP_NONE then
  335. DestCP:=tmpCP;
  336. {$endif FPC_HAS_CPSTRING}
  337. nonemptystart:=lowstart;
  338. { Check for another reuse, then we can't use
  339. the append optimization }
  340. if DestS<>'' then
  341. begin
  342. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  343. inc(lowstart);
  344. for i:=lowstart to high(sarr) do
  345. begin
  346. if Pointer(DestS)=Pointer(sarr[i]) then
  347. begin
  348. { if DestS is used somewhere in the middle of the expression,
  349. we need to make sure the original string still exists after
  350. we empty/modify DestS -- not necessary on JVM platform, ansistrings
  351. are not explicitly refrence counted there }
  352. lowstart:=nonemptystart;
  353. break;
  354. end;
  355. end;
  356. end;
  357. { Start with empty DestS if we start with concatting
  358. the first (non-empty) array element }
  359. if lowstart=nonemptystart then
  360. DestS:='';
  361. OldDestLen:=length(DestS);
  362. { Calculate size of the result so we can do
  363. a single call to SetLength() }
  364. NewLen:=0;
  365. for i:=low(sarr) to high(sarr) do
  366. inc(NewLen,length(sarr[i]));
  367. SetLength(DestS,NewLen);
  368. { Concat all strings, except the string we already
  369. copied in DestS }
  370. NewLen:=OldDestLen;
  371. for i:=lowstart to high(sarr) do
  372. begin
  373. p:=pointer(sarr[i]);
  374. if assigned(p) then
  375. begin
  376. Size:=length(ansistring(p));
  377. fpc_pchar_pchar_intern_charmove(pchar(ansistring(p)),0,pchar(DestS),NewLen,Size+1);
  378. inc(NewLen,size);
  379. end;
  380. end;
  381. if NewLen<>0 then
  382. begin
  383. SetCodePage(DestS,tmpCP,False);
  384. SetCodePage(DestS,DestCP,True);
  385. end;
  386. end;
  387. {$define FPC_HAS_ANSISTR_TO_SHORTSTR}
  388. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  389. {
  390. Converts a AnsiString to a ShortString;
  391. }
  392. Var
  393. Size : SizeInt;
  394. begin
  395. if S2='' then
  396. res:=''
  397. else
  398. begin
  399. Size:=Length(S2);
  400. If Size>high(res) then
  401. Size:=high(res);
  402. if Size>0 then
  403. JLSystem.ArrayCopy(JLObject(AnsistringClass(S2).fdata),0,JLObject(ShortstringClass(@res).fdata),0,Size);
  404. setlength(res,size);
  405. end;
  406. end;
  407. {$define FPC_HAS_PCHAR_TO_ANSISTR}
  408. Function fpc_PChar_To_AnsiStr(const p : PAnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  409. Var
  410. L : SizeInt;
  411. {$ifndef FPC_HAS_CPSTRING}
  412. cp : TSystemCodePage;
  413. {$endif FPC_HAS_CPSTRING}
  414. begin
  415. if (not assigned(p)) or (p[0]=#0) Then
  416. L := 0
  417. else
  418. L:=IndexChar(Arr1jbyte(p),-1,#0);
  419. SetLength(fpc_PChar_To_AnsiStr,L);
  420. if L > 0 then
  421. begin
  422. {$ifdef FPC_HAS_CPSTRING}
  423. cp:=TranslatePlaceholderCP(cp);
  424. {$else FPC_HAS_CPSTRING}
  425. cp:=DefaultSystemCodePage;
  426. {$endif FPC_HAS_CPSTRING}
  427. fpc_pchar_ansistr_intern_charmove(p,0,fpc_PChar_To_AnsiStr,0,L);
  428. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  429. end;
  430. end;
  431. {$define FPC_HAS_ANSISTR_TO_CHARARRAY}
  432. procedure fpc_ansistr_to_chararray(out res: array of AnsiChar; const src: RawByteString); compilerproc;
  433. var
  434. len: longint;
  435. begin
  436. len:=length(src);
  437. if len>length(res) then
  438. len:=length(res);
  439. { make sure we don't try to access element 1 of the ansistring if it's nil }
  440. if len>0 then
  441. JLSystem.ArrayCopy(JLObject(AnsistringClass(src).fdata),0,JLObject(@res),0,len);
  442. if len<=high(res) then
  443. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  444. end;
  445. function fpc_ansistr_setchar(const s: RawByteString; const index: longint; const ch: ansichar): RawByteString; compilerproc;
  446. var
  447. res: AnsistringClass;
  448. begin
  449. res:=AnsistringClass.Create(s,AnsistringClass(s).fCodePage);
  450. res.fdata[index-1]:=ch;
  451. result:=Ansistring(res);
  452. end;
  453. {$define FPC_HAS_ANSISTR_COMPARE}
  454. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  455. {
  456. Compares 2 AnsiStrings;
  457. The result is
  458. <0 if S1<S2
  459. 0 if S1=S2
  460. >0 if S1>S2
  461. }
  462. Var
  463. MaxI,Temp, i : SizeInt;
  464. cp1,cp2 : TSystemCodePage;
  465. r1,r2 : RawByteString;
  466. begin
  467. if JLObject(S1)=JLObject(S2) then
  468. begin
  469. result:=0;
  470. exit;
  471. end;
  472. if (pointer(S1)=nil) then
  473. begin
  474. result:=-Length(S2);
  475. exit;
  476. end;
  477. if (pointer(S2)=nil) then
  478. begin
  479. result:=Length(S1);
  480. exit;
  481. end;
  482. cp1:=StringCodePage(S1);
  483. cp2:=StringCodePage(S2);
  484. if cp1=cp2 then
  485. begin
  486. Maxi:=Length(S1);
  487. temp:=Length(S2);
  488. If MaxI>Temp then
  489. MaxI:=Temp;
  490. for i:=0 to MaxI-1 do
  491. begin
  492. result:=ord(AnsistringClass(S1).fdata[i])-ord(AnsistringClass(S2).fdata[i]);
  493. if result<>0 then
  494. exit;
  495. end;
  496. result:=Length(S1)-Length(S2);
  497. end
  498. else
  499. begin
  500. r1:=S1;
  501. cp1:=TranslatePlaceholderCP(cp1);
  502. if (cp1<>StringCodePage(r1)) then
  503. SetCodePage(r1,DefaultSystemCodePage,false);
  504. r2:=S2;
  505. if (cp2<>StringCodePage(r2)) then
  506. SetCodePage(r2,DefaultSystemCodePage,false);
  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 (pointer(S1)=nil) then
  533. begin
  534. result:=-Length(S2);
  535. exit;
  536. end;
  537. if (pointer(S2)=nil) then
  538. begin
  539. result:=Length(S1);
  540. exit;
  541. end;
  542. cp1:=StringCodePage(S1);
  543. cp2:=StringCodePage(S2);
  544. if cp1<>cp2 then
  545. begin
  546. r1:=S1;
  547. cp1:=TranslatePlaceholderCP(cp1);
  548. if (cp1<>StringCodePage(r1)) then
  549. SetCodePage(r1,DefaultSystemCodePage,false);
  550. r2:=S2;
  551. cp2:=TranslatePlaceholderCP(cp2);
  552. if (cp2<>StringCodePage(r2)) then
  553. SetCodePage(r2,DefaultSystemCodePage,false);
  554. //convert them to utf8 then compare
  555. SetCodePage(r1,65001);
  556. SetCodePage(r2,65001);
  557. end
  558. else
  559. begin
  560. r1:=s1;
  561. r2:=s2;
  562. end;
  563. result:=ord(not JUArrays.equals(TJByteArray(AnsistringClass(r1).fdata),TJByteArray(AnsistringClass(r2).fdata)))
  564. end;
  565. { not required, the JVM does the range checking for us }
  566. {$define FPC_HAS_ANSISTR_RANGECHECK}
  567. {$define FPC_HAS_ANSISTR_SETLENGTH}
  568. 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;
  569. {
  570. Sets The length of string S to L.
  571. Makes sure S is unique, and contains enough room.
  572. }
  573. var
  574. oldlen: longint;
  575. result: RawByteString;
  576. begin
  577. cp:=TranslatePlaceholderCP(cp);
  578. { no explicit reference counting possible -> can't reuse S because we don't
  579. know how many references exist to it }
  580. result:=RawByteString(AnsistringClass.Create(l,cp));
  581. oldlen:=length(s);
  582. if l>oldlen then
  583. l:=oldlen;
  584. if l>0 then
  585. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),0,JLObject(AnsistringClass(result).fdata),0,l);
  586. S:=result;
  587. end;
  588. {*****************************************************************************
  589. Public functions, In interface.
  590. *****************************************************************************}
  591. { lie, not needed }
  592. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  593. { can't implement reference counting since no control over what javacc-compiled
  594. code does with ansistrings -> always create a copy }
  595. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  596. procedure FPC_ANSISTR_UNIQUE(var s: AnsiString); inline;
  597. begin
  598. s:=ansistring(AnsistringClass.Create(s,AnsiStringClass(s).fCodePage));
  599. end;
  600. {$define FPC_HAS_ANSISTR_COPY}
  601. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt): RawByteString;compilerproc;
  602. var
  603. res: AnsistringClass;
  604. begin
  605. dec(index);
  606. if Index < 0 then
  607. Index := 0;
  608. { Check Size. Accounts for Zero-length S, the double check is needed because
  609. Size can be maxint and will get <0 when adding index }
  610. if (Size>Length(S)) or
  611. (Index+Size>Length(S)) then
  612. Size:=Length(S)-Index;
  613. If Size>0 then
  614. begin
  615. res:=AnsistringClass.Create;
  616. AnsistringClass(res).fcodepage:=AnsistringClass(S).fcodepage;
  617. { +1 for terminating #0 }
  618. setlength(res.fdata,size+1);
  619. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),index,JLObject(res.fdata),0,size);
  620. result:=ansistring(res);
  621. end;
  622. { default function result is empty string }
  623. end;
  624. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  625. Function Pos(Const Substr : ShortString; Const Source : RawByteString) : SizeInt;
  626. var
  627. i,j,k,MaxLen, SubstrLen : SizeInt;
  628. begin
  629. Pos:=0;
  630. SubstrLen:=Length(SubStr);
  631. if SubstrLen>0 then
  632. begin
  633. MaxLen:=Length(source)-Length(SubStr);
  634. i:=0;
  635. while (i<=MaxLen) do
  636. begin
  637. inc(i);
  638. j:=0;
  639. k:=i-1;
  640. while (j<SubstrLen) and
  641. (ShortStringClass(@SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  642. begin
  643. inc(j);
  644. inc(k);
  645. end;
  646. if (j=SubstrLen) then
  647. begin
  648. Pos:=i;
  649. exit;
  650. end;
  651. end;
  652. end;
  653. end;
  654. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  655. Function Pos(Const Substr : RawByteString; Const Source : RawByteString) : SizeInt;
  656. var
  657. i,j,k,MaxLen, SubstrLen : SizeInt;
  658. begin
  659. Pos:=0;
  660. SubstrLen:=Length(SubStr);
  661. if SubstrLen>0 then
  662. begin
  663. MaxLen:=Length(source)-Length(SubStr);
  664. i:=0;
  665. while (i<=MaxLen) do
  666. begin
  667. inc(i);
  668. j:=0;
  669. k:=i-1;
  670. while (j<SubstrLen) and
  671. (AnsistringClass(SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  672. begin
  673. inc(j);
  674. inc(k);
  675. end;
  676. if (j=SubstrLen) then
  677. begin
  678. Pos:=i;
  679. exit;
  680. end;
  681. end;
  682. end;
  683. end;
  684. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  685. { Faster version for a char alone. Must be implemented because }
  686. { pos(c: char; const s: shortstring) also exists, so otherwise }
  687. { using pos(char,pchar) will always call the shortstring version }
  688. { (exact match for first argument), also with $h+ (JM) }
  689. Function Pos (c : AnsiChar; Const s : RawByteString) : SizeInt;
  690. var
  691. i: SizeInt;
  692. begin
  693. for i:=1 to length(s) do
  694. begin
  695. if AnsistringClass(s).fdata[i-1]=c then
  696. begin
  697. pos:=i;
  698. exit;
  699. end;
  700. end;
  701. pos:=0;
  702. end;
  703. {$define FPC_HAS_ANSISTR_OF_CHAR}
  704. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  705. begin
  706. SetLength(StringOfChar,l);
  707. FillChar(AnsistringClass(result).fdata,l,c);
  708. end;
  709. {$define FPC_HAS_UPCASE_ANSISTR}
  710. function upcase(const s : ansistring) : ansistring;
  711. var
  712. u : unicodestring;
  713. begin
  714. u:=s;
  715. result:=upcase(u);
  716. end;
  717. {$define FPC_HAS_LOWERCASE_ANSISTR}
  718. function lowercase(const s : ansistring) : ansistring;
  719. var
  720. u : unicodestring;
  721. begin
  722. u:=s;
  723. result:=lowercase(u);
  724. end;
  725. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  726. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  727. begin
  728. if assigned(pointer(S)) then
  729. Result:=AnsistringClass(S).fCodePage
  730. else
  731. Result:=DefaultSystemCodePage;
  732. end;
  733. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  734. function StringElementSize(const S: RawByteString): Word; overload;
  735. begin
  736. if assigned(Pointer(S)) then
  737. Result:=AnsistringClass(S).fElementSize
  738. else
  739. Result:=SizeOf(AnsiChar);
  740. end;
  741. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  742. function StringRefCount(const S: RawByteString): SizeInt; overload;
  743. begin
  744. if assigned(Pointer(S)) then
  745. Result:=1
  746. else
  747. Result:=0;
  748. end;
  749. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  750. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  751. begin
  752. if not assigned(Pointer(S)) or (StringCodePage(S)=CodePage) then
  753. exit
  754. else if (AnsistringClass(S).length<>0) and
  755. Convert then
  756. begin
  757. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  758. end
  759. else
  760. begin
  761. UniqueString(s);
  762. AnsistringClass(S).fCodePage:=CodePage;
  763. end;
  764. end;