2
0

astrings.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  482. begin
  483. if p=nil then
  484. HandleErrorFrame(201,get_frame);
  485. end;
  486. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  487. begin
  488. if (index>len) or (Index<1) then
  489. HandleErrorFrame(201,get_frame);
  490. end;
  491. Procedure fpc_AnsiStr_SetLength (Var S : AnsiString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  492. {
  493. Sets The length of string S to L.
  494. Makes sure S is unique, and contains enough room.
  495. }
  496. Var
  497. Temp : Pointer;
  498. lens, lena,
  499. movelen : SizeInt;
  500. begin
  501. if (l>0) then
  502. begin
  503. if Pointer(S)=nil then
  504. begin
  505. GetMem(Pointer(S),AnsiRecLen+L);
  506. PAnsiRec(S)^.Ref:=1;
  507. inc(Pointer(S),firstoff);
  508. end
  509. else if PAnsiRec(Pointer(S)-FirstOff)^.Ref=1 then
  510. begin
  511. Dec(Pointer(S),FirstOff);
  512. lens:=MemSize(Pointer(s));
  513. lena:=AnsiRecLen+L;
  514. { allow shrinking string if that saves at least half of current size }
  515. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  516. reallocmem(pointer(S),AnsiRecLen+L);
  517. Inc(Pointer(S),FirstOff);
  518. end
  519. else
  520. begin
  521. { Reallocation is needed... }
  522. Temp:=Pointer(NewAnsiString(L));
  523. { also move terminating null }
  524. lens:=succ(length(s));
  525. if l < lens then
  526. movelen := l
  527. else
  528. movelen := lens;
  529. Move(Pointer(S)^,Temp^,movelen);
  530. { ref count dropped to zero in the mean time? }
  531. If (PAnsiRec(Pointer(S)-FirstOff)^.Ref > 0) and
  532. declocked(PAnsiRec(Pointer(S)-FirstOff)^.Ref) then
  533. freemem(PAnsiRec(Pointer(s)-FirstOff));
  534. Pointer(S):=Temp;
  535. end;
  536. { Force nil termination in case it gets shorter }
  537. PByte(Pointer(S)+l)^:=0;
  538. PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
  539. end
  540. else
  541. begin
  542. { Length=0 }
  543. if Pointer(S)<>nil then
  544. fpc_ansistr_decr_ref (Pointer(S));
  545. Pointer(S):=Nil;
  546. end;
  547. end;
  548. {$ifdef EXTRAANSISHORT}
  549. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  550. {
  551. Compares a AnsiString with a ShortString;
  552. The result is
  553. <0 if S1<S2
  554. 0 if S1=S2
  555. >0 if S1>S2
  556. }
  557. Var
  558. i,MaxI,Temp : SizeInt;
  559. begin
  560. Temp:=0;
  561. i:=0;
  562. MaxI:=Length(AnsiString(S1));
  563. if MaxI>byte(S2[0]) then
  564. MaxI:=Byte(S2[0]);
  565. While (i<MaxI) and (Temp=0) do
  566. begin
  567. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  568. inc(i);
  569. end;
  570. AnsiStr_ShortStr_Compare:=Temp;
  571. end;
  572. {$endif EXTRAANSISHORT}
  573. {*****************************************************************************
  574. Public functions, In interface.
  575. *****************************************************************************}
  576. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  577. Var
  578. SNew : Pointer;
  579. L : SizeInt;
  580. begin
  581. L:=PAnsiRec(Pointer(S)-FirstOff)^.len;
  582. SNew:=NewAnsiString (L);
  583. Move (Pointer(S)^,SNew^,L+1);
  584. PAnsiRec(SNew-FirstOff)^.len:=L;
  585. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  586. pointer(S):=SNew;
  587. pointer(result):=SNew;
  588. end;
  589. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  590. // MV: inline the basic checks for case that S is already unique.
  591. // Rest is too complex to inline, so factor that out as a call.
  592. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  593. {
  594. Make sure reference count of S is 1,
  595. using copy-on-write semantics.
  596. }
  597. begin
  598. pointer(result) := pointer(s);
  599. If Pointer(S)=Nil then
  600. exit;
  601. if PAnsiRec(Pointer(S)-Firstoff)^.Ref<>1 then
  602. result:=fpc_truely_ansistr_unique(s);
  603. end;
  604. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  605. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  606. begin
  607. SetLength(S,length(S)+1);
  608. // avoid unique call
  609. PChar(Pointer(S)+length(S)-1)^:=c;
  610. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  611. end;
  612. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  613. var
  614. ofs : SizeInt;
  615. begin
  616. if Str='' then
  617. exit;
  618. ofs:=Length(S);
  619. SetLength(S,ofs+length(Str));
  620. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  621. move(Str[1],(pointer(S)+ofs)^,length(Str));
  622. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  623. end;
  624. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  625. var
  626. ofs, strlength: SizeInt;
  627. samestring: boolean;
  628. begin
  629. if Str='' then
  630. exit;
  631. samestring := pointer(s) = pointer(str);
  632. { needed in case s and str are the same string }
  633. strlength := length(str);
  634. ofs:=Length(S);
  635. SetLength(S,ofs+strlength);
  636. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  637. if not(samestring) then
  638. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  639. else
  640. { the setlength may have relocated the string, so str may no longer be valid }
  641. move(S[1],(pointer(S)+ofs)^,strlength+1)
  642. end;
  643. Function Fpc_Ansistr_Copy (Const S : AnsiString; Index,Size : SizeInt) : AnsiString;compilerproc;
  644. var
  645. ResultAddress : Pointer;
  646. begin
  647. ResultAddress:=Nil;
  648. dec(index);
  649. if Index < 0 then
  650. Index := 0;
  651. { Check Size. Accounts for Zero-length S, the double check is needed because
  652. Size can be maxint and will get <0 when adding index }
  653. if (Size>Length(S)) or
  654. (Index+Size>Length(S)) then
  655. Size:=Length(S)-Index;
  656. If Size>0 then
  657. begin
  658. If Index<0 Then
  659. Index:=0;
  660. ResultAddress:=Pointer(NewAnsiString (Size));
  661. if ResultAddress<>Nil then
  662. begin
  663. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  664. PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
  665. PByte(ResultAddress+Size)^:=0;
  666. end;
  667. end;
  668. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  669. Pointer(fpc_ansistr_Copy):=ResultAddress;
  670. end;
  671. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  672. var
  673. i,MaxLen : SizeInt;
  674. pc : pchar;
  675. begin
  676. Pos:=0;
  677. if Length(SubStr)>0 then
  678. begin
  679. MaxLen:=Length(source)-Length(SubStr);
  680. i:=0;
  681. pc:=@source[1];
  682. while (i<=MaxLen) do
  683. begin
  684. inc(i);
  685. if (SubStr[1]=pc^) and
  686. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  687. begin
  688. Pos:=i;
  689. exit;
  690. end;
  691. inc(pc);
  692. end;
  693. end;
  694. end;
  695. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  696. var
  697. i,MaxLen : SizeInt;
  698. pc : pchar;
  699. begin
  700. Pos:=0;
  701. if Length(SubStr)>0 then
  702. begin
  703. MaxLen:=Length(source)-Length(SubStr);
  704. i:=0;
  705. pc:=@source[1];
  706. while (i<=MaxLen) do
  707. begin
  708. inc(i);
  709. if (SubStr[1]=pc^) and
  710. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  711. begin
  712. Pos:=i;
  713. exit;
  714. end;
  715. inc(pc);
  716. end;
  717. end;
  718. end;
  719. { Faster version for a char alone. Must be implemented because }
  720. { pos(c: char; const s: shortstring) also exists, so otherwise }
  721. { using pos(char,pchar) will always call the shortstring version }
  722. { (exact match for first argument), also with $h+ (JM) }
  723. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  724. var
  725. i: SizeInt;
  726. pc : pchar;
  727. begin
  728. pc:=@s[1];
  729. for i:=1 to length(s) do
  730. begin
  731. if pc^=c then
  732. begin
  733. pos:=i;
  734. exit;
  735. end;
  736. inc(pc);
  737. end;
  738. pos:=0;
  739. end;
  740. {$ifndef FPUNONE}
  741. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  742. Var
  743. SS : String;
  744. begin
  745. fpc_Val_Real_AnsiStr := 0;
  746. if length(S) > 255 then
  747. code := 256
  748. else
  749. begin
  750. SS := S;
  751. Val(SS,fpc_Val_Real_AnsiStr,code);
  752. end;
  753. end;
  754. {$endif}
  755. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  756. Var
  757. SS : String;
  758. begin
  759. if length(S) > 255 then
  760. begin
  761. fpc_Val_Currency_AnsiStr := 0;
  762. code := 256;
  763. end
  764. else
  765. begin
  766. SS := S;
  767. Val(SS,fpc_Val_Currency_AnsiStr,code);
  768. end;
  769. end;
  770. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  771. Var
  772. SS : ShortString;
  773. begin
  774. fpc_Val_UInt_AnsiStr := 0;
  775. if length(S) > 255 then
  776. code := 256
  777. else
  778. begin
  779. SS := S;
  780. Val(SS,fpc_Val_UInt_AnsiStr,code);
  781. end;
  782. end;
  783. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  784. Var
  785. SS : ShortString;
  786. begin
  787. fpc_Val_SInt_AnsiStr:=0;
  788. if length(S)>255 then
  789. code:=256
  790. else
  791. begin
  792. SS := S;
  793. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  794. end;
  795. end;
  796. {$ifndef CPU64}
  797. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  798. Var
  799. SS : ShortString;
  800. begin
  801. fpc_Val_qword_AnsiStr:=0;
  802. if length(S)>255 then
  803. code:=256
  804. else
  805. begin
  806. SS := S;
  807. Val(SS,fpc_Val_qword_AnsiStr,Code);
  808. end;
  809. end;
  810. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  811. Var
  812. SS : ShortString;
  813. begin
  814. fpc_Val_int64_AnsiStr:=0;
  815. if length(S)>255 then
  816. code:=256
  817. else
  818. begin
  819. SS := s;
  820. Val(SS,fpc_Val_int64_AnsiStr,Code);
  821. end;
  822. end;
  823. {$endif CPU64}
  824. {$ifndef FPUNONE}
  825. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  826. var
  827. ss: ShortString;
  828. begin
  829. str_real(len,fr,d,treal_type(rt),ss);
  830. s:=ss;
  831. end;
  832. {$endif}
  833. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  834. var ss:shortstring;
  835. begin
  836. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  837. s:=ss;
  838. end;
  839. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:ansistring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  840. begin
  841. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  842. end;
  843. {$ifdef FPC_HAS_STR_CURRENCY}
  844. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  845. var
  846. ss: ShortString;
  847. begin
  848. str(c:len:fr,ss);
  849. s:=ss;
  850. end;
  851. {$endif FPC_HAS_STR_CURRENCY}
  852. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  853. Var
  854. SS : ShortString;
  855. begin
  856. str(v:Len,SS);
  857. S:=SS;
  858. end;
  859. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  860. Var
  861. SS : ShortString;
  862. begin
  863. str (v:Len,SS);
  864. S:=SS;
  865. end;
  866. {$ifndef CPU64}
  867. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  868. Var
  869. SS : ShortString;
  870. begin
  871. str(v:Len,SS);
  872. S:=SS;
  873. end;
  874. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  875. Var
  876. SS : ShortString;
  877. begin
  878. str (v:Len,SS);
  879. S:=SS;
  880. end;
  881. {$endif CPU64}
  882. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  883. Var
  884. LS : SizeInt;
  885. begin
  886. ls:=Length(S);
  887. If (Index>LS) or (Index<=0) or (Size<=0) then
  888. exit;
  889. UniqueString (S);
  890. If (Size>LS-Index) then // Size+Index gives overflow ??
  891. Size:=LS-Index+1;
  892. If (Size<=LS-Index) then
  893. begin
  894. Dec(Index);
  895. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  896. end;
  897. Setlength(S,LS-Size);
  898. end;
  899. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  900. var
  901. Temp : AnsiString;
  902. LS : SizeInt;
  903. begin
  904. If Length(Source)=0 then
  905. exit;
  906. if index <= 0 then
  907. index := 1;
  908. Ls:=Length(S);
  909. if index > LS then
  910. index := LS+1;
  911. Dec(Index);
  912. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  913. SetLength(Temp,Length(Source)+LS);
  914. If Index>0 then
  915. move (Pointer(S)^,Pointer(Temp)^,Index);
  916. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  917. If (LS-Index)>0 then
  918. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  919. S:=Temp;
  920. end;
  921. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  922. begin
  923. SetLength(StringOfChar,l);
  924. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  925. end;
  926. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  927. begin
  928. SetLength(S,Len);
  929. If (Buf<>Nil) then
  930. Move (Buf^,Pointer(S)^,Len);
  931. end;
  932. function upcase(const s : ansistring) : ansistring;
  933. var
  934. i : SizeInt;
  935. begin
  936. Setlength(result,length(s));
  937. for i := 1 to length (s) do
  938. result[i] := upcase(s[i]);
  939. end;
  940. function lowercase(const s : ansistring) : ansistring;
  941. var
  942. i : SizeInt;
  943. begin
  944. Setlength(result,length(s));
  945. for i := 1 to length (s) do
  946. result[i] := lowercase(s[i]);
  947. end;