astrings.inc 24 KB

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