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