astrings.inc 26 KB

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