astrings.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [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. pointer(result) := pointer(s);
  465. If Pointer(S)=Nil then
  466. exit;
  467. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  468. begin
  469. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  470. SNew:=NewAnsiString (L);
  471. Move (Pointer(S)^,SNew^,L+1);
  472. PAnsiRec(SNew-FirstOff)^.len:=L;
  473. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  474. pointer(S):=SNew;
  475. pointer(result):=SNew;
  476. end;
  477. end;
  478. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  479. begin
  480. SetLength(S,length(S)+1);
  481. S[length(S)]:=c;
  482. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  483. end;
  484. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; {$ifdef hascompilerproc} compilerproc; {$endif}
  485. var
  486. ofs : StrLenInt;
  487. begin
  488. ofs:=Length(S);
  489. SetLength(S,ofs+length(Str));
  490. move(Str[1],S[ofs+1],length(Str));
  491. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  492. end;
  493. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; {$ifdef hascompilerproc} compilerproc; {$endif}
  494. var
  495. ofs : StrLenInt;
  496. begin
  497. if Str<>'' then
  498. begin
  499. ofs:=Length(S);
  500. SetLength(S,ofs+length(Str));
  501. move(Str[1],S[ofs+1],length(Str)+1);
  502. end;
  503. end;
  504. {$ifdef interncopy}
  505. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : Longint) : AnsiString;compilerproc;
  506. {$else}
  507. Function Copy (Const S : AnsiString; Index,Size : Longint) : AnsiString;
  508. {$endif}
  509. var
  510. ResultAddress : Pointer;
  511. begin
  512. ResultAddress:=Nil;
  513. dec(index);
  514. if Index < 0 then
  515. Index := 0;
  516. { Check Size. Accounts for Zero-length S, the double check is needed because
  517. Size can be maxint and will get <0 when adding index }
  518. if (Size>Length(S)) or
  519. (Index+Size>Length(S)) then
  520. Size:=Length(S)-Index;
  521. If Size>0 then
  522. begin
  523. If Index<0 Then
  524. Index:=0;
  525. ResultAddress:=Pointer(NewAnsiString (Size));
  526. if ResultAddress<>Nil then
  527. begin
  528. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  529. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  530. PByte(ResultAddress+Size)^:=0;
  531. end;
  532. end;
  533. {$ifdef interncopy}
  534. Pointer(fpc_ansistr_Copy):=ResultAddress;
  535. {$else}
  536. Pointer(Copy):=ResultAddress;
  537. {$endif}
  538. end;
  539. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : Longint;
  540. var
  541. i,MaxLen : StrLenInt;
  542. pc : pchar;
  543. begin
  544. Pos:=0;
  545. if Length(SubStr)>0 then
  546. begin
  547. MaxLen:=Length(source)-Length(SubStr);
  548. i:=0;
  549. pc:=@source[1];
  550. while (i<=MaxLen) do
  551. begin
  552. inc(i);
  553. if (SubStr[1]=pc^) and
  554. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  555. begin
  556. Pos:=i;
  557. exit;
  558. end;
  559. inc(pc);
  560. end;
  561. end;
  562. end;
  563. { Faster version for a char alone. Must be implemented because }
  564. { pos(c: char; const s: shortstring) also exists, so otherwise }
  565. { using pos(char,pchar) will always call the shortstring version }
  566. { (exact match for first argument), also with $h+ (JM) }
  567. Function Pos (c : Char; Const s : AnsiString) : Longint;
  568. var
  569. i: longint;
  570. pc : pchar;
  571. begin
  572. pc:=@s[1];
  573. for i:=1 to length(s) do
  574. begin
  575. if pc^=c then
  576. begin
  577. pos:=i;
  578. exit;
  579. end;
  580. inc(pc);
  581. end;
  582. pos:=0;
  583. end;
  584. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  585. Var
  586. SS : String;
  587. begin
  588. fpc_Val_Real_AnsiStr := 0;
  589. if length(S) > 255 then
  590. code := 256
  591. else
  592. begin
  593. SS := S;
  594. Val(SS,fpc_Val_Real_AnsiStr,code);
  595. end;
  596. end;
  597. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  598. Var
  599. SS : ShortString;
  600. begin
  601. fpc_Val_UInt_AnsiStr := 0;
  602. if length(S) > 255 then
  603. code := 256
  604. else
  605. begin
  606. SS := S;
  607. Val(SS,fpc_Val_UInt_AnsiStr,code);
  608. end;
  609. end;
  610. Function fpc_Val_SInt_AnsiStr (DestSize: longint; Const S : AnsiString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  611. Var
  612. SS : ShortString;
  613. begin
  614. fpc_Val_SInt_AnsiStr:=0;
  615. if length(S)>255 then
  616. code:=256
  617. else
  618. begin
  619. SS := S;
  620. fpc_Val_SInt_AnsiStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  621. end;
  622. end;
  623. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  624. Var
  625. SS : ShortString;
  626. begin
  627. fpc_Val_qword_AnsiStr:=0;
  628. if length(S)>255 then
  629. code:=256
  630. else
  631. begin
  632. SS := S;
  633. Val(SS,fpc_Val_qword_AnsiStr,Code);
  634. end;
  635. end;
  636. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  637. Var
  638. SS : ShortString;
  639. begin
  640. fpc_Val_int64_AnsiStr:=0;
  641. if length(S)>255 then
  642. code:=256
  643. else
  644. begin
  645. SS := s;
  646. Val(SS,fpc_Val_int64_AnsiStr,Code);
  647. end;
  648. end;
  649. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : longint;var s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  650. var
  651. ss: ShortString;
  652. begin
  653. str_real(len,fr,d,treal_type(rt),ss);
  654. s:=ss;
  655. end;
  656. Procedure fpc_AnsiStr_Longword(C : Longword;Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  657. Var
  658. SS : ShortString;
  659. begin
  660. str(C:Len,SS);
  661. S:=SS;
  662. end;
  663. Procedure fpc_AnsiStr_Longint(L : Longint; Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  664. Var
  665. SS : ShortString;
  666. begin
  667. str (L:Len,SS);
  668. S:=SS;
  669. end;
  670. Procedure Delete (Var S : AnsiString; Index,Size: Longint);
  671. Var
  672. LS : Longint;
  673. begin
  674. ls:=Length(S);
  675. If (Index>LS) or (Index<=0) or (Size<=0) then
  676. exit;
  677. UniqueString (S);
  678. If (Size>LS-Index) then // Size+Index gives overflow ??
  679. Size:=LS-Index+1;
  680. If (Size<=LS-Index) then
  681. begin
  682. Dec(Index);
  683. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  684. end;
  685. Setlength(S,LS-Size);
  686. end;
  687. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : Longint);
  688. var
  689. Temp : AnsiString;
  690. LS : Longint;
  691. begin
  692. If Length(Source)=0 then
  693. exit;
  694. if index <= 0 then
  695. index := 1;
  696. Ls:=Length(S);
  697. if index > LS then
  698. index := LS+1;
  699. Dec(Index);
  700. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  701. SetLength(Temp,Length(Source)+LS);
  702. If Index>0 then
  703. move (Pointer(S)^,Pointer(Temp)^,Index);
  704. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  705. If (LS-Index)>0 then
  706. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  707. S:=Temp;
  708. end;
  709. Function StringOfChar(c : char;l : longint) : AnsiString;
  710. begin
  711. SetLength(StringOfChar,l);
  712. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  713. end;
  714. Procedure SetString (Var S : AnsiString; Buf : PChar; Len : Longint);
  715. begin
  716. SetLength(S,Len);
  717. If (Buf<>Nil) then
  718. begin
  719. Move (Buf[0],S[1],Len);
  720. end;
  721. end;
  722. function upcase(const s : ansistring) : ansistring;
  723. var
  724. i : longint;
  725. begin
  726. Setlength(result,length(s));
  727. for i := 1 to length (s) do
  728. result[i] := upcase(s[i]);
  729. end;
  730. function lowercase(const s : ansistring) : ansistring;
  731. var
  732. i : longint;
  733. begin
  734. Setlength(result,length(s));
  735. for i := 1 to length (s) do
  736. result[i] := lowercase(s[i]);
  737. end;
  738. {
  739. $Log$
  740. Revision 1.39 2003-06-17 19:24:08 jonas
  741. * fixed conversion of fpc_*str_unique to compilerproc
  742. Revision 1.38 2003/06/17 16:38:53 jonas
  743. * fpc_{ansistr|widestr}_unique is now a function so it can be used as
  744. compilerproc
  745. Revision 1.37 2003/05/01 08:05:23 florian
  746. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  747. Revision 1.36 2003/02/26 19:16:55 jonas
  748. * fixed setstring (+- like suggested by Dimitry Sibiryakov)
  749. Revision 1.35 2002/12/09 08:33:31 michael
  750. + Fixed range check error and others in Delete
  751. Revision 1.34 2002/12/07 14:34:30 carl
  752. - avoid warnings (add typecast)
  753. Revision 1.33 2002/10/21 19:52:47 jonas
  754. Revision 1.1.2.17 2002/12/09 08:32:34 michael
  755. + Fixed range check error and others in Delete
  756. Revision 1.1.2.16 2002/10/21 19:30:57 jonas
  757. * fixed some buffer overflow errors in SetString (both short and
  758. ansistring versions) (merged)
  759. Revision 1.32 2002/10/20 12:59:21 jonas
  760. * fixed ansistring append helpers so they preserve the terminating #0
  761. * optimized SetLength() so that it uses reallocmem in case the refcount
  762. of the target string is 1
  763. Revision 1.31 2002/10/19 17:06:50 michael
  764. + Added check for nil buffer to setstring
  765. Revision 1.30 2002/10/17 12:43:00 florian
  766. + ansistring_append* implemented
  767. Revision 1.29 2002/10/02 18:21:51 peter
  768. * Copy() changed to internal function calling compilerprocs
  769. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  770. new copy functions
  771. Revision 1.28 2002/09/14 11:20:50 carl
  772. * Delphi compatibility fix (with string routines)
  773. Revision 1.27 2002/09/07 21:10:47 carl
  774. * cardinal -> longword
  775. - remove some unused routines
  776. Revision 1.26 2002/09/07 15:07:44 peter
  777. * old logs removed and tabs fixed
  778. Revision 1.25 2002/04/26 15:19:05 peter
  779. * use saveregisters for incr routines, saves also problems with
  780. the optimizer
  781. Revision 1.24 2002/04/25 20:14:56 peter
  782. * updated compilerprocs
  783. * incr ref count has now a value argument instead of var
  784. Revision 1.23 2002/01/07 13:23:53 jonas
  785. * fixed bug in fpc_char_to_ansistr when converting #0 (found by Peter)
  786. }