astrings.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 : ansistring;
  221. begin
  222. if high(sarr)=0 then
  223. begin
  224. DestS:='';
  225. exit;
  226. end;
  227. lowstart:=low(sarr);
  228. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  229. inc(lowstart);
  230. { Check for another reuse, then we can't use
  231. the append optimization }
  232. for i:=lowstart to high(sarr) do
  233. begin
  234. if Pointer(DestS)=Pointer(sarr[i]) then
  235. begin
  236. { if DestS is used somewhere in the middle of the expression,
  237. we need to make sure the original string still exists after
  238. we empty/modify DestS }
  239. destcopy:=dests;
  240. lowstart:=low(sarr);
  241. break;
  242. end;
  243. end;
  244. { Start with empty DestS if we start with concatting
  245. the first array element }
  246. if lowstart=low(sarr) then
  247. DestS:='';
  248. OldDestLen:=length(DestS);
  249. { Calculate size of the result so we can do
  250. a single call to SetLength() }
  251. NewLen:=0;
  252. for i:=low(sarr) to high(sarr) do
  253. inc(NewLen,length(sarr[i]));
  254. SetLength(DestS,NewLen);
  255. { Concat all strings, except the string we already
  256. copied in DestS }
  257. pc:=Pointer(DestS)+OldDestLen;
  258. for i:=lowstart to high(sarr) do
  259. begin
  260. p:=pointer(sarr[i]);
  261. if assigned(p) then
  262. begin
  263. Size:=length(ansistring(p));
  264. Move(p^,pc^,Size+1);
  265. inc(pc,size);
  266. end;
  267. end;
  268. end;
  269. {$endif STR_CONCAT_PROCS}
  270. {$ifdef EXTRAANSISHORT}
  271. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  272. {
  273. Concatenates a Ansi with a short string; : S2 + S2
  274. }
  275. Var
  276. Size,Location : SizeInt;
  277. begin
  278. Size:=Length(S2);
  279. Location:=Length(S1);
  280. If Size=0 then
  281. exit;
  282. { Setlength takes case of uniqueness
  283. and alllocated memory. We need to use length,
  284. to take into account possibility of S1=Nil }
  285. SetLength (S1,Size+Length(S1));
  286. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  287. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  288. end;
  289. {$endif EXTRAANSISHORT}
  290. { the following declaration has exactly the same effect as }
  291. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  292. { which is what the old helper was, so we don't need an extra implementation }
  293. { of the old helper (JM) }
  294. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring;[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  295. {
  296. Converts a AnsiString to a ShortString;
  297. }
  298. Var
  299. Size : SizeInt;
  300. begin
  301. if S2='' then
  302. fpc_AnsiStr_To_ShortStr:=''
  303. else
  304. begin
  305. Size:=Length(S2);
  306. If Size>high_of_res then
  307. Size:=high_of_res;
  308. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  309. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  310. end;
  311. end;
  312. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; compilerproc;
  313. {
  314. Converts a ShortString to a AnsiString;
  315. }
  316. Var
  317. Size : SizeInt;
  318. begin
  319. Size:=Length(S2);
  320. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  321. if Size>0 then
  322. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  323. end;
  324. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; compilerproc;
  325. {
  326. Converts a Char to a AnsiString;
  327. }
  328. begin
  329. Setlength (fpc_Char_To_AnsiStr,1);
  330. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  331. { Terminating Zero }
  332. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  333. end;
  334. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; compilerproc;
  335. Var
  336. L : SizeInt;
  337. begin
  338. if (not assigned(p)) or (p[0]=#0) Then
  339. { result is automatically set to '' }
  340. exit;
  341. l:=IndexChar(p^,-1,#0);
  342. SetLength(fpc_PChar_To_AnsiStr,L);
  343. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  344. end;
  345. Function fpc_CharArray_To_AnsiStr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
  346. var
  347. i : SizeInt;
  348. begin
  349. if (zerobased) then
  350. begin
  351. if (arr[0]=#0) Then
  352. { result is automatically set to '' }
  353. exit;
  354. i:=IndexChar(arr,high(arr)+1,#0);
  355. if i = -1 then
  356. i := high(arr)+1;
  357. end
  358. else
  359. i := high(arr)+1;
  360. SetLength(fpc_CharArray_To_AnsiStr,i);
  361. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  362. end;
  363. { note: inside the compiler, the resulttype is modified to be the length }
  364. { of the actual chararray to which we convert (JM) }
  365. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  366. var
  367. len: SizeInt;
  368. begin
  369. len := length(src);
  370. if len > arraysize then
  371. len := arraysize;
  372. { make sure we don't try to access element 1 of the ansistring if it's nil }
  373. if len > 0 then
  374. move(src[1],fpc_ansistr_to_chararray[0],len);
  375. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  376. end;
  377. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  378. {
  379. Compares 2 AnsiStrings;
  380. The result is
  381. <0 if S1<S2
  382. 0 if S1=S2
  383. >0 if S1>S2
  384. }
  385. Var
  386. MaxI,Temp : SizeInt;
  387. begin
  388. if pointer(S1)=pointer(S2) then
  389. begin
  390. result:=0;
  391. exit;
  392. end;
  393. Maxi:=Length(S1);
  394. temp:=Length(S2);
  395. If MaxI>Temp then
  396. MaxI:=Temp;
  397. if MaxI>0 then
  398. begin
  399. result:=CompareByte(S1[1],S2[1],MaxI);
  400. if result=0 then
  401. result:=Length(S1)-Length(S2);
  402. end
  403. else
  404. result:=Length(S1)-Length(S2);
  405. end;
  406. Function fpc_AnsiStr_Compare_equal(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  407. {
  408. Compares 2 AnsiStrings for equality/inequality only;
  409. The result is
  410. 0 if S1=S2
  411. <>0 if S1<>S2
  412. }
  413. Var
  414. MaxI,Temp : SizeInt;
  415. begin
  416. if pointer(S1)=pointer(S2) then
  417. begin
  418. result:=0;
  419. exit;
  420. end;
  421. Maxi:=Length(S1);
  422. temp:=Length(S2);
  423. Result := Maxi - temp;
  424. if Result = 0 then
  425. if MaxI>0 then
  426. result:=CompareByte(S1[1],S2[1],MaxI);
  427. end;
  428. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  429. begin
  430. if p=nil then
  431. HandleErrorFrame(201,get_frame);
  432. end;
  433. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  434. begin
  435. if (index>len) or (Index<1) then
  436. HandleErrorFrame(201,get_frame);
  437. end;
  438. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  439. {
  440. Sets The length of string S to L.
  441. Makes sure S is unique, and contains enough room.
  442. }
  443. Var
  444. Temp : Pointer;
  445. lens,
  446. movelen : SizeInt;
  447. begin
  448. if (l>0) then
  449. begin
  450. if Pointer(S)=nil then
  451. begin
  452. GetMem(Pointer(S),AnsiRecLen+L);
  453. PAnsiRec(S)^.Ref:=1;
  454. inc(Pointer(S),firstoff);
  455. end
  456. else if PAnsiRec(Pointer(S)-FirstOff)^.Ref=1 then
  457. begin
  458. Dec(Pointer(S),FirstOff);
  459. if AnsiRecLen+L>MemSize(Pointer(s)) then
  460. reallocmem(pointer(S),AnsiRecLen+L);
  461. Inc(Pointer(S),FirstOff);
  462. end
  463. else
  464. begin
  465. { Reallocation is needed... }
  466. Temp:=Pointer(NewAnsiString(L));
  467. { also move terminating null }
  468. lens:=succ(length(s));
  469. if l < lens then
  470. movelen := l
  471. else
  472. movelen := lens;
  473. Move(Pointer(S)^,Temp^,movelen);
  474. { ref count dropped to zero in the mean time? }
  475. If (PAnsiRec(Pointer(S)-FirstOff)^.Ref > 0) and
  476. declocked(PAnsiRec(Pointer(S)-FirstOff)^.Ref) then
  477. freemem(PAnsiRec(Pointer(s)-FirstOff));
  478. Pointer(S):=Temp;
  479. end;
  480. { Force nil termination in case it gets shorter }
  481. PByte(Pointer(S)+l)^:=0;
  482. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  483. end
  484. else
  485. begin
  486. { Length=0 }
  487. if Pointer(S)<>nil then
  488. fpc_ansistr_decr_ref (Pointer(S));
  489. Pointer(S):=Nil;
  490. end;
  491. end;
  492. {$ifdef EXTRAANSISHORT}
  493. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  494. {
  495. Compares a AnsiString with a ShortString;
  496. The result is
  497. <0 if S1<S2
  498. 0 if S1=S2
  499. >0 if S1>S2
  500. }
  501. Var
  502. i,MaxI,Temp : SizeInt;
  503. begin
  504. Temp:=0;
  505. i:=0;
  506. MaxI:=Length(AnsiString(S1));
  507. if MaxI>byte(S2[0]) then
  508. MaxI:=Byte(S2[0]);
  509. While (i<MaxI) and (Temp=0) do
  510. begin
  511. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  512. inc(i);
  513. end;
  514. AnsiStr_ShortStr_Compare:=Temp;
  515. end;
  516. {$endif EXTRAANSISHORT}
  517. {*****************************************************************************
  518. Public functions, In interface.
  519. *****************************************************************************}
  520. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  521. Var
  522. SNew : Pointer;
  523. L : SizeInt;
  524. begin
  525. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  526. SNew:=NewAnsiString (L);
  527. Move (Pointer(S)^,SNew^,L+1);
  528. PAnsiRec(SNew-FirstOff)^.len:=L;
  529. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  530. pointer(S):=SNew;
  531. pointer(result):=SNew;
  532. end;
  533. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  534. // MV: inline the basic checks for case that S is already unique.
  535. // Rest is too complex to inline, so factor that out as a call.
  536. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  537. {
  538. Make sure reference count of S is 1,
  539. using copy-on-write semantics.
  540. }
  541. begin
  542. pointer(result) := pointer(s);
  543. If Pointer(S)=Nil then
  544. exit;
  545. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  546. result:=fpc_truely_ansistr_unique(s);
  547. end;
  548. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  549. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  550. begin
  551. SetLength(S,length(S)+1);
  552. // avoid unique call
  553. PChar(Pointer(S)+length(S)-1)^:=c;
  554. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  555. end;
  556. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  557. var
  558. ofs : SizeInt;
  559. begin
  560. if Str='' then
  561. exit;
  562. ofs:=Length(S);
  563. SetLength(S,ofs+length(Str));
  564. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  565. move(Str[1],(pointer(S)+ofs)^,length(Str));
  566. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  567. end;
  568. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  569. var
  570. ofs, strlength: SizeInt;
  571. samestring: boolean;
  572. begin
  573. if Str='' then
  574. exit;
  575. samestring := pointer(s) = pointer(str);
  576. { needed in case s and str are the same string }
  577. strlength := length(str);
  578. ofs:=Length(S);
  579. SetLength(S,ofs+strlength);
  580. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  581. if not(samestring) then
  582. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  583. else
  584. { the setlength may have relocated the string, so str may no longer be valid }
  585. move(S[1],(pointer(S)+ofs)^,strlength+1)
  586. end;
  587. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  588. var
  589. ResultAddress : Pointer;
  590. begin
  591. ResultAddress:=Nil;
  592. dec(index);
  593. if Index < 0 then
  594. Index := 0;
  595. { Check Size. Accounts for Zero-length S, the double check is needed because
  596. Size can be maxint and will get <0 when adding index }
  597. if (Size>Length(S)) or
  598. (Index+Size>Length(S)) then
  599. Size:=Length(S)-Index;
  600. If Size>0 then
  601. begin
  602. If Index<0 Then
  603. Index:=0;
  604. ResultAddress:=Pointer(NewAnsiString (Size));
  605. if ResultAddress<>Nil then
  606. begin
  607. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  608. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  609. PByte(ResultAddress+Size)^:=0;
  610. end;
  611. end;
  612. Pointer(fpc_ansistr_Copy):=ResultAddress;
  613. end;
  614. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  615. var
  616. i,MaxLen : SizeInt;
  617. pc : pchar;
  618. begin
  619. Pos:=0;
  620. if Length(SubStr)>0 then
  621. begin
  622. MaxLen:=Length(source)-Length(SubStr);
  623. i:=0;
  624. pc:=@source[1];
  625. while (i<=MaxLen) do
  626. begin
  627. inc(i);
  628. if (SubStr[1]=pc^) and
  629. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  630. begin
  631. Pos:=i;
  632. exit;
  633. end;
  634. inc(pc);
  635. end;
  636. end;
  637. end;
  638. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  639. var
  640. i,MaxLen : SizeInt;
  641. pc : pchar;
  642. begin
  643. Pos:=0;
  644. if Length(SubStr)>0 then
  645. begin
  646. MaxLen:=Length(source)-Length(SubStr);
  647. i:=0;
  648. pc:=@source[1];
  649. while (i<=MaxLen) do
  650. begin
  651. inc(i);
  652. if (SubStr[1]=pc^) and
  653. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  654. begin
  655. Pos:=i;
  656. exit;
  657. end;
  658. inc(pc);
  659. end;
  660. end;
  661. end;
  662. { Faster version for a char alone. Must be implemented because }
  663. { pos(c: char; const s: shortstring) also exists, so otherwise }
  664. { using pos(char,pchar) will always call the shortstring version }
  665. { (exact match for first argument), also with $h+ (JM) }
  666. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  667. var
  668. i: SizeInt;
  669. pc : pchar;
  670. begin
  671. pc:=@s[1];
  672. for i:=1 to length(s) do
  673. begin
  674. if pc^=c then
  675. begin
  676. pos:=i;
  677. exit;
  678. end;
  679. inc(pc);
  680. end;
  681. pos:=0;
  682. end;
  683. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  684. Var
  685. SS : String;
  686. begin
  687. fpc_Val_Real_AnsiStr := 0;
  688. if length(S) > 255 then
  689. code := 256
  690. else
  691. begin
  692. SS := S;
  693. Val(SS,fpc_Val_Real_AnsiStr,code);
  694. end;
  695. end;
  696. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  697. Var
  698. SS : String;
  699. begin
  700. if length(S) > 255 then
  701. begin
  702. fpc_Val_Currency_AnsiStr := 0;
  703. code := 256;
  704. end
  705. else
  706. begin
  707. SS := S;
  708. Val(SS,fpc_Val_Currency_AnsiStr,code);
  709. end;
  710. end;
  711. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  712. Var
  713. SS : ShortString;
  714. begin
  715. fpc_Val_UInt_AnsiStr := 0;
  716. if length(S) > 255 then
  717. code := 256
  718. else
  719. begin
  720. SS := S;
  721. Val(SS,fpc_Val_UInt_AnsiStr,code);
  722. end;
  723. end;
  724. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  725. Var
  726. SS : ShortString;
  727. begin
  728. fpc_Val_SInt_AnsiStr:=0;
  729. if length(S)>255 then
  730. code:=256
  731. else
  732. begin
  733. SS := S;
  734. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  735. end;
  736. end;
  737. {$ifndef CPU64}
  738. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  739. Var
  740. SS : ShortString;
  741. begin
  742. fpc_Val_qword_AnsiStr:=0;
  743. if length(S)>255 then
  744. code:=256
  745. else
  746. begin
  747. SS := S;
  748. Val(SS,fpc_Val_qword_AnsiStr,Code);
  749. end;
  750. end;
  751. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  752. Var
  753. SS : ShortString;
  754. begin
  755. fpc_Val_int64_AnsiStr:=0;
  756. if length(S)>255 then
  757. code:=256
  758. else
  759. begin
  760. SS := s;
  761. Val(SS,fpc_Val_int64_AnsiStr,Code);
  762. end;
  763. end;
  764. {$endif CPU64}
  765. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  766. var
  767. ss: ShortString;
  768. begin
  769. str_real(len,fr,d,treal_type(rt),ss);
  770. s:=ss;
  771. end;
  772. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  773. var ss:shortstring;
  774. begin
  775. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  776. s:=ss;
  777. end;
  778. {$ifdef FPC_HAS_STR_CURRENCY}
  779. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  780. var
  781. ss: ShortString;
  782. begin
  783. str(c:len:fr,ss);
  784. s:=ss;
  785. end;
  786. {$endif FPC_HAS_STR_CURRENCY}
  787. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  788. Var
  789. SS : ShortString;
  790. begin
  791. str(v:Len,SS);
  792. S:=SS;
  793. end;
  794. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  795. Var
  796. SS : ShortString;
  797. begin
  798. str (v:Len,SS);
  799. S:=SS;
  800. end;
  801. {$ifndef CPU64}
  802. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  803. Var
  804. SS : ShortString;
  805. begin
  806. str(v:Len,SS);
  807. S:=SS;
  808. end;
  809. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  810. Var
  811. SS : ShortString;
  812. begin
  813. str (v:Len,SS);
  814. S:=SS;
  815. end;
  816. {$endif CPU64}
  817. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  818. Var
  819. LS : SizeInt;
  820. begin
  821. ls:=Length(S);
  822. If (Index>LS) or (Index<=0) or (Size<=0) then
  823. exit;
  824. UniqueString (S);
  825. If (Size>LS-Index) then // Size+Index gives overflow ??
  826. Size:=LS-Index+1;
  827. If (Size<=LS-Index) then
  828. begin
  829. Dec(Index);
  830. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  831. end;
  832. Setlength(S,LS-Size);
  833. end;
  834. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  835. var
  836. Temp : AnsiString;
  837. LS : SizeInt;
  838. begin
  839. If Length(Source)=0 then
  840. exit;
  841. if index <= 0 then
  842. index := 1;
  843. Ls:=Length(S);
  844. if index > LS then
  845. index := LS+1;
  846. Dec(Index);
  847. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  848. SetLength(Temp,Length(Source)+LS);
  849. If Index>0 then
  850. move (Pointer(S)^,Pointer(Temp)^,Index);
  851. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  852. If (LS-Index)>0 then
  853. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  854. S:=Temp;
  855. end;
  856. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  857. begin
  858. SetLength(StringOfChar,l);
  859. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  860. end;
  861. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  862. begin
  863. SetLength(S,Len);
  864. If (Buf<>Nil) then
  865. Move (Buf^,Pointer(S)^,Len);
  866. end;
  867. function upcase(const s : ansistring) : ansistring;
  868. var
  869. i : SizeInt;
  870. begin
  871. Setlength(result,length(s));
  872. for i := 1 to length (s) do
  873. result[i] := upcase(s[i]);
  874. end;
  875. function lowercase(const s : ansistring) : ansistring;
  876. var
  877. i : SizeInt;
  878. begin
  879. Setlength(result,length(s));
  880. for i := 1 to length (s) do
  881. result[i] := lowercase(s[i]);
  882. end;