astrings.inc 28 KB

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