astrings.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. {$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 : Longint;
  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 : Longint;
  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: longint;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 : Longint;
  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 : Longint;
  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 : Longint;
  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 : longint;
  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: longint);[Public,Alias : 'FPC_CHARARRAY_TO_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  284. var
  285. src: pchar;
  286. i: longint;
  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: longint; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  306. var
  307. len: longint;
  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): Longint;[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 : Longint;
  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 : longint);[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 : Longint);
  355. {$else INTERNSETLENGTH}
  356. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : Longint);[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: longint;
  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): Longint; {$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 : Longint;
  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) : Longint;
  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 : Longint;
  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 : StrLenInt;
  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 : StrLenInt;
  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 : Longint) : AnsiString;compilerproc;
  507. {$else}
  508. Function Copy (Const S : AnsiString; Index,Size : Longint) : 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) : Longint;
  541. var
  542. i,MaxLen : StrLenInt;
  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) : Longint;
  569. var
  570. i: longint;
  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: longint; 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. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  625. Var
  626. SS : ShortString;
  627. begin
  628. fpc_Val_qword_AnsiStr:=0;
  629. if length(S)>255 then
  630. code:=256
  631. else
  632. begin
  633. SS := S;
  634. Val(SS,fpc_Val_qword_AnsiStr,Code);
  635. end;
  636. end;
  637. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  638. Var
  639. SS : ShortString;
  640. begin
  641. fpc_Val_int64_AnsiStr:=0;
  642. if length(S)>255 then
  643. code:=256
  644. else
  645. begin
  646. SS := s;
  647. Val(SS,fpc_Val_int64_AnsiStr,Code);
  648. end;
  649. end;
  650. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : longint;var s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  651. var
  652. ss: ShortString;
  653. begin
  654. str_real(len,fr,d,treal_type(rt),ss);
  655. s:=ss;
  656. end;
  657. Procedure fpc_AnsiStr_Longword(C : Longword;Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  658. Var
  659. SS : ShortString;
  660. begin
  661. str(C:Len,SS);
  662. S:=SS;
  663. end;
  664. Procedure fpc_AnsiStr_Longint(L : Longint; Len : Longint; Var S : AnsiString);[Public,Alias : 'FPC_ANSISTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  665. Var
  666. SS : ShortString;
  667. begin
  668. str (L:Len,SS);
  669. S:=SS;
  670. end;
  671. Procedure Delete (Var S : AnsiString; Index,Size: Longint);
  672. Var
  673. LS : Longint;
  674. begin
  675. ls:=Length(S);
  676. If (Index>LS) or (Index<=0) or (Size<=0) then
  677. exit;
  678. UniqueString (S);
  679. If (Size>LS-Index) then // Size+Index gives overflow ??
  680. Size:=LS-Index+1;
  681. If (Size<=LS-Index) then
  682. begin
  683. Dec(Index);
  684. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  685. end;
  686. Setlength(S,LS-Size);
  687. end;
  688. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : Longint);
  689. var
  690. Temp : AnsiString;
  691. LS : Longint;
  692. begin
  693. If Length(Source)=0 then
  694. exit;
  695. if index <= 0 then
  696. index := 1;
  697. Ls:=Length(S);
  698. if index > LS then
  699. index := LS+1;
  700. Dec(Index);
  701. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  702. SetLength(Temp,Length(Source)+LS);
  703. If Index>0 then
  704. move (Pointer(S)^,Pointer(Temp)^,Index);
  705. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  706. If (LS-Index)>0 then
  707. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  708. S:=Temp;
  709. end;
  710. Function StringOfChar(c : char;l : longint) : AnsiString;
  711. begin
  712. SetLength(StringOfChar,l);
  713. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  714. end;
  715. Procedure SetString (Var S : AnsiString; Buf : PChar; Len : Longint);
  716. begin
  717. SetLength(S,Len);
  718. If (Buf<>Nil) then
  719. begin
  720. Move (Buf[0],S[1],Len);
  721. end;
  722. end;
  723. function upcase(const s : ansistring) : ansistring;
  724. var
  725. i : longint;
  726. begin
  727. Setlength(result,length(s));
  728. for i := 1 to length (s) do
  729. result[i] := upcase(s[i]);
  730. end;
  731. function lowercase(const s : ansistring) : ansistring;
  732. var
  733. i : longint;
  734. begin
  735. Setlength(result,length(s));
  736. for i := 1 to length (s) do
  737. result[i] := lowercase(s[i]);
  738. end;
  739. {
  740. $Log$
  741. Revision 1.41 2004-01-21 22:14:05 peter
  742. * 1.0.x fix
  743. Revision 1.40 2004/01/21 22:02:18 peter
  744. * decrref does not reset always to nil, only when string is disposed.
  745. the reset to nil for temps is done by the compiler
  746. Revision 1.39 2003/06/17 19:24:08 jonas
  747. * fixed conversion of fpc_*str_unique to compilerproc
  748. Revision 1.38 2003/06/17 16:38:53 jonas
  749. * fpc_{ansistr|widestr}_unique is now a function so it can be used as
  750. compilerproc
  751. Revision 1.37 2003/05/01 08:05:23 florian
  752. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  753. Revision 1.36 2003/02/26 19:16:55 jonas
  754. * fixed setstring (+- like suggested by Dimitry Sibiryakov)
  755. Revision 1.35 2002/12/09 08:33:31 michael
  756. + Fixed range check error and others in Delete
  757. Revision 1.34 2002/12/07 14:34:30 carl
  758. - avoid warnings (add typecast)
  759. Revision 1.33 2002/10/21 19:52:47 jonas
  760. Revision 1.1.2.17 2002/12/09 08:32:34 michael
  761. + Fixed range check error and others in Delete
  762. Revision 1.1.2.16 2002/10/21 19:30:57 jonas
  763. * fixed some buffer overflow errors in SetString (both short and
  764. ansistring versions) (merged)
  765. Revision 1.32 2002/10/20 12:59:21 jonas
  766. * fixed ansistring append helpers so they preserve the terminating #0
  767. * optimized SetLength() so that it uses reallocmem in case the refcount
  768. of the target string is 1
  769. Revision 1.31 2002/10/19 17:06:50 michael
  770. + Added check for nil buffer to setstring
  771. Revision 1.30 2002/10/17 12:43:00 florian
  772. + ansistring_append* implemented
  773. Revision 1.29 2002/10/02 18:21:51 peter
  774. * Copy() changed to internal function calling compilerprocs
  775. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  776. new copy functions
  777. Revision 1.28 2002/09/14 11:20:50 carl
  778. * Delphi compatibility fix (with string routines)
  779. Revision 1.27 2002/09/07 21:10:47 carl
  780. * cardinal -> longword
  781. - remove some unused routines
  782. Revision 1.26 2002/09/07 15:07:44 peter
  783. * old logs removed and tabs fixed
  784. Revision 1.25 2002/04/26 15:19:05 peter
  785. * use saveregisters for incr routines, saves also problems with
  786. the optimizer
  787. Revision 1.24 2002/04/25 20:14:56 peter
  788. * updated compilerprocs
  789. * incr ref count has now a value argument instead of var
  790. Revision 1.23 2002/01/07 13:23:53 jonas
  791. * fixed bug in fpc_char_to_ansistr when converting #0 (found by Peter)
  792. }