astrings.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. samestring: boolean;
  447. begin
  448. if Str='' then
  449. exit;
  450. samestring := pointer(s) = pointer(str);
  451. { needed in case s and str are the same string }
  452. strlength := length(str);
  453. ofs:=Length(S);
  454. SetLength(S,ofs+strlength);
  455. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  456. if not(samestring) then
  457. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  458. else
  459. { the setlength may have relocated the string, so str may no longer be valid }
  460. move(S[1],(pointer(S)+ofs)^,strlength+1)
  461. end;
  462. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  463. var
  464. ResultAddress : Pointer;
  465. begin
  466. ResultAddress:=Nil;
  467. dec(index);
  468. if Index < 0 then
  469. Index := 0;
  470. { Check Size. Accounts for Zero-length S, the double check is needed because
  471. Size can be maxint and will get <0 when adding index }
  472. if (Size>Length(S)) or
  473. (Index+Size>Length(S)) then
  474. Size:=Length(S)-Index;
  475. If Size>0 then
  476. begin
  477. If Index<0 Then
  478. Index:=0;
  479. ResultAddress:=Pointer(NewAnsiString (Size));
  480. if ResultAddress<>Nil then
  481. begin
  482. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  483. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  484. PByte(ResultAddress+Size)^:=0;
  485. end;
  486. end;
  487. Pointer(fpc_ansistr_Copy):=ResultAddress;
  488. end;
  489. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  490. var
  491. i,MaxLen : SizeInt;
  492. pc : pchar;
  493. begin
  494. Pos:=0;
  495. if Length(SubStr)>0 then
  496. begin
  497. MaxLen:=Length(source)-Length(SubStr);
  498. i:=0;
  499. pc:=@source[1];
  500. while (i<=MaxLen) do
  501. begin
  502. inc(i);
  503. if (SubStr[1]=pc^) and
  504. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  505. begin
  506. Pos:=i;
  507. exit;
  508. end;
  509. inc(pc);
  510. end;
  511. end;
  512. end;
  513. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  514. var
  515. i,MaxLen : SizeInt;
  516. pc : pchar;
  517. begin
  518. Pos:=0;
  519. if Length(SubStr)>0 then
  520. begin
  521. MaxLen:=Length(source)-Length(SubStr);
  522. i:=0;
  523. pc:=@source[1];
  524. while (i<=MaxLen) do
  525. begin
  526. inc(i);
  527. if (SubStr[1]=pc^) and
  528. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  529. begin
  530. Pos:=i;
  531. exit;
  532. end;
  533. inc(pc);
  534. end;
  535. end;
  536. end;
  537. { Faster version for a char alone. Must be implemented because }
  538. { pos(c: char; const s: shortstring) also exists, so otherwise }
  539. { using pos(char,pchar) will always call the shortstring version }
  540. { (exact match for first argument), also with $h+ (JM) }
  541. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  542. var
  543. i: SizeInt;
  544. pc : pchar;
  545. begin
  546. pc:=@s[1];
  547. for i:=1 to length(s) do
  548. begin
  549. if pc^=c then
  550. begin
  551. pos:=i;
  552. exit;
  553. end;
  554. inc(pc);
  555. end;
  556. pos:=0;
  557. end;
  558. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  559. Var
  560. SS : String;
  561. begin
  562. fpc_Val_Real_AnsiStr := 0;
  563. if length(S) > 255 then
  564. code := 256
  565. else
  566. begin
  567. SS := S;
  568. Val(SS,fpc_Val_Real_AnsiStr,code);
  569. end;
  570. end;
  571. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  572. Var
  573. SS : ShortString;
  574. begin
  575. fpc_Val_UInt_AnsiStr := 0;
  576. if length(S) > 255 then
  577. code := 256
  578. else
  579. begin
  580. SS := S;
  581. Val(SS,fpc_Val_UInt_AnsiStr,code);
  582. end;
  583. end;
  584. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  585. Var
  586. SS : ShortString;
  587. begin
  588. fpc_Val_SInt_AnsiStr:=0;
  589. if length(S)>255 then
  590. code:=256
  591. else
  592. begin
  593. SS := S;
  594. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  595. end;
  596. end;
  597. {$ifndef CPU64}
  598. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  599. Var
  600. SS : ShortString;
  601. begin
  602. fpc_Val_qword_AnsiStr:=0;
  603. if length(S)>255 then
  604. code:=256
  605. else
  606. begin
  607. SS := S;
  608. Val(SS,fpc_Val_qword_AnsiStr,Code);
  609. end;
  610. end;
  611. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  612. Var
  613. SS : ShortString;
  614. begin
  615. fpc_Val_int64_AnsiStr:=0;
  616. if length(S)>255 then
  617. code:=256
  618. else
  619. begin
  620. SS := s;
  621. Val(SS,fpc_Val_int64_AnsiStr,Code);
  622. end;
  623. end;
  624. {$endif CPU64}
  625. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  626. var
  627. ss: ShortString;
  628. begin
  629. str_real(len,fr,d,treal_type(rt),ss);
  630. s:=ss;
  631. end;
  632. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  633. Var
  634. SS : ShortString;
  635. begin
  636. str(v:Len,SS);
  637. S:=SS;
  638. end;
  639. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  640. Var
  641. SS : ShortString;
  642. begin
  643. str (v:Len,SS);
  644. S:=SS;
  645. end;
  646. {$ifndef CPU64}
  647. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  648. Var
  649. SS : ShortString;
  650. begin
  651. str(v:Len,SS);
  652. S:=SS;
  653. end;
  654. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  655. Var
  656. SS : ShortString;
  657. begin
  658. str (v:Len,SS);
  659. S:=SS;
  660. end;
  661. {$endif CPU64}
  662. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  663. Var
  664. LS : SizeInt;
  665. begin
  666. ls:=Length(S);
  667. If (Index>LS) or (Index<=0) or (Size<=0) then
  668. exit;
  669. UniqueString (S);
  670. If (Size>LS-Index) then // Size+Index gives overflow ??
  671. Size:=LS-Index+1;
  672. If (Size<=LS-Index) then
  673. begin
  674. Dec(Index);
  675. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  676. end;
  677. Setlength(S,LS-Size);
  678. end;
  679. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  680. var
  681. Temp : AnsiString;
  682. LS : SizeInt;
  683. begin
  684. If Length(Source)=0 then
  685. exit;
  686. if index <= 0 then
  687. index := 1;
  688. Ls:=Length(S);
  689. if index > LS then
  690. index := LS+1;
  691. Dec(Index);
  692. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  693. SetLength(Temp,Length(Source)+LS);
  694. If Index>0 then
  695. move (Pointer(S)^,Pointer(Temp)^,Index);
  696. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  697. If (LS-Index)>0 then
  698. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  699. S:=Temp;
  700. end;
  701. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  702. begin
  703. SetLength(StringOfChar,l);
  704. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  705. end;
  706. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  707. begin
  708. SetLength(S,Len);
  709. If (Buf<>Nil) then
  710. begin
  711. Move (Buf[0],S[1],Len);
  712. end;
  713. end;
  714. function upcase(const s : ansistring) : ansistring;
  715. var
  716. i : SizeInt;
  717. begin
  718. Setlength(result,length(s));
  719. for i := 1 to length (s) do
  720. result[i] := upcase(s[i]);
  721. end;
  722. function lowercase(const s : ansistring) : ansistring;
  723. var
  724. i : SizeInt;
  725. begin
  726. Setlength(result,length(s));
  727. for i := 1 to length (s) do
  728. result[i] := lowercase(s[i]);
  729. end;