astrings.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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 (PAnsiRec(Pointer(S)-FirstOff)^.Ref > 0) and
  352. declocked(PAnsiRec(Pointer(S)-FirstOff)^.Ref) then
  353. freemem(PAnsiRec(Pointer(s)-FirstOff));
  354. Pointer(S):=Temp;
  355. end;
  356. { Force nil termination in case it gets shorter }
  357. PByte(Pointer(S)+l)^:=0;
  358. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  359. end
  360. else
  361. begin
  362. { Length=0 }
  363. if Pointer(S)<>nil then
  364. fpc_ansistr_decr_ref (Pointer(S));
  365. Pointer(S):=Nil;
  366. end;
  367. end;
  368. {$ifdef EXTRAANSISHORT}
  369. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  370. {
  371. Compares a AnsiString with a ShortString;
  372. The result is
  373. <0 if S1<S2
  374. 0 if S1=S2
  375. >0 if S1>S2
  376. }
  377. Var
  378. i,MaxI,Temp : SizeInt;
  379. begin
  380. Temp:=0;
  381. i:=0;
  382. MaxI:=Length(AnsiString(S1));
  383. if MaxI>byte(S2[0]) then
  384. MaxI:=Byte(S2[0]);
  385. While (i<MaxI) and (Temp=0) do
  386. begin
  387. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  388. inc(i);
  389. end;
  390. AnsiStr_ShortStr_Compare:=Temp;
  391. end;
  392. {$endif EXTRAANSISHORT}
  393. {*****************************************************************************
  394. Public functions, In interface.
  395. *****************************************************************************}
  396. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  397. Var
  398. SNew : Pointer;
  399. L : SizeInt;
  400. begin
  401. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  402. SNew:=NewAnsiString (L);
  403. Move (Pointer(S)^,SNew^,L+1);
  404. PAnsiRec(SNew-FirstOff)^.len:=L;
  405. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  406. pointer(S):=SNew;
  407. pointer(result):=SNew;
  408. end;
  409. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  410. // MV: inline the basic checks for case that S is already unique.
  411. // Rest is too complex to inline, so factor that out as a call.
  412. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  413. {
  414. Make sure reference count of S is 1,
  415. using copy-on-write semantics.
  416. }
  417. begin
  418. pointer(result) := pointer(s);
  419. If Pointer(S)=Nil then
  420. exit;
  421. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  422. result:=fpc_truely_ansistr_unique(s);
  423. end;
  424. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  425. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  426. begin
  427. SetLength(S,length(S)+1);
  428. // avoid unique call
  429. PChar(Pointer(S)+length(S)-1)^:=c;
  430. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  431. end;
  432. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  433. var
  434. ofs : SizeInt;
  435. begin
  436. if Str='' then
  437. exit;
  438. ofs:=Length(S);
  439. SetLength(S,ofs+length(Str));
  440. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  441. move(Str[1],(pointer(S)+ofs)^,length(Str));
  442. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  443. end;
  444. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  445. var
  446. ofs, strlength: SizeInt;
  447. samestring: boolean;
  448. begin
  449. if Str='' then
  450. exit;
  451. samestring := pointer(s) = pointer(str);
  452. { needed in case s and str are the same string }
  453. strlength := length(str);
  454. ofs:=Length(S);
  455. SetLength(S,ofs+strlength);
  456. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  457. if not(samestring) then
  458. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  459. else
  460. { the setlength may have relocated the string, so str may no longer be valid }
  461. move(S[1],(pointer(S)+ofs)^,strlength+1)
  462. end;
  463. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  464. var
  465. ResultAddress : Pointer;
  466. begin
  467. ResultAddress:=Nil;
  468. dec(index);
  469. if Index < 0 then
  470. Index := 0;
  471. { Check Size. Accounts for Zero-length S, the double check is needed because
  472. Size can be maxint and will get <0 when adding index }
  473. if (Size>Length(S)) or
  474. (Index+Size>Length(S)) then
  475. Size:=Length(S)-Index;
  476. If Size>0 then
  477. begin
  478. If Index<0 Then
  479. Index:=0;
  480. ResultAddress:=Pointer(NewAnsiString (Size));
  481. if ResultAddress<>Nil then
  482. begin
  483. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  484. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  485. PByte(ResultAddress+Size)^:=0;
  486. end;
  487. end;
  488. Pointer(fpc_ansistr_Copy):=ResultAddress;
  489. end;
  490. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  491. var
  492. i,MaxLen : SizeInt;
  493. pc : pchar;
  494. begin
  495. Pos:=0;
  496. if Length(SubStr)>0 then
  497. begin
  498. MaxLen:=Length(source)-Length(SubStr);
  499. i:=0;
  500. pc:=@source[1];
  501. while (i<=MaxLen) do
  502. begin
  503. inc(i);
  504. if (SubStr[1]=pc^) and
  505. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  506. begin
  507. Pos:=i;
  508. exit;
  509. end;
  510. inc(pc);
  511. end;
  512. end;
  513. end;
  514. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  515. var
  516. i,MaxLen : SizeInt;
  517. pc : pchar;
  518. begin
  519. Pos:=0;
  520. if Length(SubStr)>0 then
  521. begin
  522. MaxLen:=Length(source)-Length(SubStr);
  523. i:=0;
  524. pc:=@source[1];
  525. while (i<=MaxLen) do
  526. begin
  527. inc(i);
  528. if (SubStr[1]=pc^) and
  529. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  530. begin
  531. Pos:=i;
  532. exit;
  533. end;
  534. inc(pc);
  535. end;
  536. end;
  537. end;
  538. { Faster version for a char alone. Must be implemented because }
  539. { pos(c: char; const s: shortstring) also exists, so otherwise }
  540. { using pos(char,pchar) will always call the shortstring version }
  541. { (exact match for first argument), also with $h+ (JM) }
  542. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  543. var
  544. i: SizeInt;
  545. pc : pchar;
  546. begin
  547. pc:=@s[1];
  548. for i:=1 to length(s) do
  549. begin
  550. if pc^=c then
  551. begin
  552. pos:=i;
  553. exit;
  554. end;
  555. inc(pc);
  556. end;
  557. pos:=0;
  558. end;
  559. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  560. Var
  561. SS : String;
  562. begin
  563. fpc_Val_Real_AnsiStr := 0;
  564. if length(S) > 255 then
  565. code := 256
  566. else
  567. begin
  568. SS := S;
  569. Val(SS,fpc_Val_Real_AnsiStr,code);
  570. end;
  571. end;
  572. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  573. Var
  574. SS : ShortString;
  575. begin
  576. fpc_Val_UInt_AnsiStr := 0;
  577. if length(S) > 255 then
  578. code := 256
  579. else
  580. begin
  581. SS := S;
  582. Val(SS,fpc_Val_UInt_AnsiStr,code);
  583. end;
  584. end;
  585. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  586. Var
  587. SS : ShortString;
  588. begin
  589. fpc_Val_SInt_AnsiStr:=0;
  590. if length(S)>255 then
  591. code:=256
  592. else
  593. begin
  594. SS := S;
  595. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  596. end;
  597. end;
  598. {$ifndef CPU64}
  599. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  600. Var
  601. SS : ShortString;
  602. begin
  603. fpc_Val_qword_AnsiStr:=0;
  604. if length(S)>255 then
  605. code:=256
  606. else
  607. begin
  608. SS := S;
  609. Val(SS,fpc_Val_qword_AnsiStr,Code);
  610. end;
  611. end;
  612. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  613. Var
  614. SS : ShortString;
  615. begin
  616. fpc_Val_int64_AnsiStr:=0;
  617. if length(S)>255 then
  618. code:=256
  619. else
  620. begin
  621. SS := s;
  622. Val(SS,fpc_Val_int64_AnsiStr,Code);
  623. end;
  624. end;
  625. {$endif CPU64}
  626. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  627. var
  628. ss: ShortString;
  629. begin
  630. str_real(len,fr,d,treal_type(rt),ss);
  631. s:=ss;
  632. end;
  633. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  634. Var
  635. SS : ShortString;
  636. begin
  637. str(v:Len,SS);
  638. S:=SS;
  639. end;
  640. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  641. Var
  642. SS : ShortString;
  643. begin
  644. str (v:Len,SS);
  645. S:=SS;
  646. end;
  647. {$ifndef CPU64}
  648. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  649. Var
  650. SS : ShortString;
  651. begin
  652. str(v:Len,SS);
  653. S:=SS;
  654. end;
  655. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  656. Var
  657. SS : ShortString;
  658. begin
  659. str (v:Len,SS);
  660. S:=SS;
  661. end;
  662. {$endif CPU64}
  663. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  664. Var
  665. LS : SizeInt;
  666. begin
  667. ls:=Length(S);
  668. If (Index>LS) or (Index<=0) or (Size<=0) then
  669. exit;
  670. UniqueString (S);
  671. If (Size>LS-Index) then // Size+Index gives overflow ??
  672. Size:=LS-Index+1;
  673. If (Size<=LS-Index) then
  674. begin
  675. Dec(Index);
  676. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  677. end;
  678. Setlength(S,LS-Size);
  679. end;
  680. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  681. var
  682. Temp : AnsiString;
  683. LS : SizeInt;
  684. begin
  685. If Length(Source)=0 then
  686. exit;
  687. if index <= 0 then
  688. index := 1;
  689. Ls:=Length(S);
  690. if index > LS then
  691. index := LS+1;
  692. Dec(Index);
  693. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  694. SetLength(Temp,Length(Source)+LS);
  695. If Index>0 then
  696. move (Pointer(S)^,Pointer(Temp)^,Index);
  697. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  698. If (LS-Index)>0 then
  699. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  700. S:=Temp;
  701. end;
  702. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  703. begin
  704. SetLength(StringOfChar,l);
  705. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  706. end;
  707. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  708. begin
  709. SetLength(S,Len);
  710. If (Buf<>Nil) then
  711. begin
  712. Move (Buf[0],S[1],Len);
  713. end;
  714. end;
  715. function upcase(const s : ansistring) : ansistring;
  716. var
  717. i : SizeInt;
  718. begin
  719. Setlength(result,length(s));
  720. for i := 1 to length (s) do
  721. result[i] := upcase(s[i]);
  722. end;
  723. function lowercase(const s : ansistring) : ansistring;
  724. var
  725. i : SizeInt;
  726. begin
  727. Setlength(result,length(s));
  728. for i := 1 to length (s) do
  729. result[i] := lowercase(s[i]);
  730. end;