astrings.inc 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. member of the Free Pascal development team.
  5. This file implements AnsiStrings for FPC
  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. { This will release some functions for special shortstring support }
  13. { define EXTRAANSISHORT}
  14. {
  15. This file contains the implementation of the AnsiString type,
  16. and all things that are needed for it.
  17. AnsiString is defined as a 'silent' pchar :
  18. a pchar that points to :
  19. @-8 : SizeInt for reference count;
  20. @-4 : SizeInt for size;
  21. @ : String + Terminating #0;
  22. Pchar(Ansistring) is a valid typecast.
  23. So AS[i] is converted to the address @AS+i-1.
  24. Constants should be assigned a reference count of -1
  25. Meaning that they can't be disposed of.
  26. }
  27. Type
  28. PAnsiRec = ^TAnsiRec;
  29. TAnsiRec = Packed Record
  30. CodePage : TSystemCodePage;
  31. ElementSize : Word;
  32. {$ifdef CPU64}
  33. { align fields }
  34. Dummy : DWord;
  35. {$endif CPU64}
  36. Ref : SizeInt;
  37. Len : SizeInt;
  38. First : Char;
  39. end;
  40. Const
  41. AnsiRecLen = SizeOf(TAnsiRec);
  42. AnsiFirstOff = SizeOf(TAnsiRec)-1;
  43. {****************************************************************************
  44. Internal functions, not in interface.
  45. ****************************************************************************}
  46. Function NewAnsiString(Len : SizeInt) : Pointer;
  47. {
  48. Allocate a new AnsiString on the heap.
  49. initialize it to zero length and reference count 1.
  50. }
  51. Var
  52. P : Pointer;
  53. begin
  54. { request a multiple of 16 because the heap manager alloctes anyways chunks of 16 bytes }
  55. GetMem(P,Len+AnsiRecLen);
  56. If P<>Nil then
  57. begin
  58. PAnsiRec(P)^.Ref:=1; { Set reference count }
  59. PAnsiRec(P)^.Len:=0; { Initial length }
  60. PAnsiRec(P)^.CodePage:=DefaultSystemCodePage;
  61. PAnsiRec(P)^.ElementSize:=SizeOf(AnsiChar);
  62. PAnsiRec(P)^.First:=#0; { Terminating #0 }
  63. inc(p,AnsiFirstOff); { Points to string now }
  64. end;
  65. NewAnsiString:=P;
  66. end;
  67. Procedure DisposeAnsiString(Var S : Pointer); {$IFNDEF VER2_0} Inline; {$ENDIF}
  68. {
  69. Deallocates a AnsiString From the heap.
  70. }
  71. begin
  72. If S=Nil then
  73. exit;
  74. Dec (S,AnsiFirstOff);
  75. FreeMem (S);
  76. S:=Nil;
  77. end;
  78. {$ifndef FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  79. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [Public,Alias:'FPC_ANSISTR_DECR_REF']; compilerproc;
  80. {
  81. Decreases the ReferenceCount of a non constant ansistring;
  82. If the reference count is zero, deallocate the string;
  83. }
  84. Type
  85. pSizeInt = ^SizeInt;
  86. Var
  87. l : pSizeInt;
  88. Begin
  89. { Zero string }
  90. If S=Nil then exit;
  91. { check for constant strings ...}
  92. l:=@PAnsiRec(S-AnsiFirstOff)^.Ref;
  93. If l^<0 then exit;
  94. { declocked does a MT safe dec and returns true, if the counter is 0 }
  95. If declocked(l^) then
  96. { Ref count dropped to zero }
  97. DisposeAnsiString (S); { Remove...}
  98. end;
  99. {$endif FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  100. { also define alias for internal use in the system unit }
  101. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [external name 'FPC_ANSISTR_DECR_REF'];
  102. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [Public,Alias:'FPC_ANSISTR_INCR_REF']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  103. Begin
  104. If S=Nil then
  105. exit;
  106. { Let's be paranoid : Constant string ??}
  107. If PAnsiRec(S-AnsiFirstOff)^.Ref<0 then exit;
  108. inclocked(PAnsiRec(S-AnsiFirstOff)^.Ref);
  109. end;
  110. { also define alias which can be used inside the system unit }
  111. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [external name 'FPC_ANSISTR_INCR_REF'];
  112. Procedure fpc_AnsiStr_Assign (Var DestS : Pointer;S2 : Pointer);[Public,Alias:'FPC_ANSISTR_ASSIGN']; compilerproc;
  113. {
  114. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  115. }
  116. begin
  117. if DestS=S2 then
  118. exit;
  119. If S2<>nil then
  120. If PAnsiRec(S2-AnsiFirstOff)^.Ref>0 then
  121. inclocked(PAnsiRec(S2-AnsiFirstOff)^.Ref);
  122. { Decrease the reference count on the old S1 }
  123. fpc_ansistr_decr_ref (DestS);
  124. { And finally, have DestS pointing to S2 (or its copy) }
  125. DestS:=S2;
  126. end;
  127. { alias for internal use }
  128. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_ANSISTR_ASSIGN'];
  129. {$ifndef STR_CONCAT_PROCS}
  130. function fpc_AnsiStr_Concat (const S1,S2 : AnsiString): ansistring; compilerproc;
  131. Var
  132. Size,Location : SizeInt;
  133. pc : pchar;
  134. begin
  135. { only assign if s1 or s2 is empty }
  136. if (S1='') then
  137. begin
  138. result:=s2;
  139. exit;
  140. end;
  141. if (S2='') then
  142. begin
  143. result:=s1;
  144. exit;
  145. end;
  146. Location:=Length(S1);
  147. Size:=length(S2);
  148. SetLength(result,Size+Location);
  149. pc:=pchar(result);
  150. Move(S1[1],pc^,Location);
  151. inc(pc,location);
  152. Move(S2[1],pc^,Size+1);
  153. end;
  154. function fpc_AnsiStr_Concat_multi (const sarr:array of Ansistring): ansistring; compilerproc;
  155. Var
  156. i : Longint;
  157. p : pointer;
  158. pc : pchar;
  159. Size,NewLen : SizeInt;
  160. begin
  161. { First calculate size of the result so we can do
  162. a single call to SetLength() }
  163. NewLen:=0;
  164. for i:=low(sarr) to high(sarr) do
  165. inc(NewLen,length(sarr[i]));
  166. SetLength(result,NewLen);
  167. pc:=pchar(result);
  168. for i:=low(sarr) to high(sarr) do
  169. begin
  170. p:=pointer(sarr[i]);
  171. if assigned(p) then
  172. begin
  173. Size:=length(ansistring(p));
  174. Move(pchar(p)^,pc^,Size+1);
  175. inc(pc,size);
  176. end;
  177. end;
  178. end;
  179. {$else STR_CONCAT_PROCS}
  180. procedure fpc_AnsiStr_Concat (var DestS:ansistring;const S1,S2 : AnsiString); compilerproc;
  181. Var
  182. Size,Location : SizeInt;
  183. same : boolean;
  184. begin
  185. { only assign if s1 or s2 is empty }
  186. if (S1='') then
  187. begin
  188. DestS:=s2;
  189. exit;
  190. end;
  191. if (S2='') then
  192. begin
  193. DestS:=s1;
  194. exit;
  195. end;
  196. Location:=Length(S1);
  197. Size:=length(S2);
  198. { Use Pointer() typecasts to prevent extra conversion code }
  199. if Pointer(DestS)=Pointer(S1) then
  200. begin
  201. same:=Pointer(S1)=Pointer(S2);
  202. SetLength(DestS,Size+Location);
  203. if same then
  204. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size)
  205. else
  206. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  207. end
  208. else if Pointer(DestS)=Pointer(S2) then
  209. begin
  210. SetLength(DestS,Size+Location);
  211. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size+1);
  212. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  213. end
  214. else
  215. begin
  216. DestS:='';
  217. SetLength(DestS,Size+Location);
  218. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  219. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  220. end;
  221. end;
  222. procedure fpc_AnsiStr_Concat_multi (var DestS:ansistring;const sarr:array of Ansistring); compilerproc;
  223. Var
  224. lowstart,i : Longint;
  225. p,pc : pointer;
  226. Size,NewLen,
  227. OldDestLen : SizeInt;
  228. destcopy : pointer;
  229. begin
  230. if high(sarr)=0 then
  231. begin
  232. DestS:='';
  233. exit;
  234. end;
  235. destcopy:=nil;
  236. lowstart:=low(sarr);
  237. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  238. inc(lowstart);
  239. { Check for another reuse, then we can't use
  240. the append optimization }
  241. for i:=lowstart to high(sarr) do
  242. begin
  243. if Pointer(DestS)=Pointer(sarr[i]) then
  244. begin
  245. { if DestS is used somewhere in the middle of the expression,
  246. we need to make sure the original string still exists after
  247. we empty/modify DestS }
  248. destcopy:=pointer(dests);
  249. fpc_AnsiStr_Incr_Ref(destcopy);
  250. lowstart:=low(sarr);
  251. break;
  252. end;
  253. end;
  254. { Start with empty DestS if we start with concatting
  255. the first array element }
  256. if lowstart=low(sarr) then
  257. DestS:='';
  258. OldDestLen:=length(DestS);
  259. { Calculate size of the result so we can do
  260. a single call to SetLength() }
  261. NewLen:=0;
  262. for i:=low(sarr) to high(sarr) do
  263. inc(NewLen,length(sarr[i]));
  264. SetLength(DestS,NewLen);
  265. { Concat all strings, except the string we already
  266. copied in DestS }
  267. pc:=Pointer(DestS)+OldDestLen;
  268. for i:=lowstart to high(sarr) do
  269. begin
  270. p:=pointer(sarr[i]);
  271. if assigned(p) then
  272. begin
  273. Size:=length(ansistring(p));
  274. Move(p^,pc^,Size+1);
  275. inc(pc,size);
  276. end;
  277. end;
  278. fpc_AnsiStr_Decr_Ref(destcopy);
  279. end;
  280. {$endif STR_CONCAT_PROCS}
  281. {$ifdef EXTRAANSISHORT}
  282. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  283. {
  284. Concatenates a Ansi with a short string; : S2 + S2
  285. }
  286. Var
  287. Size,Location : SizeInt;
  288. begin
  289. Size:=Length(S2);
  290. Location:=Length(S1);
  291. If Size=0 then
  292. exit;
  293. { Setlength takes case of uniqueness
  294. and alllocated memory. We need to use length,
  295. to take into account possibility of S1=Nil }
  296. SetLength (S1,Size+Length(S1));
  297. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  298. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  299. end;
  300. {$endif EXTRAANSISHORT}
  301. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  302. { the following declaration has exactly the same effect as }
  303. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  304. { which is what the old helper was, so we don't need an extra implementation }
  305. { of the old helper (JM) }
  306. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  307. {
  308. Converts a AnsiString to a ShortString;
  309. }
  310. Var
  311. Size : SizeInt;
  312. begin
  313. if S2='' then
  314. fpc_AnsiStr_To_ShortStr:=''
  315. else
  316. begin
  317. Size:=Length(S2);
  318. If Size>high_of_res then
  319. Size:=high_of_res;
  320. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  321. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  322. end;
  323. end;
  324. {$else FPC_STRTOSHORTSTRINGPROC}
  325. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : Ansistring);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  326. {
  327. Converts a AnsiString to a ShortString;
  328. }
  329. Var
  330. Size : SizeInt;
  331. begin
  332. if S2='' then
  333. res:=''
  334. else
  335. begin
  336. Size:=Length(S2);
  337. If Size>high(res) then
  338. Size:=high(res);
  339. Move (S2[1],res[1],Size);
  340. byte(res[0]):=byte(Size);
  341. end;
  342. end;
  343. {$endif FPC_STRTOSHORTSTRINGPROC}
  344. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; compilerproc;
  345. {
  346. Converts a ShortString to a AnsiString;
  347. }
  348. Var
  349. Size : SizeInt;
  350. begin
  351. Size:=Length(S2);
  352. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  353. if Size>0 then
  354. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  355. end;
  356. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; compilerproc;
  357. {
  358. Converts a Char to a AnsiString;
  359. }
  360. begin
  361. Setlength (fpc_Char_To_AnsiStr,1);
  362. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  363. { Terminating Zero }
  364. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  365. end;
  366. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; compilerproc;
  367. Var
  368. L : SizeInt;
  369. begin
  370. if (not assigned(p)) or (p[0]=#0) Then
  371. L := 0
  372. else
  373. l:=IndexChar(p^,-1,#0);
  374. SetLength(fpc_PChar_To_AnsiStr,L);
  375. if L > 0 then
  376. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  377. end;
  378. Function fpc_CharArray_To_AnsiStr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
  379. var
  380. i : SizeInt;
  381. begin
  382. if (zerobased) then
  383. begin
  384. if (arr[0]=#0) Then
  385. i := 0
  386. else
  387. begin
  388. i:=IndexChar(arr,high(arr)+1,#0);
  389. if i = -1 then
  390. i := high(arr)+1;
  391. end;
  392. end
  393. else
  394. i := high(arr)+1;
  395. SetLength(fpc_CharArray_To_AnsiStr,i);
  396. if i > 0 then
  397. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  398. end;
  399. {$ifndef FPC_STRTOCHARARRAYPROC}
  400. { note: inside the compiler, the resulttype is modified to be the length }
  401. { of the actual chararray to which we convert (JM) }
  402. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  403. var
  404. len: SizeInt;
  405. begin
  406. len := length(src);
  407. if len > arraysize then
  408. len := arraysize;
  409. {$r-}
  410. { make sure we don't try to access element 1 of the ansistring if it's nil }
  411. if len > 0 then
  412. move(src[1],fpc_ansistr_to_chararray[0],len);
  413. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  414. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  415. {$ifdef RangeCheckWasOn}
  416. {$r+}
  417. {$endif}
  418. end;
  419. {$else ndef FPC_STRTOCHARARRAYPROC}
  420. procedure fpc_ansistr_to_chararray(out res: array of char; const src: ansistring); compilerproc;
  421. var
  422. len: SizeInt;
  423. begin
  424. len := length(src);
  425. if len > length(res) then
  426. len := length(res);
  427. {$r-}
  428. { make sure we don't try to access element 1 of the ansistring if it's nil }
  429. if len > 0 then
  430. move(src[1],res[0],len);
  431. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  432. fillchar(res[len],length(res)-len,0);
  433. {$ifdef RangeCheckWasOn}
  434. {$r+}
  435. {$endif}
  436. end;
  437. {$endif ndef FPC_STRTOCHARARRAYPROC}
  438. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  439. {
  440. Compares 2 AnsiStrings;
  441. The result is
  442. <0 if S1<S2
  443. 0 if S1=S2
  444. >0 if S1>S2
  445. }
  446. Var
  447. MaxI,Temp : SizeInt;
  448. begin
  449. if pointer(S1)=pointer(S2) then
  450. begin
  451. result:=0;
  452. exit;
  453. end;
  454. Maxi:=Length(S1);
  455. temp:=Length(S2);
  456. If MaxI>Temp then
  457. MaxI:=Temp;
  458. if MaxI>0 then
  459. begin
  460. result:=CompareByte(S1[1],S2[1],MaxI);
  461. if result=0 then
  462. result:=Length(S1)-Length(S2);
  463. end
  464. else
  465. result:=Length(S1)-Length(S2);
  466. end;
  467. Function fpc_AnsiStr_Compare_equal(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  468. {
  469. Compares 2 AnsiStrings for equality/inequality only;
  470. The result is
  471. 0 if S1=S2
  472. <>0 if S1<>S2
  473. }
  474. Var
  475. MaxI,Temp : SizeInt;
  476. begin
  477. if pointer(S1)=pointer(S2) then
  478. begin
  479. result:=0;
  480. exit;
  481. end;
  482. Maxi:=Length(S1);
  483. temp:=Length(S2);
  484. Result := Maxi - temp;
  485. if Result = 0 then
  486. if MaxI>0 then
  487. result:=CompareByte(S1[1],S2[1],MaxI);
  488. end;
  489. {$ifdef VER2_4}
  490. // obsolete but needed for boostrapping with 2.4
  491. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  492. begin
  493. if p=nil then
  494. HandleErrorFrame(201,get_frame);
  495. end;
  496. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  497. begin
  498. if (index>len) or (Index<1) then
  499. HandleErrorFrame(201,get_frame);
  500. end;
  501. {$else VER2_4}
  502. Procedure fpc_AnsiStr_CheckRange(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  503. begin
  504. if (p=nil) or (index>PAnsiRec(p-FirstOff)^.Len) or (Index<1) then
  505. HandleErrorFrame(201,get_frame);
  506. end;
  507. {$endif VER2_4}
  508. Procedure fpc_AnsiStr_SetLength (Var S : RawByteString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  509. {
  510. Sets The length of string S to L.
  511. Makes sure S is unique, and contains enough room.
  512. }
  513. Var
  514. Temp : Pointer;
  515. lens, lena,
  516. movelen : SizeInt;
  517. begin
  518. if (l>0) then
  519. begin
  520. if Pointer(S)=nil then
  521. begin
  522. GetMem(Pointer(S),AnsiRecLen+L);
  523. PAnsiRec(S)^.Ref:=1;
  524. inc(Pointer(S),AnsiFirstOff);
  525. end
  526. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  527. begin
  528. Dec(Pointer(S),AnsiFirstOff);
  529. lens:=MemSize(Pointer(s));
  530. lena:=AnsiRecLen+L;
  531. { allow shrinking string if that saves at least half of current size }
  532. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  533. reallocmem(pointer(S),AnsiRecLen+L);
  534. Inc(Pointer(S),AnsiFirstOff);
  535. end
  536. else
  537. begin
  538. { Reallocation is needed... }
  539. Temp:=Pointer(NewAnsiString(L));
  540. { also move terminating null }
  541. lens:=succ(length(s));
  542. if l < lens then
  543. movelen := l
  544. else
  545. movelen := lens;
  546. Move(Pointer(S)^,Temp^,movelen);
  547. { ref count dropped to zero in the mean time? }
  548. If (PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref > 0) and
  549. declocked(PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref) then
  550. freemem(PAnsiRec(Pointer(s)-AnsiFirstOff));
  551. Pointer(S):=Temp;
  552. end;
  553. { Force nil termination in case it gets shorter }
  554. PByte(Pointer(S)+l)^:=0;
  555. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  556. end
  557. else
  558. begin
  559. { Length=0 }
  560. if Pointer(S)<>nil then
  561. fpc_ansistr_decr_ref (Pointer(S));
  562. Pointer(S):=Nil;
  563. end;
  564. end;
  565. {$ifdef EXTRAANSISHORT}
  566. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  567. {
  568. Compares a AnsiString with a ShortString;
  569. The result is
  570. <0 if S1<S2
  571. 0 if S1=S2
  572. >0 if S1>S2
  573. }
  574. Var
  575. i,MaxI,Temp : SizeInt;
  576. begin
  577. Temp:=0;
  578. i:=0;
  579. MaxI:=Length(AnsiString(S1));
  580. if MaxI>byte(S2[0]) then
  581. MaxI:=Byte(S2[0]);
  582. While (i<MaxI) and (Temp=0) do
  583. begin
  584. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  585. inc(i);
  586. end;
  587. AnsiStr_ShortStr_Compare:=Temp;
  588. end;
  589. {$endif EXTRAANSISHORT}
  590. {*****************************************************************************
  591. Public functions, In interface.
  592. *****************************************************************************}
  593. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  594. Var
  595. SNew : Pointer;
  596. L : SizeInt;
  597. begin
  598. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  599. SNew:=NewAnsiString (L);
  600. Move (Pointer(S)^,SNew^,L+1);
  601. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  602. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  603. pointer(S):=SNew;
  604. pointer(result):=SNew;
  605. end;
  606. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  607. // MV: inline the basic checks for case that S is already unique.
  608. // Rest is too complex to inline, so factor that out as a call.
  609. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  610. {
  611. Make sure reference count of S is 1,
  612. using copy-on-write semantics.
  613. }
  614. begin
  615. pointer(result) := pointer(s);
  616. If Pointer(S)=Nil then
  617. exit;
  618. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  619. result:=fpc_truely_ansistr_unique(s);
  620. end;
  621. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  622. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  623. begin
  624. SetLength(S,length(S)+1);
  625. // avoid unique call
  626. PChar(Pointer(S)+length(S)-1)^:=c;
  627. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  628. end;
  629. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  630. var
  631. ofs : SizeInt;
  632. begin
  633. if Str='' then
  634. exit;
  635. ofs:=Length(S);
  636. SetLength(S,ofs+length(Str));
  637. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  638. move(Str[1],(pointer(S)+ofs)^,length(Str));
  639. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  640. end;
  641. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  642. var
  643. ofs, strlength: SizeInt;
  644. samestring: boolean;
  645. begin
  646. if Str='' then
  647. exit;
  648. samestring := pointer(s) = pointer(str);
  649. { needed in case s and str are the same string }
  650. strlength := length(str);
  651. ofs:=Length(S);
  652. SetLength(S,ofs+strlength);
  653. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  654. if not(samestring) then
  655. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  656. else
  657. { the setlength may have relocated the string, so str may no longer be valid }
  658. move(S[1],(pointer(S)+ofs)^,strlength+1)
  659. end;
  660. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  661. var
  662. ResultAddress : Pointer;
  663. begin
  664. ResultAddress:=Nil;
  665. dec(index);
  666. if Index < 0 then
  667. Index := 0;
  668. { Check Size. Accounts for Zero-length S, the double check is needed because
  669. Size can be maxint and will get <0 when adding index }
  670. if (Size>Length(S)) or
  671. (Index+Size>Length(S)) then
  672. Size:=Length(S)-Index;
  673. If Size>0 then
  674. begin
  675. If Index<0 Then
  676. Index:=0;
  677. ResultAddress:=Pointer(NewAnsiString (Size));
  678. if ResultAddress<>Nil then
  679. begin
  680. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  681. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  682. PByte(ResultAddress+Size)^:=0;
  683. end;
  684. end;
  685. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  686. Pointer(fpc_ansistr_Copy):=ResultAddress;
  687. end;
  688. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  689. var
  690. i,MaxLen : SizeInt;
  691. pc : pchar;
  692. begin
  693. Pos:=0;
  694. if Length(SubStr)>0 then
  695. begin
  696. MaxLen:=Length(source)-Length(SubStr);
  697. i:=0;
  698. pc:=@source[1];
  699. while (i<=MaxLen) do
  700. begin
  701. inc(i);
  702. if (SubStr[1]=pc^) and
  703. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  704. begin
  705. Pos:=i;
  706. exit;
  707. end;
  708. inc(pc);
  709. end;
  710. end;
  711. end;
  712. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  713. var
  714. i,MaxLen : SizeInt;
  715. pc : pchar;
  716. begin
  717. Pos:=0;
  718. if Length(SubStr)>0 then
  719. begin
  720. MaxLen:=Length(source)-Length(SubStr);
  721. i:=0;
  722. pc:=@source[1];
  723. while (i<=MaxLen) do
  724. begin
  725. inc(i);
  726. if (SubStr[1]=pc^) and
  727. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  728. begin
  729. Pos:=i;
  730. exit;
  731. end;
  732. inc(pc);
  733. end;
  734. end;
  735. end;
  736. { Faster version for a char alone. Must be implemented because }
  737. { pos(c: char; const s: shortstring) also exists, so otherwise }
  738. { using pos(char,pchar) will always call the shortstring version }
  739. { (exact match for first argument), also with $h+ (JM) }
  740. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  741. var
  742. i: SizeInt;
  743. pc : pchar;
  744. begin
  745. pc:=@s[1];
  746. for i:=1 to length(s) do
  747. begin
  748. if pc^=c then
  749. begin
  750. pos:=i;
  751. exit;
  752. end;
  753. inc(pc);
  754. end;
  755. pos:=0;
  756. end;
  757. {$ifndef FPUNONE}
  758. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  759. Var
  760. SS : String;
  761. begin
  762. fpc_Val_Real_AnsiStr := 0;
  763. if length(S) > 255 then
  764. code := 256
  765. else
  766. begin
  767. SS := S;
  768. Val(SS,fpc_Val_Real_AnsiStr,code);
  769. end;
  770. end;
  771. {$endif}
  772. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  773. Var
  774. SS : String;
  775. begin
  776. if length(S) > 255 then
  777. begin
  778. fpc_Val_Currency_AnsiStr := 0;
  779. code := 256;
  780. end
  781. else
  782. begin
  783. SS := S;
  784. Val(SS,fpc_Val_Currency_AnsiStr,code);
  785. end;
  786. end;
  787. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  788. Var
  789. SS : ShortString;
  790. begin
  791. fpc_Val_UInt_AnsiStr := 0;
  792. if length(S) > 255 then
  793. code := 256
  794. else
  795. begin
  796. SS := S;
  797. Val(SS,fpc_Val_UInt_AnsiStr,code);
  798. end;
  799. end;
  800. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  801. Var
  802. SS : ShortString;
  803. begin
  804. fpc_Val_SInt_AnsiStr:=0;
  805. if length(S)>255 then
  806. code:=256
  807. else
  808. begin
  809. SS := S;
  810. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  811. end;
  812. end;
  813. {$ifndef CPU64}
  814. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  815. Var
  816. SS : ShortString;
  817. begin
  818. fpc_Val_qword_AnsiStr:=0;
  819. if length(S)>255 then
  820. code:=256
  821. else
  822. begin
  823. SS := S;
  824. Val(SS,fpc_Val_qword_AnsiStr,Code);
  825. end;
  826. end;
  827. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  828. Var
  829. SS : ShortString;
  830. begin
  831. fpc_Val_int64_AnsiStr:=0;
  832. if length(S)>255 then
  833. code:=256
  834. else
  835. begin
  836. SS := s;
  837. Val(SS,fpc_Val_int64_AnsiStr,Code);
  838. end;
  839. end;
  840. {$endif CPU64}
  841. {$ifndef FPUNONE}
  842. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  843. var
  844. ss: ShortString;
  845. begin
  846. str_real(len,fr,d,treal_type(rt),ss);
  847. s:=ss;
  848. end;
  849. {$endif}
  850. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:ansistring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  851. var ss:shortstring;
  852. begin
  853. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  854. s:=ss;
  855. end;
  856. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:ansistring);[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  857. var
  858. ss:shortstring;
  859. begin
  860. fpc_shortstr_bool(b,len,ss);
  861. s:=ss;
  862. end;
  863. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:ansistring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  864. begin
  865. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  866. end;
  867. {$ifdef FPC_HAS_STR_CURRENCY}
  868. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  869. var
  870. ss: ShortString;
  871. begin
  872. str(c:len:fr,ss);
  873. s:=ss;
  874. end;
  875. {$endif FPC_HAS_STR_CURRENCY}
  876. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  877. Var
  878. SS : ShortString;
  879. begin
  880. str(v:Len,SS);
  881. S:=SS;
  882. end;
  883. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  884. Var
  885. SS : ShortString;
  886. begin
  887. str (v:Len,SS);
  888. S:=SS;
  889. end;
  890. {$ifndef CPU64}
  891. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  892. Var
  893. SS : ShortString;
  894. begin
  895. str(v:Len,SS);
  896. S:=SS;
  897. end;
  898. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  899. Var
  900. SS : ShortString;
  901. begin
  902. str (v:Len,SS);
  903. S:=SS;
  904. end;
  905. {$endif CPU64}
  906. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  907. Var
  908. LS : SizeInt;
  909. begin
  910. ls:=Length(S);
  911. If (Index>LS) or (Index<=0) or (Size<=0) then
  912. exit;
  913. UniqueString (S);
  914. If (Size>LS-Index) then // Size+Index gives overflow ??
  915. Size:=LS-Index+1;
  916. If (Size<=LS-Index) then
  917. begin
  918. Dec(Index);
  919. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  920. end;
  921. Setlength(S,LS-Size);
  922. end;
  923. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  924. var
  925. Temp : AnsiString;
  926. LS : SizeInt;
  927. begin
  928. If Length(Source)=0 then
  929. exit;
  930. if index <= 0 then
  931. index := 1;
  932. Ls:=Length(S);
  933. if index > LS then
  934. index := LS+1;
  935. Dec(Index);
  936. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  937. SetLength(Temp,Length(Source)+LS);
  938. If Index>0 then
  939. move (Pointer(S)^,Pointer(Temp)^,Index);
  940. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  941. If (LS-Index)>0 then
  942. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  943. S:=Temp;
  944. end;
  945. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  946. begin
  947. SetLength(StringOfChar,l);
  948. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  949. end;
  950. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  951. begin
  952. SetLength(S,Len);
  953. If (Buf<>Nil) then
  954. Move (Buf^,Pointer(S)^,Len);
  955. end;
  956. Procedure SetString (Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  957. begin
  958. if (Buf<>nil) and (Len>0) then
  959. widestringmanager.Wide2AnsiMoveProc(Buf,S,Len)
  960. else
  961. SetLength(S, Len);
  962. end;
  963. function upcase(const s : ansistring) : ansistring;
  964. var
  965. i : SizeInt;
  966. begin
  967. Setlength(result,length(s));
  968. for i := 1 to length (s) do
  969. result[i] := upcase(s[i]);
  970. end;
  971. function lowercase(const s : ansistring) : ansistring;
  972. var
  973. i : SizeInt;
  974. begin
  975. Setlength(result,length(s));
  976. for i := 1 to length (s) do
  977. result[i] := lowercase(s[i]);
  978. end;
  979. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  980. begin
  981. if assigned(Pointer(S)) then
  982. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  983. else
  984. Result:=SizeOf(AnsiChar);
  985. end;
  986. function StringElementSize(const S: RawByteString): Word; overload;
  987. begin
  988. if assigned(Pointer(S)) then
  989. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  990. else
  991. Result:=SizeOf(AnsiChar);
  992. end;
  993. function StringRefCount(const S: RawByteString): SizeInt; overload;
  994. begin
  995. if assigned(Pointer(S)) then
  996. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  997. else
  998. Result:=SizeOf(AnsiChar);
  999. end;