astrings.inc 24 KB

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