astrings.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. {
  15. This file contains the implementation of the AnsiString type,
  16. and all things that are needed for it.
  17. AnsiString is defined as a 'silent' pchar :
  18. a pchar that points to :
  19. @-8 : SizeInt for reference count;
  20. @-4 : SizeInt for size;
  21. @ : String + Terminating #0;
  22. Pchar(Ansistring) is a valid typecast.
  23. So AS[i] is converted to the address @AS+i-1.
  24. Constants should be assigned a reference count of -1
  25. Meaning that they can't be disposed of.
  26. }
  27. Type
  28. PAnsiRec = ^TAnsiRec;
  29. TAnsiRec = Packed Record
  30. Ref,
  31. Len : SizeInt;
  32. First : Char;
  33. end;
  34. Const
  35. AnsiRecLen = SizeOf(TAnsiRec);
  36. FirstOff = SizeOf(TAnsiRec)-1;
  37. {****************************************************************************
  38. Internal functions, not in interface.
  39. ****************************************************************************}
  40. Function NewAnsiString(Len : SizeInt) : Pointer;
  41. {
  42. Allocate a new AnsiString on the heap.
  43. initialize it to zero length and reference count 1.
  44. }
  45. Var
  46. P : Pointer;
  47. begin
  48. { request a multiple of 16 because the heap manager alloctes anyways chunks of 16 bytes }
  49. GetMem(P,Len+AnsiRecLen);
  50. If P<>Nil then
  51. begin
  52. PAnsiRec(P)^.Ref:=1; { Set reference count }
  53. PAnsiRec(P)^.Len:=0; { Initial length }
  54. PAnsiRec(P)^.First:=#0; { Terminating #0 }
  55. inc(p,firstoff); { Points to string now }
  56. end;
  57. NewAnsiString:=P;
  58. end;
  59. Procedure DisposeAnsiString(Var S : Pointer); {$IFNDEF VER2_0} Inline; {$ENDIF}
  60. {
  61. Deallocates a AnsiString From the heap.
  62. }
  63. begin
  64. If S=Nil then
  65. exit;
  66. Dec (S,FirstOff);
  67. FreeMem (S);
  68. S:=Nil;
  69. end;
  70. {$ifndef FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  71. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [Public,Alias:'FPC_ANSISTR_DECR_REF']; compilerproc;
  72. {
  73. Decreases the ReferenceCount of a non constant ansistring;
  74. If the reference count is zero, deallocate the string;
  75. }
  76. Type
  77. pSizeInt = ^SizeInt;
  78. Var
  79. l : pSizeInt;
  80. Begin
  81. { Zero string }
  82. If S=Nil then exit;
  83. { check for constant strings ...}
  84. l:=@PAnsiRec(S-FirstOff)^.Ref;
  85. If l^<0 then exit;
  86. { declocked does a MT safe dec and returns true, if the counter is 0 }
  87. If declocked(l^) then
  88. { Ref count dropped to zero }
  89. DisposeAnsiString (S); { Remove...}
  90. end;
  91. {$endif FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  92. { also define alias for internal use in the system unit }
  93. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [external name 'FPC_ANSISTR_DECR_REF'];
  94. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [Public,Alias:'FPC_ANSISTR_INCR_REF']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  95. Begin
  96. If S=Nil then
  97. exit;
  98. { Let's be paranoid : Constant string ??}
  99. If PAnsiRec(S-FirstOff)^.Ref<0 then exit;
  100. inclocked(PAnsiRec(S-FirstOff)^.Ref);
  101. end;
  102. { also define alias which can be used inside the system unit }
  103. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [external name 'FPC_ANSISTR_INCR_REF'];
  104. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_ANSISTR_ASSIGN']; compilerproc;
  105. {
  106. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  107. }
  108. begin
  109. If S2<>nil then
  110. If PAnsiRec(S2-FirstOff)^.Ref>0 then
  111. inclocked(PAnsiRec(S2-FirstOff)^.ref);
  112. { Decrease the reference count on the old S1 }
  113. fpc_ansistr_decr_ref (S1);
  114. { And finally, have S1 pointing to S2 (or its copy) }
  115. S1:=S2;
  116. end;
  117. { alias for internal use }
  118. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_ANSISTR_ASSIGN'];
  119. function fpc_AnsiStr_Concat (const S1,S2 : AnsiString): ansistring; compilerproc;
  120. var
  121. S3: ansistring absolute result;
  122. {
  123. Concatenates 2 AnsiStrings : S1+S2.
  124. Result Goes to S3;
  125. }
  126. Var
  127. Size,Location : SizeInt;
  128. begin
  129. { only assign if s1 or s2 is empty }
  130. if (S1='') then
  131. s3 := s2
  132. else if (S2='') then
  133. s3 := s1
  134. else
  135. begin
  136. Size:=length(S2);
  137. Location:=Length(S1);
  138. SetLength (S3,Size+Location);
  139. { the cast to a pointer avoids the unique call }
  140. { and we don't need an unique call }
  141. { because of the SetLength S3 is unique }
  142. Move (S1[1],pointer(S3)^,Location);
  143. Move (S2[1],pointer(pointer(S3)+location)^,Size+1);
  144. end;
  145. end;
  146. {$ifdef EXTRAANSISHORT}
  147. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  148. {
  149. Concatenates a Ansi with a short string; : S2 + S2
  150. }
  151. Var
  152. Size,Location : SizeInt;
  153. begin
  154. Size:=Length(S2);
  155. Location:=Length(S1);
  156. If Size=0 then
  157. exit;
  158. { Setlength takes case of uniqueness
  159. and alllocated memory. We need to use length,
  160. to take into account possibility of S1=Nil }
  161. SetLength (S1,Size+Length(S1));
  162. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  163. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  164. end;
  165. {$endif EXTRAANSISHORT}
  166. { the following declaration has exactly the same effect as }
  167. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  168. { which is what the old helper was, so we don't need an extra implementation }
  169. { of the old helper (JM) }
  170. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  171. {
  172. Converts a AnsiString to a ShortString;
  173. }
  174. Var
  175. Size : SizeInt;
  176. begin
  177. if S2='' then
  178. fpc_AnsiStr_To_ShortStr:=''
  179. else
  180. begin
  181. Size:=Length(S2);
  182. If Size>high_of_res then
  183. Size:=high_of_res;
  184. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  185. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  186. end;
  187. end;
  188. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; compilerproc;
  189. {
  190. Converts a ShortString to a AnsiString;
  191. }
  192. Var
  193. Size : SizeInt;
  194. begin
  195. Size:=Length(S2);
  196. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  197. if Size>0 then
  198. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  199. end;
  200. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; compilerproc;
  201. {
  202. Converts a Char to a AnsiString;
  203. }
  204. begin
  205. Setlength (fpc_Char_To_AnsiStr,1);
  206. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  207. { Terminating Zero }
  208. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  209. end;
  210. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; compilerproc;
  211. Var
  212. L : SizeInt;
  213. begin
  214. if (not assigned(p)) or (p[0]=#0) Then
  215. { result is automatically set to '' }
  216. exit;
  217. l:=IndexChar(p^,-1,#0);
  218. SetLength(fpc_PChar_To_AnsiStr,L);
  219. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  220. end;
  221. Function fpc_CharArray_To_AnsiStr(const arr: array of char): ansistring; compilerproc;
  222. var
  223. i : SizeInt;
  224. begin
  225. if arr[0]=#0 Then
  226. { result is automatically set to '' }
  227. exit;
  228. i:=IndexChar(arr,high(arr)+1,#0);
  229. if i = -1 then
  230. i := high(arr)+1;
  231. SetLength(fpc_CharArray_To_AnsiStr,i);
  232. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  233. end;
  234. { note: inside the compiler, the resulttype is modified to be the length }
  235. { of the actual chararray to which we convert (JM) }
  236. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  237. var
  238. len: SizeInt;
  239. begin
  240. len := length(src);
  241. if len > arraysize then
  242. len := arraysize;
  243. { make sure we don't try to access element 1 of the ansistring if it's nil }
  244. if len > 0 then
  245. move(src[1],fpc_ansistr_to_chararray[0],len);
  246. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  247. end;
  248. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  249. {
  250. Compares 2 AnsiStrings;
  251. The result is
  252. <0 if S1<S2
  253. 0 if S1=S2
  254. >0 if S1>S2
  255. }
  256. Var
  257. MaxI,Temp : SizeInt;
  258. begin
  259. if pointer(S1)=pointer(S2) then
  260. begin
  261. result:=0;
  262. exit;
  263. end;
  264. Maxi:=Length(S1);
  265. temp:=Length(S2);
  266. If MaxI>Temp then
  267. MaxI:=Temp;
  268. if MaxI>0 then
  269. begin
  270. result:=CompareByte(S1[1],S2[1],MaxI);
  271. if result=0 then
  272. result:=Length(S1)-Length(S2);
  273. end
  274. else
  275. result:=Length(S1)-Length(S2);
  276. end;
  277. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  278. begin
  279. if p=nil then
  280. HandleErrorFrame(201,get_frame);
  281. end;
  282. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  283. begin
  284. if (index>len) or (Index<1) then
  285. HandleErrorFrame(201,get_frame);
  286. end;
  287. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  288. {
  289. Sets The length of string S to L.
  290. Makes sure S is unique, and contains enough room.
  291. }
  292. Var
  293. Temp : Pointer;
  294. movelen : SizeInt;
  295. begin
  296. if (l>0) then
  297. begin
  298. if Pointer(S)=nil then
  299. begin
  300. { Need a complete new string...}
  301. Pointer(s):=NewAnsiString(l);
  302. end
  303. else if (PAnsiRec(Pointer(S)-FirstOff)^.Ref = 1) then
  304. begin
  305. Dec(Pointer(S),FirstOff);
  306. if AnsiRecLen+L>MemSize(Pointer(s)) then
  307. reallocmem(pointer(S),AnsiRecLen+L);
  308. Inc(Pointer(S),FirstOff);
  309. end
  310. else
  311. begin
  312. { Reallocation is needed... }
  313. Temp:=Pointer(NewAnsiString(L));
  314. if Length(S)>0 then
  315. begin
  316. if l < succ(length(s)) then
  317. movelen := l
  318. { also move terminating null }
  319. else movelen := succ(length(s));
  320. Move(Pointer(S)^,Temp^,movelen);
  321. end;
  322. fpc_ansistr_decr_ref(Pointer(S));
  323. Pointer(S):=Temp;
  324. end;
  325. { Force nil termination in case it gets shorter }
  326. PByte(Pointer(S)+l)^:=0;
  327. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  328. end
  329. else
  330. begin
  331. { Length=0 }
  332. if Pointer(S)<>nil then
  333. fpc_ansistr_decr_ref (Pointer(S));
  334. Pointer(S):=Nil;
  335. end;
  336. end;
  337. {$ifdef EXTRAANSISHORT}
  338. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  339. {
  340. Compares a AnsiString with a ShortString;
  341. The result is
  342. <0 if S1<S2
  343. 0 if S1=S2
  344. >0 if S1>S2
  345. }
  346. Var
  347. i,MaxI,Temp : SizeInt;
  348. begin
  349. Temp:=0;
  350. i:=0;
  351. MaxI:=Length(AnsiString(S1));
  352. if MaxI>byte(S2[0]) then
  353. MaxI:=Byte(S2[0]);
  354. While (i<MaxI) and (Temp=0) do
  355. begin
  356. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  357. inc(i);
  358. end;
  359. AnsiStr_ShortStr_Compare:=Temp;
  360. end;
  361. {$endif EXTRAANSISHORT}
  362. {*****************************************************************************
  363. Public functions, In interface.
  364. *****************************************************************************}
  365. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  366. Var
  367. SNew : Pointer;
  368. L : SizeInt;
  369. begin
  370. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  371. SNew:=NewAnsiString (L);
  372. Move (Pointer(S)^,SNew^,L+1);
  373. PAnsiRec(SNew-FirstOff)^.len:=L;
  374. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  375. pointer(S):=SNew;
  376. pointer(result):=SNew;
  377. end;
  378. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  379. // MV: inline the basic checks for case that S is already unique.
  380. // Rest is too complex to inline, so factor that out as a call.
  381. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  382. {
  383. Make sure reference count of S is 1,
  384. using copy-on-write semantics.
  385. }
  386. begin
  387. pointer(result) := pointer(s);
  388. If Pointer(S)=Nil then
  389. exit;
  390. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  391. result:=fpc_truely_ansistr_unique(s);
  392. end;
  393. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  394. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  395. begin
  396. SetLength(S,length(S)+1);
  397. // avoid unique call
  398. PChar(Pointer(S)+length(S)-1)^:=c;
  399. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  400. end;
  401. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  402. var
  403. ofs : SizeInt;
  404. begin
  405. if Str='' then
  406. exit;
  407. ofs:=Length(S);
  408. SetLength(S,ofs+length(Str));
  409. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  410. move(Str[1],(pointer(S)+ofs)^,length(Str));
  411. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  412. end;
  413. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  414. var
  415. ofs, strlength : SizeInt;
  416. begin
  417. if Str='' then
  418. exit;
  419. { needed in case s and str are the same string }
  420. strlength := length(str);
  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)^,strlength+1);
  425. end;
  426. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  427. var
  428. ResultAddress : Pointer;
  429. begin
  430. ResultAddress:=Nil;
  431. dec(index);
  432. if Index < 0 then
  433. Index := 0;
  434. { Check Size. Accounts for Zero-length S, the double check is needed because
  435. Size can be maxint and will get <0 when adding index }
  436. if (Size>Length(S)) or
  437. (Index+Size>Length(S)) then
  438. Size:=Length(S)-Index;
  439. If Size>0 then
  440. begin
  441. If Index<0 Then
  442. Index:=0;
  443. ResultAddress:=Pointer(NewAnsiString (Size));
  444. if ResultAddress<>Nil then
  445. begin
  446. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  447. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  448. PByte(ResultAddress+Size)^:=0;
  449. end;
  450. end;
  451. Pointer(fpc_ansistr_Copy):=ResultAddress;
  452. end;
  453. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  454. var
  455. i,MaxLen : SizeInt;
  456. pc : pchar;
  457. begin
  458. Pos:=0;
  459. if Length(SubStr)>0 then
  460. begin
  461. MaxLen:=Length(source)-Length(SubStr);
  462. i:=0;
  463. pc:=@source[1];
  464. while (i<=MaxLen) do
  465. begin
  466. inc(i);
  467. if (SubStr[1]=pc^) and
  468. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  469. begin
  470. Pos:=i;
  471. exit;
  472. end;
  473. inc(pc);
  474. end;
  475. end;
  476. end;
  477. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  478. var
  479. i,MaxLen : SizeInt;
  480. pc : pchar;
  481. begin
  482. Pos:=0;
  483. if Length(SubStr)>0 then
  484. begin
  485. MaxLen:=Length(source)-Length(SubStr);
  486. i:=0;
  487. pc:=@source[1];
  488. while (i<=MaxLen) do
  489. begin
  490. inc(i);
  491. if (SubStr[1]=pc^) and
  492. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  493. begin
  494. Pos:=i;
  495. exit;
  496. end;
  497. inc(pc);
  498. end;
  499. end;
  500. end;
  501. { Faster version for a char alone. Must be implemented because }
  502. { pos(c: char; const s: shortstring) also exists, so otherwise }
  503. { using pos(char,pchar) will always call the shortstring version }
  504. { (exact match for first argument), also with $h+ (JM) }
  505. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  506. var
  507. i: SizeInt;
  508. pc : pchar;
  509. begin
  510. pc:=@s[1];
  511. for i:=1 to length(s) do
  512. begin
  513. if pc^=c then
  514. begin
  515. pos:=i;
  516. exit;
  517. end;
  518. inc(pc);
  519. end;
  520. pos:=0;
  521. end;
  522. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  523. Var
  524. SS : String;
  525. begin
  526. fpc_Val_Real_AnsiStr := 0;
  527. if length(S) > 255 then
  528. code := 256
  529. else
  530. begin
  531. SS := S;
  532. Val(SS,fpc_Val_Real_AnsiStr,code);
  533. end;
  534. end;
  535. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  536. Var
  537. SS : ShortString;
  538. begin
  539. fpc_Val_UInt_AnsiStr := 0;
  540. if length(S) > 255 then
  541. code := 256
  542. else
  543. begin
  544. SS := S;
  545. Val(SS,fpc_Val_UInt_AnsiStr,code);
  546. end;
  547. end;
  548. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  549. Var
  550. SS : ShortString;
  551. begin
  552. fpc_Val_SInt_AnsiStr:=0;
  553. if length(S)>255 then
  554. code:=256
  555. else
  556. begin
  557. SS := S;
  558. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  559. end;
  560. end;
  561. {$ifndef CPU64}
  562. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  563. Var
  564. SS : ShortString;
  565. begin
  566. fpc_Val_qword_AnsiStr:=0;
  567. if length(S)>255 then
  568. code:=256
  569. else
  570. begin
  571. SS := S;
  572. Val(SS,fpc_Val_qword_AnsiStr,Code);
  573. end;
  574. end;
  575. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  576. Var
  577. SS : ShortString;
  578. begin
  579. fpc_Val_int64_AnsiStr:=0;
  580. if length(S)>255 then
  581. code:=256
  582. else
  583. begin
  584. SS := s;
  585. Val(SS,fpc_Val_int64_AnsiStr,Code);
  586. end;
  587. end;
  588. {$endif CPU64}
  589. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  590. var
  591. ss: ShortString;
  592. begin
  593. str_real(len,fr,d,treal_type(rt),ss);
  594. s:=ss;
  595. end;
  596. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  597. Var
  598. SS : ShortString;
  599. begin
  600. str(v:Len,SS);
  601. S:=SS;
  602. end;
  603. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  604. Var
  605. SS : ShortString;
  606. begin
  607. str (v:Len,SS);
  608. S:=SS;
  609. end;
  610. {$ifndef CPU64}
  611. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  612. Var
  613. SS : ShortString;
  614. begin
  615. str(v:Len,SS);
  616. S:=SS;
  617. end;
  618. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  619. Var
  620. SS : ShortString;
  621. begin
  622. str (v:Len,SS);
  623. S:=SS;
  624. end;
  625. {$endif CPU64}
  626. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  627. Var
  628. LS : SizeInt;
  629. begin
  630. ls:=Length(S);
  631. If (Index>LS) or (Index<=0) or (Size<=0) then
  632. exit;
  633. UniqueString (S);
  634. If (Size>LS-Index) then // Size+Index gives overflow ??
  635. Size:=LS-Index+1;
  636. If (Size<=LS-Index) then
  637. begin
  638. Dec(Index);
  639. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  640. end;
  641. Setlength(S,LS-Size);
  642. end;
  643. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  644. var
  645. Temp : AnsiString;
  646. LS : SizeInt;
  647. begin
  648. If Length(Source)=0 then
  649. exit;
  650. if index <= 0 then
  651. index := 1;
  652. Ls:=Length(S);
  653. if index > LS then
  654. index := LS+1;
  655. Dec(Index);
  656. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  657. SetLength(Temp,Length(Source)+LS);
  658. If Index>0 then
  659. move (Pointer(S)^,Pointer(Temp)^,Index);
  660. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  661. If (LS-Index)>0 then
  662. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  663. S:=Temp;
  664. end;
  665. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  666. begin
  667. SetLength(StringOfChar,l);
  668. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  669. end;
  670. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  671. begin
  672. SetLength(S,Len);
  673. If (Buf<>Nil) then
  674. begin
  675. Move (Buf[0],S[1],Len);
  676. end;
  677. end;
  678. function upcase(const s : ansistring) : ansistring;
  679. var
  680. i : SizeInt;
  681. begin
  682. Setlength(result,length(s));
  683. for i := 1 to length (s) do
  684. result[i] := upcase(s[i]);
  685. end;
  686. function lowercase(const s : ansistring) : ansistring;
  687. var
  688. i : SizeInt;
  689. begin
  690. Setlength(result,length(s));
  691. for i := 1 to length (s) do
  692. result[i] := lowercase(s[i]);
  693. end;