jastrings.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 AnsiChar 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 AnsiChar 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 AnsiChar 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 AnsiChar 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(PAnsiChar(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 PAnsiChar: 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: PAnsiChar; 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: PAnsiChar; const srcindex: sizeint; const dst: PAnsiChar; 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. {$ifndef FPC_HAS_ANSISTR_CONCAT}
  247. {$define FPC_HAS_ANSISTR_CONCAT}
  248. procedure fpc_AnsiStr_Concat (var DestS:RawByteString;const S1,S2 : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); compilerproc;
  249. Var
  250. S1Len, S2Len: SizeInt;
  251. same : boolean;
  252. S1CP, S2CP, DestCP: TSystemCodePage;
  253. begin
  254. {$ifdef FPC_HAS_CPSTRING}
  255. DestCP:=cp;
  256. if DestCp=CP_NONE then
  257. DestCP:=DefaultSystemCodePage;
  258. {$else FPC_HAS_CPSTRING}
  259. DestCP:=StringCodePage(DestS);
  260. {$endif FPC_HAS_CPSTRING}
  261. DestCP:=TranslatePlaceholderCP(DestCP);
  262. { if codepages are different then concat using unicodestring,
  263. but avoid conversions if either addend is empty (StringCodePage will return
  264. DefaultSystemCodePage in that case, which may differ from other addend/dest) }
  265. if Length(S1)=0 then
  266. S1CP:=DestCP
  267. else
  268. S1CP:=StringCodePage(S1);
  269. S1CP:=TranslatePlaceholderCP(S1CP);
  270. if Length(S2)=0 then
  271. S2CP:=DestCP
  272. else
  273. S2CP:=StringCodePage(S2);
  274. S2CP:=TranslatePlaceholderCP(S2CP);
  275. {$ifdef FPC_HAS_CPSTRING}
  276. { if the result is rawbytestring and both strings have the same code page,
  277. keep that code page or keep the code page if the other string is empty }
  278. if cp=CP_NONE then
  279. begin
  280. if (S1CP=S2CP) or (Length(S2)=0) then
  281. DestCP:=S1CP
  282. else if Length(S1)=0 then
  283. DestCP:=S2CP;
  284. end;
  285. {$endif FPC_HAS_CPSTRING}
  286. if ((S1CP<>DestCP) and (Length(s1)>0)) or ((S2CP<>DestCP) and (Length(s2)>0)) then
  287. begin
  288. ansistr_concat_complex(DestS,S1,S2,DestCP);
  289. exit;
  290. end;
  291. { only assign if s1 or s2 is empty }
  292. if (Length(S1)=0) then
  293. begin
  294. DestS:=s2;
  295. exit;
  296. end;
  297. if (Length(S2)=0) then
  298. begin
  299. DestS:=s1;
  300. exit;
  301. end;
  302. S1Len:=Length(S1);
  303. S2Len:=length(S2);
  304. { Use Pointer() typecasts to prevent extra conversion code }
  305. if Pointer(DestS)=Pointer(S1) then
  306. begin
  307. same:=Pointer(S1)=Pointer(S2);
  308. SetLength(DestS,S1Len+S2Len);
  309. if same then
  310. fpc_pchar_ansistr_intern_charmove(PAnsiChar(DestS),0,DestS,S1Len,S2Len)
  311. else
  312. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S2),0,DestS,S1Len,S2Len+1)
  313. end
  314. else if Pointer(DestS)=Pointer(S2) then
  315. begin
  316. SetLength(DestS,S1Len+S2Len);
  317. fpc_pchar_ansistr_intern_charmove(PAnsiChar(DestS),0,DestS,S1Len,S2Len+1);
  318. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S1),0,DestS,0,S1Len);
  319. end
  320. else
  321. begin
  322. SetLength(DestS,S1Len+S2Len);
  323. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S1),0,DestS,0,S1Len);
  324. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S2),0,DestS,S1Len,S2Len+1);
  325. end;
  326. SetCodePage(DestS,DestCP,false);
  327. end;
  328. {$endif FPC_HAS_ANSISTR_CONCAT}
  329. {$define FPC_HAS_ANSISTR_TO_ANSISTR}
  330. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; compilerproc;
  331. {
  332. Converts an AnsiString to an AnsiString taking code pages into care
  333. }
  334. Var
  335. Size : SizeInt;
  336. temp : UnicodeString;
  337. orgcp: TSystemCodePage;
  338. begin
  339. result:='';
  340. Size:=Length(S);
  341. if Size>0 then
  342. begin
  343. cp:=TranslatePlaceholderCP(cp);
  344. orgcp:=TranslatePlaceholderCP(StringCodePage(S));
  345. if (orgcp=cp) or (orgcp=CP_NONE) then
  346. begin
  347. result:=RawByteString(AnsistringClass.Create(S,cp));
  348. end
  349. else
  350. begin
  351. temp:=UnicodeString(S);
  352. Size:=Length(temp);
  353. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(temp).toCharArray),result,cp,Size);
  354. end;
  355. end;
  356. end;
  357. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [external name 'fpc_ansistr_to_ansistr'];
  358. {$define FPC_HAS_ANSISTR_CONCAT_MULTI}
  359. procedure fpc_AnsiStr_Concat_multi (var DestS:RawByteString;const sarr:array of RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); compilerproc;
  360. Var
  361. lowstart,
  362. nonemptystart,
  363. i : Longint;
  364. p : pointer;
  365. Size,NewLen,
  366. OldDestLen : SizeInt;
  367. destcopy : RawByteString;
  368. U : UnicodeString;
  369. DestCP : TSystemCodePage;
  370. tmpCP : TSystemCodePage;
  371. sameCP : Boolean;
  372. begin
  373. if high(sarr)=0 then
  374. begin
  375. DestS:='';
  376. exit;
  377. end;
  378. {$ifdef FPC_HAS_CPSTRING}
  379. DestCP:=cp;
  380. if DestCp=CP_NONE then
  381. DestCP:=DefaultSystemCodePage;
  382. {$else FPC_HAS_CPSTRING}
  383. DestCP:=StringCodePage(DestS);
  384. {$endif FPC_HAS_CPSTRING}
  385. lowstart:=low(sarr);
  386. { skip empty strings }
  387. while (lowstart<=high(sarr)) and
  388. (sarr[lowstart]='') do
  389. inc(lowstart);
  390. if lowstart>high(sarr) then
  391. begin
  392. DestS:=''; { All source strings empty }
  393. exit;
  394. end;
  395. DestCP:=TranslatePlaceholderCP(DestCP);
  396. sameCP:=true;
  397. tmpCP:=TranslatePlaceholderCP(StringCodePage(sarr[lowstart]));
  398. for i:=lowstart+1 to high(sarr) do
  399. begin
  400. { ignore the code page of empty strings, it will always be
  401. DefaultSystemCodePage but it doesn't matter for the outcome }
  402. if (sarr[i]<>'') and
  403. (tmpCP<>TranslatePlaceholderCP(StringCodePage(sarr[i]))) then
  404. begin
  405. sameCP:=false;
  406. break;
  407. end;
  408. end;
  409. if not sameCP then
  410. begin
  411. U:='';
  412. for i:=lowstart to high(sarr) do
  413. if sarr[i]<>'' then
  414. U:=U+UnicodeString(sarr[i]);
  415. DestS:='';
  416. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(JLString(U).toCharArray),DestS,DestCP,Length(U));
  417. exit;
  418. end;
  419. {$ifdef FPC_HAS_CPSTRING}
  420. { if the result is rawbytestring and all strings have the same code page,
  421. keep that code page }
  422. if cp=CP_NONE then
  423. DestCP:=tmpCP;
  424. {$endif FPC_HAS_CPSTRING}
  425. nonemptystart:=lowstart;
  426. { Check for another reuse, then we can't use
  427. the append optimization }
  428. if DestS<>'' then
  429. begin
  430. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  431. inc(lowstart);
  432. for i:=lowstart to high(sarr) do
  433. begin
  434. if Pointer(DestS)=Pointer(sarr[i]) then
  435. begin
  436. { if DestS is used somewhere in the middle of the expression,
  437. we need to make sure the original string still exists after
  438. we empty/modify DestS -- not necessary on JVM platform, ansistrings
  439. are not explicitly refrence counted there }
  440. lowstart:=nonemptystart;
  441. break;
  442. end;
  443. end;
  444. end;
  445. { Start with empty DestS if we start with concatting
  446. the first (non-empty) array element }
  447. if lowstart=nonemptystart then
  448. DestS:='';
  449. OldDestLen:=length(DestS);
  450. { Calculate size of the result so we can do
  451. a single call to SetLength() }
  452. NewLen:=0;
  453. for i:=nonemptystart to high(sarr) do
  454. inc(NewLen,length(sarr[i]));
  455. SetLength(DestS,NewLen);
  456. { Concat all strings, except the string we already
  457. copied in DestS }
  458. NewLen:=OldDestLen;
  459. for i:=lowstart to high(sarr) do
  460. begin
  461. p:=pointer(sarr[i]);
  462. if assigned(p) then
  463. begin
  464. Size:=length(ansistring(p));
  465. fpc_pchar_pchar_intern_charmove(PAnsiChar(ansistring(p)),0,PAnsiChar(DestS),NewLen,Size+1);
  466. inc(NewLen,size);
  467. end;
  468. end;
  469. if NewLen<>0 then
  470. begin
  471. SetCodePage(DestS,tmpCP,False);
  472. SetCodePage(DestS,DestCP,True);
  473. end;
  474. end;
  475. {$define FPC_HAS_ANSISTR_TO_SHORTSTR}
  476. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  477. {
  478. Converts a AnsiString to a ShortString;
  479. }
  480. Var
  481. Size : SizeInt;
  482. begin
  483. if S2='' then
  484. res:=''
  485. else
  486. begin
  487. Size:=Length(S2);
  488. If Size>high(res) then
  489. Size:=high(res);
  490. if Size>0 then
  491. JLSystem.ArrayCopy(JLObject(AnsistringClass(S2).fdata),0,JLObject(ShortstringClass(@res).fdata),0,Size);
  492. setlength(res,size);
  493. end;
  494. end;
  495. {$define FPC_HAS_PCHAR_TO_ANSISTR}
  496. Function fpc_PChar_To_AnsiStr(const p : PAnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  497. Var
  498. L : SizeInt;
  499. {$ifndef FPC_HAS_CPSTRING}
  500. cp : TSystemCodePage;
  501. {$endif FPC_HAS_CPSTRING}
  502. begin
  503. if (not assigned(p)) or (p[0]=#0) Then
  504. L := 0
  505. else
  506. L:=IndexChar(Arr1jbyte(p),-1,#0);
  507. SetLength(fpc_PChar_To_AnsiStr,L);
  508. if L > 0 then
  509. begin
  510. {$ifdef FPC_HAS_CPSTRING}
  511. cp:=TranslatePlaceholderCP(cp);
  512. {$else FPC_HAS_CPSTRING}
  513. cp:=DefaultSystemCodePage;
  514. {$endif FPC_HAS_CPSTRING}
  515. fpc_pchar_ansistr_intern_charmove(p,0,fpc_PChar_To_AnsiStr,0,L);
  516. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  517. end;
  518. end;
  519. {$define FPC_HAS_ANSISTR_TO_CHARARRAY}
  520. procedure fpc_ansistr_to_chararray(out res: array of AnsiChar; const src: RawByteString); compilerproc;
  521. var
  522. len: longint;
  523. begin
  524. len:=length(src);
  525. if len>length(res) then
  526. len:=length(res);
  527. { make sure we don't try to access element 1 of the ansistring if it's nil }
  528. if len>0 then
  529. JLSystem.ArrayCopy(JLObject(AnsistringClass(src).fdata),0,JLObject(@res),0,len);
  530. if len<=high(res) then
  531. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  532. end;
  533. function fpc_ansistr_setchar(const s: RawByteString; const index: longint; const ch: ansichar): RawByteString; compilerproc;
  534. var
  535. res: AnsistringClass;
  536. begin
  537. res:=AnsistringClass.Create(s,AnsistringClass(s).fCodePage);
  538. res.fdata[index-1]:=ch;
  539. result:=Ansistring(res);
  540. end;
  541. {$define FPC_HAS_ANSISTR_COMPARE}
  542. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  543. {
  544. Compares 2 AnsiStrings;
  545. The result is
  546. <0 if S1<S2
  547. 0 if S1=S2
  548. >0 if S1>S2
  549. }
  550. Var
  551. MaxI,Temp, i : SizeInt;
  552. cp1,cp2 : TSystemCodePage;
  553. r1,r2 : RawByteString;
  554. begin
  555. if JLObject(S1)=JLObject(S2) then
  556. begin
  557. result:=0;
  558. exit;
  559. end;
  560. if (pointer(S1)=nil) then
  561. begin
  562. result:=-Length(S2);
  563. exit;
  564. end;
  565. if (pointer(S2)=nil) then
  566. begin
  567. result:=Length(S1);
  568. exit;
  569. end;
  570. cp1:=TranslatePlaceholderCP(StringCodePage(S1));
  571. cp2:=TranslatePlaceholderCP(StringCodePage(S2));
  572. if cp1=cp2 then
  573. begin
  574. Maxi:=Length(S1);
  575. temp:=Length(S2);
  576. If MaxI>Temp then
  577. MaxI:=Temp;
  578. for i:=0 to MaxI-1 do
  579. begin
  580. result:=ord(AnsistringClass(S1).fdata[i])-ord(AnsistringClass(S2).fdata[i]);
  581. if result<>0 then
  582. exit;
  583. end;
  584. result:=Length(S1)-Length(S2);
  585. end
  586. else
  587. begin
  588. r1:=S1;
  589. r2:=S2;
  590. //convert them to utf8 then compare
  591. SetCodePage(r1,65001);
  592. SetCodePage(r2,65001);
  593. Result:=fpc_AnsiStr_Compare(r1,r2);
  594. end;
  595. end;
  596. {$define FPC_HAS_ANSISTR_COMPARE_EQUAL}
  597. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt; compilerproc;
  598. {
  599. Compares 2 AnsiStrings for equality/inequality only;
  600. The result is
  601. 0 if S1=S2
  602. <>0 if S1<>S2
  603. }
  604. Var
  605. MaxI,Temp : SizeInt;
  606. cp1,cp2 : TSystemCodePage;
  607. r1,r2 : RawByteString;
  608. begin
  609. if JLObject(S1)=JLObject(S2) then
  610. begin
  611. result:=0;
  612. exit;
  613. end;
  614. { don't compare strings if one of them is empty }
  615. if (length(S1)=0) then
  616. begin
  617. { in the JVM, one string may be nil and the other may be empty -> the jlobject()
  618. equals check may have failed even if both strings are technically empty }
  619. result:=ord(length(S2)<>0);
  620. exit;
  621. end;
  622. if (length(S2)=0) then
  623. begin
  624. { length(S1)<>0, we checked that above }
  625. result:=1;
  626. exit;
  627. end;
  628. cp1:=TranslatePlaceholderCP(StringCodePage(S1));
  629. cp2:=TranslatePlaceholderCP(StringCodePage(S2));
  630. if cp1=cp2 then
  631. begin
  632. r1:=s1;
  633. r2:=s2;
  634. end
  635. else
  636. begin
  637. r1:=S1;
  638. r2:=S2;
  639. //convert them to utf8 then compare
  640. SetCodePage(r1,65001);
  641. SetCodePage(r2,65001);
  642. end;
  643. result:=ord(not JUArrays.equals(TJByteArray(AnsistringClass(r1).fdata),TJByteArray(AnsistringClass(r2).fdata)))
  644. end;
  645. { not required, the JVM does the range checking for us }
  646. {$define FPC_HAS_ANSISTR_RANGECHECK}
  647. {$define FPC_HAS_ANSISTR_SETLENGTH}
  648. 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;
  649. {
  650. Sets The length of string S to L.
  651. Makes sure S is unique, and contains enough room.
  652. }
  653. var
  654. oldlen: longint;
  655. result: RawByteString;
  656. begin
  657. cp:=TranslatePlaceholderCP(cp);
  658. { no explicit reference counting possible -> can't reuse S because we don't
  659. know how many references exist to it }
  660. result:=RawByteString(AnsistringClass.Create(l,cp));
  661. oldlen:=length(s);
  662. if l>oldlen then
  663. l:=oldlen;
  664. if l>0 then
  665. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),0,JLObject(AnsistringClass(result).fdata),0,l);
  666. S:=result;
  667. end;
  668. {*****************************************************************************
  669. Public functions, In interface.
  670. *****************************************************************************}
  671. { lie, not needed }
  672. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  673. { can't implement reference counting since no control over what javacc-compiled
  674. code does with ansistrings -> always create a copy }
  675. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  676. function FPC_ANSISTR_UNIQUE(var s: AnsiString): pointer; inline;
  677. begin
  678. s:=ansistring(AnsistringClass.Create(s,AnsiStringClass(s).fCodePage));
  679. result:=pointer(s);
  680. end;
  681. {$define FPC_HAS_ANSISTR_COPY}
  682. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt): RawByteString;compilerproc;
  683. var
  684. res: AnsistringClass;
  685. begin
  686. result:='';
  687. dec(index);
  688. if Index < 0 then
  689. Index := 0;
  690. { Check Size. Accounts for Zero-length S, the double check is needed because
  691. Size can be maxint and will get <0 when adding index }
  692. if (Size>Length(S)) or
  693. (Index+Size>Length(S)) then
  694. Size:=Length(S)-Index;
  695. If Size>0 then
  696. begin
  697. res:=AnsistringClass.Create;
  698. AnsistringClass(res).fcodepage:=AnsistringClass(S).fcodepage;
  699. { +1 for terminating #0 }
  700. setlength(res.fdata,size+1);
  701. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),index,JLObject(res.fdata),0,size);
  702. result:=ansistring(res);
  703. end;
  704. end;
  705. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  706. Function Pos(Const Substr : ShortString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  707. var
  708. i,j,k,MaxLen, SubstrLen : SizeInt;
  709. begin
  710. Pos:=0;
  711. SubstrLen:=Length(SubStr);
  712. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  713. begin
  714. MaxLen:=Length(source)-Length(SubStr);
  715. i:=Offset-1;
  716. while (i<=MaxLen) do
  717. begin
  718. inc(i);
  719. j:=0;
  720. k:=i-1;
  721. while (j<SubstrLen) and
  722. (ShortStringClass(@SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  723. begin
  724. inc(j);
  725. inc(k);
  726. end;
  727. if (j=SubstrLen) then
  728. begin
  729. Pos:=i;
  730. exit;
  731. end;
  732. end;
  733. end;
  734. end;
  735. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  736. Function Pos(Const Substr : RawByteString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  737. var
  738. i,j,k,MaxLen, SubstrLen : SizeInt;
  739. begin
  740. Pos:=0;
  741. SubstrLen:=Length(SubStr);
  742. if (SubstrLen>0) and (Offset>0) and (Offset<=Length(Source)) then
  743. begin
  744. MaxLen:=Length(source)-Length(SubStr);
  745. i:=Offset-1;
  746. while (i<=MaxLen) do
  747. begin
  748. inc(i);
  749. j:=0;
  750. k:=i-1;
  751. while (j<SubstrLen) and
  752. (AnsistringClass(SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  753. begin
  754. inc(j);
  755. inc(k);
  756. end;
  757. if (j=SubstrLen) then
  758. begin
  759. Pos:=i;
  760. exit;
  761. end;
  762. end;
  763. end;
  764. end;
  765. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  766. { Faster version for a AnsiChar alone. Must be implemented because }
  767. { pos(c: AnsiChar; const s: shortstring) also exists, so otherwise }
  768. { using pos(AnsiChar,PAnsiChar) will always call the shortstring version }
  769. { (exact match for first argument), also with $h+ (JM) }
  770. Function Pos(c : AnsiChar; Const s : RawByteString; Offset : Sizeint = 1) : SizeInt;var
  771. i: SizeInt;
  772. begin
  773. Pos:=0;
  774. If (Offset<1) or (Offset>Length(S)) then
  775. exit;
  776. for i:=Offset to length(s) do
  777. begin
  778. if AnsistringClass(s).fdata[i-1]=c then
  779. begin
  780. pos:=i;
  781. exit;
  782. end;
  783. end;
  784. end;
  785. {$define FPC_HAS_ANSISTR_OF_CHAR}
  786. Function StringOfChar(c : Ansichar;l : SizeInt) : AnsiString;
  787. begin
  788. SetLength(StringOfChar,l);
  789. FillChar(AnsistringClass(result).fdata,l,c);
  790. end;
  791. {$define FPC_HAS_UPCASE_ANSISTR}
  792. function upcase(const s : ansistring) : ansistring;
  793. var
  794. u : unicodestring;
  795. begin
  796. u:=s;
  797. result:=upcase(u);
  798. end;
  799. {$define FPC_HAS_LOWERCASE_ANSISTR}
  800. function lowercase(const s : ansistring) : ansistring;
  801. var
  802. u : unicodestring;
  803. begin
  804. u:=s;
  805. result:=lowercase(u);
  806. end;
  807. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  808. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  809. begin
  810. if assigned(pointer(S)) then
  811. Result:=AnsistringClass(S).fCodePage
  812. else
  813. Result:=DefaultSystemCodePage;
  814. end;
  815. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  816. function StringElementSize(const S: RawByteString): Word; overload;
  817. begin
  818. if assigned(Pointer(S)) then
  819. Result:=AnsistringClass(S).fElementSize
  820. else
  821. Result:=SizeOf(AnsiChar);
  822. end;
  823. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  824. function StringRefCount(const S: RawByteString): SizeInt; overload;
  825. begin
  826. if assigned(Pointer(S)) then
  827. Result:=1
  828. else
  829. Result:=0;
  830. end;
  831. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  832. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  833. begin
  834. if not assigned(Pointer(S)) or (StringCodePage(S)=CodePage) then
  835. exit
  836. else if (AnsistringClass(S).length<>0) and
  837. Convert then
  838. begin
  839. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  840. end
  841. else
  842. begin
  843. UniqueString(s);
  844. AnsistringClass(S).fCodePage:=CodePage;
  845. end;
  846. end;