jastrings.inc 25 KB

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