2
0

astrings.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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; zerobased: boolean = true): ansistring; compilerproc;
  222. var
  223. i : SizeInt;
  224. begin
  225. if (zerobased) then
  226. begin
  227. if (arr[0]=#0) Then
  228. { result is automatically set to '' }
  229. exit;
  230. i:=IndexChar(arr,high(arr)+1,#0);
  231. if i = -1 then
  232. i := high(arr)+1;
  233. end
  234. else
  235. i := high(arr)+1;
  236. SetLength(fpc_CharArray_To_AnsiStr,i);
  237. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  238. end;
  239. { note: inside the compiler, the resulttype is modified to be the length }
  240. { of the actual chararray to which we convert (JM) }
  241. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  242. var
  243. len: SizeInt;
  244. begin
  245. len := length(src);
  246. if len > arraysize then
  247. len := arraysize;
  248. { make sure we don't try to access element 1 of the ansistring if it's nil }
  249. if len > 0 then
  250. move(src[1],fpc_ansistr_to_chararray[0],len);
  251. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  252. end;
  253. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  254. {
  255. Compares 2 AnsiStrings;
  256. The result is
  257. <0 if S1<S2
  258. 0 if S1=S2
  259. >0 if S1>S2
  260. }
  261. Var
  262. MaxI,Temp : SizeInt;
  263. begin
  264. if pointer(S1)=pointer(S2) then
  265. begin
  266. result:=0;
  267. exit;
  268. end;
  269. Maxi:=Length(S1);
  270. temp:=Length(S2);
  271. If MaxI>Temp then
  272. MaxI:=Temp;
  273. if MaxI>0 then
  274. begin
  275. result:=CompareByte(S1[1],S2[1],MaxI);
  276. if result=0 then
  277. result:=Length(S1)-Length(S2);
  278. end
  279. else
  280. result:=Length(S1)-Length(S2);
  281. end;
  282. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  283. begin
  284. if p=nil then
  285. HandleErrorFrame(201,get_frame);
  286. end;
  287. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  288. begin
  289. if (index>len) or (Index<1) then
  290. HandleErrorFrame(201,get_frame);
  291. end;
  292. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  293. {
  294. Sets The length of string S to L.
  295. Makes sure S is unique, and contains enough room.
  296. }
  297. Var
  298. Temp : Pointer;
  299. lens,
  300. movelen : SizeInt;
  301. begin
  302. if (l>0) then
  303. begin
  304. if Pointer(S)=nil then
  305. begin
  306. GetMem(Pointer(S),AnsiRecLen+L);
  307. PAnsiRec(S)^.Ref:=1;
  308. inc(Pointer(S),firstoff);
  309. end
  310. else if PAnsiRec(Pointer(S)-FirstOff)^.Ref=1 then
  311. begin
  312. Dec(Pointer(S),FirstOff);
  313. if AnsiRecLen+L>MemSize(Pointer(s)) then
  314. reallocmem(pointer(S),AnsiRecLen+L);
  315. Inc(Pointer(S),FirstOff);
  316. end
  317. else
  318. begin
  319. { Reallocation is needed... }
  320. Temp:=Pointer(NewAnsiString(L));
  321. { also move terminating null }
  322. lens:=succ(length(s));
  323. if l < lens then
  324. movelen := l
  325. else
  326. movelen := lens;
  327. Move(Pointer(S)^,Temp^,movelen);
  328. { ref count dropped to zero in the mean time? }
  329. If (PAnsiRec(Pointer(S)-FirstOff)^.Ref > 0) and
  330. declocked(PAnsiRec(Pointer(S)-FirstOff)^.Ref) then
  331. freemem(PAnsiRec(Pointer(s)-FirstOff));
  332. Pointer(S):=Temp;
  333. end;
  334. { Force nil termination in case it gets shorter }
  335. PByte(Pointer(S)+l)^:=0;
  336. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  337. end
  338. else
  339. begin
  340. { Length=0 }
  341. if Pointer(S)<>nil then
  342. fpc_ansistr_decr_ref (Pointer(S));
  343. Pointer(S):=Nil;
  344. end;
  345. end;
  346. {$ifdef EXTRAANSISHORT}
  347. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  348. {
  349. Compares a AnsiString with a ShortString;
  350. The result is
  351. <0 if S1<S2
  352. 0 if S1=S2
  353. >0 if S1>S2
  354. }
  355. Var
  356. i,MaxI,Temp : SizeInt;
  357. begin
  358. Temp:=0;
  359. i:=0;
  360. MaxI:=Length(AnsiString(S1));
  361. if MaxI>byte(S2[0]) then
  362. MaxI:=Byte(S2[0]);
  363. While (i<MaxI) and (Temp=0) do
  364. begin
  365. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  366. inc(i);
  367. end;
  368. AnsiStr_ShortStr_Compare:=Temp;
  369. end;
  370. {$endif EXTRAANSISHORT}
  371. {*****************************************************************************
  372. Public functions, In interface.
  373. *****************************************************************************}
  374. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  375. Var
  376. SNew : Pointer;
  377. L : SizeInt;
  378. begin
  379. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  380. SNew:=NewAnsiString (L);
  381. Move (Pointer(S)^,SNew^,L+1);
  382. PAnsiRec(SNew-FirstOff)^.len:=L;
  383. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  384. pointer(S):=SNew;
  385. pointer(result):=SNew;
  386. end;
  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 : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  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. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  404. begin
  405. SetLength(S,length(S)+1);
  406. // avoid unique call
  407. PChar(Pointer(S)+length(S)-1)^:=c;
  408. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  409. end;
  410. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  411. var
  412. ofs : SizeInt;
  413. begin
  414. if Str='' then
  415. exit;
  416. ofs:=Length(S);
  417. SetLength(S,ofs+length(Str));
  418. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  419. move(Str[1],(pointer(S)+ofs)^,length(Str));
  420. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  421. end;
  422. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  423. var
  424. ofs, strlength: SizeInt;
  425. samestring: boolean;
  426. begin
  427. if Str='' then
  428. exit;
  429. samestring := pointer(s) = pointer(str);
  430. { needed in case s and str are the same string }
  431. strlength := length(str);
  432. ofs:=Length(S);
  433. SetLength(S,ofs+strlength);
  434. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  435. if not(samestring) then
  436. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  437. else
  438. { the setlength may have relocated the string, so str may no longer be valid }
  439. move(S[1],(pointer(S)+ofs)^,strlength+1)
  440. end;
  441. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  442. var
  443. ResultAddress : Pointer;
  444. begin
  445. ResultAddress:=Nil;
  446. dec(index);
  447. if Index < 0 then
  448. Index := 0;
  449. { Check Size. Accounts for Zero-length S, the double check is needed because
  450. Size can be maxint and will get <0 when adding index }
  451. if (Size>Length(S)) or
  452. (Index+Size>Length(S)) then
  453. Size:=Length(S)-Index;
  454. If Size>0 then
  455. begin
  456. If Index<0 Then
  457. Index:=0;
  458. ResultAddress:=Pointer(NewAnsiString (Size));
  459. if ResultAddress<>Nil then
  460. begin
  461. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  462. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  463. PByte(ResultAddress+Size)^:=0;
  464. end;
  465. end;
  466. Pointer(fpc_ansistr_Copy):=ResultAddress;
  467. end;
  468. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  469. var
  470. i,MaxLen : SizeInt;
  471. pc : pchar;
  472. begin
  473. Pos:=0;
  474. if Length(SubStr)>0 then
  475. begin
  476. MaxLen:=Length(source)-Length(SubStr);
  477. i:=0;
  478. pc:=@source[1];
  479. while (i<=MaxLen) do
  480. begin
  481. inc(i);
  482. if (SubStr[1]=pc^) and
  483. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  484. begin
  485. Pos:=i;
  486. exit;
  487. end;
  488. inc(pc);
  489. end;
  490. end;
  491. end;
  492. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  493. var
  494. i,MaxLen : SizeInt;
  495. pc : pchar;
  496. begin
  497. Pos:=0;
  498. if Length(SubStr)>0 then
  499. begin
  500. MaxLen:=Length(source)-Length(SubStr);
  501. i:=0;
  502. pc:=@source[1];
  503. while (i<=MaxLen) do
  504. begin
  505. inc(i);
  506. if (SubStr[1]=pc^) and
  507. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  508. begin
  509. Pos:=i;
  510. exit;
  511. end;
  512. inc(pc);
  513. end;
  514. end;
  515. end;
  516. { Faster version for a char alone. Must be implemented because }
  517. { pos(c: char; const s: shortstring) also exists, so otherwise }
  518. { using pos(char,pchar) will always call the shortstring version }
  519. { (exact match for first argument), also with $h+ (JM) }
  520. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  521. var
  522. i: SizeInt;
  523. pc : pchar;
  524. begin
  525. pc:=@s[1];
  526. for i:=1 to length(s) do
  527. begin
  528. if pc^=c then
  529. begin
  530. pos:=i;
  531. exit;
  532. end;
  533. inc(pc);
  534. end;
  535. pos:=0;
  536. end;
  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. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  551. Var
  552. SS : ShortString;
  553. begin
  554. fpc_Val_UInt_AnsiStr := 0;
  555. if length(S) > 255 then
  556. code := 256
  557. else
  558. begin
  559. SS := S;
  560. Val(SS,fpc_Val_UInt_AnsiStr,code);
  561. end;
  562. end;
  563. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  564. Var
  565. SS : ShortString;
  566. begin
  567. fpc_Val_SInt_AnsiStr:=0;
  568. if length(S)>255 then
  569. code:=256
  570. else
  571. begin
  572. SS := S;
  573. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  574. end;
  575. end;
  576. {$ifndef CPU64}
  577. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  578. Var
  579. SS : ShortString;
  580. begin
  581. fpc_Val_qword_AnsiStr:=0;
  582. if length(S)>255 then
  583. code:=256
  584. else
  585. begin
  586. SS := S;
  587. Val(SS,fpc_Val_qword_AnsiStr,Code);
  588. end;
  589. end;
  590. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  591. Var
  592. SS : ShortString;
  593. begin
  594. fpc_Val_int64_AnsiStr:=0;
  595. if length(S)>255 then
  596. code:=256
  597. else
  598. begin
  599. SS := s;
  600. Val(SS,fpc_Val_int64_AnsiStr,Code);
  601. end;
  602. end;
  603. {$endif CPU64}
  604. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  605. var
  606. ss: ShortString;
  607. begin
  608. str_real(len,fr,d,treal_type(rt),ss);
  609. s:=ss;
  610. end;
  611. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; 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_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  619. Var
  620. SS : ShortString;
  621. begin
  622. str (v:Len,SS);
  623. S:=SS;
  624. end;
  625. {$ifndef CPU64}
  626. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  627. Var
  628. SS : ShortString;
  629. begin
  630. str(v:Len,SS);
  631. S:=SS;
  632. end;
  633. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  634. Var
  635. SS : ShortString;
  636. begin
  637. str (v:Len,SS);
  638. S:=SS;
  639. end;
  640. {$endif CPU64}
  641. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  642. Var
  643. LS : SizeInt;
  644. begin
  645. ls:=Length(S);
  646. If (Index>LS) or (Index<=0) or (Size<=0) then
  647. exit;
  648. UniqueString (S);
  649. If (Size>LS-Index) then // Size+Index gives overflow ??
  650. Size:=LS-Index+1;
  651. If (Size<=LS-Index) then
  652. begin
  653. Dec(Index);
  654. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  655. end;
  656. Setlength(S,LS-Size);
  657. end;
  658. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  659. var
  660. Temp : AnsiString;
  661. LS : SizeInt;
  662. begin
  663. If Length(Source)=0 then
  664. exit;
  665. if index <= 0 then
  666. index := 1;
  667. Ls:=Length(S);
  668. if index > LS then
  669. index := LS+1;
  670. Dec(Index);
  671. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  672. SetLength(Temp,Length(Source)+LS);
  673. If Index>0 then
  674. move (Pointer(S)^,Pointer(Temp)^,Index);
  675. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  676. If (LS-Index)>0 then
  677. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  678. S:=Temp;
  679. end;
  680. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  681. begin
  682. SetLength(StringOfChar,l);
  683. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  684. end;
  685. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  686. begin
  687. SetLength(S,Len);
  688. If (Buf<>Nil) then
  689. begin
  690. Move (Buf[0],S[1],Len);
  691. end;
  692. end;
  693. function upcase(const s : ansistring) : ansistring;
  694. var
  695. i : SizeInt;
  696. begin
  697. Setlength(result,length(s));
  698. for i := 1 to length (s) do
  699. result[i] := upcase(s[i]);
  700. end;
  701. function lowercase(const s : ansistring) : ansistring;
  702. var
  703. i : SizeInt;
  704. begin
  705. Setlength(result,length(s));
  706. for i := 1 to length (s) do
  707. result[i] := lowercase(s[i]);
  708. end;