astrings.inc 21 KB

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