astrings.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. constructor AnsistringClass.Create(const arr: array of ansichar);
  15. begin
  16. { make explicit copy so that changing the array afterwards doesn't change
  17. the string }
  18. if high(arr)=-1 then
  19. exit;
  20. setlength(fdata,high(arr)+1);
  21. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(fdata),0,high(arr)+1);
  22. end;
  23. constructor AnsistringClass.Create(const arr: array of unicodechar);
  24. begin
  25. if high(arr)=-1 then
  26. exit;
  27. fdata:=TAnsiCharArray(JLString.Create(arr).getBytes);
  28. end;
  29. constructor AnsistringClass.Create(const u: unicodestring);
  30. begin
  31. if system.length(u)=0 then
  32. exit;
  33. fdata:=TAnsiCharArray(JLString(u).getBytes);
  34. end;
  35. constructor AnsistringClass.Create(const a: ansistring);
  36. begin
  37. Create(AnsistringClass(a).fdata);
  38. end;
  39. constructor AnsistringClass.Create(ch: ansichar);
  40. begin
  41. setlength(fdata,1);
  42. fdata[0]:=ch;
  43. end;
  44. constructor AnsistringClass.Create(ch: unicodechar);
  45. begin
  46. fdata:=TAnsiCharArray(JLString.Create(ch).getBytes);
  47. end;
  48. class function AnsistringClass.CreateFromLiteralStringBytes(const u: unicodestring): ansistring;
  49. var
  50. res: AnsistringClass;
  51. i: longint;
  52. begin
  53. { used to construct constant ansistrings from Java string constants }
  54. res:=AnsistringClass.Create;
  55. setlength(res.fdata,system.length(u));
  56. for i:=1 to system.length(u) do
  57. res.fdata[i-1]:=ansichar(ord(u[i]));
  58. result:=ansistring(res);
  59. end;
  60. function AnsistringClass.charAt(index: jint): ansichar;
  61. begin
  62. { index is already decreased by one, because same calling code is used for
  63. JLString.charAt() }
  64. result:=fdata[index];
  65. end;
  66. function AnsistringClass.toUnicodeString: unicodestring;
  67. begin
  68. result:=UnicodeString(JLString.Create(TJByteArray(fdata)));
  69. end;
  70. function AnsistringClass.toString: JLString;
  71. begin
  72. result:=JLString.Create(TJByteArray(fdata));
  73. end;
  74. (*
  75. function AnsistringClass.concat(const a: ansistring): ansistring;
  76. var
  77. newdata: array of ansichar;
  78. addlen: sizeint;
  79. begin
  80. addlen:=length(a);
  81. thislen:=this.length;
  82. setlength(newdata,addlen+thislen);
  83. if thislen>0 then
  84. JLSystem.ArrayCopy(JLObject(fdata),0,JLObject(newdata),0,thislen);
  85. if addlen>0 then
  86. JLSystem.ArrayCopy(JLObject(AnsistringClass(a).fdata),0,JLObject(newdata),thislen,addlen);
  87. end;
  88. procedure AnsistringClass.concatmultiple(const arr: array of ansistring): ansistring;
  89. Var
  90. i : longint;
  91. size, newsize : sizeint;
  92. curlen, addlen : sizeint
  93. newdata: array of ansichar;
  94. begin
  95. { First calculate size of the result so we can allocate an array of
  96. the right size }
  97. NewSize:=0;
  98. for i:=low(arr) to high(arr) do
  99. inc(newsize,length(arr[i]));
  100. setlength(newdata,newsize);
  101. curlen
  102. for i:=low(arr) to high(arr) do
  103. begin
  104. if length(arr[i])>0 then
  105. sb.append(arr[i]);
  106. end;
  107. DestS:=sb.toString;
  108. end;
  109. *)
  110. function AnsiStringClass.length: jint;
  111. begin
  112. result:=system.length(fdata);
  113. end;
  114. {****************************************************************************
  115. Internal functions, not in interface.
  116. ****************************************************************************}
  117. function fpc_AnsiStr_Concat (const S1,S2 : AnsiString): ansistring; compilerproc;
  118. var
  119. newdata: array of ansichar;
  120. thislen, addlen: sizeint;
  121. begin
  122. thislen:=length(s1);
  123. addlen:=length(s2);
  124. setlength(newdata,thislen+addlen);
  125. if thislen>0 then
  126. JLSystem.ArrayCopy(JLObject(AnsistringClass(s1).fdata),0,JLObject(newdata),0,thislen);
  127. if addlen>0 then
  128. JLSystem.ArrayCopy(JLObject(AnsistringClass(s2).fdata),0,JLObject(newdata),thislen,addlen);
  129. result:=Ansistring(AnsistringClass.Create);
  130. AnsistringClass(result).fdata:=newdata;
  131. end;
  132. procedure fpc_AnsiStr_Concat_multi (var DestS:Ansistring;const sarr:array of Ansistring); compilerproc;
  133. Var
  134. i : longint;
  135. size, newsize : sizeint;
  136. curlen, addlen, nextlen : sizeint;
  137. newdata: array of ansichar;
  138. res : AnsistringClass;
  139. begin
  140. { First calculate size of the result so we can allocate an array of
  141. the right size }
  142. NewSize:=0;
  143. for i:=low(sarr) to high(sarr) do
  144. inc(newsize,length(sarr[i]));
  145. setlength(newdata,newsize);
  146. curlen:=0;
  147. for i:=low(sarr) to high(sarr) do
  148. begin
  149. nextlen:=length(sarr[i]);
  150. if nextlen>0 then
  151. begin
  152. JLSystem.ArrayCopy(JLObject(AnsistringClass(sarr[i]).fdata),0,JLObject(newdata),curlen,nextlen);
  153. inc(curlen,nextlen);
  154. end;
  155. end;
  156. res:=AnsistringClass.Create;
  157. res.fdata:=newdata;
  158. dests:=Ansistring(res);
  159. end;
  160. (*
  161. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  162. { the following declaration has exactly the same effect as }
  163. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  164. { which is what the old helper was, so we don't need an extra implementation }
  165. { of the old helper (JM) }
  166. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  167. {
  168. Converts a AnsiString to a ShortString;
  169. }
  170. Var
  171. Size : SizeInt;
  172. begin
  173. if S2='' then
  174. fpc_AnsiStr_To_ShortStr:=''
  175. else
  176. begin
  177. Size:=Length(S2);
  178. If Size>high_of_res then
  179. Size:=high_of_res;
  180. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  181. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  182. end;
  183. end;
  184. {$else FPC_STRTOSHORTSTRINGPROC}
  185. *)
  186. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : Ansistring);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  187. {
  188. Converts a AnsiString to a ShortString;
  189. }
  190. (*
  191. Var
  192. Size : SizeInt;
  193. *)
  194. begin
  195. (*
  196. if S2='' then
  197. res:=''
  198. else
  199. begin
  200. Size:=Length(S2);
  201. If Size>high(res) then
  202. Size:=high(res);
  203. Move (S2[1],res[1],Size);
  204. byte(res[0]):=byte(Size);
  205. end;
  206. *)
  207. end;
  208. (*
  209. {$endif FPC_STRTOSHORTSTRINGPROC}
  210. *)
  211. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; compilerproc;
  212. {
  213. Converts a ShortString to a AnsiString;
  214. }
  215. (*
  216. Var
  217. Size : SizeInt;
  218. *)
  219. begin
  220. (*
  221. Size:=Length(S2);
  222. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  223. if Size>0 then
  224. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  225. *)
  226. end;
  227. Function fpc_Char_To_AnsiStr(const c : AnsiChar): AnsiString; compilerproc;
  228. {
  229. Converts a Char to a AnsiString;
  230. }
  231. begin
  232. result:=ansistring(AnsistringClass.Create(c));
  233. end;
  234. (*
  235. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; compilerproc;
  236. Var
  237. L : SizeInt;
  238. begin
  239. if (not assigned(p)) or (p[0]=#0) Then
  240. L := 0
  241. else
  242. l:=IndexChar(p^,-1,#0);
  243. SetLength(fpc_PChar_To_AnsiStr,L);
  244. if L > 0 then
  245. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  246. end;
  247. *)
  248. Function fpc_CharArray_To_AnsiStr(const arr: array of ansichar; zerobased: boolean = true): ansistring; compilerproc;
  249. var
  250. i,j : SizeInt;
  251. localarr: array of jbyte;
  252. foundnull: boolean;
  253. res: AnsistringClass;
  254. begin
  255. if (zerobased) then
  256. begin
  257. if (arr[0]=#0) Then
  258. begin
  259. fpc_CharArray_To_AnsiStr := '';
  260. exit;
  261. end;
  262. foundnull:=false;
  263. for i:=low(arr) to high(arr) do
  264. if arr[i]=#0 then
  265. begin
  266. foundnull:=true;
  267. break;
  268. end;
  269. if not foundnull then
  270. begin
  271. res:=AnsistringClass.Create(arr);
  272. exit;
  273. end
  274. end
  275. else
  276. begin
  277. res:=AnsistringClass.Create(arr);
  278. exit;
  279. end;
  280. res:=AnsistringClass.Create;
  281. setlength(res.fdata,i);
  282. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(res.fdata),0,i);
  283. result:=Ansistring(res);
  284. end;
  285. procedure fpc_ansistr_to_chararray(out res: array of ansichar; const src: ansistring); compilerproc;
  286. var
  287. i, len: SizeInt;
  288. begin
  289. len := length(src);
  290. if len > length(res) then
  291. len := length(res);
  292. { make sure we don't try to access element 1 of the ansistring if it's nil }
  293. if len > 0 then
  294. JLSystem.ArrayCopy(JLObject(AnsistringClass(src).fdata),0,JLObject(@res),0,len);
  295. for i:=len to length(res) do
  296. res[i]:=#0;
  297. end;
  298. function fpc_ansistr_setchar(const s: AnsiString; const index: longint; const ch: ansichar): AnsiString; compilerproc;
  299. var
  300. res: AnsistringClass;
  301. begin
  302. res:=AnsistringClass.Create(s);
  303. res.fdata[index-1]:=ch;
  304. result:=Ansistring(res);
  305. end;
  306. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt; compilerproc;
  307. {
  308. Compares 2 AnsiStrings;
  309. The result is
  310. <0 if S1<S2
  311. 0 if S1=S2
  312. >0 if S1>S2
  313. }
  314. Var
  315. MaxI,Temp, i : SizeInt;
  316. begin
  317. if JLObject(S1)=JLObject(S2) then
  318. begin
  319. result:=0;
  320. exit;
  321. end;
  322. Maxi:=Length(S1);
  323. temp:=Length(S2);
  324. If MaxI>Temp then
  325. MaxI:=Temp;
  326. if MaxI>0 then
  327. begin
  328. for i:=0 to MaxI-1 do
  329. begin
  330. result:=ord(AnsistringClass(S1).fdata[i])-ord(AnsistringClass(S2).fdata[i]);
  331. if result<>0 then
  332. exit;
  333. end;
  334. result:=Length(S1)-Length(S2);
  335. end
  336. else
  337. result:=Length(S1)-Length(S2);
  338. end;
  339. Function fpc_AnsiStr_Compare_equal(const S1,S2 : AnsiString): SizeInt; compilerproc;
  340. {
  341. Compares 2 AnsiStrings for equality/inequality only;
  342. The result is
  343. 0 if S1=S2
  344. <>0 if S1<>S2
  345. }
  346. Var
  347. MaxI,Temp : SizeInt;
  348. begin
  349. if JLObject(S1)=JLObject(S2) then
  350. begin
  351. result:=0;
  352. exit;
  353. end;
  354. result:=ord(not JUArrays.equals(TJByteArray(AnsistringClass(S1).fdata),TJByteArray(AnsistringClass(S2).fdata)));
  355. end;
  356. function fpc_AnsiStr_SetLength (S : AnsiString; l : SizeInt): Ansistring; compilerproc;
  357. {
  358. Sets The length of string S to L.
  359. Makes sure S is unique, and contains enough room.
  360. }
  361. Var
  362. lens, lena,
  363. movelen : SizeInt;
  364. begin
  365. setlength(AnsistringClass(s).fdata,l);
  366. result:=s;
  367. end;
  368. {*****************************************************************************
  369. Public functions, In interface.
  370. *****************************************************************************}
  371. (*
  372. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  373. Var
  374. SNew : Pointer;
  375. L : SizeInt;
  376. begin
  377. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  378. SNew:=NewAnsiString (L);
  379. Move (Pointer(S)^,SNew^,L+1);
  380. PAnsiRec(SNew-FirstOff)^.len:=L;
  381. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  382. pointer(S):=SNew;
  383. pointer(result):=SNew;
  384. end;
  385. *)
  386. (*
  387. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  388. // MV: inline the basic checks for case that S is already unique.
  389. // Rest is too complex to inline, so factor that out as a call.
  390. Function fpc_ansistr_Unique(Var S : jlobject): jlobject; compilerproc;
  391. {
  392. Make sure reference count of S is 1,
  393. using copy-on-write semantics.
  394. }
  395. begin
  396. pointer(result) := pointer(s);
  397. If Pointer(S)=Nil then
  398. exit;
  399. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  400. result:=fpc_truely_ansistr_unique(s);
  401. end;
  402. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  403. *)
  404. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : ansichar); compilerproc;
  405. var
  406. curlen: sizeint;
  407. begin
  408. curlen:=length(s);
  409. SetLength(s,curlen+1);
  410. AnsistringClass(s).fdata[curlen]:=c;
  411. end;
  412. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); compilerproc;
  413. (*
  414. var
  415. ofs : SizeInt;
  416. *)
  417. begin
  418. (*
  419. if Str='' then
  420. exit;
  421. ofs:=Length(S);
  422. SetLength(S,ofs+length(Str));
  423. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  424. move(Str[1],(pointer(S)+ofs)^,length(Str));
  425. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  426. *)
  427. end;
  428. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); compilerproc;
  429. var
  430. ofs, strlength: longint;
  431. begin
  432. if Str='' then
  433. exit;
  434. strlength:=length(str);
  435. ofs:=Length(S);
  436. { no problem if s and str are the same string, because "var" parameters are
  437. copy-in/out for ansistring }
  438. SetLength(S,ofs+strlength);
  439. JLSystem.ArrayCopy(JLObject(AnsistringClass(Str).fdata),0,JLObject(AnsistringClass(S).fdata),ofs,strlength);
  440. end;
  441. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  442. var
  443. res: AnsistringClass;
  444. begin
  445. dec(index);
  446. if Index < 0 then
  447. Index := 0;
  448. { Check Size. Accounts for Zero-length S, the double check is needed because
  449. Size can be maxint and will get <0 when adding index }
  450. if (Size>Length(S)) or
  451. (Index+Size>Length(S)) then
  452. Size:=Length(S)-Index;
  453. If Size>0 then
  454. begin
  455. res:=AnsistringClass.Create;
  456. setlength(res.fdata,size);
  457. JLSystem.ArrayCopy(JLObject(AnsistringClass(S).fdata),index,JLObject(res.fdata),0,size);
  458. result:=ansistring(res);
  459. end;
  460. { default function result is empty string }
  461. end;
  462. (*
  463. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  464. var
  465. i,MaxLen : SizeInt;
  466. pc : pchar;
  467. begin
  468. Pos:=0;
  469. if Length(SubStr)>0 then
  470. begin
  471. MaxLen:=Length(source)-Length(SubStr);
  472. i:=0;
  473. pc:=@source[1];
  474. while (i<=MaxLen) do
  475. begin
  476. inc(i);
  477. if (SubStr[1]=pc^) and
  478. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  479. begin
  480. Pos:=i;
  481. exit;
  482. end;
  483. inc(pc);
  484. end;
  485. end;
  486. end;
  487. *)
  488. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  489. var
  490. i,j,k,MaxLen, SubstrLen : SizeInt;
  491. begin
  492. Pos:=0;
  493. SubstrLen:=Length(SubStr);
  494. if SubstrLen>0 then
  495. begin
  496. MaxLen:=Length(source)-Length(SubStr);
  497. i:=0;
  498. while (i<=MaxLen) do
  499. begin
  500. inc(i);
  501. j:=0;
  502. k:=i-1;
  503. while (j<SubstrLen) and
  504. (AnsistringClass(SubStr).fdata[j]=AnsistringClass(Source).fdata[k]) do
  505. begin
  506. inc(j);
  507. inc(k);
  508. end;
  509. if (j=SubstrLen) then
  510. begin
  511. Pos:=i;
  512. exit;
  513. end;
  514. end;
  515. end;
  516. end;
  517. { Faster version for a char alone. Must be implemented because }
  518. { pos(c: char; const s: shortstring) also exists, so otherwise }
  519. { using pos(char,pchar) will always call the shortstring version }
  520. { (exact match for first argument), also with $h+ (JM) }
  521. Function Pos (c : AnsiChar; Const s : AnsiString) : SizeInt;
  522. var
  523. i: SizeInt;
  524. begin
  525. for i:=1 to length(s) do
  526. begin
  527. if AnsistringClass(s).fdata[i-1]=c then
  528. begin
  529. pos:=i;
  530. exit;
  531. end;
  532. end;
  533. pos:=0;
  534. end;
  535. (*
  536. {$ifndef FPUNONE}
  537. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  538. Var
  539. SS : String;
  540. begin
  541. fpc_Val_Real_AnsiStr := 0;
  542. if length(S) > 255 then
  543. code := 256
  544. else
  545. begin
  546. SS := S;
  547. Val(SS,fpc_Val_Real_AnsiStr,code);
  548. end;
  549. end;
  550. {$endif}
  551. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  552. Var
  553. SS : String;
  554. begin
  555. if length(S) > 255 then
  556. begin
  557. fpc_Val_Currency_AnsiStr := 0;
  558. code := 256;
  559. end
  560. else
  561. begin
  562. SS := S;
  563. Val(SS,fpc_Val_Currency_AnsiStr,code);
  564. end;
  565. end;
  566. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  567. Var
  568. SS : ShortString;
  569. begin
  570. fpc_Val_UInt_AnsiStr := 0;
  571. if length(S) > 255 then
  572. code := 256
  573. else
  574. begin
  575. SS := S;
  576. Val(SS,fpc_Val_UInt_AnsiStr,code);
  577. end;
  578. end;
  579. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  580. Var
  581. SS : ShortString;
  582. begin
  583. fpc_Val_SInt_AnsiStr:=0;
  584. if length(S)>255 then
  585. code:=256
  586. else
  587. begin
  588. SS := S;
  589. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  590. end;
  591. end;
  592. {$ifndef CPU64}
  593. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  594. Var
  595. SS : ShortString;
  596. begin
  597. fpc_Val_qword_AnsiStr:=0;
  598. if length(S)>255 then
  599. code:=256
  600. else
  601. begin
  602. SS := S;
  603. Val(SS,fpc_Val_qword_AnsiStr,Code);
  604. end;
  605. end;
  606. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  607. Var
  608. SS : ShortString;
  609. begin
  610. fpc_Val_int64_AnsiStr:=0;
  611. if length(S)>255 then
  612. code:=256
  613. else
  614. begin
  615. SS := s;
  616. Val(SS,fpc_Val_int64_AnsiStr,Code);
  617. end;
  618. end;
  619. {$endif CPU64}
  620. {$ifndef FPUNONE}
  621. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  622. var
  623. ss: ShortString;
  624. begin
  625. str_real(len,fr,d,treal_type(rt),ss);
  626. s:=ss;
  627. end;
  628. {$endif}
  629. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:ansistring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  630. var ss:shortstring;
  631. begin
  632. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  633. s:=ss;
  634. end;
  635. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:ansistring);[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  636. var
  637. ss:shortstring;
  638. begin
  639. fpc_shortstr_bool(b,len,ss);
  640. s:=ss;
  641. end;
  642. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:ansistring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  643. begin
  644. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  645. end;
  646. {$ifdef FPC_HAS_STR_CURRENCY}
  647. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  648. var
  649. ss: ShortString;
  650. begin
  651. str(c:len:fr,ss);
  652. s:=ss;
  653. end;
  654. {$endif FPC_HAS_STR_CURRENCY}
  655. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  656. Var
  657. SS : ShortString;
  658. begin
  659. str(v:Len,SS);
  660. S:=SS;
  661. end;
  662. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  663. Var
  664. SS : ShortString;
  665. begin
  666. str (v:Len,SS);
  667. S:=SS;
  668. end;
  669. {$ifndef CPU64}
  670. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  671. Var
  672. SS : ShortString;
  673. begin
  674. str(v:Len,SS);
  675. S:=SS;
  676. end;
  677. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  678. Var
  679. SS : ShortString;
  680. begin
  681. str (v:Len,SS);
  682. S:=SS;
  683. end;
  684. {$endif CPU64}
  685. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  686. Var
  687. LS : SizeInt;
  688. begin
  689. ls:=Length(S);
  690. If (Index>LS) or (Index<=0) or (Size<=0) then
  691. exit;
  692. UniqueString (S);
  693. If (Size>LS-Index) then // Size+Index gives overflow ??
  694. Size:=LS-Index+1;
  695. If (Size<=LS-Index) then
  696. begin
  697. Dec(Index);
  698. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  699. end;
  700. Setlength(S,LS-Size);
  701. end;
  702. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  703. var
  704. Temp : AnsiString;
  705. LS : SizeInt;
  706. begin
  707. If Length(Source)=0 then
  708. exit;
  709. if index <= 0 then
  710. index := 1;
  711. Ls:=Length(S);
  712. if index > LS then
  713. index := LS+1;
  714. Dec(Index);
  715. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  716. SetLength(Temp,Length(Source)+LS);
  717. If Index>0 then
  718. move (Pointer(S)^,Pointer(Temp)^,Index);
  719. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  720. If (LS-Index)>0 then
  721. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  722. S:=Temp;
  723. end;
  724. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  725. begin
  726. SetLength(StringOfChar,l);
  727. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  728. end;
  729. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  730. begin
  731. SetLength(S,Len);
  732. If (Buf<>Nil) then
  733. Move (Buf^,Pointer(S)^,Len);
  734. end;
  735. Procedure SetString (Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  736. begin
  737. if (Buf<>nil) and (Len>0) then
  738. widestringmanager.Wide2AnsiMoveProc(Buf,S,Len)
  739. else
  740. SetLength(S, Len);
  741. end;
  742. *)
  743. function upcase(const s : ansistring) : ansistring;
  744. var
  745. u : unicodestring;
  746. begin
  747. u:=s;
  748. result:=upcase(u);
  749. end;
  750. function lowercase(const s : ansistring) : ansistring;
  751. var
  752. u : unicodestring;
  753. begin
  754. u:=s;
  755. result:=lowercase(u);
  756. end;