astrings.inc 30 KB

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