astrings.inc 25 KB

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