astrings.inc 26 KB

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