2
0

astrings.inc 26 KB

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