astrings.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. {$ifdef interncopy}
  462. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : Longint) : AnsiString;compilerproc;
  463. {$else}
  464. Function Copy (Const S : AnsiString; Index,Size : Longint) : AnsiString;
  465. {$endif}
  466. var
  467. ResultAddress : Pointer;
  468. begin
  469. ResultAddress:=Nil;
  470. dec(index);
  471. if Index < 0 then
  472. Index := 0;
  473. { Check Size. Accounts for Zero-length S, the double check is needed because
  474. Size can be maxint and will get <0 when adding index }
  475. if (Size>Length(S)) or
  476. (Index+Size>Length(S)) then
  477. Size:=Length(S)-Index;
  478. If Size>0 then
  479. begin
  480. If Index<0 Then
  481. Index:=0;
  482. ResultAddress:=Pointer(NewAnsiString (Size));
  483. if ResultAddress<>Nil then
  484. begin
  485. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  486. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  487. PByte(ResultAddress+Size)^:=0;
  488. end;
  489. end;
  490. {$ifdef interncopy}
  491. Pointer(fpc_ansistr_Copy):=ResultAddress;
  492. {$else}
  493. Pointer(Copy):=ResultAddress;
  494. {$endif}
  495. end;
  496. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : Longint;
  497. var
  498. i,MaxLen : StrLenInt;
  499. pc : pchar;
  500. begin
  501. Pos:=0;
  502. if Length(SubStr)>0 then
  503. begin
  504. MaxLen:=Length(source)-Length(SubStr);
  505. i:=0;
  506. pc:=@source[1];
  507. while (i<=MaxLen) do
  508. begin
  509. inc(i);
  510. if (SubStr[1]=pc^) and
  511. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  512. begin
  513. Pos:=i;
  514. exit;
  515. end;
  516. inc(pc);
  517. end;
  518. end;
  519. end;
  520. { Faster version for a char alone. Must be implemented because }
  521. { pos(c: char; const s: shortstring) also exists, so otherwise }
  522. { using pos(char,pchar) will always call the shortstring version }
  523. { (exact match for first argument), also with $h+ (JM) }
  524. Function Pos (c : Char; Const s : AnsiString) : Longint;
  525. var
  526. i: longint;
  527. pc : pchar;
  528. begin
  529. pc:=@s[1];
  530. for i:=1 to length(s) do
  531. begin
  532. if pc^=c then
  533. begin
  534. pos:=i;
  535. exit;
  536. end;
  537. inc(pc);
  538. end;
  539. pos:=0;
  540. end;
  541. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  542. Var
  543. SS : String;
  544. begin
  545. fpc_Val_Real_AnsiStr := 0;
  546. if length(S) > 255 then
  547. code := 256
  548. else
  549. begin
  550. SS := S;
  551. Val(SS,fpc_Val_Real_AnsiStr,code);
  552. end;
  553. end;
  554. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  555. Var
  556. SS : ShortString;
  557. begin
  558. fpc_Val_UInt_AnsiStr := 0;
  559. if length(S) > 255 then
  560. code := 256
  561. else
  562. begin
  563. SS := S;
  564. Val(SS,fpc_Val_UInt_AnsiStr,code);
  565. end;
  566. end;
  567. Function fpc_Val_SInt_AnsiStr (DestSize: longint; Const S : AnsiString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  568. Var
  569. SS : ShortString;
  570. begin
  571. fpc_Val_SInt_AnsiStr:=0;
  572. if length(S)>255 then
  573. code:=256
  574. else
  575. begin
  576. SS := S;
  577. fpc_Val_SInt_AnsiStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  578. end;
  579. end;
  580. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  581. Var
  582. SS : ShortString;
  583. begin
  584. fpc_Val_qword_AnsiStr:=0;
  585. if length(S)>255 then
  586. code:=256
  587. else
  588. begin
  589. SS := S;
  590. Val(SS,fpc_Val_qword_AnsiStr,Code);
  591. end;
  592. end;
  593. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  594. Var
  595. SS : ShortString;
  596. begin
  597. fpc_Val_int64_AnsiStr:=0;
  598. if length(S)>255 then
  599. code:=256
  600. else
  601. begin
  602. SS := s;
  603. Val(SS,fpc_Val_int64_AnsiStr,Code);
  604. end;
  605. end;
  606. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : longint;var s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  607. var
  608. ss: ShortString;
  609. begin
  610. str_real(len,fr,d,treal_type(rt),ss);
  611. s:=ss;
  612. end;
  613. Procedure fpc_AnsiStr_Longword(C : Longword;Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  614. Var
  615. SS : ShortString;
  616. begin
  617. str(C:Len,SS);
  618. S:=SS;
  619. end;
  620. Procedure fpc_AnsiStr_Longint(L : Longint; Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  621. Var
  622. SS : ShortString;
  623. begin
  624. str (L:Len,SS);
  625. S:=SS;
  626. end;
  627. Procedure Delete (Var S : AnsiString; Index,Size: Longint);
  628. Var
  629. LS : Longint;
  630. begin
  631. If Length(S)=0 then
  632. exit;
  633. if index<=0 then
  634. exit;
  635. LS:=PAnsiRec(Pointer(S)-FirstOff)^.Len;
  636. if (Index<=LS) and (Size>0) then
  637. begin
  638. UniqueString (S);
  639. if Size+Index>LS then
  640. Size:=LS-Index+1;
  641. if Index+Size<=LS then
  642. begin
  643. Dec(Index);
  644. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index+1);
  645. end;
  646. Setlength(s,LS-Size);
  647. end;
  648. end;
  649. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : Longint);
  650. var
  651. Temp : AnsiString;
  652. LS : Longint;
  653. begin
  654. If Length(Source)=0 then
  655. exit;
  656. if index <= 0 then
  657. index := 1;
  658. Ls:=Length(S);
  659. if index > LS then
  660. index := LS+1;
  661. Dec(Index);
  662. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  663. SetLength(Temp,Length(Source)+LS);
  664. If Index>0 then
  665. move (Pointer(S)^,Pointer(Temp)^,Index);
  666. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  667. If (LS-Index)>0 then
  668. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  669. S:=Temp;
  670. end;
  671. Function StringOfChar(c : char;l : longint) : AnsiString;
  672. begin
  673. SetLength(StringOfChar,l);
  674. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  675. end;
  676. Procedure SetString (Var S : AnsiString; Buf : PChar; Len : Longint);
  677. begin
  678. SetLength(S,Len);
  679. Move (Buf[0],S[1],Len);
  680. end;
  681. function upcase(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] := upcase(s[i]);
  688. end;
  689. function lowercase(const s : ansistring) : ansistring;
  690. var
  691. i : longint;
  692. begin
  693. Setlength(result,length(s));
  694. for i := 1 to length (s) do
  695. result[i] := lowercase(s[i]);
  696. end;
  697. {
  698. $Log$
  699. Revision 1.29 2002-10-02 18:21:51 peter
  700. * Copy() changed to internal function calling compilerprocs
  701. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  702. new copy functions
  703. Revision 1.28 2002/09/14 11:20:50 carl
  704. * Delphi compatibility fix (with string routines)
  705. Revision 1.27 2002/09/07 21:10:47 carl
  706. * cardinal -> longword
  707. - remove some unused routines
  708. Revision 1.26 2002/09/07 15:07:44 peter
  709. * old logs removed and tabs fixed
  710. Revision 1.25 2002/04/26 15:19:05 peter
  711. * use saveregisters for incr routines, saves also problems with
  712. the optimizer
  713. Revision 1.24 2002/04/25 20:14:56 peter
  714. * updated compilerprocs
  715. * incr ref count has now a value argument instead of var
  716. Revision 1.23 2002/01/07 13:23:53 jonas
  717. * fixed bug in fpc_char_to_ansistr when converting #0 (found by Peter)
  718. }