astrings.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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; Var S2 : AnsiString; Maxlen : Longint);
  208. {
  209. Converts a AnsiString to a ShortString;
  210. if maxlen<>-1, the resulting string has maximal length maxlen
  211. else a default length of 255 is taken.
  212. }
  213. Var Size : Longint;
  214. begin
  215. Size:=PAnsiRec(Pointer(S2)-FirstOff)^.Len;
  216. if maxlen=-1 then maxlen:=255;
  217. If Size>maxlen then Size:=maxlen;
  218. Move (Pointer(S2)^,S1[1],Size);
  219. byte(S1[0]):=Size;
  220. end;
  221. Procedure Short_To_AnsiString (Var S1 : AnsiString; Var S2 : ShortString);
  222. {
  223. Converts a ShortString to a AnsiString;
  224. }
  225. Var Size : Longint;
  226. begin
  227. Size:=Byte(S2[0]);
  228. Setlength (S1,Size);
  229. Move (S2[1],Pointer(S1)^,Size);
  230. PByte(Pointer(S1)+Size)^:=0; { Terminating Zero }
  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 Write_Text_AnsiString (Len : Longint; T : TextRec; Var S : AnsiString);[Public, alias: 'WRITE_TEXT_ANSISTRING'];
  276. {
  277. Writes a AnsiString to the Text file T
  278. }
  279. begin
  280. end;
  281. Procedure SetCharAtIndex (Var S : AnsiString; Index : Longint; C : CHar);
  282. begin
  283. if Index<=Length(S) then
  284. begin
  285. UniqueAnsiString(S);
  286. Pbyte(Pointer(S)+index-1)^:=Byte(C);
  287. end;
  288. end;
  289. { ---------------------------------------------------------------------
  290. Public functions, In interface.
  291. ---------------------------------------------------------------------}
  292. Function Length (Var S : AnsiString) : Longint;
  293. {
  294. Returns the length of an AnsiString.
  295. Takes in acount that zero strings are NIL;
  296. }
  297. begin
  298. If Pointer(S)=Nil then
  299. Length:=0
  300. else
  301. Length:=PAnsiRec(Pointer(S)-FirstOff)^.Len;
  302. end;
  303. Procedure SetLength (Var S : AnsiString; l : Longint);
  304. {
  305. Sets The length of string S to L.
  306. Makes sure S is unique, and contains enough room.
  307. }
  308. Var Temp : Pointer;
  309. begin
  310. If (Pointer(S)=Nil) and (l>0) then
  311. begin
  312. { Need a complete new string...}
  313. S:=NewAnsiString(l);
  314. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  315. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  316. PByte (Pointer(S)+l)^:=0;
  317. end
  318. else if l>0 then
  319. begin
  320. If (PAnsiRec(Pointer(S)-FirstOff)^.Maxlen < L) or
  321. (PAnsiRec(Pointer(S)-FirstOff)^.Ref <> 1) then
  322. begin
  323. { Reallocation is needed... }
  324. Temp:=Pointer(NewAnsiString(L));
  325. if Length(S)>0 then
  326. Move (Pointer(S)^,Temp^,Length(S)+1);
  327. Decr_Ansi_ref (S);
  328. S:=AnsiString(Temp);
  329. end;
  330. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l
  331. end
  332. else
  333. { Length=0 }
  334. begin
  335. Decr_Ansi_Ref (S);
  336. S:=Nil;
  337. end;
  338. end;
  339. Function Copy (Var S : AnsiString; Index,Size : Longint) : AnsiString;
  340. var ResultAddress : Pointer;
  341. begin
  342. ResultAddress:=Nil;
  343. dec(index);
  344. { Check Size. Accounts for Zero-length S }
  345. if Length(S)<Index+Size then
  346. Size:=Length(S)-Index;
  347. If Size>0 then
  348. begin
  349. ResultAddress:=Pointer(NewAnsiString (Size));
  350. if ResultAddress<>Nil then
  351. begin
  352. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  353. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  354. PByte(ResultAddress+Size)^:=0;
  355. end;
  356. end;
  357. Copy:=AnsiString(ResultAddress);
  358. end;
  359. Function Pos (Var Substr : AnsiString; Var Source : AnsiString) : Longint;
  360. var i,j : longint;
  361. e : boolean;
  362. s : Pointer;
  363. begin
  364. i := 0;
  365. j := 0;
  366. e := true;
  367. if Plongint(substr)^=0 then e := false;
  368. while (e) and (i <= length (Source) - length (substr)) do
  369. begin
  370. inc (i);
  371. S:=Pointer(copy(Source,i,length(Substr)));
  372. if AnsiCompare(substr,AnsiString(s))=0 then
  373. begin
  374. j := i;
  375. e := false;
  376. end;
  377. DisposeAnsiString(AnsiString(S));
  378. end;
  379. pos := j;
  380. end;
  381. Procedure Val (var S : AnsiString; var R : real; Var Code : Integer);
  382. Var SS : String;
  383. begin
  384. Ansi_To_ShortString (SS,S,255);
  385. Val(SS,R,Code);
  386. end;
  387. {
  388. Procedure Val (var S : AnsiString; var D : Double; Var Code : Integer);
  389. Var SS : ShortString;
  390. begin
  391. Ansi_To_ShortString (SS,S,255);
  392. Val(SS,D,Code);
  393. end;
  394. }
  395. Procedure Val (var S : AnsiString; var E : Extended; Code : Integer);
  396. Var SS : ShortString;
  397. begin
  398. Ansi_To_ShortString (SS,S,255);
  399. Val(SS,E,Code);
  400. end;
  401. Procedure Val (var S : AnsiString; var C : Cardinal; Code : Integer);
  402. Var SS : ShortString;
  403. begin
  404. Ansi_To_ShortString (SS,S,255);
  405. Val(SS,C,Code);
  406. end;
  407. Procedure Val (var S : AnsiString; var L : Longint; Var Code : Integer);
  408. Var SS : ShortString;
  409. begin
  410. Ansi_To_ShortString (SS,S,255);
  411. Val(SS,L,Code);
  412. end;
  413. Procedure Val (var S : AnsiString; var W : Word; Var Code : Integer);
  414. Var SS : ShortString;
  415. begin
  416. Ansi_To_ShortString (SS,S,255);
  417. Val(SS,W,Code);
  418. end;
  419. Procedure Val (var S : AnsiString; var I : Integer; Var Code : Integer);
  420. Var SS : ShortString;
  421. begin
  422. Ansi_To_ShortString (SS,S,255);
  423. Val(SS,I,Code);
  424. end;
  425. Procedure Val (var S : AnsiString; var B : Byte; Var Code : Integer);
  426. Var SS : ShortString;
  427. begin
  428. Ansi_To_ShortString (SS,S,255);
  429. Val(SS,B,Code);
  430. end;
  431. Procedure Val (var S : AnsiString; var SI : ShortInt; Var Code : Integer);
  432. Var SS : ShortString;
  433. begin
  434. Ansi_To_ShortString (SS,S,255);
  435. Val(SS,SI,Code);
  436. end;
  437. {
  438. Procedure Str (Const R : Real;Len,fr : Longint; Const S : AnsiString);
  439. Var SS : ShortString;
  440. begin
  441. {int_Str_Real (R,Len,fr,SS);}
  442. Short_To_AnsiString (S,SS);
  443. end;
  444. {
  445. Procedure Str (Var D : Double;Len,fr: Longint; Var S : AnsiString);
  446. Var SS : ShortString;
  447. begin
  448. {int_Str_Double (D,Len,fr,SS);}
  449. Short_To_AnsiString (S,SS);
  450. end;
  451. }
  452. Procedure Str (Var E : Extended;Lenf,Fr: Longint; Var S : AnsiString);
  453. Var SS : ShortString;
  454. begin
  455. {int_Str_Extended (E,Len,fr,SS);}
  456. Short_To_AnsiString (S,SS);
  457. end;
  458. Procedure Str (Var C : Cardinal;Len : Longint; Var S : AnsiString);
  459. begin
  460. end;
  461. Procedure Str (Var L : Longint; Len : Longint; Var S : AnsiString);
  462. Var SS : ShortString;
  463. begin
  464. {int_Str_Longint (L,Len,fr,SS);}
  465. Short_To_AnsiString (S,SS);
  466. end;
  467. Procedure Str (Var W : Word;Len : Longint; Var S : AnsiString);
  468. begin
  469. end;
  470. Procedure Str (Var I : Integer;Len : Longint; Var S : AnsiString);
  471. begin
  472. end;
  473. Procedure Str (Var B : Byte; Len : Longint; Var S : AnsiString);
  474. begin
  475. end;
  476. Procedure Str (Var SI : ShortInt; Len : Longint; Var S : AnsiString);
  477. begin
  478. end;
  479. }
  480. Procedure Delete (Var S : AnsiString; Index,Size: Longint);
  481. begin
  482. if index<=0 then
  483. begin
  484. Size:=Size+index-1;
  485. index:=1;
  486. end;
  487. if (Index<=length(s)) and (Size>0) then
  488. begin
  489. UniqueAnsiString (S);
  490. if Size+Index>Length(S) then
  491. Size:=Length(s)-Index+1;
  492. Setlength(s,Length(s)-Size);
  493. if Index<=Length(s) then
  494. Move(Pointer(Pointer(S)+Index+Size-1)^,
  495. Pointer(Pointer(s)+Index-1)^,Length(s)-Index+2)
  496. else
  497. Pbyte(Pointer(S)+Length(S))^:=0;
  498. end;
  499. end;
  500. Procedure Insert (Var Source : AnsiString; Var S : AnsiString; Index : Longint);
  501. var s3,s4 : Pointer;
  502. begin
  503. If Length(Source)=0 then exit;
  504. if index <= 0 then index := 1;
  505. s3 := Pointer(copy(s,index,length(s)));
  506. if index > Length(s) then
  507. index := Length(S)+1;
  508. SetLength(s,index - 1);
  509. s4 := Pointer ( NewAnsiString(PansiRec(Pointer(Source)-Firstoff)^.len) );
  510. Ansi_String_Concat(AnsiString(s4),Source);
  511. if S4<>Nil then
  512. Ansi_String_Concat(AnsiString(S4),AnsiString(s3));
  513. Ansi_String_Concat(S,AnsiString(S4));
  514. Decr_ansi_ref (AnsiString(S3));
  515. Decr_ansi_ref (AnsiString(S4));
  516. end;
  517. {
  518. $Log$
  519. Revision 1.9 1998-07-20 23:36:56 michael
  520. changes for ansistrings
  521. Revision 1.8 1998/07/13 21:19:09 florian
  522. * some problems with ansi string support fixed
  523. Revision 1.7 1998/07/06 14:29:08 michael
  524. + Added Public,Alias directives for some calls
  525. Revision 1.6 1998/06/25 08:41:44 florian
  526. * better rtti
  527. Revision 1.5 1998/06/12 07:39:13 michael
  528. + Added aliases for Incr/Decr ref.
  529. Revision 1.4 1998/06/08 19:35:02 michael
  530. Some changes to integrate in system unit
  531. Revision 1.3 1998/06/08 12:38:22 michael
  532. Implemented rtti, inserted ansistrings again
  533. Revision 1.2 1998/05/12 10:42:44 peter
  534. * moved getopts to inc/, all supported OS's need argc,argv exported
  535. + strpas, strlen are now exported in the systemunit
  536. * removed logs
  537. * removed $ifdef ver_above
  538. }