astrings.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { ---------------------------------------------------------------------
  13. This file implements AnsiStrings for FPC
  14. ---------------------------------------------------------------------}
  15. {
  16. This file contains the implementation of the LongString 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 shortstring=string;
  30. Function NewAnsiString (Len : Longint) : AnsiString; forward;
  31. Procedure DisposeAnsiString (Var S : AnsiString); forward;
  32. Procedure Decr_Ansi_Ref (Var S : AnsiString); forward;
  33. Procedure Incr_Ansi_Ref (Var S : AnsiString); forward;
  34. Procedure AssignAnsiString (Var S1 : AnsiString; S2 : Pointer); forward;
  35. Procedure Ansi_String_Concat (Var S1 : AnsiString; Var S2 : AnsiString); forward;
  36. Procedure Ansi_ShortString_Concat (Var S1: AnsiString; Var S2 : ShortString); forward;
  37. Procedure Ansi_To_ShortString (Var S1 : ShortString; Var S2 : AnsiString; maxlen : longint); forward;
  38. Procedure Short_To_AnsiString (Var S1 : AnsiString; Var S2 : ShortString); forward;
  39. Function AnsiCompare (Var S1,S2 : AnsiString): Longint; forward;
  40. Function AnsiCompare (var S1 : AnsiString; Var S2 : ShortString): Longint; forward;
  41. Procedure SetCharAtIndex (Var S : AnsiString; Index : Longint; C : CHar); forward;
  42. { Public functions, Will end up in systemh.inc }
  43. {$PACKRECORDS 1}
  44. Type TAnsiRec = Record
  45. Maxlen, len, ref : Longint;
  46. First : Char;
  47. end;
  48. PAnsiRec = ^TAnsiRec;
  49. Const AnsiRecLen = SizeOf(TAnsiRec);
  50. FirstOff = SizeOf(TAnsiRec)-1;
  51. { ---------------------------------------------------------------------
  52. Internal functions, not in interface.
  53. ---------------------------------------------------------------------}
  54. Procedure DumpAnsiRec (Var S : Ansistring);
  55. begin
  56. If Pointer(S)=Nil then
  57. Writeln ('String is nil')
  58. Else
  59. Begin
  60. With PansiRec(Pointer(S)-Firstoff)^ do
  61. begin
  62. Writeln ('MAxlen : ',maxlen);
  63. Writeln ('Len : ',len);
  64. Writeln ('Ref : ',ref);
  65. end;
  66. end;
  67. end;
  68. Function NewAnsiString (Len : Longint) : AnsiString;
  69. {
  70. Allocate a new AnsiString on the heap.
  71. initialize it to zero length and reference count 1.
  72. }
  73. Var P : Pointer;
  74. begin
  75. GetMem(P,Len+AnsiRecLen);
  76. If P<>Nil then
  77. begin
  78. PAnsiRec(P)^.Maxlen:=Len; { Maximal length }
  79. PAnsiRec(P)^.Len:=0; { Initial length }
  80. PAnsiRec(P)^.Ref:=1; { Set reference count }
  81. PAnsiRec(P)^.First:=#0; { Terminating #0 }
  82. P:=P+FirstOff; { Points to string now }
  83. end;
  84. Pointer(NewAnsiString):=P;
  85. end;
  86. Procedure DisposeAnsiString (Var S : AnsiString);
  87. {
  88. Deallocates a AnsiString From the heap.
  89. }
  90. begin
  91. // Writeln ('In disposeAnsiSTring');
  92. If Pointer(S)=Nil then exit;
  93. Dec (Longint(S),FirstOff);
  94. FreeMem (Pointer(S),PAnsiRec(Pointer(S))^.Maxlen+AnsiRecLen);
  95. Pointer(S):=Nil;
  96. end;
  97. Procedure Decr_Ansi_Ref (Var S : AnsiString);[Alias : 'DECR_ANSI_REF'];
  98. {
  99. Decreases the ReferenceCount of a non constant ansistring;
  100. If the reference count is zero, deallocate the string;
  101. }
  102. Type plongint = ^longint;
  103. Var l : plongint;
  104. Begin
  105. // dumpansirec(s);
  106. If Pointer(S)=Nil then exit; { Zero string }
  107. { check for constant strings ...}
  108. l:=Pointer(S)-FirstOff+8;
  109. If l^<0 then exit;
  110. l^:=l^-1;
  111. // dumpansirec(s);
  112. If l^=0 then
  113. { Ref count dropped to zero }
  114. begin
  115. // Writeln ('CAlling disposestring');
  116. DisposeAnsiString (S); { Remove...}
  117. end
  118. end;
  119. Procedure Incr_Ansi_Ref (Var S : AnsiString);[Alias : 'INCR_ANSI_REF'];
  120. Begin
  121. If Pointer(S)=Nil then exit;
  122. { Let's be paranoid : Constant string ??}
  123. If PansiRec(Pointer(S)-FirstOff)^.Ref<0 then exit;
  124. inc(PAnsiRec(Pointer(S)-FirstOff)^.Ref);
  125. end;
  126. Procedure UniqueAnsiString (Var S : AnsiString);
  127. {
  128. Make sure reference count of S is 1,
  129. using copy-on-write semantics.
  130. }
  131. Var SNew : Pointer;
  132. begin
  133. If Pointer(S)=Nil then exit;
  134. if PAnsiRec(Pointer(S)-Firstoff)^.Ref>1 then
  135. begin
  136. SNew:=Pointer(NewAnsiString (PAnsiRec(Pointer(S)-FirstOff)^.len));
  137. Move (Pointer(S)^,SNew^,PAnsiRec(Pointer(S)-FirstOff)^.len+1);
  138. PAnsiRec(SNew-8)^.len:=PAnsiRec(Pchar(S)-FirstOff)^.len;
  139. Decr_Ansi_Ref (S); { Thread safe }
  140. Pchar(S):=Pchar(SNew);
  141. end;
  142. end;
  143. Procedure AssignAnsiString (Var S1 : AnsiString; S2 : Pointer); [Public, Alias : 'ASSIGN_ANSI_STRING'];
  144. {
  145. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  146. If S2 is a constant string, a new S1 is allocated on the heap.
  147. }
  148. Var Temp : Pointer;
  149. begin
  150. If S2<>nil then
  151. begin
  152. If PAnsiRec(S2-FirstOff)^.Ref<0 then
  153. begin
  154. { S2 is a constant string, Create new string with copy. }
  155. Temp:=Pointer(NewAnsiString(PansiRec(S2-FirstOff)^.Len));
  156. Move (S2^,Temp^,PAnsiRec(S2-FirstOff)^.len+1);
  157. PAnsiRec(Temp-FirstOff)^.Len:=PAnsiRec(S2-FirstOff)^.len;
  158. end
  159. else
  160. begin
  161. Inc(PAnsiRec(S2-FirstOff)^.ref);
  162. Temp:=S2;
  163. end;
  164. end;
  165. { Decrease the reference count on the old S1 }
  166. Decr_Ansi_Ref (S1);
  167. { And finally, have S1 pointing to S2 (or its copy) }
  168. Pointer(S1):=Temp;
  169. end;
  170. Procedure Ansi_String_Concat (Var S1 : AnsiString; Var S2 : AnsiString);
  171. {
  172. Concatenates 2 AnsiStrings : S1+S2.
  173. Result Goes to S1;
  174. }
  175. Var Size,Location : Longint;
  176. begin
  177. if Pointer(S2)=Nil then exit;
  178. if (Pointer(S1)=Nil) then
  179. AssignAnsiString(S1,S2)
  180. else
  181. begin
  182. Size:=PAnsiRec(Pointer(S2)-FirstOff)^.Len;
  183. Location:=Length(S1);
  184. { Setlength takes case of uniqueness
  185. and allocated memory. We need to use length,
  186. to take into account possibility of S1=Nil }
  187. //!! SetLength (S1,Size+Location);
  188. Move (Pointer(S2)^,Pointer(Pointer(S1)+location)^,Size+1);
  189. end;
  190. end;
  191. Procedure Ansi_ShortString_Concat (Var S1: AnsiString; Var S2 : ShortString);
  192. {
  193. Concatenates a Ansi with a short string; : S2 + S2
  194. }
  195. Var Size,Location : Longint;
  196. begin
  197. Size:=byte(S2[0]);
  198. Location:=Length(S1);
  199. If Size=0 then exit;
  200. { Setlength takes case of uniqueness
  201. and alllocated memory. We need to use length,
  202. to take into account possibility of S1=Nil }
  203. SetLength (S1,Size+Length(S1));
  204. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  205. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  206. end;
  207. Procedure Ansi_To_ShortString (Var S1 : ShortString;const S2 : AnsiString; Maxlen : Longint);
  208. [Public, alias: 'FPC_TO_ANSISTRING_SHORT'];
  209. {
  210. Converts a AnsiString to a ShortString;
  211. }
  212. Var Size : Longint;
  213. begin
  214. Size:=PAnsiRec(Pointer(S2)-FirstOff)^.Len;
  215. If Size>maxlen then Size:=maxlen;
  216. Move (Pointer(S2)^,S1[1],Size);
  217. byte(S1[0]):=Size;
  218. end;
  219. Procedure Short_To_AnsiString (Const S1 : AnsiString; Var S2 : ShortString);
  220. [Public, alias: 'FPC_SHORT_TO_ANSISTRING'];
  221. {
  222. Converts a ShortString to a AnsiString;
  223. }
  224. Var Size : Longint;
  225. begin
  226. Size:=Byte(S2[0]);
  227. Setlength (S1,Size);
  228. Move (S2[1],Pointer(S1)^,Size);
  229. { Terminating Zero }
  230. PByte(Pointer(S1)+Size)^:=0;
  231. end;
  232. Function AnsiCompare (Var S1,S2 : AnsiString): Longint;
  233. {
  234. Compares 2 AnsiStrings;
  235. The result is
  236. <0 if S1<S2
  237. 0 if S1=S2
  238. >0 if S1>S2
  239. }
  240. Var i,MaxI,Temp : Longint;
  241. begin
  242. Temp:=0;
  243. i:=0;
  244. MaxI:=Length(S1);
  245. if MaxI>Length(S2) then MaxI:=Length(S2);
  246. While (i<MaxI) and (Temp=0) do
  247. begin
  248. Temp:= PByte(Pointer(S1)+I)^ - PByte(Pointer(S2)+i)^;
  249. inc(i);
  250. end;
  251. if temp=0 then temp:=Length(S1)-Length(S2);
  252. AnsiCompare:=Temp;
  253. end;
  254. Function AnsiCompare (Var S1 : AnsiString; Var S2 : ShortString): Longint;
  255. {
  256. Compares a AnsiString with a ShortString;
  257. The result is
  258. <0 if S1<S2
  259. 0 if S1=S2
  260. >0 if S1>S2
  261. }
  262. Var i,MaxI,Temp : Longint;
  263. begin
  264. Temp:=0;
  265. i:=0;
  266. MaxI:=Length(S1);
  267. if MaxI>byte(S2[0]) then MaxI:=Byte(S2[0]);
  268. While (i<MaxI) and (Temp=0) do
  269. begin
  270. Temp:= PByte(Pointer(S1)+I)^ - Byte(S2[i+1]);
  271. inc(i);
  272. end;
  273. AnsiCompare:=Temp;
  274. end;
  275. Procedure SetCharAtIndex (Var S : AnsiString; Index : Longint; C : CHar);
  276. begin
  277. if Index<=Length(S) then
  278. begin
  279. UniqueAnsiString(S);
  280. Pbyte(Pointer(S)+index-1)^:=Byte(C);
  281. end;
  282. end;
  283. { ---------------------------------------------------------------------
  284. Public functions, In interface.
  285. ---------------------------------------------------------------------}
  286. Function Length (Var S : AnsiString) : Longint;
  287. {
  288. Returns the length of an AnsiString.
  289. Takes in acount that zero strings are NIL;
  290. }
  291. begin
  292. If Pointer(S)=Nil then
  293. Length:=0
  294. else
  295. Length:=PAnsiRec(Pointer(S)-FirstOff)^.Len;
  296. end;
  297. Procedure SetLength (Var S : AnsiString; l : Longint);
  298. {
  299. Sets The length of string S to L.
  300. Makes sure S is unique, and contains enough room.
  301. }
  302. Var Temp : Pointer;
  303. begin
  304. If (Pointer(S)=Nil) and (l>0) then
  305. begin
  306. { Need a complete new string...}
  307. S:=NewAnsiString(l);
  308. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  309. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  310. PByte (Pointer(S)+l)^:=0;
  311. end
  312. else if l>0 then
  313. begin
  314. If (PAnsiRec(Pointer(S)-FirstOff)^.Maxlen < L) or
  315. (PAnsiRec(Pointer(S)-FirstOff)^.Ref <> 1) then
  316. begin
  317. { Reallocation is needed... }
  318. Temp:=Pointer(NewAnsiString(L));
  319. if Length(S)>0 then
  320. Move (Pointer(S)^,Temp^,Length(S)+1);
  321. Decr_Ansi_ref (S);
  322. Pointer(S):=Temp;
  323. end;
  324. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l
  325. end
  326. else
  327. { Length=0 }
  328. begin
  329. Decr_Ansi_Ref (S);
  330. S:=Nil;
  331. end;
  332. end;
  333. Function Copy (Var S : AnsiString; Index,Size : Longint) : AnsiString;
  334. var ResultAddress : Pointer;
  335. begin
  336. ResultAddress:=Nil;
  337. dec(index);
  338. { Check Size. Accounts for Zero-length S }
  339. if Length(S)<Index+Size then
  340. Size:=Length(S)-Index;
  341. If Size>0 then
  342. begin
  343. ResultAddress:=Pointer(NewAnsiString (Size));
  344. if ResultAddress<>Nil then
  345. begin
  346. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  347. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  348. PByte(ResultAddress+Size)^:=0;
  349. end;
  350. end;
  351. Copy:=AnsiString(ResultAddress);
  352. end;
  353. Function Pos (Var Substr : AnsiString; Var Source : AnsiString) : Longint;
  354. var i,j : longint;
  355. e : boolean;
  356. s : Pointer;
  357. begin
  358. i := 0;
  359. j := 0;
  360. e := true;
  361. if Plongint(substr)^=0 then e := false;
  362. while (e) and (i <= length (Source) - length (substr)) do
  363. begin
  364. inc (i);
  365. S:=Pointer(copy(Source,i,length(Substr)));
  366. if AnsiCompare(substr,AnsiString(s))=0 then
  367. begin
  368. j := i;
  369. e := false;
  370. end;
  371. DisposeAnsiString(AnsiString(S));
  372. end;
  373. pos := j;
  374. end;
  375. Procedure Val (var S : AnsiString; var R : real; Var Code : Integer);
  376. Var SS : String;
  377. begin
  378. Ansi_To_ShortString (SS,S,255);
  379. Val(SS,R,Code);
  380. end;
  381. {
  382. Procedure Val (var S : AnsiString; var D : Double; Var Code : Integer);
  383. Var SS : ShortString;
  384. begin
  385. Ansi_To_ShortString (SS,S,255);
  386. Val(SS,D,Code);
  387. end;
  388. }
  389. Procedure Val (var S : AnsiString; var E : Extended; Code : Integer);
  390. Var SS : ShortString;
  391. begin
  392. Ansi_To_ShortString (SS,S,255);
  393. Val(SS,E,Code);
  394. end;
  395. Procedure Val (var S : AnsiString; var C : Cardinal; Code : Integer);
  396. Var SS : ShortString;
  397. begin
  398. Ansi_To_ShortString (SS,S,255);
  399. Val(SS,C,Code);
  400. end;
  401. Procedure Val (var S : AnsiString; var L : Longint; Var Code : Integer);
  402. Var SS : ShortString;
  403. begin
  404. Ansi_To_ShortString (SS,S,255);
  405. Val(SS,L,Code);
  406. end;
  407. Procedure Val (var S : AnsiString; var W : Word; Var Code : Integer);
  408. Var SS : ShortString;
  409. begin
  410. Ansi_To_ShortString (SS,S,255);
  411. Val(SS,W,Code);
  412. end;
  413. Procedure Val (var S : AnsiString; var I : Integer; Var Code : Integer);
  414. Var SS : ShortString;
  415. begin
  416. Ansi_To_ShortString (SS,S,255);
  417. Val(SS,I,Code);
  418. end;
  419. Procedure Val (var S : AnsiString; var B : Byte; Var Code : Integer);
  420. Var SS : ShortString;
  421. begin
  422. Ansi_To_ShortString (SS,S,255);
  423. Val(SS,B,Code);
  424. end;
  425. Procedure Val (var S : AnsiString; var SI : ShortInt; Var Code : Integer);
  426. Var SS : ShortString;
  427. begin
  428. Ansi_To_ShortString (SS,S,255);
  429. Val(SS,SI,Code);
  430. end;
  431. {
  432. Procedure Str (Const R : Real;Len,fr : Longint; Const S : AnsiString);
  433. Var SS : ShortString;
  434. begin
  435. {int_Str_Real (R,Len,fr,SS);}
  436. Short_To_AnsiString (S,SS);
  437. end;
  438. {
  439. Procedure Str (Var D : Double;Len,fr: Longint; Var S : AnsiString);
  440. Var SS : ShortString;
  441. begin
  442. {int_Str_Double (D,Len,fr,SS);}
  443. Short_To_AnsiString (S,SS);
  444. end;
  445. }
  446. Procedure Str (Var E : Extended;Lenf,Fr: Longint; Var S : AnsiString);
  447. Var SS : ShortString;
  448. begin
  449. {int_Str_Extended (E,Len,fr,SS);}
  450. Short_To_AnsiString (S,SS);
  451. end;
  452. Procedure Str (Var C : Cardinal;Len : Longint; Var S : AnsiString);
  453. begin
  454. end;
  455. Procedure Str (Var L : Longint; Len : Longint; Var S : AnsiString);
  456. Var SS : ShortString;
  457. begin
  458. {int_Str_Longint (L,Len,fr,SS);}
  459. Short_To_AnsiString (S,SS);
  460. end;
  461. Procedure Str (Var W : Word;Len : Longint; Var S : AnsiString);
  462. begin
  463. end;
  464. Procedure Str (Var I : Integer;Len : Longint; Var S : AnsiString);
  465. begin
  466. end;
  467. Procedure Str (Var B : Byte; Len : Longint; Var S : AnsiString);
  468. begin
  469. end;
  470. Procedure Str (Var SI : ShortInt; Len : Longint; Var S : AnsiString);
  471. begin
  472. end;
  473. }
  474. Procedure Delete (Var S : AnsiString; Index,Size: Longint);
  475. begin
  476. if index<=0 then
  477. begin
  478. Size:=Size+index-1;
  479. index:=1;
  480. end;
  481. if (Index<=length(s)) and (Size>0) then
  482. begin
  483. UniqueAnsiString (S);
  484. if Size+Index>Length(S) then
  485. Size:=Length(s)-Index+1;
  486. Setlength(s,Length(s)-Size);
  487. if Index<=Length(s) then
  488. Move(Pointer(Pointer(S)+Index+Size-1)^,
  489. Pointer(Pointer(s)+Index-1)^,Length(s)-Index+2)
  490. else
  491. Pbyte(Pointer(S)+Length(S))^:=0;
  492. end;
  493. end;
  494. Procedure Insert (Var Source : AnsiString; Var S : AnsiString; Index : Longint);
  495. var s3,s4 : Pointer;
  496. begin
  497. If Length(Source)=0 then exit;
  498. if index <= 0 then index := 1;
  499. s3 := Pointer(copy(s,index,length(s)));
  500. if index > Length(s) then
  501. index := Length(S)+1;
  502. SetLength(s,index - 1);
  503. s4 := Pointer ( NewAnsiString(PansiRec(Pointer(Source)-Firstoff)^.len) );
  504. Ansi_String_Concat(AnsiString(s4),Source);
  505. if S4<>Nil then
  506. Ansi_String_Concat(AnsiString(S4),AnsiString(s3));
  507. Ansi_String_Concat(S,AnsiString(S4));
  508. Decr_ansi_ref (AnsiString(S3));
  509. Decr_ansi_ref (AnsiString(S4));
  510. end;
  511. {
  512. $Log$
  513. Revision 1.11 1998-08-08 12:28:10 florian
  514. * a lot small fixes to the extended data type work
  515. Revision 1.10 1998/07/29 21:44:34 michael
  516. + Implemented reading/writing of ansistrings
  517. Revision 1.9 1998/07/20 23:36:56 michael
  518. changes for ansistrings
  519. Revision 1.8 1998/07/13 21:19:09 florian
  520. * some problems with ansi string support fixed
  521. Revision 1.7 1998/07/06 14:29:08 michael
  522. + Added Public,Alias directives for some calls
  523. Revision 1.6 1998/06/25 08:41:44 florian
  524. * better rtti
  525. Revision 1.5 1998/06/12 07:39:13 michael
  526. + Added aliases for Incr/Decr ref.
  527. Revision 1.4 1998/06/08 19:35:02 michael
  528. Some changes to integrate in system unit
  529. Revision 1.3 1998/06/08 12:38:22 michael
  530. Implemented rtti, inserted ansistrings again
  531. Revision 1.2 1998/05/12 10:42:44 peter
  532. * moved getopts to inc/, all supported OS's need argc,argv exported
  533. + strpas, strlen are now exported in the systemunit
  534. * removed logs
  535. * removed $ifdef ver_above
  536. }