astrings.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. Size,Location : SizeInt;
  122. pc : pchar;
  123. begin
  124. { only assign if s1 or s2 is empty }
  125. if (S1='') then
  126. begin
  127. result:=s2;
  128. exit;
  129. end;
  130. if (S2='') then
  131. begin
  132. result:=s1;
  133. exit;
  134. end;
  135. Location:=Length(S1);
  136. Size:=length(S2);
  137. SetLength(result,Size+Location);
  138. pc:=pchar(result);
  139. Move(S1[1],pc^,Location);
  140. inc(pc,location);
  141. Move(S2[1],pc^,Size+1);
  142. end;
  143. function fpc_AnsiStr_Concat_multi (const sarr:array of Ansistring): ansistring; compilerproc;
  144. Var
  145. i : Longint;
  146. p : pointer;
  147. pc : pchar;
  148. Size,NewSize : SizeInt;
  149. begin
  150. { First calculate size of the result so we can do
  151. a single call to SetLength() }
  152. NewSize:=0;
  153. for i:=low(sarr) to high(sarr) do
  154. inc(Newsize,length(sarr[i]));
  155. SetLength(result,NewSize);
  156. pc:=pchar(result);
  157. for i:=low(sarr) to high(sarr) do
  158. begin
  159. p:=pointer(sarr[i]);
  160. if assigned(p) then
  161. begin
  162. Size:=length(ansistring(p));
  163. Move(pchar(p)^,pc^,Size+1);
  164. inc(pc,size);
  165. end;
  166. end;
  167. end;
  168. {$ifdef EXTRAANSISHORT}
  169. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  170. {
  171. Concatenates a Ansi with a short string; : S2 + S2
  172. }
  173. Var
  174. Size,Location : SizeInt;
  175. begin
  176. Size:=Length(S2);
  177. Location:=Length(S1);
  178. If Size=0 then
  179. exit;
  180. { Setlength takes case of uniqueness
  181. and alllocated memory. We need to use length,
  182. to take into account possibility of S1=Nil }
  183. SetLength (S1,Size+Length(S1));
  184. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  185. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  186. end;
  187. {$endif EXTRAANSISHORT}
  188. { the following declaration has exactly the same effect as }
  189. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  190. { which is what the old helper was, so we don't need an extra implementation }
  191. { of the old helper (JM) }
  192. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  193. {
  194. Converts a AnsiString to a ShortString;
  195. }
  196. Var
  197. Size : SizeInt;
  198. begin
  199. if S2='' then
  200. fpc_AnsiStr_To_ShortStr:=''
  201. else
  202. begin
  203. Size:=Length(S2);
  204. If Size>high_of_res then
  205. Size:=high_of_res;
  206. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  207. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  208. end;
  209. end;
  210. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; compilerproc;
  211. {
  212. Converts a ShortString to a AnsiString;
  213. }
  214. Var
  215. Size : SizeInt;
  216. begin
  217. Size:=Length(S2);
  218. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  219. if Size>0 then
  220. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  221. end;
  222. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; compilerproc;
  223. {
  224. Converts a Char to a AnsiString;
  225. }
  226. begin
  227. Setlength (fpc_Char_To_AnsiStr,1);
  228. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  229. { Terminating Zero }
  230. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  231. end;
  232. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; compilerproc;
  233. Var
  234. L : SizeInt;
  235. begin
  236. if (not assigned(p)) or (p[0]=#0) Then
  237. { result is automatically set to '' }
  238. exit;
  239. l:=IndexChar(p^,-1,#0);
  240. SetLength(fpc_PChar_To_AnsiStr,L);
  241. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  242. end;
  243. Function fpc_CharArray_To_AnsiStr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
  244. var
  245. i : SizeInt;
  246. begin
  247. if (zerobased) then
  248. begin
  249. if (arr[0]=#0) Then
  250. { result is automatically set to '' }
  251. exit;
  252. i:=IndexChar(arr,high(arr)+1,#0);
  253. if i = -1 then
  254. i := high(arr)+1;
  255. end
  256. else
  257. i := high(arr)+1;
  258. SetLength(fpc_CharArray_To_AnsiStr,i);
  259. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  260. end;
  261. { note: inside the compiler, the resulttype is modified to be the length }
  262. { of the actual chararray to which we convert (JM) }
  263. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  264. var
  265. len: SizeInt;
  266. begin
  267. len := length(src);
  268. if len > arraysize then
  269. len := arraysize;
  270. { make sure we don't try to access element 1 of the ansistring if it's nil }
  271. if len > 0 then
  272. move(src[1],fpc_ansistr_to_chararray[0],len);
  273. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  274. end;
  275. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  276. {
  277. Compares 2 AnsiStrings;
  278. The result is
  279. <0 if S1<S2
  280. 0 if S1=S2
  281. >0 if S1>S2
  282. }
  283. Var
  284. MaxI,Temp : SizeInt;
  285. begin
  286. if pointer(S1)=pointer(S2) then
  287. begin
  288. result:=0;
  289. exit;
  290. end;
  291. Maxi:=Length(S1);
  292. temp:=Length(S2);
  293. If MaxI>Temp then
  294. MaxI:=Temp;
  295. if MaxI>0 then
  296. begin
  297. result:=CompareByte(S1[1],S2[1],MaxI);
  298. if result=0 then
  299. result:=Length(S1)-Length(S2);
  300. end
  301. else
  302. result:=Length(S1)-Length(S2);
  303. end;
  304. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  305. begin
  306. if p=nil then
  307. HandleErrorFrame(201,get_frame);
  308. end;
  309. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  310. begin
  311. if (index>len) or (Index<1) then
  312. HandleErrorFrame(201,get_frame);
  313. end;
  314. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  315. {
  316. Sets The length of string S to L.
  317. Makes sure S is unique, and contains enough room.
  318. }
  319. Var
  320. Temp : Pointer;
  321. lens,
  322. movelen : SizeInt;
  323. begin
  324. if (l>0) then
  325. begin
  326. if Pointer(S)=nil then
  327. begin
  328. GetMem(Pointer(S),AnsiRecLen+L);
  329. PAnsiRec(S)^.Ref:=1;
  330. inc(Pointer(S),firstoff);
  331. end
  332. else if PAnsiRec(Pointer(S)-FirstOff)^.Ref=1 then
  333. begin
  334. Dec(Pointer(S),FirstOff);
  335. if AnsiRecLen+L>MemSize(Pointer(s)) then
  336. reallocmem(pointer(S),AnsiRecLen+L);
  337. Inc(Pointer(S),FirstOff);
  338. end
  339. else
  340. begin
  341. { Reallocation is needed... }
  342. Temp:=Pointer(NewAnsiString(L));
  343. { also move terminating null }
  344. lens:=succ(length(s));
  345. if l < lens then
  346. movelen := l
  347. else
  348. movelen := lens;
  349. Move(Pointer(S)^,Temp^,movelen);
  350. { ref count dropped to zero in the mean time? }
  351. If declocked(PAnsiRec(Pointer(S)-FirstOff)^.Ref) then
  352. freemem(PAnsiRec(Pointer(s)-FirstOff));
  353. Pointer(S):=Temp;
  354. end;
  355. { Force nil termination in case it gets shorter }
  356. PByte(Pointer(S)+l)^:=0;
  357. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  358. end
  359. else
  360. begin
  361. { Length=0 }
  362. if Pointer(S)<>nil then
  363. fpc_ansistr_decr_ref (Pointer(S));
  364. Pointer(S):=Nil;
  365. end;
  366. end;
  367. {$ifdef EXTRAANSISHORT}
  368. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  369. {
  370. Compares a AnsiString with a ShortString;
  371. The result is
  372. <0 if S1<S2
  373. 0 if S1=S2
  374. >0 if S1>S2
  375. }
  376. Var
  377. i,MaxI,Temp : SizeInt;
  378. begin
  379. Temp:=0;
  380. i:=0;
  381. MaxI:=Length(AnsiString(S1));
  382. if MaxI>byte(S2[0]) then
  383. MaxI:=Byte(S2[0]);
  384. While (i<MaxI) and (Temp=0) do
  385. begin
  386. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  387. inc(i);
  388. end;
  389. AnsiStr_ShortStr_Compare:=Temp;
  390. end;
  391. {$endif EXTRAANSISHORT}
  392. {*****************************************************************************
  393. Public functions, In interface.
  394. *****************************************************************************}
  395. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  396. Var
  397. SNew : Pointer;
  398. L : SizeInt;
  399. begin
  400. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  401. SNew:=NewAnsiString (L);
  402. Move (Pointer(S)^,SNew^,L+1);
  403. PAnsiRec(SNew-FirstOff)^.len:=L;
  404. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  405. pointer(S):=SNew;
  406. pointer(result):=SNew;
  407. end;
  408. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  409. // MV: inline the basic checks for case that S is already unique.
  410. // Rest is too complex to inline, so factor that out as a call.
  411. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  412. {
  413. Make sure reference count of S is 1,
  414. using copy-on-write semantics.
  415. }
  416. begin
  417. pointer(result) := pointer(s);
  418. If Pointer(S)=Nil then
  419. exit;
  420. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  421. result:=fpc_truely_ansistr_unique(s);
  422. end;
  423. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  424. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  425. begin
  426. SetLength(S,length(S)+1);
  427. // avoid unique call
  428. PChar(Pointer(S)+length(S)-1)^:=c;
  429. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  430. end;
  431. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  432. var
  433. ofs : SizeInt;
  434. begin
  435. if Str='' then
  436. exit;
  437. ofs:=Length(S);
  438. SetLength(S,ofs+length(Str));
  439. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  440. move(Str[1],(pointer(S)+ofs)^,length(Str));
  441. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  442. end;
  443. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  444. var
  445. ofs, strlength : SizeInt;
  446. begin
  447. if Str='' then
  448. exit;
  449. { needed in case s and str are the same string }
  450. strlength := length(str);
  451. ofs:=Length(S);
  452. SetLength(S,ofs+strlength);
  453. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  454. move(Str[1],(pointer(S)+ofs)^,strlength+1);
  455. end;
  456. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  457. var
  458. ResultAddress : Pointer;
  459. begin
  460. ResultAddress:=Nil;
  461. dec(index);
  462. if Index < 0 then
  463. Index := 0;
  464. { Check Size. Accounts for Zero-length S, the double check is needed because
  465. Size can be maxint and will get <0 when adding index }
  466. if (Size>Length(S)) or
  467. (Index+Size>Length(S)) then
  468. Size:=Length(S)-Index;
  469. If Size>0 then
  470. begin
  471. If Index<0 Then
  472. Index:=0;
  473. ResultAddress:=Pointer(NewAnsiString (Size));
  474. if ResultAddress<>Nil then
  475. begin
  476. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  477. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  478. PByte(ResultAddress+Size)^:=0;
  479. end;
  480. end;
  481. Pointer(fpc_ansistr_Copy):=ResultAddress;
  482. end;
  483. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  484. var
  485. i,MaxLen : SizeInt;
  486. pc : pchar;
  487. begin
  488. Pos:=0;
  489. if Length(SubStr)>0 then
  490. begin
  491. MaxLen:=Length(source)-Length(SubStr);
  492. i:=0;
  493. pc:=@source[1];
  494. while (i<=MaxLen) do
  495. begin
  496. inc(i);
  497. if (SubStr[1]=pc^) and
  498. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  499. begin
  500. Pos:=i;
  501. exit;
  502. end;
  503. inc(pc);
  504. end;
  505. end;
  506. end;
  507. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  508. var
  509. i,MaxLen : SizeInt;
  510. pc : pchar;
  511. begin
  512. Pos:=0;
  513. if Length(SubStr)>0 then
  514. begin
  515. MaxLen:=Length(source)-Length(SubStr);
  516. i:=0;
  517. pc:=@source[1];
  518. while (i<=MaxLen) do
  519. begin
  520. inc(i);
  521. if (SubStr[1]=pc^) and
  522. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  523. begin
  524. Pos:=i;
  525. exit;
  526. end;
  527. inc(pc);
  528. end;
  529. end;
  530. end;
  531. { Faster version for a char alone. Must be implemented because }
  532. { pos(c: char; const s: shortstring) also exists, so otherwise }
  533. { using pos(char,pchar) will always call the shortstring version }
  534. { (exact match for first argument), also with $h+ (JM) }
  535. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  536. var
  537. i: SizeInt;
  538. pc : pchar;
  539. begin
  540. pc:=@s[1];
  541. for i:=1 to length(s) do
  542. begin
  543. if pc^=c then
  544. begin
  545. pos:=i;
  546. exit;
  547. end;
  548. inc(pc);
  549. end;
  550. pos:=0;
  551. end;
  552. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  553. Var
  554. SS : String;
  555. begin
  556. fpc_Val_Real_AnsiStr := 0;
  557. if length(S) > 255 then
  558. code := 256
  559. else
  560. begin
  561. SS := S;
  562. Val(SS,fpc_Val_Real_AnsiStr,code);
  563. end;
  564. end;
  565. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  566. Var
  567. SS : ShortString;
  568. begin
  569. fpc_Val_UInt_AnsiStr := 0;
  570. if length(S) > 255 then
  571. code := 256
  572. else
  573. begin
  574. SS := S;
  575. Val(SS,fpc_Val_UInt_AnsiStr,code);
  576. end;
  577. end;
  578. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  579. Var
  580. SS : ShortString;
  581. begin
  582. fpc_Val_SInt_AnsiStr:=0;
  583. if length(S)>255 then
  584. code:=256
  585. else
  586. begin
  587. SS := S;
  588. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  589. end;
  590. end;
  591. {$ifndef CPU64}
  592. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  593. Var
  594. SS : ShortString;
  595. begin
  596. fpc_Val_qword_AnsiStr:=0;
  597. if length(S)>255 then
  598. code:=256
  599. else
  600. begin
  601. SS := S;
  602. Val(SS,fpc_Val_qword_AnsiStr,Code);
  603. end;
  604. end;
  605. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  606. Var
  607. SS : ShortString;
  608. begin
  609. fpc_Val_int64_AnsiStr:=0;
  610. if length(S)>255 then
  611. code:=256
  612. else
  613. begin
  614. SS := s;
  615. Val(SS,fpc_Val_int64_AnsiStr,Code);
  616. end;
  617. end;
  618. {$endif CPU64}
  619. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  620. var
  621. ss: ShortString;
  622. begin
  623. str_real(len,fr,d,treal_type(rt),ss);
  624. s:=ss;
  625. end;
  626. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; 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_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  634. Var
  635. SS : ShortString;
  636. begin
  637. str (v:Len,SS);
  638. S:=SS;
  639. end;
  640. {$ifndef CPU64}
  641. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  642. Var
  643. SS : ShortString;
  644. begin
  645. str(v:Len,SS);
  646. S:=SS;
  647. end;
  648. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  649. Var
  650. SS : ShortString;
  651. begin
  652. str (v:Len,SS);
  653. S:=SS;
  654. end;
  655. {$endif CPU64}
  656. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  657. Var
  658. LS : SizeInt;
  659. begin
  660. ls:=Length(S);
  661. If (Index>LS) or (Index<=0) or (Size<=0) then
  662. exit;
  663. UniqueString (S);
  664. If (Size>LS-Index) then // Size+Index gives overflow ??
  665. Size:=LS-Index+1;
  666. If (Size<=LS-Index) then
  667. begin
  668. Dec(Index);
  669. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  670. end;
  671. Setlength(S,LS-Size);
  672. end;
  673. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  674. var
  675. Temp : AnsiString;
  676. LS : SizeInt;
  677. begin
  678. If Length(Source)=0 then
  679. exit;
  680. if index <= 0 then
  681. index := 1;
  682. Ls:=Length(S);
  683. if index > LS then
  684. index := LS+1;
  685. Dec(Index);
  686. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  687. SetLength(Temp,Length(Source)+LS);
  688. If Index>0 then
  689. move (Pointer(S)^,Pointer(Temp)^,Index);
  690. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  691. If (LS-Index)>0 then
  692. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  693. S:=Temp;
  694. end;
  695. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  696. begin
  697. SetLength(StringOfChar,l);
  698. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  699. end;
  700. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  701. begin
  702. SetLength(S,Len);
  703. If (Buf<>Nil) then
  704. begin
  705. Move (Buf[0],S[1],Len);
  706. end;
  707. end;
  708. function upcase(const s : ansistring) : ansistring;
  709. var
  710. i : SizeInt;
  711. begin
  712. Setlength(result,length(s));
  713. for i := 1 to length (s) do
  714. result[i] := upcase(s[i]);
  715. end;
  716. function lowercase(const s : ansistring) : ansistring;
  717. var
  718. i : SizeInt;
  719. begin
  720. Setlength(result,length(s));
  721. for i := 1 to length (s) do
  722. result[i] := lowercase(s[i]);
  723. end;