astrings.inc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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:RawByteString;const S1,S2 : RawByteString); compilerproc;
  183. Var
  184. Size,Location : SizeInt;
  185. same : boolean;
  186. S1CP, S2CP, DestCP: TSystemCodePage;
  187. U: UnicodeString;
  188. begin
  189. { if codepages are differ then concat using unicodestring }
  190. S1CP:=StringCodePage(S1);
  191. if S1CP=$ffff then
  192. S1CP:=DefaultSystemCodePage;
  193. S2CP:=StringCodePage(S2);
  194. if S2CP=$ffff then
  195. S2CP:=DefaultSystemCodePage;
  196. DestCP:=StringCodePage(DestS);
  197. if DestCP=$ffff then
  198. DestCP:=DefaultSystemCodePage;
  199. if (S1CP<>DestCP) or (S2CP<>DestCP) then
  200. begin
  201. U:=UnicodeString(S1)+UnicodeString(S2);
  202. DestS:='';
  203. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(U)),DestS,DestCP,Length(U));
  204. exit;
  205. end;
  206. { only assign if s1 or s2 is empty }
  207. if (S1='') then
  208. begin
  209. DestS:=s2;
  210. exit;
  211. end;
  212. if (S2='') then
  213. begin
  214. DestS:=s1;
  215. exit;
  216. end;
  217. Location:=Length(S1);
  218. Size:=length(S2);
  219. { Use Pointer() typecasts to prevent extra conversion code }
  220. if Pointer(DestS)=Pointer(S1) then
  221. begin
  222. same:=Pointer(S1)=Pointer(S2);
  223. SetLength(DestS,Size+Location);
  224. if same then
  225. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size)
  226. else
  227. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  228. end
  229. else if Pointer(DestS)=Pointer(S2) then
  230. begin
  231. SetLength(DestS,Size+Location);
  232. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size+1);
  233. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  234. end
  235. else
  236. begin
  237. DestS:='';
  238. SetLength(DestS,Size+Location);
  239. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  240. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  241. end;
  242. end;
  243. procedure fpc_AnsiStr_Concat_multi (var DestS:ansistring;const sarr:array of Ansistring); compilerproc;
  244. Var
  245. lowstart,i : Longint;
  246. p,pc : pointer;
  247. Size,NewLen,
  248. OldDestLen : SizeInt;
  249. destcopy : pointer;
  250. begin
  251. if high(sarr)=0 then
  252. begin
  253. DestS:='';
  254. exit;
  255. end;
  256. destcopy:=nil;
  257. lowstart:=low(sarr);
  258. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  259. inc(lowstart);
  260. { Check for another reuse, then we can't use
  261. the append optimization }
  262. for i:=lowstart to high(sarr) do
  263. begin
  264. if Pointer(DestS)=Pointer(sarr[i]) then
  265. begin
  266. { if DestS is used somewhere in the middle of the expression,
  267. we need to make sure the original string still exists after
  268. we empty/modify DestS }
  269. destcopy:=pointer(dests);
  270. fpc_AnsiStr_Incr_Ref(destcopy);
  271. lowstart:=low(sarr);
  272. break;
  273. end;
  274. end;
  275. { Start with empty DestS if we start with concatting
  276. the first array element }
  277. if lowstart=low(sarr) then
  278. DestS:='';
  279. OldDestLen:=length(DestS);
  280. { Calculate size of the result so we can do
  281. a single call to SetLength() }
  282. NewLen:=0;
  283. for i:=low(sarr) to high(sarr) do
  284. inc(NewLen,length(sarr[i]));
  285. SetLength(DestS,NewLen);
  286. { Concat all strings, except the string we already
  287. copied in DestS }
  288. pc:=Pointer(DestS)+OldDestLen;
  289. for i:=lowstart to high(sarr) do
  290. begin
  291. p:=pointer(sarr[i]);
  292. if assigned(p) then
  293. begin
  294. Size:=length(ansistring(p));
  295. Move(p^,pc^,Size+1);
  296. inc(pc,size);
  297. end;
  298. end;
  299. fpc_AnsiStr_Decr_Ref(destcopy);
  300. end;
  301. {$endif STR_CONCAT_PROCS}
  302. {$ifdef EXTRAANSISHORT}
  303. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  304. {
  305. Concatenates a Ansi with a short string; : S2 + S2
  306. }
  307. Var
  308. Size,Location : SizeInt;
  309. begin
  310. Size:=Length(S2);
  311. Location:=Length(S1);
  312. If Size=0 then
  313. exit;
  314. { Setlength takes case of uniqueness
  315. and alllocated memory. We need to use length,
  316. to take into account possibility of S1=Nil }
  317. SetLength (S1,Size+Length(S1));
  318. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  319. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  320. end;
  321. {$endif EXTRAANSISHORT}
  322. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [Public, alias: 'FPC_ANSISTR_TO_ANSISTR']; compilerproc;
  323. {
  324. Converts an AnsiString to an AnsiString taking code pages into care
  325. }
  326. Var
  327. Size : SizeInt;
  328. temp : UnicodeString;
  329. begin
  330. temp:=S;
  331. result:='';
  332. Size:=Length(temp);
  333. if Size>0 then
  334. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(temp)),result,cp,Size);
  335. end;
  336. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [external name 'FPC_ANSISTR_TO_ANSISTR'];
  337. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  338. { the following declaration has exactly the same effect as }
  339. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  340. { which is what the old helper was, so we don't need an extra implementation }
  341. { of the old helper (JM) }
  342. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring; [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. fpc_AnsiStr_To_ShortStr:=''
  351. else
  352. begin
  353. Size:=Length(S2);
  354. If Size>high_of_res then
  355. Size:=high_of_res;
  356. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  357. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  358. end;
  359. end;
  360. {$else FPC_STRTOSHORTSTRINGPROC}
  361. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : Ansistring);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  362. {
  363. Converts a AnsiString to a ShortString;
  364. }
  365. Var
  366. Size : SizeInt;
  367. begin
  368. if S2='' then
  369. res:=''
  370. else
  371. begin
  372. Size:=Length(S2);
  373. If Size>high(res) then
  374. Size:=high(res);
  375. Move (S2[1],res[1],Size);
  376. byte(res[0]):=byte(Size);
  377. end;
  378. end;
  379. {$endif FPC_STRTOSHORTSTRINGPROC}
  380. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString): ansistring; compilerproc;
  381. {
  382. Converts a ShortString to a AnsiString;
  383. }
  384. Var
  385. Size : SizeInt;
  386. begin
  387. Size:=Length(S2);
  388. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  389. if Size>0 then
  390. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  391. end;
  392. Function fpc_Char_To_AnsiStr(const c : Char): AnsiString; compilerproc;
  393. {
  394. Converts a Char to a AnsiString;
  395. }
  396. begin
  397. Setlength (fpc_Char_To_AnsiStr,1);
  398. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  399. { Terminating Zero }
  400. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  401. end;
  402. Function fpc_PChar_To_AnsiStr(const p : pchar): ansistring; compilerproc;
  403. Var
  404. L : SizeInt;
  405. begin
  406. if (not assigned(p)) or (p[0]=#0) Then
  407. L := 0
  408. else
  409. l:=IndexChar(p^,-1,#0);
  410. SetLength(fpc_PChar_To_AnsiStr,L);
  411. if L > 0 then
  412. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L)
  413. end;
  414. Function fpc_CharArray_To_AnsiStr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
  415. var
  416. i : SizeInt;
  417. begin
  418. if (zerobased) then
  419. begin
  420. if (arr[0]=#0) Then
  421. i := 0
  422. else
  423. begin
  424. i:=IndexChar(arr,high(arr)+1,#0);
  425. if i = -1 then
  426. i := high(arr)+1;
  427. end;
  428. end
  429. else
  430. i := high(arr)+1;
  431. SetLength(fpc_CharArray_To_AnsiStr,i);
  432. if i > 0 then
  433. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  434. end;
  435. {$ifndef FPC_STRTOCHARARRAYPROC}
  436. { note: inside the compiler, the resulttype is modified to be the length }
  437. { of the actual chararray to which we convert (JM) }
  438. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  439. var
  440. len: SizeInt;
  441. begin
  442. len := length(src);
  443. if len > arraysize then
  444. len := arraysize;
  445. {$r-}
  446. { make sure we don't try to access element 1 of the ansistring if it's nil }
  447. if len > 0 then
  448. move(src[1],fpc_ansistr_to_chararray[0],len);
  449. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  450. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  451. {$ifdef RangeCheckWasOn}
  452. {$r+}
  453. {$endif}
  454. end;
  455. {$else ndef FPC_STRTOCHARARRAYPROC}
  456. procedure fpc_ansistr_to_chararray(out res: array of char; const src: ansistring); compilerproc;
  457. var
  458. len: SizeInt;
  459. begin
  460. len := length(src);
  461. if len > length(res) then
  462. len := length(res);
  463. {$r-}
  464. { make sure we don't try to access element 1 of the ansistring if it's nil }
  465. if len > 0 then
  466. move(src[1],res[0],len);
  467. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  468. fillchar(res[len],length(res)-len,0);
  469. {$ifdef RangeCheckWasOn}
  470. {$r+}
  471. {$endif}
  472. end;
  473. {$endif ndef FPC_STRTOCHARARRAYPROC}
  474. Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  475. {
  476. Compares 2 AnsiStrings;
  477. The result is
  478. <0 if S1<S2
  479. 0 if S1=S2
  480. >0 if S1>S2
  481. }
  482. Var
  483. MaxI,Temp : SizeInt;
  484. begin
  485. if pointer(S1)=pointer(S2) then
  486. begin
  487. result:=0;
  488. exit;
  489. end;
  490. Maxi:=Length(S1);
  491. temp:=Length(S2);
  492. If MaxI>Temp then
  493. MaxI:=Temp;
  494. if MaxI>0 then
  495. begin
  496. result:=CompareByte(S1[1],S2[1],MaxI);
  497. if result=0 then
  498. result:=Length(S1)-Length(S2);
  499. end
  500. else
  501. result:=Length(S1)-Length(S2);
  502. end;
  503. Function fpc_AnsiStr_Compare_equal(const S1,S2 : AnsiString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  504. {
  505. Compares 2 AnsiStrings for equality/inequality only;
  506. The result is
  507. 0 if S1=S2
  508. <>0 if S1<>S2
  509. }
  510. Var
  511. MaxI,Temp : SizeInt;
  512. begin
  513. if pointer(S1)=pointer(S2) then
  514. begin
  515. result:=0;
  516. exit;
  517. end;
  518. Maxi:=Length(S1);
  519. temp:=Length(S2);
  520. Result := Maxi - temp;
  521. if Result = 0 then
  522. if MaxI>0 then
  523. result:=CompareByte(S1[1],S2[1],MaxI);
  524. end;
  525. {$ifdef VER2_4}
  526. // obsolete but needed for boostrapping with 2.4
  527. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  528. begin
  529. if p=nil then
  530. HandleErrorFrame(201,get_frame);
  531. end;
  532. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  533. begin
  534. if (index>len) or (Index<1) then
  535. HandleErrorFrame(201,get_frame);
  536. end;
  537. {$else VER2_4}
  538. Procedure fpc_AnsiStr_CheckRange(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  539. begin
  540. if (p=nil) or (index>PAnsiRec(p-FirstOff)^.Len) or (Index<1) then
  541. HandleErrorFrame(201,get_frame);
  542. end;
  543. {$endif VER2_4}
  544. Procedure fpc_AnsiStr_SetLength (Var S : RawByteString; l : SizeInt);[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  545. {
  546. Sets The length of string S to L.
  547. Makes sure S is unique, and contains enough room.
  548. }
  549. Var
  550. Temp : Pointer;
  551. lens, lena,
  552. movelen : SizeInt;
  553. begin
  554. if (l>0) then
  555. begin
  556. if Pointer(S)=nil then
  557. begin
  558. GetMem(Pointer(S),AnsiRecLen+L);
  559. PAnsiRec(S)^.Ref:=1;
  560. PAnsiRec(S)^.CodePage:=DefaultSystemCodePage;
  561. PAnsiRec(S)^.ElementSize:=1;
  562. inc(Pointer(S),AnsiFirstOff);
  563. end
  564. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  565. begin
  566. Dec(Pointer(S),AnsiFirstOff);
  567. lens:=MemSize(Pointer(s));
  568. lena:=AnsiRecLen+L;
  569. { allow shrinking string if that saves at least half of current size }
  570. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  571. reallocmem(pointer(S),AnsiRecLen+L);
  572. Inc(Pointer(S),AnsiFirstOff);
  573. end
  574. else
  575. begin
  576. { Reallocation is needed... }
  577. Temp:=Pointer(NewAnsiString(L));
  578. { also move terminating null }
  579. lens:=succ(length(s));
  580. if l<lens then
  581. movelen:=l
  582. else
  583. movelen:=lens;
  584. Move(Pointer(S)^,Temp^,movelen);
  585. { ref count dropped to zero in the mean time? }
  586. If (PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref>0) and
  587. declocked(PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref) then
  588. freemem(PAnsiRec(Pointer(s)-AnsiFirstOff));
  589. Pointer(S):=Temp;
  590. end;
  591. { Force nil termination in case it gets shorter }
  592. PByte(Pointer(S)+l)^:=0;
  593. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  594. end
  595. else
  596. begin
  597. { Length=0 }
  598. if Pointer(S)<>nil then
  599. fpc_ansistr_decr_ref (Pointer(S));
  600. Pointer(S):=Nil;
  601. end;
  602. end;
  603. {$ifdef EXTRAANSISHORT}
  604. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  605. {
  606. Compares a AnsiString with a ShortString;
  607. The result is
  608. <0 if S1<S2
  609. 0 if S1=S2
  610. >0 if S1>S2
  611. }
  612. Var
  613. i,MaxI,Temp : SizeInt;
  614. begin
  615. Temp:=0;
  616. i:=0;
  617. MaxI:=Length(AnsiString(S1));
  618. if MaxI>byte(S2[0]) then
  619. MaxI:=Byte(S2[0]);
  620. While (i<MaxI) and (Temp=0) do
  621. begin
  622. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  623. inc(i);
  624. end;
  625. AnsiStr_ShortStr_Compare:=Temp;
  626. end;
  627. {$endif EXTRAANSISHORT}
  628. {*****************************************************************************
  629. Public functions, In interface.
  630. *****************************************************************************}
  631. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  632. Var
  633. SNew : Pointer;
  634. L : SizeInt;
  635. begin
  636. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  637. SNew:=NewAnsiString (L);
  638. Move (Pointer(S)^,SNew^,L+1);
  639. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  640. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  641. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  642. pointer(S):=SNew;
  643. pointer(result):=SNew;
  644. end;
  645. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  646. // MV: inline the basic checks for case that S is already unique.
  647. // Rest is too complex to inline, so factor that out as a call.
  648. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  649. {
  650. Make sure reference count of S is 1,
  651. using copy-on-write semantics.
  652. }
  653. begin
  654. pointer(result) := pointer(s);
  655. If Pointer(S)=Nil then
  656. exit;
  657. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  658. result:=fpc_truely_ansistr_unique(s);
  659. end;
  660. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  661. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  662. begin
  663. SetLength(S,length(S)+1);
  664. // avoid unique call
  665. PChar(Pointer(S)+length(S)-1)^:=c;
  666. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  667. end;
  668. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  669. var
  670. ofs : SizeInt;
  671. begin
  672. if Str='' then
  673. exit;
  674. ofs:=Length(S);
  675. SetLength(S,ofs+length(Str));
  676. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  677. move(Str[1],(pointer(S)+ofs)^,length(Str));
  678. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  679. end;
  680. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  681. var
  682. ofs, strlength: SizeInt;
  683. samestring: boolean;
  684. begin
  685. if Str='' then
  686. exit;
  687. samestring := pointer(s) = pointer(str);
  688. { needed in case s and str are the same string }
  689. strlength := length(str);
  690. ofs:=Length(S);
  691. SetLength(S,ofs+strlength);
  692. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  693. if not(samestring) then
  694. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  695. else
  696. { the setlength may have relocated the string, so str may no longer be valid }
  697. move(S[1],(pointer(S)+ofs)^,strlength+1)
  698. end;
  699. Function Fpc_Ansistr_Copy (Const S : RawByteString; Index,Size : SizeInt) : AnsiString;compilerproc;
  700. var
  701. ResultAddress : Pointer;
  702. begin
  703. ResultAddress:=Nil;
  704. dec(index);
  705. if Index < 0 then
  706. Index := 0;
  707. { Check Size. Accounts for Zero-length S, the double check is needed because
  708. Size can be maxint and will get <0 when adding index }
  709. if (Size>Length(S)) or
  710. (Index+Size>Length(S)) then
  711. Size:=Length(S)-Index;
  712. If Size>0 then
  713. begin
  714. If Index<0 Then
  715. Index:=0;
  716. ResultAddress:=Pointer(NewAnsiString (Size));
  717. if ResultAddress<>Nil then
  718. begin
  719. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  720. PByte(ResultAddress+Size)^:=0;
  721. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  722. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  723. end;
  724. end;
  725. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  726. Pointer(fpc_ansistr_Copy):=ResultAddress;
  727. end;
  728. Function Pos (Const Substr : ShortString; Const Source : AnsiString) : SizeInt;
  729. var
  730. i,MaxLen : SizeInt;
  731. pc : pchar;
  732. begin
  733. Pos:=0;
  734. if Length(SubStr)>0 then
  735. begin
  736. MaxLen:=Length(source)-Length(SubStr);
  737. i:=0;
  738. pc:=@source[1];
  739. while (i<=MaxLen) do
  740. begin
  741. inc(i);
  742. if (SubStr[1]=pc^) and
  743. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  744. begin
  745. Pos:=i;
  746. exit;
  747. end;
  748. inc(pc);
  749. end;
  750. end;
  751. end;
  752. Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : SizeInt;
  753. var
  754. i,MaxLen : SizeInt;
  755. pc : pchar;
  756. begin
  757. Pos:=0;
  758. if Length(SubStr)>0 then
  759. begin
  760. MaxLen:=Length(source)-Length(SubStr);
  761. i:=0;
  762. pc:=@source[1];
  763. while (i<=MaxLen) do
  764. begin
  765. inc(i);
  766. if (SubStr[1]=pc^) and
  767. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  768. begin
  769. Pos:=i;
  770. exit;
  771. end;
  772. inc(pc);
  773. end;
  774. end;
  775. end;
  776. { Faster version for a char alone. Must be implemented because }
  777. { pos(c: char; const s: shortstring) also exists, so otherwise }
  778. { using pos(char,pchar) will always call the shortstring version }
  779. { (exact match for first argument), also with $h+ (JM) }
  780. Function Pos (c : Char; Const s : AnsiString) : SizeInt;
  781. var
  782. i: SizeInt;
  783. pc : pchar;
  784. begin
  785. pc:=@s[1];
  786. for i:=1 to length(s) do
  787. begin
  788. if pc^=c then
  789. begin
  790. pos:=i;
  791. exit;
  792. end;
  793. inc(pc);
  794. end;
  795. pos:=0;
  796. end;
  797. {$ifndef FPUNONE}
  798. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  799. Var
  800. SS : String;
  801. begin
  802. fpc_Val_Real_AnsiStr := 0;
  803. if length(S) > 255 then
  804. code := 256
  805. else
  806. begin
  807. SS := S;
  808. Val(SS,fpc_Val_Real_AnsiStr,code);
  809. end;
  810. end;
  811. {$endif}
  812. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  813. Var
  814. SS : String;
  815. begin
  816. if length(S) > 255 then
  817. begin
  818. fpc_Val_Currency_AnsiStr := 0;
  819. code := 256;
  820. end
  821. else
  822. begin
  823. SS := S;
  824. Val(SS,fpc_Val_Currency_AnsiStr,code);
  825. end;
  826. end;
  827. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  828. Var
  829. SS : ShortString;
  830. begin
  831. fpc_Val_UInt_AnsiStr := 0;
  832. if length(S) > 255 then
  833. code := 256
  834. else
  835. begin
  836. SS := S;
  837. Val(SS,fpc_Val_UInt_AnsiStr,code);
  838. end;
  839. end;
  840. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  841. Var
  842. SS : ShortString;
  843. begin
  844. fpc_Val_SInt_AnsiStr:=0;
  845. if length(S)>255 then
  846. code:=256
  847. else
  848. begin
  849. SS := S;
  850. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  851. end;
  852. end;
  853. {$ifndef CPU64}
  854. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  855. Var
  856. SS : ShortString;
  857. begin
  858. fpc_Val_qword_AnsiStr:=0;
  859. if length(S)>255 then
  860. code:=256
  861. else
  862. begin
  863. SS := S;
  864. Val(SS,fpc_Val_qword_AnsiStr,Code);
  865. end;
  866. end;
  867. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  868. Var
  869. SS : ShortString;
  870. begin
  871. fpc_Val_int64_AnsiStr:=0;
  872. if length(S)>255 then
  873. code:=256
  874. else
  875. begin
  876. SS := s;
  877. Val(SS,fpc_Val_int64_AnsiStr,Code);
  878. end;
  879. end;
  880. {$endif CPU64}
  881. {$ifndef FPUNONE}
  882. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  883. var
  884. ss: ShortString;
  885. begin
  886. str_real(len,fr,d,treal_type(rt),ss);
  887. s:=ss;
  888. end;
  889. {$endif}
  890. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:ansistring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  891. var ss:shortstring;
  892. begin
  893. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  894. s:=ss;
  895. end;
  896. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:ansistring);[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  897. var
  898. ss:shortstring;
  899. begin
  900. fpc_shortstr_bool(b,len,ss);
  901. s:=ss;
  902. end;
  903. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:ansistring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  904. begin
  905. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  906. end;
  907. {$ifdef FPC_HAS_STR_CURRENCY}
  908. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  909. var
  910. ss: ShortString;
  911. begin
  912. str(c:len:fr,ss);
  913. s:=ss;
  914. end;
  915. {$endif FPC_HAS_STR_CURRENCY}
  916. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  917. Var
  918. SS : ShortString;
  919. begin
  920. str(v:Len,SS);
  921. S:=SS;
  922. end;
  923. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  924. Var
  925. SS : ShortString;
  926. begin
  927. str (v:Len,SS);
  928. S:=SS;
  929. end;
  930. {$ifndef CPU64}
  931. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  932. Var
  933. SS : ShortString;
  934. begin
  935. str(v:Len,SS);
  936. S:=SS;
  937. end;
  938. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  939. Var
  940. SS : ShortString;
  941. begin
  942. str (v:Len,SS);
  943. S:=SS;
  944. end;
  945. {$endif CPU64}
  946. Procedure Delete (Var S : AnsiString; Index,Size: SizeInt);
  947. Var
  948. LS : SizeInt;
  949. begin
  950. ls:=Length(S);
  951. If (Index>LS) or (Index<=0) or (Size<=0) then
  952. exit;
  953. UniqueString (S);
  954. If (Size>LS-Index) then // Size+Index gives overflow ??
  955. Size:=LS-Index+1;
  956. If (Size<=LS-Index) then
  957. begin
  958. Dec(Index);
  959. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  960. end;
  961. Setlength(S,LS-Size);
  962. end;
  963. Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : SizeInt);
  964. var
  965. Temp : AnsiString;
  966. LS : SizeInt;
  967. begin
  968. If Length(Source)=0 then
  969. exit;
  970. if index <= 0 then
  971. index := 1;
  972. Ls:=Length(S);
  973. if index > LS then
  974. index := LS+1;
  975. Dec(Index);
  976. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  977. SetLength(Temp,Length(Source)+LS);
  978. If Index>0 then
  979. move (Pointer(S)^,Pointer(Temp)^,Index);
  980. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  981. If (LS-Index)>0 then
  982. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  983. S:=Temp;
  984. end;
  985. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  986. begin
  987. SetLength(StringOfChar,l);
  988. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  989. end;
  990. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  991. begin
  992. SetLength(S,Len);
  993. If (Buf<>Nil) then
  994. Move (Buf^,Pointer(S)^,Len);
  995. end;
  996. Procedure SetString (Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  997. begin
  998. if (Buf<>nil) and (Len>0) then
  999. widestringmanager.Wide2AnsiMoveProc(Buf,S,Len)
  1000. else
  1001. SetLength(S, Len);
  1002. end;
  1003. function upcase(const s : ansistring) : ansistring;
  1004. var
  1005. i : SizeInt;
  1006. begin
  1007. Setlength(result,length(s));
  1008. for i := 1 to length (s) do
  1009. result[i] := upcase(s[i]);
  1010. end;
  1011. function lowercase(const s : ansistring) : ansistring;
  1012. var
  1013. i : SizeInt;
  1014. begin
  1015. Setlength(result,length(s));
  1016. for i := 1 to length (s) do
  1017. result[i] := lowercase(s[i]);
  1018. end;
  1019. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1020. begin
  1021. if assigned(Pointer(S)) then
  1022. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1023. else
  1024. Result:=DefaultSystemCodePage;
  1025. end;
  1026. function StringElementSize(const S: RawByteString): Word; overload;
  1027. begin
  1028. if assigned(Pointer(S)) then
  1029. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1030. else
  1031. Result:=SizeOf(AnsiChar);
  1032. end;
  1033. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1034. begin
  1035. if assigned(Pointer(S)) then
  1036. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1037. else
  1038. Result:=0;
  1039. end;
  1040. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1041. begin
  1042. if (S='') or (StringCodePage(S)=CodePage) then
  1043. exit
  1044. else if Convert then
  1045. begin
  1046. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1047. end
  1048. else
  1049. begin
  1050. UniqueString(s);
  1051. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1052. end;
  1053. end;
  1054. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1055. begin
  1056. DefaultSystemCodePage:=CodePage;
  1057. end;