jastrings.inc 25 KB

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