astrings.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 : SizeInt for maximum size;
  21. @-8 : SizeInt for size;
  22. @-4 : SizeInt 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 : SizeInt;
  35. First : Char;
  36. end;
  37. Const
  38. AnsiRecLen = SizeOf(TAnsiRec);
  39. FirstOff = SizeOf(TAnsiRec)-1;
  40. {****************************************************************************
  41. Internal functions, not in interface.
  42. ****************************************************************************}
  43. Function NewAnsiString(Len : SizeInt) : Pointer;
  44. {
  45. Allocate a new AnsiString on the heap.
  46. initialize it to zero length and reference count 1.
  47. }
  48. Var
  49. P : Pointer;
  50. l : SizeInt;
  51. begin
  52. l:=Len+AnsiRecLen;
  53. { request a multiple of 16 because the heap manager alloctes anyways chunks of 16 bytes }
  54. if (l mod 16)<>0 then
  55. inc(l,16-(l mod 16));
  56. GetMem(P,l);
  57. If P<>Nil then
  58. begin
  59. PAnsiRec(P)^.Maxlen:=l-AnsiRecLen; { Maximal length }
  60. PAnsiRec(P)^.Len:=0; { Initial length }
  61. PAnsiRec(P)^.Ref:=1; { Set reference count }
  62. PAnsiRec(P)^.First:=#0; { Terminating #0 }
  63. P:=P+FirstOff; { Points to string now }
  64. end;
  65. NewAnsiString:=P;
  66. end;
  67. Procedure DisposeAnsiString(Var S : Pointer);
  68. {
  69. Deallocates a AnsiString From the heap.
  70. }
  71. begin
  72. If S=Nil then
  73. exit;
  74. Dec (S,FirstOff);
  75. FreeMem (S);
  76. S:=Nil;
  77. end;
  78. Procedure fpc_AnsiStr_Decr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_ANSISTR_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  79. {
  80. Decreases the ReferenceCount of a non constant ansistring;
  81. If the reference count is zero, deallocate the string;
  82. }
  83. Type
  84. pSizeInt = ^SizeInt;
  85. Var
  86. l : pSizeInt;
  87. Begin
  88. { Zero string }
  89. If S=Nil then exit;
  90. { check for constant strings ...}
  91. l:=@PANSIREC(S-FirstOff)^.Ref;
  92. If l^<0 then exit;
  93. { declocked does a MT safe dec and returns true, if the counter is 0 }
  94. If declocked(l^) then
  95. { Ref count dropped to zero }
  96. DisposeAnsiString (S); { Remove...}
  97. {$ifndef decrrefnotnil}
  98. s:=nil;
  99. {$endif}
  100. end;
  101. {$ifdef hascompilerproc}
  102. { also define alias for internal use in the system unit }
  103. Procedure fpc_AnsiStr_Decr_Ref (Var S : Pointer);saveregisters; [external name 'FPC_ANSISTR_DECR_REF'];
  104. {$endif hascompilerproc}
  105. {$ifdef hascompilerproc}
  106. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer);saveregisters;[Public,Alias:'FPC_ANSISTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  107. {$else}
  108. Procedure fpc_AnsiStr_Incr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_ANSISTR_INCR_REF'];
  109. {$endif}
  110. Begin
  111. If S=Nil then
  112. exit;
  113. { Let's be paranoid : Constant string ??}
  114. If PAnsiRec(S-FirstOff)^.Ref<0 then exit;
  115. inclocked(PAnsiRec(S-FirstOff)^.Ref);
  116. end;
  117. {$ifdef hascompilerproc}
  118. { also define alias which can be used inside the system unit }
  119. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer);saveregisters; [external name 'FPC_ANSISTR_INCR_REF'];
  120. {$endif hascompilerproc}
  121. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_ANSISTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  122. {
  123. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  124. }
  125. begin
  126. If S2<>nil then
  127. If PAnsiRec(S2-FirstOff)^.Ref>0 then
  128. inclocked(PAnsiRec(S2-FirstOff)^.ref);
  129. { Decrease the reference count on the old S1 }
  130. fpc_ansistr_decr_ref (S1);
  131. { And finally, have S1 pointing to S2 (or its copy) }
  132. S1:=S2;
  133. end;
  134. {$ifdef hascompilerproc}
  135. { alias for internal use }
  136. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_ANSISTR_ASSIGN'];
  137. {$endif hascompilerproc}
  138. {$ifdef hascompilerproc}
  139. function fpc_AnsiStr_Concat (const S1,S2 : AnsiString): ansistring; compilerproc;
  140. var
  141. S3: ansistring absolute result;
  142. {$else hascompilerproc}
  143. Procedure fpc_AnsiStr_Concat (const S1,S2 : ansistring;var S3 : ansistring);[Public, alias: 'FPC_ANSISTR_CONCAT'];
  144. {$endif hascompilerproc}
  145. {
  146. Concatenates 2 AnsiStrings : S1+S2.
  147. Result Goes to S3;
  148. }
  149. Var
  150. Size,Location : SizeInt;
  151. begin
  152. { only assign if s1 or s2 is empty }
  153. if (S1='') then
  154. s3 := s2
  155. else if (S2='') then
  156. s3 := s1
  157. else
  158. begin
  159. Size:=length(S2);
  160. Location:=Length(S1);
  161. SetLength (S3,Size+Location);
  162. { the cast to a pointer avoids the unique call }
  163. { and we don't need an unique call }
  164. { because of the SetLength S3 is unique }
  165. Move (S1[1],pointer(S3)^,Location);
  166. Move (S2[1],pointer(pointer(S3)+location)^,Size+1);
  167. end;
  168. end;
  169. {$ifdef EXTRAANSISHORT}
  170. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  171. {
  172. Concatenates a Ansi with a short string; : S2 + S2
  173. }
  174. Var
  175. Size,Location : SizeInt;
  176. begin
  177. Size:=Length(S2);
  178. Location:=Length(S1);
  179. If Size=0 then
  180. exit;
  181. { Setlength takes case of uniqueness
  182. and alllocated memory. We need to use length,
  183. to take into account possibility of S1=Nil }
  184. SetLength (S1,Size+Length(S1));
  185. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  186. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  187. end;
  188. {$endif EXTRAANSISHORT}
  189. { the following declaration has exactly the same effect as }
  190. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  191. { which is what the old helper was, so we don't need an extra implementation }
  192. { of the old helper (JM) }
  193. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  194. {
  195. Converts a AnsiString to a ShortString;
  196. }
  197. Var
  198. Size : SizeInt;
  199. begin
  200. if S2='' then
  201. fpc_AnsiStr_To_ShortStr:=''
  202. else
  203. begin
  204. Size:=Length(S2);
  205. If Size>high_of_res then
  206. Size:=high_of_res;
  207. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  208. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  209. end;
  210. end;
  211. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; {$ifdef hascompilerproc} compilerproc; {$endif}
  212. {
  213. Converts a ShortString to a AnsiString;
  214. }
  215. Var
  216. Size : SizeInt;
  217. begin
  218. Size:=Length(S2);
  219. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  220. if Size>0 then
  221. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  222. end;
  223. { old style helper }
  224. {$ifndef hascompilerproc}
  225. Procedure fpc_ShortStr_To_AnsiStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_ANSISTR'];
  226. begin
  227. s1 := pointer(fpc_ShortStr_To_AnsiStr(s2));
  228. end;
  229. {$endif hascompilerproc}
  230. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
  231. {
  232. Converts a Char to a AnsiString;
  233. }
  234. begin
  235. Setlength (fpc_Char_To_AnsiStr,1);
  236. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  237. { Terminating Zero }
  238. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  239. end;
  240. { old style helper }
  241. {$ifndef hascompilerproc}
  242. Procedure fpc_Char_To_AnsiStr(var S1 : Pointer; c : Char);[Public, alias: 'FPC_CHAR_TO_ANSISTR'];
  243. begin
  244. s1 := pointer(fpc_Char_To_AnsiStr(c));
  245. end;
  246. {$endif hascompilerproc}
  247. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; {$ifdef hascompilerproc} compilerproc; {$endif}
  248. Var
  249. L : SizeInt;
  250. begin
  251. if (not assigned(p)) or (p[0]=#0) Then
  252. { result is automatically set to '' }
  253. exit;
  254. l:=IndexChar(p^,-1,#0);
  255. SetLength(fpc_PChar_To_AnsiStr,L);
  256. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  257. end;
  258. { old style helper }
  259. {$ifndef hascompilerproc}
  260. Procedure fpc_PChar_To_AnsiStr(var a : ansistring;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  261. begin
  262. pointer(a) := pointer(fpc_PChar_To_AnsiStr(p));
  263. end;
  264. {$endif hascompilerproc}
  265. Function fpc_CharArray_To_AnsiStr(const arr: array of char): ansistring; {$ifdef hascompilerproc} compilerproc; {$endif}
  266. var
  267. i : SizeInt;
  268. begin
  269. if arr[0]=#0 Then
  270. { result is automatically set to '' }
  271. exit;
  272. i:=IndexChar(arr,high(arr)+1,#0);
  273. if i = -1 then
  274. i := high(arr)+1;
  275. SetLength(fpc_CharArray_To_AnsiStr,i);
  276. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  277. end;
  278. { old style helper }
  279. {$ifndef hascompilerproc}
  280. { the declaration below is the same as }
  281. { which is what the old helper was (we need the parameter as "array of char" type }
  282. { so we can pass it to the new style helper (JM) }
  283. Procedure fpc_CharArray_To_AnsiStr(var a : ansistring; p: pointer; len: SizeInt);[Public,Alias : 'FPC_CHARARRAY_TO_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  284. var
  285. src: pchar;
  286. i: SizeInt;
  287. begin
  288. src := pchar(p);
  289. if src[0]=#0 Then
  290. { result is automatically set to '' }
  291. begin
  292. pointer(a) := nil;
  293. exit;
  294. end;
  295. i:=IndexChar(src^,len,#0);
  296. if i = -1 then
  297. i := len;
  298. pointer(a) := NewAnsiString(i);
  299. Move (src^,a[1],i);
  300. end;
  301. {$endif not hascompilerproc}
  302. {$ifdef hascompilerproc}
  303. { note: inside the compiler, the resulttype is modified to be the length }
  304. { of the actual chararray to which we convert (JM) }
  305. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  306. var
  307. len: SizeInt;
  308. begin
  309. len := length(src);
  310. if len > arraysize then
  311. len := arraysize;
  312. { make sure we don't try to access element 1 of the ansistring if it's nil }
  313. if len > 0 then
  314. move(src[1],fpc_ansistr_to_chararray[0],len);
  315. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  316. end;
  317. {$endif hascompilerproc}
  318. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  319. {
  320. Compares 2 AnsiStrings;
  321. The result is
  322. <0 if S1<S2
  323. 0 if S1=S2
  324. >0 if S1>S2
  325. }
  326. Var
  327. MaxI,Temp : SizeInt;
  328. begin
  329. if pointer(S1)=pointer(S2) then
  330. begin
  331. fpc_AnsiStr_Compare:=0;
  332. exit;
  333. end;
  334. Maxi:=Length(S1);
  335. temp:=Length(S2);
  336. If MaxI>Temp then
  337. MaxI:=Temp;
  338. Temp:=CompareByte(S1[1],S2[1],MaxI);
  339. if temp=0 then
  340. temp:=Length(S1)-Length(S2);
  341. fpc_AnsiStr_Compare:=Temp;
  342. end;
  343. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  344. begin
  345. if p=nil then
  346. HandleErrorFrame(201,get_frame);
  347. end;
  348. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  349. begin
  350. if (index>len) or (Index<1) then
  351. HandleErrorFrame(201,get_frame);
  352. end;
  353. {$ifndef INTERNSETLENGTH}
  354. Procedure SetLength (Var S : AnsiString; l : SizeInt);
  355. {$else INTERNSETLENGTH}
  356. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  357. {$endif INTERNSETLENGTH}
  358. {
  359. Sets The length of string S to L.
  360. Makes sure S is unique, and contains enough room.
  361. }
  362. Var
  363. Temp : Pointer;
  364. movelen, NewLen: SizeInt;
  365. begin
  366. if (l>0) then
  367. begin
  368. if Pointer(S)=nil then
  369. begin
  370. { Need a complete new string...}
  371. Pointer(s):=NewAnsiString(l);
  372. end
  373. else if (PAnsiRec(Pointer(S)-FirstOff)^.Ref = 1) then
  374. begin
  375. if (PAnsiRec(Pointer(S)-FirstOff)^.Maxlen < L) then
  376. begin
  377. Dec(Pointer(S),FirstOff);
  378. NewLen := (L+AnsiRecLen+15) and not(15) - AnsiRecLen;
  379. reallocmem(pointer(S),AnsiRecLen+NewLen);
  380. PAnsiRec(S)^.MaxLen := NewLen;
  381. Inc(Pointer(S),FirstOff);
  382. end;
  383. PAnsiRec(Pointer(S)-FirstOff)^.Len := L;
  384. PByte(Pointer(S)+L)^:=0;
  385. end
  386. else
  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): SizeInt; {$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 : SizeInt;
  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) : SizeInt;
  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. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [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 : SizeInt;
  464. begin
  465. pointer(result) := pointer(s);
  466. If Pointer(S)=Nil then
  467. exit;
  468. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  469. begin
  470. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  471. SNew:=NewAnsiString (L);
  472. Move (Pointer(S)^,SNew^,L+1);
  473. PAnsiRec(SNew-FirstOff)^.len:=L;
  474. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  475. pointer(S):=SNew;
  476. pointer(result):=SNew;
  477. end;
  478. end;
  479. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  480. begin
  481. SetLength(S,length(S)+1);
  482. S[length(S)]:=c;
  483. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  484. end;
  485. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; {$ifdef hascompilerproc} compilerproc; {$endif}
  486. var
  487. ofs : SizeInt;
  488. begin
  489. ofs:=Length(S);
  490. SetLength(S,ofs+length(Str));
  491. move(Str[1],S[ofs+1],length(Str));
  492. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  493. end;
  494. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; {$ifdef hascompilerproc} compilerproc; {$endif}
  495. var
  496. ofs : SizeInt;
  497. begin
  498. if Str<>'' then
  499. begin
  500. ofs:=Length(S);
  501. SetLength(S,ofs+length(Str));
  502. move(Str[1],S[ofs+1],length(Str)+1);
  503. end;
  504. end;
  505. {$ifdef interncopy}
  506. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  507. {$else}
  508. Function Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;
  509. {$endif}
  510. var
  511. ResultAddress : Pointer;
  512. begin
  513. ResultAddress:=Nil;
  514. dec(index);
  515. if Index < 0 then
  516. Index := 0;
  517. { Check Size. Accounts for Zero-length S, the double check is needed because
  518. Size can be maxint and will get <0 when adding index }
  519. if (Size>Length(S)) or
  520. (Index+Size>Length(S)) then
  521. Size:=Length(S)-Index;
  522. If Size>0 then
  523. begin
  524. If Index<0 Then
  525. Index:=0;
  526. ResultAddress:=Pointer(NewAnsiString (Size));
  527. if ResultAddress<>Nil then
  528. begin
  529. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  530. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  531. PByte(ResultAddress+Size)^:=0;
  532. end;
  533. end;
  534. {$ifdef interncopy}
  535. Pointer(fpc_ansistr_Copy):=ResultAddress;
  536. {$else}
  537. Pointer(Copy):=ResultAddress;
  538. {$endif}
  539. end;
  540. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  541. var
  542. i,MaxLen : SizeInt;
  543. pc : pchar;
  544. begin
  545. Pos:=0;
  546. if Length(SubStr)>0 then
  547. begin
  548. MaxLen:=Length(source)-Length(SubStr);
  549. i:=0;
  550. pc:=@source[1];
  551. while (i<=MaxLen) do
  552. begin
  553. inc(i);
  554. if (SubStr[1]=pc^) and
  555. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  556. begin
  557. Pos:=i;
  558. exit;
  559. end;
  560. inc(pc);
  561. end;
  562. end;
  563. end;
  564. { Faster version for a char alone. Must be implemented because }
  565. { pos(c: char; const s: shortstring) also exists, so otherwise }
  566. { using pos(char,pchar) will always call the shortstring version }
  567. { (exact match for first argument), also with $h+ (JM) }
  568. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  569. var
  570. i: SizeInt;
  571. pc : pchar;
  572. begin
  573. pc:=@s[1];
  574. for i:=1 to length(s) do
  575. begin
  576. if pc^=c then
  577. begin
  578. pos:=i;
  579. exit;
  580. end;
  581. inc(pc);
  582. end;
  583. pos:=0;
  584. end;
  585. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  586. Var
  587. SS : String;
  588. begin
  589. fpc_Val_Real_AnsiStr := 0;
  590. if length(S) > 255 then
  591. code := 256
  592. else
  593. begin
  594. SS := S;
  595. Val(SS,fpc_Val_Real_AnsiStr,code);
  596. end;
  597. end;
  598. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  599. Var
  600. SS : ShortString;
  601. begin
  602. fpc_Val_UInt_AnsiStr := 0;
  603. if length(S) > 255 then
  604. code := 256
  605. else
  606. begin
  607. SS := S;
  608. Val(SS,fpc_Val_UInt_AnsiStr,code);
  609. end;
  610. end;
  611. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  612. Var
  613. SS : ShortString;
  614. begin
  615. fpc_Val_SInt_AnsiStr:=0;
  616. if length(S)>255 then
  617. code:=256
  618. else
  619. begin
  620. SS := S;
  621. fpc_Val_SInt_AnsiStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  622. end;
  623. end;
  624. {$ifndef CPU64}
  625. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  626. Var
  627. SS : ShortString;
  628. begin
  629. fpc_Val_qword_AnsiStr:=0;
  630. if length(S)>255 then
  631. code:=256
  632. else
  633. begin
  634. SS := S;
  635. Val(SS,fpc_Val_qword_AnsiStr,Code);
  636. end;
  637. end;
  638. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  639. Var
  640. SS : ShortString;
  641. begin
  642. fpc_Val_int64_AnsiStr:=0;
  643. if length(S)>255 then
  644. code:=256
  645. else
  646. begin
  647. SS := s;
  648. Val(SS,fpc_Val_int64_AnsiStr,Code);
  649. end;
  650. end;
  651. {$endif CPU64}
  652. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;var s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  653. var
  654. ss: ShortString;
  655. begin
  656. str_real(len,fr,d,treal_type(rt),ss);
  657. s:=ss;
  658. end;
  659. {$ifdef STR_USES_VALINT}
  660. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  661. {$else}
  662. Procedure fpc_AnsiStr_Longword(v : Longword;Len : SizeInt; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  663. {$endif}
  664. Var
  665. SS : ShortString;
  666. begin
  667. str(v:Len,SS);
  668. S:=SS;
  669. end;
  670. {$ifdef STR_USES_VALINT}
  671. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  672. {$else}
  673. Procedure fpc_AnsiStr_Longint(v : Longint; Len : SizeInt; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  674. {$endif}
  675. Var
  676. SS : ShortString;
  677. begin
  678. str (v:Len,SS);
  679. S:=SS;
  680. end;
  681. {$ifndef CPU64}
  682. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  683. Var
  684. SS : ShortString;
  685. begin
  686. str(v:Len,SS);
  687. S:=SS;
  688. end;
  689. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; {$ifdef hascompilerproc} compilerproc; {$endif}
  690. Var
  691. SS : ShortString;
  692. begin
  693. str (v:Len,SS);
  694. S:=SS;
  695. end;
  696. {$endif CPU64}
  697. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  698. Var
  699. LS : SizeInt;
  700. begin
  701. ls:=Length(S);
  702. If (Index>LS) or (Index<=0) or (Size<=0) then
  703. exit;
  704. UniqueString (S);
  705. If (Size>LS-Index) then // Size+Index gives overflow ??
  706. Size:=LS-Index+1;
  707. If (Size<=LS-Index) then
  708. begin
  709. Dec(Index);
  710. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  711. end;
  712. Setlength(S,LS-Size);
  713. end;
  714. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  715. var
  716. Temp : AnsiString;
  717. LS : SizeInt;
  718. begin
  719. If Length(Source)=0 then
  720. exit;
  721. if index <= 0 then
  722. index := 1;
  723. Ls:=Length(S);
  724. if index > LS then
  725. index := LS+1;
  726. Dec(Index);
  727. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  728. SetLength(Temp,Length(Source)+LS);
  729. If Index>0 then
  730. move (Pointer(S)^,Pointer(Temp)^,Index);
  731. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  732. If (LS-Index)>0 then
  733. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  734. S:=Temp;
  735. end;
  736. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  737. begin
  738. SetLength(StringOfChar,l);
  739. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  740. end;
  741. Procedure SetString (Var S : AnsiString; Buf : PChar; Len : SizeInt);
  742. begin
  743. SetLength(S,Len);
  744. If (Buf<>Nil) then
  745. begin
  746. Move (Buf[0],S[1],Len);
  747. end;
  748. end;
  749. function upcase(const s : ansistring) : ansistring;
  750. var
  751. i : SizeInt;
  752. begin
  753. Setlength(result,length(s));
  754. for i := 1 to length (s) do
  755. result[i] := upcase(s[i]);
  756. end;
  757. function lowercase(const s : ansistring) : ansistring;
  758. var
  759. i : SizeInt;
  760. begin
  761. Setlength(result,length(s));
  762. for i := 1 to length (s) do
  763. result[i] := lowercase(s[i]);
  764. end;
  765. {
  766. $Log$
  767. Revision 1.43 2004-05-01 23:55:18 peter
  768. * replace strlenint with sizeint
  769. Revision 1.42 2004/04/29 18:59:43 peter
  770. * str() helpers now also use valint/valuint
  771. * int64/qword helpers disabled for cpu64
  772. Revision 1.41 2004/01/21 22:14:05 peter
  773. * 1.0.x fix
  774. Revision 1.40 2004/01/21 22:02:18 peter
  775. * decrref does not reset always to nil, only when string is disposed.
  776. the reset to nil for temps is done by the compiler
  777. Revision 1.39 2003/06/17 19:24:08 jonas
  778. * fixed conversion of fpc_*str_unique to compilerproc
  779. Revision 1.38 2003/06/17 16:38:53 jonas
  780. * fpc_ansistr|widestr_unique is now a function so it can be used as
  781. compilerproc
  782. Revision 1.37 2003/05/01 08:05:23 florian
  783. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  784. Revision 1.36 2003/02/26 19:16:55 jonas
  785. * fixed setstring (+- like suggested by Dimitry Sibiryakov)
  786. Revision 1.35 2002/12/09 08:33:31 michael
  787. + Fixed range check error and others in Delete
  788. Revision 1.34 2002/12/07 14:34:30 carl
  789. - avoid warnings (add typecast)
  790. Revision 1.33 2002/10/21 19:52:47 jonas
  791. Revision 1.1.2.17 2002/12/09 08:32:34 michael
  792. + Fixed range check error and others in Delete
  793. Revision 1.1.2.16 2002/10/21 19:30:57 jonas
  794. * fixed some buffer overflow errors in SetString (both short and
  795. ansistring versions) (merged)
  796. Revision 1.32 2002/10/20 12:59:21 jonas
  797. * fixed ansistring append helpers so they preserve the terminating #0
  798. * optimized SetLength() so that it uses reallocmem in case the refcount
  799. of the target string is 1
  800. Revision 1.31 2002/10/19 17:06:50 michael
  801. + Added check for nil buffer to setstring
  802. Revision 1.30 2002/10/17 12:43:00 florian
  803. + ansistring_append* implemented
  804. Revision 1.29 2002/10/02 18:21:51 peter
  805. * Copy() changed to internal function calling compilerprocs
  806. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  807. new copy functions
  808. Revision 1.28 2002/09/14 11:20:50 carl
  809. * Delphi compatibility fix (with string routines)
  810. Revision 1.27 2002/09/07 21:10:47 carl
  811. * cardinal -> longword
  812. - remove some unused routines
  813. Revision 1.26 2002/09/07 15:07:44 peter
  814. * old logs removed and tabs fixed
  815. Revision 1.25 2002/04/26 15:19:05 peter
  816. * use saveregisters for incr routines, saves also problems with
  817. the optimizer
  818. Revision 1.24 2002/04/25 20:14:56 peter
  819. * updated compilerprocs
  820. * incr ref count has now a value argument instead of var
  821. Revision 1.23 2002/01/07 13:23:53 jonas
  822. * fixed bug in fpc_char_to_ansistr when converting #0 (found by Peter)
  823. }