astrings.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. {$ifdef AnsiStrDebug}
  44. Procedure DumpAnsiRec(S : Pointer);
  45. begin
  46. If S=Nil then
  47. Writeln ('String is nil')
  48. Else
  49. Begin
  50. With PAnsiRec(S-Firstoff)^ do
  51. begin
  52. Write ('(Maxlen: ',maxlen);
  53. Write (' Len:',len);
  54. Writeln (' Ref: ',ref,')');
  55. end;
  56. end;
  57. end;
  58. {$endif}
  59. Function NewAnsiString(Len : Longint) : Pointer;
  60. {
  61. Allocate a new AnsiString on the heap.
  62. initialize it to zero length and reference count 1.
  63. }
  64. Var
  65. P : Pointer;
  66. begin
  67. { Also add +1 for a terminating zero }
  68. GetMem(P,Len+AnsiRecLen);
  69. If P<>Nil then
  70. begin
  71. PAnsiRec(P)^.Maxlen:=Len; { Maximal length }
  72. PAnsiRec(P)^.Len:=0; { Initial length }
  73. PAnsiRec(P)^.Ref:=1; { Set reference count }
  74. PAnsiRec(P)^.First:=#0; { Terminating #0 }
  75. P:=P+FirstOff; { Points to string now }
  76. end;
  77. NewAnsiString:=P;
  78. end;
  79. Procedure DisposeAnsiString(Var S : Pointer);
  80. {
  81. Deallocates a AnsiString From the heap.
  82. }
  83. begin
  84. If S=Nil then
  85. exit;
  86. Dec (Longint(S),FirstOff);
  87. FreeMem (S);
  88. S:=Nil;
  89. end;
  90. Procedure fpc_AnsiStr_Decr_Ref (Var S : Pointer);[Public,Alias:'FPC_ANSISTR_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  91. {
  92. Decreases the ReferenceCount of a non constant ansistring;
  93. If the reference count is zero, deallocate the string;
  94. }
  95. Type
  96. plongint = ^longint;
  97. Var
  98. l : plongint;
  99. Begin
  100. { Zero string }
  101. If S=Nil then exit;
  102. { check for constant strings ...}
  103. l:=@PANSIREC(S-FirstOff)^.Ref;
  104. If l^<0 then exit;
  105. { declocked does a MT safe dec and returns true, if the counter is 0 }
  106. If declocked(l^) then
  107. { Ref count dropped to zero }
  108. DisposeAnsiString (S); { Remove...}
  109. { this pointer is not valid anymore, so set it to zero }
  110. S:=nil;
  111. end;
  112. {$ifdef hascompilerproc}
  113. { also define alias for internal use in the system unit }
  114. Procedure fpc_AnsiStr_Decr_Ref (Var S : Pointer); [external name 'FPC_ANSISTR_DECR_REF'];
  115. {$endif hascompilerproc}
  116. Procedure fpc_AnsiStr_Incr_Ref (Var S : Pointer);[Public,Alias:'FPC_ANSISTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  117. Begin
  118. If S=Nil then
  119. exit;
  120. { Let's be paranoid : Constant string ??}
  121. If PAnsiRec(S-FirstOff)^.Ref<0 then exit;
  122. inclocked(PAnsiRec(S-FirstOff)^.Ref);
  123. end;
  124. {$ifdef hascompilerproc}
  125. { also define alias which can be used inside the system unit }
  126. Procedure fpc_AnsiStr_Incr_Ref (Var S : Pointer); [external name 'FPC_ANSISTR_INCR_REF'];
  127. {$endif hascompilerproc}
  128. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_ANSISTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  129. {
  130. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  131. }
  132. begin
  133. If S2<>nil then
  134. If PAnsiRec(S2-FirstOff)^.Ref>0 then
  135. inclocked(PAnsiRec(S2-FirstOff)^.ref);
  136. { Decrease the reference count on the old S1 }
  137. fpc_ansistr_decr_ref (S1);
  138. { And finally, have S1 pointing to S2 (or its copy) }
  139. S1:=S2;
  140. end;
  141. {$ifdef hascompilerproc}
  142. { alias for internal use }
  143. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_ANSISTR_ASSIGN'];
  144. {$endif hascompilerproc}
  145. {$ifdef hascompilerproc}
  146. function fpc_AnsiStr_Concat (const S1,S2 : AnsiString): ansistring; compilerproc;
  147. var
  148. S3: ansistring absolute result;
  149. {$else hascompilerproc}
  150. Procedure fpc_AnsiStr_Concat (const S1,S2 : ansistring;var S3 : ansistring);[Public, alias: 'FPC_ANSISTR_CONCAT'];
  151. {$endif hascompilerproc}
  152. {
  153. Concatenates 2 AnsiStrings : S1+S2.
  154. Result Goes to S3;
  155. }
  156. Var
  157. Size,Location : Longint;
  158. begin
  159. { only assign if s1 or s2 is empty }
  160. if (S1='') then
  161. s3 := s2
  162. else
  163. if (S2='') then
  164. s3 := s1
  165. else
  166. begin
  167. Size:=length(S2);
  168. Location:=Length(S1);
  169. SetLength (S3,Size+Location);
  170. Move (S1[1],S3[1],Location);
  171. Move (S2[1],S3[location+1],Size+1);
  172. end;
  173. end;
  174. {$ifdef EXTRAANSISHORT}
  175. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  176. {
  177. Concatenates a Ansi with a short string; : S2 + S2
  178. }
  179. Var
  180. Size,Location : Longint;
  181. begin
  182. Size:=Length(S2);
  183. Location:=Length(S1);
  184. If Size=0 then
  185. exit;
  186. { Setlength takes case of uniqueness
  187. and alllocated memory. We need to use length,
  188. to take into account possibility of S1=Nil }
  189. SetLength (S1,Size+Length(S1));
  190. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  191. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  192. end;
  193. {$endif EXTRAANSISHORT}
  194. { the following declaration has exactly the same effect as }
  195. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  196. { which is what the old helper was, so we don't need an extra implementation }
  197. { of the old helper (JM) }
  198. function fpc_AnsiStr_To_ShortStr (high_of_res: longint;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  199. {
  200. Converts a AnsiString to a ShortString;
  201. }
  202. Var
  203. Size : Longint;
  204. begin
  205. if S2='' then
  206. fpc_AnsiStr_To_ShortStr:=''
  207. else
  208. begin
  209. Size:=Length(S2);
  210. If Size>high_of_res then
  211. Size:=high_of_res;
  212. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  213. byte(fpc_AnsiStr_To_ShortStr[0]):=Size;
  214. end;
  215. end;
  216. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; {$ifdef hascompilerproc} compilerproc; {$endif}
  217. {
  218. Converts a ShortString to a AnsiString;
  219. }
  220. Var
  221. Size : Longint;
  222. begin
  223. Size:=Length(S2);
  224. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  225. if Size>0 then
  226. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  227. end;
  228. { old style helper }
  229. {$ifndef hascompilerproc}
  230. Procedure fpc_ShortStr_To_AnsiStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_ANSISTR'];
  231. begin
  232. s1 := pointer(fpc_ShortStr_To_AnsiStr(s2));
  233. end;
  234. {$endif hascompilerproc}
  235. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
  236. {
  237. Converts a Char to a AnsiString;
  238. }
  239. begin
  240. if c = #0 then
  241. { result is automatically set to '' }
  242. exit;
  243. Setlength (fpc_Char_To_AnsiStr,1);
  244. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  245. { Terminating Zero }
  246. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  247. end;
  248. { old style helper }
  249. {$ifndef hascompilerproc}
  250. Procedure fpc_Char_To_AnsiStr(var S1 : Pointer; c : Char);[Public, alias: 'FPC_CHAR_TO_ANSISTR'];
  251. begin
  252. s1 := pointer(fpc_Char_To_AnsiStr(c));
  253. end;
  254. {$endif hascompilerproc}
  255. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; {$ifdef hascompilerproc} compilerproc; {$endif}
  256. Var
  257. L : Longint;
  258. begin
  259. if (not assigned(p)) or (p[0]=#0) Then
  260. { result is automatically set to '' }
  261. exit;
  262. l:=IndexChar(p^,-1,#0);
  263. SetLength(fpc_PChar_To_AnsiStr,L);
  264. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  265. end;
  266. { old style helper }
  267. {$ifndef hascompilerproc}
  268. Procedure fpc_PChar_To_AnsiStr(var a : ansistring;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  269. begin
  270. pointer(a) := pointer(fpc_PChar_To_AnsiStr(p));
  271. end;
  272. {$endif hascompilerproc}
  273. Function fpc_CharArray_To_AnsiStr(const arr: array of char): ansistring; {$ifdef hascompilerproc} compilerproc; {$endif}
  274. var
  275. i : longint;
  276. begin
  277. if arr[0]=#0 Then
  278. { result is automatically set to '' }
  279. exit;
  280. i:=IndexChar(arr,high(arr)+1,#0);
  281. if i = -1 then
  282. i := high(arr)+1;
  283. SetLength(fpc_CharArray_To_AnsiStr,i);
  284. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  285. end;
  286. { old style helper }
  287. {$ifndef hascompilerproc}
  288. { the declaration below is the same as }
  289. { which is what the old helper was (we need the parameter as "array of char" type }
  290. { so we can pass it to the new style helper (JM) }
  291. Procedure fpc_CharArray_To_AnsiStr(var a : ansistring; p: pointer; len: longint);[Public,Alias : 'FPC_CHARARRAY_TO_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  292. var
  293. src: pchar;
  294. i: longint;
  295. begin
  296. src := pchar(p);
  297. if src[0]=#0 Then
  298. { result is automatically set to '' }
  299. begin
  300. pointer(a) := nil;
  301. exit;
  302. end;
  303. i:=IndexChar(src^,len,#0);
  304. if i = -1 then
  305. i := len;
  306. pointer(a) := NewAnsiString(i);
  307. Move (src^,a[1],i);
  308. end;
  309. {$endif not hascompilerproc}
  310. {$ifdef hascompilerproc}
  311. { note: inside the compiler, the resulttype is modified to be the length }
  312. { of the actual chararray to which we convert (JM) }
  313. function fpc_ansistr_to_chararray(arraysize: longint; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  314. var
  315. len: longint;
  316. begin
  317. len := length(src);
  318. if len > arraysize then
  319. len := arraysize;
  320. { make sure we don't try to access element 1 of the ansistring if it's nil }
  321. if len > 0 then
  322. move(src[1],fpc_ansistr_to_chararray[0],len);
  323. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  324. end;
  325. {$endif hascompilerproc}
  326. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): Longint;[Public,Alias : 'FPC_ANSISTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  327. {
  328. Compares 2 AnsiStrings;
  329. The result is
  330. <0 if S1<S2
  331. 0 if S1=S2
  332. >0 if S1>S2
  333. }
  334. Var
  335. MaxI,Temp : Longint;
  336. begin
  337. if pointer(S1)=pointer(S2) then
  338. begin
  339. fpc_AnsiStr_Compare:=0;
  340. exit;
  341. end;
  342. Maxi:=Length(S1);
  343. temp:=Length(S2);
  344. If MaxI>Temp then
  345. MaxI:=Temp;
  346. Temp:=CompareByte(S1[1],S2[1],MaxI);
  347. if temp=0 then
  348. temp:=Length(S1)-Length(S2);
  349. fpc_AnsiStr_Compare:=Temp;
  350. end;
  351. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  352. begin
  353. if p=nil then
  354. HandleErrorFrame(201,get_frame);
  355. end;
  356. Procedure fpc_AnsiStr_CheckRange(len,index : longint);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  357. begin
  358. if (index>len) or (Index<1) then
  359. HandleErrorFrame(201,get_frame);
  360. end;
  361. {$ifndef INTERNSETLENGTH}
  362. Procedure SetLength (Var S : AnsiString; l : Longint);
  363. {$else INTERNSETLENGTH}
  364. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : Longint);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  365. {$endif INTERNSETLENGTH}
  366. {
  367. Sets The length of string S to L.
  368. Makes sure S is unique, and contains enough room.
  369. }
  370. Var
  371. Temp : Pointer;
  372. movelen: longint;
  373. begin
  374. if (l>0) then
  375. begin
  376. if Pointer(S)=nil then
  377. begin
  378. { Need a complete new string...}
  379. Pointer(s):=NewAnsiString(l);
  380. end
  381. else
  382. If (PAnsiRec(Pointer(S)-FirstOff)^.Maxlen < L) or
  383. (PAnsiRec(Pointer(S)-FirstOff)^.Ref <> 1) then
  384. begin
  385. { Reallocation is needed... }
  386. Temp:=Pointer(NewAnsiString(L));
  387. if Length(S)>0 then
  388. begin
  389. if l < succ(length(s)) then
  390. movelen := l
  391. { also move terminating null }
  392. else movelen := succ(length(s));
  393. Move(Pointer(S)^,Temp^,movelen);
  394. end;
  395. fpc_ansistr_decr_ref(Pointer(S));
  396. Pointer(S):=Temp;
  397. end;
  398. { Force nil termination in case it gets shorter }
  399. PByte(Pointer(S)+l)^:=0;
  400. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  401. end
  402. else
  403. begin
  404. { Length=0 }
  405. if Pointer(S)<>nil then
  406. fpc_ansistr_decr_ref (Pointer(S));
  407. Pointer(S):=Nil;
  408. end;
  409. end;
  410. {$ifdef EXTRAANSISHORT}
  411. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): Longint; {$ifdef hascompilerproc} compilerproc; {$endif}
  412. {
  413. Compares a AnsiString with a ShortString;
  414. The result is
  415. <0 if S1<S2
  416. 0 if S1=S2
  417. >0 if S1>S2
  418. }
  419. Var
  420. i,MaxI,Temp : Longint;
  421. begin
  422. Temp:=0;
  423. i:=0;
  424. MaxI:=Length(AnsiString(S1));
  425. if MaxI>byte(S2[0]) then
  426. MaxI:=Byte(S2[0]);
  427. While (i<MaxI) and (Temp=0) do
  428. begin
  429. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  430. inc(i);
  431. end;
  432. AnsiStr_ShortStr_Compare:=Temp;
  433. end;
  434. {$endif EXTRAANSISHORT}
  435. {*****************************************************************************
  436. Public functions, In interface.
  437. *****************************************************************************}
  438. {$ifndef INTERNLENGTH}
  439. Function Length (Const S : AnsiString) : Longint;
  440. {
  441. Returns the length of an AnsiString.
  442. Takes in acount that zero strings are NIL;
  443. }
  444. begin
  445. If Pointer(S)=Nil then
  446. Length:=0
  447. else
  448. Length:=PAnsiRec(Pointer(S)-FirstOff)^.Len;
  449. end;
  450. {$endif INTERNLENGTH}
  451. { overloaded version of UniqueString for interface }
  452. Procedure UniqueString(Var S : AnsiString); [external name 'FPC_ANSISTR_UNIQUE'];
  453. Procedure fpc_ansistr_Unique(Var S : AnsiString); [Public,Alias : 'FPC_ANSISTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  454. {
  455. Make sure reference count of S is 1,
  456. using copy-on-write semantics.
  457. }
  458. Var
  459. SNew : Pointer;
  460. L : Longint;
  461. begin
  462. If Pointer(S)=Nil then
  463. exit;
  464. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  465. begin
  466. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  467. SNew:=NewAnsiString (L);
  468. Move (Pointer(S)^,SNew^,L+1);
  469. PAnsiRec(SNew-FirstOff)^.len:=L;
  470. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  471. Pointer(S):=SNew;
  472. end;
  473. end;
  474. Function Copy (Const S : AnsiString; Index,Size : Longint) : AnsiString;
  475. var
  476. ResultAddress : Pointer;
  477. begin
  478. ResultAddress:=Nil;
  479. dec(index);
  480. if Index < 0 then
  481. Index := 0;
  482. { Check Size. Accounts for Zero-length S, the double check is needed because
  483. Size can be maxint and will get <0 when adding index }
  484. if (Size>Length(S)) or
  485. (Index+Size>Length(S)) then
  486. Size:=Length(S)-Index;
  487. If Size>0 then
  488. begin
  489. If Index<0 Then
  490. Index:=0;
  491. ResultAddress:=Pointer(NewAnsiString (Size));
  492. if ResultAddress<>Nil then
  493. begin
  494. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  495. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  496. PByte(ResultAddress+Size)^:=0;
  497. end;
  498. end;
  499. Pointer(Copy):=ResultAddress;
  500. end;
  501. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : Longint;
  502. var
  503. i,MaxLen : StrLenInt;
  504. pc : pchar;
  505. begin
  506. Pos:=0;
  507. if Length(SubStr)>0 then
  508. begin
  509. MaxLen:=Length(source)-Length(SubStr);
  510. i:=0;
  511. pc:=@source[1];
  512. while (i<=MaxLen) do
  513. begin
  514. inc(i);
  515. if (SubStr[1]=pc^) and
  516. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  517. begin
  518. Pos:=i;
  519. exit;
  520. end;
  521. inc(pc);
  522. end;
  523. end;
  524. end;
  525. { Faster version for a char alone. Must be implemented because }
  526. { pos(c: char; const s: shortstring) also exists, so otherwise }
  527. { using pos(char,pchar) will always call the shortstring version }
  528. { (exact match for first argument), also with $h+ (JM) }
  529. Function Pos (c : Char; Const s : AnsiString) : Longint;
  530. var
  531. i: longint;
  532. pc : pchar;
  533. begin
  534. pc:=@s[1];
  535. for i:=1 to length(s) do
  536. begin
  537. if pc^=c then
  538. begin
  539. pos:=i;
  540. exit;
  541. end;
  542. inc(pc);
  543. end;
  544. pos:=0;
  545. end;
  546. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  547. Var
  548. SS : String;
  549. begin
  550. fpc_Val_Real_AnsiStr := 0;
  551. if length(S) > 255 then
  552. code := 256
  553. else
  554. begin
  555. SS := S;
  556. Val(SS,fpc_Val_Real_AnsiStr,code);
  557. end;
  558. end;
  559. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  560. Var
  561. SS : ShortString;
  562. begin
  563. fpc_Val_UInt_AnsiStr := 0;
  564. if length(S) > 255 then
  565. code := 256
  566. else
  567. begin
  568. SS := S;
  569. Val(SS,fpc_Val_UInt_AnsiStr,code);
  570. end;
  571. end;
  572. Function fpc_Val_SInt_AnsiStr (DestSize: longint; Const S : AnsiString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  573. Var
  574. SS : ShortString;
  575. begin
  576. fpc_Val_SInt_AnsiStr:=0;
  577. if length(S)>255 then
  578. code:=256
  579. else
  580. begin
  581. SS := S;
  582. fpc_Val_SInt_AnsiStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  583. end;
  584. end;
  585. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  586. Var
  587. SS : ShortString;
  588. begin
  589. fpc_Val_qword_AnsiStr:=0;
  590. if length(S)>255 then
  591. code:=256
  592. else
  593. begin
  594. SS := S;
  595. Val(SS,fpc_Val_qword_AnsiStr,Code);
  596. end;
  597. end;
  598. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  599. Var
  600. SS : ShortString;
  601. begin
  602. fpc_Val_int64_AnsiStr:=0;
  603. if length(S)>255 then
  604. code:=256
  605. else
  606. begin
  607. SS := s;
  608. Val(SS,fpc_Val_int64_AnsiStr,Code);
  609. end;
  610. end;
  611. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : longint;var s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  612. var
  613. ss: ShortString;
  614. begin
  615. str_real(len,fr,d,treal_type(rt),ss);
  616. s:=ss;
  617. end;
  618. Procedure fpc_AnsiStr_Cardinal(C : Cardinal;Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_CARDINAL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  619. Var
  620. SS : ShortString;
  621. begin
  622. str(C:Len,SS);
  623. S:=SS;
  624. end;
  625. Procedure fpc_AnsiStr_Longint(L : Longint; Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  626. Var
  627. SS : ShortString;
  628. begin
  629. str (L:Len,SS);
  630. S:=SS;
  631. end;
  632. Procedure Delete (Var S : AnsiString; Index,Size: Longint);
  633. Var
  634. LS : Longint;
  635. begin
  636. If Length(S)=0 then
  637. exit;
  638. if index<=0 then
  639. begin
  640. inc(Size,index-1);
  641. index:=1;
  642. end;
  643. LS:=PAnsiRec(Pointer(S)-FirstOff)^.Len;
  644. if (Index<=LS) and (Size>0) then
  645. begin
  646. UniqueString (S);
  647. if Size+Index>LS then
  648. Size:=LS-Index+1;
  649. if Index+Size<=LS then
  650. begin
  651. Dec(Index);
  652. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index+1);
  653. end;
  654. Setlength(s,LS-Size);
  655. end;
  656. end;
  657. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : Longint);
  658. var
  659. Temp : AnsiString;
  660. LS : Longint;
  661. begin
  662. If Length(Source)=0 then
  663. exit;
  664. if index <= 0 then
  665. index := 1;
  666. Ls:=Length(S);
  667. if index > LS then
  668. index := LS+1;
  669. Dec(Index);
  670. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  671. SetLength(Temp,Length(Source)+LS);
  672. If Index>0 then
  673. move (Pointer(S)^,Pointer(Temp)^,Index);
  674. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  675. If (LS-Index)>0 then
  676. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  677. S:=Temp;
  678. end;
  679. Function StringOfChar(c : char;l : longint) : AnsiString;
  680. begin
  681. SetLength(StringOfChar,l);
  682. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  683. end;
  684. Procedure SetString (Var S : AnsiString; Buf : PChar; Len : Longint);
  685. begin
  686. SetLength(S,Len);
  687. Move (Buf[0],S[1],Len);
  688. end;
  689. function upcase(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] := upcase(s[i]);
  696. end;
  697. function lowercase(const s : ansistring) : ansistring;
  698. var
  699. i : longint;
  700. begin
  701. Setlength(result,length(s));
  702. for i := 1 to length (s) do
  703. result[i] := lowercase(s[i]);
  704. end;
  705. {
  706. $Log$
  707. Revision 1.21 2001-08-30 15:43:15 jonas
  708. * converted adding/comparing of strings to compileproc. Note that due
  709. to the way the shortstring helpers for i386 are written, they are
  710. still handled by the old code (reason: fpc_shortstr_compare returns
  711. results in the flags instead of in eax and fpc_shortstr_concat
  712. has wierd parameter conventions). The compilerproc stuff should work
  713. fine with the generic implementations though.
  714. * removed some nested comments warnings
  715. Revision 1.20 2001/08/29 19:49:04 jonas
  716. * some fixes in compilerprocs for chararray to string conversions
  717. * conversion from string to chararray is now also done via compilerprocs
  718. Revision 1.19 2001/08/28 13:24:47 jonas
  719. + compilerproc implementation of most string-related type conversions
  720. - removed all code from the compiler which has been replaced by
  721. compilerproc implementations (using (ifdef hascompilerproc) is not
  722. necessary in the compiler)
  723. Revision 1.18 2001/08/13 12:40:16 jonas
  724. * renamed some str(x,y) and val(x,y) helpers so the naming scheme is the
  725. same for all string types
  726. + added the str(x,y) and val(x,y,z) helpers for int64/qword to
  727. compproc.inc
  728. Revision 1.17 2001/08/01 15:00:10 jonas
  729. + "compproc" helpers
  730. * renamed several helpers so that their name is the same as their
  731. "public alias", which should facilitate the conversion of processor
  732. specific code in the code generator to processor independent code
  733. * some small fixes to the val_ansistring and val_widestring helpers
  734. (always immediately exit if the source string is longer than 255
  735. chars)
  736. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  737. still nil (used to crash, now return resp -1 and 0)
  738. Revision 1.16 2001/07/10 18:04:37 peter
  739. * merged textfile, readlink and concat ansistring fixes
  740. Revision 1.15 2001/07/09 21:15:41 peter
  741. * Length made internal
  742. * Add array support for Length
  743. Revision 1.14 2001/07/09 11:41:57 florian
  744. * another MT fix
  745. Revision 1.13 2001/07/08 21:00:18 peter
  746. * various widestring updates, it works now mostly without charset
  747. mapping supported
  748. Revision 1.12 2001/07/04 12:17:09 jonas
  749. * removed DestSize parameter from declaration of ValAnsiSignedInt64
  750. (merged)
  751. Revision 1.11 2001/05/27 14:28:44 florian
  752. + made the ref. couting MT safe
  753. Revision 1.10 2001/04/13 18:06:07 peter
  754. * upcase, lowercase for ansistring
  755. Revision 1.9 2000/12/10 15:00:14 florian
  756. * val for int64 hopefully works now correct
  757. Revision 1.8 2000/12/08 14:04:43 jonas
  758. + added pos(char,ansistring), because there is also a pos(char,shortstring)
  759. and without the ansistring version, the shortstring version is always
  760. called when calling pos(char,pchar), even when using $h+ (because the
  761. first parameter matches exactly) (merged)
  762. Revision 1.7 2000/11/06 20:34:24 peter
  763. * changed ver1_0 defines to temporary defs
  764. Revision 1.6 2000/10/21 18:20:17 florian
  765. * a lot of small changes:
  766. - setlength is internal
  767. - win32 graph unit extended
  768. ....
  769. Revision 1.5 2000/08/29 18:39:42 peter
  770. * fixed chararray to ansistring (merged)
  771. Revision 1.4 2000/08/24 07:37:21 jonas
  772. * fixed bug in setlength (it sometimes read after the end of the heap)
  773. and small improvement to ansistring_to_chararray conversion (merged
  774. from fixes branch)
  775. Revision 1.3 2000/08/09 19:31:18 marco
  776. * fixes for val(int64 or qword) to ansistring
  777. Revision 1.2 2000/07/13 11:33:42 michael
  778. + removed logs
  779. }