astrings.inc 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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. {$ifndef FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  68. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [Public,Alias:'FPC_ANSISTR_DECR_REF']; compilerproc;
  69. {
  70. Decreases the ReferenceCount of a non constant ansistring;
  71. If the reference count is zero, deallocate the string;
  72. }
  73. Var
  74. p: PAnsiRec;
  75. Begin
  76. { Zero string }
  77. If S=Nil then
  78. exit;
  79. { check for constant strings ...}
  80. p:=PAnsiRec(S-AnsiFirstOff);
  81. If p^.ref<0 then exit;
  82. { declocked does a MT safe dec and returns true, if the counter is 0 }
  83. If declocked(p^.ref) then
  84. begin
  85. FreeMem(p);
  86. s:=nil;
  87. end;
  88. end;
  89. {$endif FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  90. { also define alias for internal use in the system unit }
  91. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [external name 'FPC_ANSISTR_DECR_REF'];
  92. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [Public,Alias:'FPC_ANSISTR_INCR_REF']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  93. Begin
  94. If S=Nil then
  95. exit;
  96. { Let's be paranoid : Constant string ??}
  97. If PAnsiRec(S-AnsiFirstOff)^.Ref<0 then exit;
  98. inclocked(PAnsiRec(S-AnsiFirstOff)^.Ref);
  99. end;
  100. { also define alias which can be used inside the system unit }
  101. Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [external name 'FPC_ANSISTR_INCR_REF'];
  102. Procedure fpc_AnsiStr_Assign (Var DestS : Pointer;S2 : Pointer);[Public,Alias:'FPC_ANSISTR_ASSIGN']; compilerproc;
  103. {
  104. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  105. }
  106. begin
  107. if DestS=S2 then
  108. exit;
  109. If S2<>nil then
  110. If PAnsiRec(S2-AnsiFirstOff)^.Ref>0 then
  111. inclocked(PAnsiRec(S2-AnsiFirstOff)^.Ref);
  112. { Decrease the reference count on the old S1 }
  113. fpc_ansistr_decr_ref (DestS);
  114. { And finally, have DestS pointing to S2 (or its copy) }
  115. DestS:=S2;
  116. end;
  117. { alias for internal use }
  118. Procedure fpc_AnsiStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_ANSISTR_ASSIGN'];
  119. {$ifndef STR_CONCAT_PROCS}
  120. function fpc_AnsiStr_Concat (const S1,S2 : AnsiString): ansistring; compilerproc;
  121. Var
  122. Size,Location : SizeInt;
  123. pc : pchar;
  124. begin
  125. { only assign if s1 or s2 is empty }
  126. if (S1='') then
  127. begin
  128. result:=s2;
  129. exit;
  130. end;
  131. if (S2='') then
  132. begin
  133. result:=s1;
  134. exit;
  135. end;
  136. Location:=Length(S1);
  137. Size:=length(S2);
  138. SetLength(result,Size+Location);
  139. pc:=pchar(result);
  140. Move(S1[1],pc^,Location);
  141. inc(pc,location);
  142. Move(S2[1],pc^,Size+1);
  143. end;
  144. function fpc_AnsiStr_Concat_multi (const sarr:array of Ansistring): ansistring; compilerproc;
  145. Var
  146. i : Longint;
  147. p : pointer;
  148. pc : pchar;
  149. Size,NewLen : SizeInt;
  150. begin
  151. { First calculate size of the result so we can do
  152. a single call to SetLength() }
  153. NewLen:=0;
  154. for i:=low(sarr) to high(sarr) do
  155. inc(NewLen,length(sarr[i]));
  156. SetLength(result,NewLen);
  157. pc:=pchar(result);
  158. for i:=low(sarr) to high(sarr) do
  159. begin
  160. p:=pointer(sarr[i]);
  161. if assigned(p) then
  162. begin
  163. Size:=length(ansistring(p));
  164. Move(pchar(p)^,pc^,Size+1);
  165. inc(pc,size);
  166. end;
  167. end;
  168. end;
  169. {$else STR_CONCAT_PROCS}
  170. procedure fpc_AnsiStr_Concat (var DestS:RawByteString;const S1,S2 : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); compilerproc;
  171. Var
  172. Size,Location : SizeInt;
  173. same : boolean;
  174. S1CP, S2CP, DestCP: TSystemCodePage;
  175. U: UnicodeString;
  176. begin
  177. { if codepages are differ then concat using unicodestring }
  178. S1CP:=StringCodePage(S1);
  179. if (S1CP=$ffff) or (S1CP=0) then
  180. S1CP:=DefaultSystemCodePage;
  181. S2CP:=StringCodePage(S2);
  182. if (S2CP=$ffff) or (S2CP=0) then
  183. S2CP:=DefaultSystemCodePage;
  184. {$ifdef FPC_HAS_CPSTRING}
  185. if (Pointer(DestS)=nil) then
  186. DestCP:=cp
  187. else
  188. DestCP:=StringCodePage(DestS);
  189. {$else FPC_HAS_CPSTRING}
  190. DestCP:=StringCodePage(DestS);
  191. {$endif FPC_HAS_CPSTRING}
  192. if (DestCP=$ffff) or (DestCP=0) then
  193. DestCP:=DefaultSystemCodePage;
  194. if (S1CP<>DestCP) or (S2CP<>DestCP) then
  195. begin
  196. U:=UnicodeString(S1)+UnicodeString(S2);
  197. DestS:='';
  198. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(U)),DestS,DestCP,Length(U));
  199. exit;
  200. end;
  201. { only assign if s1 or s2 is empty }
  202. if (S1='') then
  203. begin
  204. DestS:=s2;
  205. exit;
  206. end;
  207. if (S2='') then
  208. begin
  209. DestS:=s1;
  210. exit;
  211. end;
  212. Location:=Length(S1);
  213. Size:=length(S2);
  214. { Use Pointer() typecasts to prevent extra conversion code }
  215. if Pointer(DestS)=Pointer(S1) then
  216. begin
  217. same:=Pointer(S1)=Pointer(S2);
  218. SetLength(DestS,Size+Location);
  219. if same then
  220. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size)
  221. else
  222. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  223. end
  224. else if Pointer(DestS)=Pointer(S2) then
  225. begin
  226. SetLength(DestS,Size+Location);
  227. Move(Pointer(DestS)^,(Pointer(DestS)+Location)^,Size+1);
  228. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  229. end
  230. else
  231. begin
  232. DestS:='';
  233. SetLength(DestS,Size+Location);
  234. SetCodePage(DestS,DestCP,false);
  235. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  236. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  237. end;
  238. end;
  239. procedure fpc_AnsiStr_Concat_multi (var DestS:RawByteString;const sarr:array of RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); compilerproc;
  240. Var
  241. lowstart,i : Longint;
  242. p,pc : pointer;
  243. Size,NewLen,
  244. OldDestLen : SizeInt;
  245. destcopy : pointer;
  246. DestCP : TSystemCodePage;
  247. U : UnicodeString;
  248. sameCP : Boolean;
  249. tmpStr : RawByteString;
  250. tmpCP : TSystemCodePage;
  251. begin
  252. if high(sarr)=0 then
  253. begin
  254. DestS:='';
  255. exit;
  256. end;
  257. {$ifdef FPC_HAS_CPSTRING}
  258. if (Pointer(DestS)=nil) then
  259. DestCP:=cp
  260. else
  261. DestCP:=StringCodePage(DestS);
  262. {$else FPC_HAS_CPSTRING}
  263. DestCP:=StringCodePage(DestS);
  264. {$endif FPC_HAS_CPSTRING}
  265. if (DestCP=$ffff) or (DestCP=0) then
  266. DestCP:=DefaultSystemCodePage;
  267. sameCP:=true;
  268. lowstart:=low(sarr);
  269. for i:=lowstart to high(sarr) do
  270. begin
  271. if (DestCP<>StringCodePage(sarr[i])) then
  272. begin
  273. sameCP:=false;
  274. break;
  275. end;
  276. end;
  277. if not sameCP then
  278. begin
  279. U:='';
  280. for i:=lowstart to high(sarr) do begin
  281. tmpCP:=StringCodePage(sarr[i]);
  282. if (tmpCP=$ffff) or (tmpCP=0) then
  283. begin
  284. tmpStr:=sarr[i];
  285. SetCodePage(tmpStr,DefaultSystemCodePage,False);
  286. U:=U+UnicodeString(tmpStr);
  287. end
  288. else
  289. U:=U+UnicodeString(sarr[i]);
  290. end;
  291. DestS:='';
  292. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(U)),DestS,DestCP,Length(U));
  293. exit;
  294. end;
  295. destcopy:=nil;
  296. lowstart:=low(sarr);
  297. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  298. inc(lowstart);
  299. { Check for another reuse, then we can't use
  300. the append optimization }
  301. for i:=lowstart to high(sarr) do
  302. begin
  303. if Pointer(DestS)=Pointer(sarr[i]) then
  304. begin
  305. { if DestS is used somewhere in the middle of the expression,
  306. we need to make sure the original string still exists after
  307. we empty/modify DestS }
  308. destcopy:=pointer(dests);
  309. fpc_AnsiStr_Incr_Ref(destcopy);
  310. lowstart:=low(sarr);
  311. break;
  312. end;
  313. end;
  314. { Start with empty DestS if we start with concatting
  315. the first array element }
  316. if lowstart=low(sarr) then
  317. DestS:='';
  318. OldDestLen:=length(DestS);
  319. { Calculate size of the result so we can do
  320. a single call to SetLength() }
  321. NewLen:=0;
  322. for i:=low(sarr) to high(sarr) do
  323. inc(NewLen,length(sarr[i]));
  324. SetLength(DestS,NewLen);
  325. if (StringCodePage(DestS) <> DestCP) then
  326. SetCodePage(DestS,DestCP,False);
  327. { Concat all strings, except the string we already
  328. copied in DestS }
  329. pc:=Pointer(DestS)+OldDestLen;
  330. for i:=lowstart to high(sarr) do
  331. begin
  332. p:=pointer(sarr[i]);
  333. if assigned(p) then
  334. begin
  335. Size:=length(ansistring(p));
  336. Move(p^,pc^,Size+1);
  337. inc(pc,size);
  338. end;
  339. end;
  340. fpc_AnsiStr_Decr_Ref(destcopy);
  341. end;
  342. {$endif STR_CONCAT_PROCS}
  343. {$ifdef EXTRAANSISHORT}
  344. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  345. {
  346. Concatenates a Ansi with a short string; : S2 + S2
  347. }
  348. Var
  349. Size,Location : SizeInt;
  350. begin
  351. Size:=Length(S2);
  352. Location:=Length(S1);
  353. If Size=0 then
  354. exit;
  355. { Setlength takes case of uniqueness
  356. and alllocated memory. We need to use length,
  357. to take into account possibility of S1=Nil }
  358. SetLength (S1,Size+Length(S1));
  359. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  360. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  361. end;
  362. {$endif EXTRAANSISHORT}
  363. {$ifdef FPC_HAS_CPSTRING}
  364. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [Public, alias: 'FPC_ANSISTR_TO_ANSISTR']; compilerproc;
  365. {
  366. Converts an AnsiString to an AnsiString taking code pages into care
  367. }
  368. Var
  369. Size : SizeInt;
  370. temp : UnicodeString;
  371. begin
  372. result:='';
  373. Size:=Length(S);
  374. if Size>0 then
  375. begin
  376. if (cp=0) then
  377. cp:=DefaultSystemCodePage;
  378. if (StringCodePage(S)=cp) then
  379. begin
  380. SetLength(result,Size);
  381. Move(S[1],result[1],Size);
  382. PAnsiRec(Pointer(result)-AnsiFirstOff)^.CodePage:=cp;
  383. end
  384. else
  385. begin
  386. temp:=S;
  387. Size:=Length(temp);
  388. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(temp)),result,cp,Size);
  389. end;
  390. end;
  391. end;
  392. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [external name 'FPC_ANSISTR_TO_ANSISTR'];
  393. {$endif FPC_HAS_CPSTRING}
  394. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  395. { the following declaration has exactly the same effect as }
  396. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  397. { which is what the old helper was, so we don't need an extra implementation }
  398. { of the old helper (JM) }
  399. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring; [Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  400. {
  401. Converts a AnsiString to a ShortString;
  402. }
  403. Var
  404. Size : SizeInt;
  405. begin
  406. if S2='' then
  407. fpc_AnsiStr_To_ShortStr:=''
  408. else
  409. begin
  410. Size:=Length(S2);
  411. If Size>high_of_res then
  412. Size:=high_of_res;
  413. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  414. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  415. end;
  416. end;
  417. {$else FPC_STRTOSHORTSTRINGPROC}
  418. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  419. {
  420. Converts a AnsiString to a ShortString;
  421. }
  422. Var
  423. Size : SizeInt;
  424. begin
  425. if S2='' then
  426. res:=''
  427. else
  428. begin
  429. Size:=Length(S2);
  430. If Size>high(res) then
  431. Size:=high(res);
  432. Move (S2[1],res[1],Size);
  433. byte(res[0]):=byte(Size);
  434. end;
  435. end;
  436. {$endif FPC_STRTOSHORTSTRINGPROC}
  437. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  438. {
  439. Converts a ShortString to a AnsiString;
  440. }
  441. Var
  442. Size : SizeInt;
  443. {$ifndef FPC_HAS_CPSTRING}
  444. cp : TSystemCodePage;
  445. {$endif FPC_HAS_CPSTRING}
  446. begin
  447. {$ifdef FPC_HAS_CPSTRING}
  448. if (cp=0) then
  449. cp:=DefaultSystemCodePage;
  450. {$else FPC_HAS_CPSTRING}
  451. cp:=DefaultSystemCodePage;
  452. {$endif FPC_HAS_CPSTRING}
  453. Size:=Length(S2);
  454. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  455. if Size>0 then
  456. begin
  457. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  458. SetCodePage(fpc_ShortStr_To_AnsiStr,cp,False);
  459. end
  460. end;
  461. Function fpc_Char_To_AnsiStr(const c : Char{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  462. {
  463. Converts a Char to a AnsiString;
  464. }
  465. {$ifndef FPC_HAS_CPSTRING}
  466. var
  467. cp : TSystemCodePage;
  468. {$endif FPC_HAS_CPSTRING}
  469. begin
  470. {$ifdef FPC_HAS_CPSTRING}
  471. if (cp=0) then
  472. cp:=DefaultSystemCodePage;
  473. {$else FPC_HAS_CPSTRING}
  474. cp:=DefaultSystemCodePage;
  475. {$endif FPC_HAS_CPSTRING}
  476. Setlength (fpc_Char_To_AnsiStr,1);
  477. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  478. { Terminating Zero }
  479. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  480. SetCodePage(fpc_Char_To_AnsiStr,cp,False);
  481. end;
  482. Function fpc_PChar_To_AnsiStr(const p : pchar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  483. Var
  484. L : SizeInt;
  485. {$ifndef FPC_HAS_CPSTRING}
  486. cp : TSystemCodePage;
  487. {$endif FPC_HAS_CPSTRING}
  488. begin
  489. if (not assigned(p)) or (p[0]=#0) Then
  490. L := 0
  491. else
  492. l:=IndexChar(p^,-1,#0);
  493. SetLength(fpc_PChar_To_AnsiStr,L);
  494. if L > 0 then
  495. begin
  496. {$ifdef FPC_HAS_CPSTRING}
  497. if (cp=0) then
  498. cp:=DefaultSystemCodePage;
  499. {$else FPC_HAS_CPSTRING}
  500. cp:=DefaultSystemCodePage;
  501. {$endif FPC_HAS_CPSTRING}
  502. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L);
  503. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  504. end;
  505. end;
  506. Function fpc_CharArray_To_AnsiStr(const arr: array of ansichar; {$ifdef FPC_HAS_CPSTRING}cp : TSystemCodePage;{$endif FPC_HAS_CPSTRING}zerobased: boolean = true): RawByteString; compilerproc;
  507. var
  508. i : SizeInt;
  509. {$ifndef FPC_HAS_CPSTRING}
  510. cp : TSystemCodePage;
  511. {$endif FPC_HAS_CPSTRING}
  512. begin
  513. if (zerobased) then
  514. begin
  515. if (arr[0]=#0) Then
  516. i := 0
  517. else
  518. begin
  519. i:=IndexChar(arr,high(arr)+1,#0);
  520. if i = -1 then
  521. i := high(arr)+1;
  522. end;
  523. end
  524. else
  525. i := high(arr)+1;
  526. SetLength(fpc_CharArray_To_AnsiStr,i);
  527. if i > 0 then
  528. begin
  529. {$ifdef FPC_HAS_CPSTRING}
  530. if (cp=0) then
  531. cp:=DefaultSystemCodePage;
  532. {$else FPC_HAS_CPSTRING}
  533. cp:=DefaultSystemCodePage;
  534. {$endif FPC_HAS_CPSTRING}
  535. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  536. SetCodePage(fpc_CharArray_To_AnsiStr,cp,False);
  537. end;
  538. end;
  539. {$ifndef FPC_STRTOCHARARRAYPROC}
  540. { note: inside the compiler, the resulttype is modified to be the length }
  541. { of the actual chararray to which we convert (JM) }
  542. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  543. var
  544. len: SizeInt;
  545. begin
  546. len := length(src);
  547. if len > arraysize then
  548. len := arraysize;
  549. {$r-}
  550. { make sure we don't try to access element 1 of the ansistring if it's nil }
  551. if len > 0 then
  552. move(src[1],fpc_ansistr_to_chararray[0],len);
  553. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  554. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  555. {$ifdef RangeCheckWasOn}
  556. {$r+}
  557. {$endif}
  558. end;
  559. {$else ndef FPC_STRTOCHARARRAYPROC}
  560. procedure fpc_ansistr_to_chararray(out res: array of char; const src: RawByteString); compilerproc;
  561. var
  562. len: SizeInt;
  563. begin
  564. len := length(src);
  565. if len > length(res) then
  566. len := length(res);
  567. {$r-}
  568. { make sure we don't try to access element 1 of the ansistring if it's nil }
  569. if len > 0 then
  570. move(src[1],res[0],len);
  571. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  572. fillchar(res[len],length(res)-len,0);
  573. {$ifdef RangeCheckWasOn}
  574. {$r+}
  575. {$endif}
  576. end;
  577. {$endif ndef FPC_STRTOCHARARRAYPROC}
  578. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  579. {
  580. Compares 2 AnsiStrings;
  581. The result is
  582. <0 if S1<S2
  583. 0 if S1=S2
  584. >0 if S1>S2
  585. }
  586. Var
  587. MaxI,Temp : SizeInt;
  588. cp1,cp2 : TSystemCodePage;
  589. r1,r2 : RawByteString;
  590. u1,u2 : UTF8String;
  591. begin
  592. if pointer(S1)=pointer(S2) then
  593. begin
  594. result:=0;
  595. exit;
  596. end;
  597. if (pointer(S1)=nil) then
  598. begin
  599. result:=-Length(S2);
  600. exit;
  601. end;
  602. if (pointer(S2)=nil) then
  603. begin
  604. result:=Length(S1);
  605. exit;
  606. end;
  607. cp1:=StringCodePage(S1);
  608. cp2:=StringCodePage(S2);
  609. if (cp1=cp2) then
  610. begin
  611. Maxi:=Length(S1);
  612. temp:=Length(S2);
  613. If MaxI>Temp then
  614. MaxI:=Temp;
  615. if MaxI>0 then
  616. begin
  617. result:=CompareByte(S1[1],S2[1],MaxI);
  618. if result=0 then
  619. result:=Length(S1)-Length(S2);
  620. end
  621. else
  622. result:=Length(S1)-Length(S2);
  623. end
  624. else
  625. begin
  626. r1:=S1;
  627. if (cp1=$ffff) or (cp1=0) then
  628. SetCodePage(r1,DefaultSystemCodePage,false);
  629. r2:=S2;
  630. if (cp2=$ffff) or (cp2=0) then
  631. SetCodePage(r2,DefaultSystemCodePage,false);
  632. //convert them to utf8 then compare
  633. SetCodePage(r1,65001);
  634. SetCodePage(r2,65001);
  635. Result := fpc_AnsiStr_Compare(r1,r2);
  636. end;
  637. end;
  638. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  639. {
  640. Compares 2 AnsiStrings for equality/inequality only;
  641. The result is
  642. 0 if S1=S2
  643. <>0 if S1<>S2
  644. }
  645. Var
  646. MaxI,Temp : SizeInt;
  647. cp1,cp2 : TSystemCodePage;
  648. r1,r2 : RawByteString;
  649. begin
  650. if pointer(S1)=pointer(S2) then
  651. begin
  652. result:=0;
  653. exit;
  654. end;
  655. cp1:=StringCodePage(S1);
  656. cp2:=StringCodePage(S2);
  657. if (cp1=cp2) then
  658. begin
  659. Maxi:=Length(S1);
  660. temp:=Length(S2);
  661. Result := Maxi - temp;
  662. if Result = 0 then
  663. if MaxI>0 then
  664. result:=CompareByte(S1[1],S2[1],MaxI);
  665. end
  666. else
  667. begin
  668. r1:=S1;
  669. if (cp1=$ffff) or (cp1=0) then
  670. SetCodePage(r1,DefaultSystemCodePage,false);
  671. r2:=S2;
  672. if (cp2=$ffff) or (cp2=0) then
  673. SetCodePage(r2,DefaultSystemCodePage,false);
  674. //convert them to utf8 then compare
  675. SetCodePage(r1,65001);
  676. SetCodePage(r2,65001);
  677. Maxi:=Length(r1);
  678. temp:=Length(r2);
  679. Result := Maxi - temp;
  680. if Result = 0 then
  681. if MaxI>0 then
  682. result:=CompareByte(r1[1],r2[1],MaxI);
  683. end;
  684. end;
  685. {$ifdef VER2_4}
  686. // obsolete but needed for boostrapping with 2.4
  687. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  688. begin
  689. if p=nil then
  690. HandleErrorFrame(201,get_frame);
  691. end;
  692. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  693. begin
  694. if (index>len) or (Index<1) then
  695. HandleErrorFrame(201,get_frame);
  696. end;
  697. {$else VER2_4}
  698. Procedure fpc_AnsiStr_CheckRange(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  699. begin
  700. if (p=nil) or (index>PAnsiRec(p-AnsiFirstOff)^.Len) or (Index<1) then
  701. HandleErrorFrame(201,get_frame);
  702. end;
  703. {$endif VER2_4}
  704. Procedure fpc_AnsiStr_SetLength (Var S : RawByteString; l : SizeInt{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_SETLENGTH']; compilerproc;
  705. {
  706. Sets The length of string S to L.
  707. Makes sure S is unique, and contains enough room.
  708. }
  709. Var
  710. Temp : Pointer;
  711. lens, lena,
  712. movelen : SizeInt;
  713. begin
  714. if (l>0) then
  715. begin
  716. if Pointer(S)=nil then
  717. begin
  718. GetMem(Pointer(S),AnsiRecLen+L);
  719. PAnsiRec(S)^.Ref:=1;
  720. {$ifdef FPC_HAS_CPSTRING}
  721. if (cp=0) then
  722. cp:=DefaultSystemCodePage;
  723. PAnsiRec(S)^.CodePage:=cp;
  724. {$else}
  725. PAnsiRec(S)^.CodePage:=DefaultSystemCodePage;
  726. {$endif FPC_HAS_CPSTRING}
  727. PAnsiRec(S)^.ElementSize:=1;
  728. inc(Pointer(S),AnsiFirstOff);
  729. end
  730. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  731. begin
  732. Dec(Pointer(S),AnsiFirstOff);
  733. lens:=MemSize(Pointer(s));
  734. lena:=AnsiRecLen+L;
  735. { allow shrinking string if that saves at least half of current size }
  736. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  737. reallocmem(pointer(S),AnsiRecLen+L);
  738. Inc(Pointer(S),AnsiFirstOff);
  739. end
  740. else
  741. begin
  742. { Reallocation is needed... }
  743. Temp:=Pointer(NewAnsiString(L));
  744. {$ifdef FPC_HAS_CPSTRING}
  745. PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=cp;
  746. {$endif FPC_HAS_CPSTRING}
  747. { also move terminating null }
  748. lens:=succ(length(s));
  749. if l<lens then
  750. movelen:=l
  751. else
  752. movelen:=lens;
  753. Move(Pointer(S)^,Temp^,movelen);
  754. { ref count dropped to zero in the mean time? }
  755. If (PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref>0) and
  756. declocked(PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref) then
  757. freemem(PAnsiRec(Pointer(s)-AnsiFirstOff));
  758. Pointer(S):=Temp;
  759. end;
  760. { Force nil termination in case it gets shorter }
  761. PByte(Pointer(S)+l)^:=0;
  762. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  763. end
  764. else
  765. begin
  766. { Length=0 }
  767. if Pointer(S)<>nil then
  768. fpc_ansistr_decr_ref (Pointer(S));
  769. Pointer(S):=Nil;
  770. end;
  771. end;
  772. {$ifdef EXTRAANSISHORT}
  773. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  774. {
  775. Compares a AnsiString with a ShortString;
  776. The result is
  777. <0 if S1<S2
  778. 0 if S1=S2
  779. >0 if S1>S2
  780. }
  781. Var
  782. i,MaxI,Temp : SizeInt;
  783. begin
  784. Temp:=0;
  785. i:=0;
  786. MaxI:=Length(AnsiString(S1));
  787. if MaxI>byte(S2[0]) then
  788. MaxI:=Byte(S2[0]);
  789. While (i<MaxI) and (Temp=0) do
  790. begin
  791. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  792. inc(i);
  793. end;
  794. AnsiStr_ShortStr_Compare:=Temp;
  795. end;
  796. {$endif EXTRAANSISHORT}
  797. {*****************************************************************************
  798. Public functions, In interface.
  799. *****************************************************************************}
  800. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  801. Var
  802. SNew : Pointer;
  803. L : SizeInt;
  804. begin
  805. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  806. SNew:=NewAnsiString (L);
  807. Move (Pointer(S)^,SNew^,L+1);
  808. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  809. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  810. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  811. pointer(S):=SNew;
  812. pointer(result):=SNew;
  813. end;
  814. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  815. // MV: inline the basic checks for case that S is already unique.
  816. // Rest is too complex to inline, so factor that out as a call.
  817. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  818. {
  819. Make sure reference count of S is 1,
  820. using copy-on-write semantics.
  821. }
  822. begin
  823. pointer(result) := pointer(s);
  824. If Pointer(S)=Nil then
  825. exit;
  826. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  827. result:=fpc_truely_ansistr_unique(s);
  828. end;
  829. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  830. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  831. begin
  832. SetLength(S,length(S)+1);
  833. // avoid unique call
  834. PChar(Pointer(S)+length(S)-1)^:=c;
  835. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  836. end;
  837. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  838. var
  839. ofs : SizeInt;
  840. begin
  841. if Str='' then
  842. exit;
  843. ofs:=Length(S);
  844. SetLength(S,ofs+length(Str));
  845. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  846. move(Str[1],(pointer(S)+ofs)^,length(Str));
  847. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  848. end;
  849. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  850. var
  851. ofs, strlength: SizeInt;
  852. samestring: boolean;
  853. begin
  854. if Str='' then
  855. exit;
  856. samestring := pointer(s) = pointer(str);
  857. { needed in case s and str are the same string }
  858. strlength := length(str);
  859. ofs:=Length(S);
  860. SetLength(S,ofs+strlength);
  861. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  862. if not(samestring) then
  863. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  864. else
  865. { the setlength may have relocated the string, so str may no longer be valid }
  866. move(S[1],(pointer(S)+ofs)^,strlength+1)
  867. end;
  868. Function Fpc_Ansistr_Copy (Const S : RawByteString; Index,Size : SizeInt) : AnsiString;compilerproc;
  869. var
  870. ResultAddress : Pointer;
  871. begin
  872. ResultAddress:=Nil;
  873. dec(index);
  874. if Index < 0 then
  875. Index := 0;
  876. { Check Size. Accounts for Zero-length S, the double check is needed because
  877. Size can be maxint and will get <0 when adding index }
  878. if (Size>Length(S)) or
  879. (Index+Size>Length(S)) then
  880. Size:=Length(S)-Index;
  881. If Size>0 then
  882. begin
  883. If Index<0 Then
  884. Index:=0;
  885. ResultAddress:=Pointer(NewAnsiString (Size));
  886. if ResultAddress<>Nil then
  887. begin
  888. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  889. PByte(ResultAddress+Size)^:=0;
  890. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  891. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  892. end;
  893. end;
  894. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  895. Pointer(fpc_ansistr_Copy):=ResultAddress;
  896. end;
  897. Function Pos (Const Substr : ShortString; Const Source : RawByteString) : SizeInt;
  898. var
  899. i,MaxLen : SizeInt;
  900. pc : pchar;
  901. begin
  902. Pos:=0;
  903. if Length(SubStr)>0 then
  904. begin
  905. MaxLen:=Length(source)-Length(SubStr);
  906. i:=0;
  907. pc:=@source[1];
  908. while (i<=MaxLen) do
  909. begin
  910. inc(i);
  911. if (SubStr[1]=pc^) and
  912. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  913. begin
  914. Pos:=i;
  915. exit;
  916. end;
  917. inc(pc);
  918. end;
  919. end;
  920. end;
  921. Function Pos (Const Substr : RawByteString; Const Source : RawByteString) : SizeInt;
  922. var
  923. i,MaxLen : SizeInt;
  924. pc : pchar;
  925. begin
  926. Pos:=0;
  927. if Length(SubStr)>0 then
  928. begin
  929. MaxLen:=Length(source)-Length(SubStr);
  930. i:=0;
  931. pc:=@source[1];
  932. while (i<=MaxLen) do
  933. begin
  934. inc(i);
  935. if (SubStr[1]=pc^) and
  936. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  937. begin
  938. Pos:=i;
  939. exit;
  940. end;
  941. inc(pc);
  942. end;
  943. end;
  944. end;
  945. { Faster version for a char alone. Must be implemented because }
  946. { pos(c: char; const s: shortstring) also exists, so otherwise }
  947. { using pos(char,pchar) will always call the shortstring version }
  948. { (exact match for first argument), also with $h+ (JM) }
  949. Function Pos (c : Char; Const s : RawByteString) : SizeInt;
  950. var
  951. i: SizeInt;
  952. pc : pchar;
  953. begin
  954. pc:=@s[1];
  955. for i:=1 to length(s) do
  956. begin
  957. if pc^=c then
  958. begin
  959. pos:=i;
  960. exit;
  961. end;
  962. inc(pc);
  963. end;
  964. pos:=0;
  965. end;
  966. {$ifndef FPUNONE}
  967. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  968. Var
  969. SS : String;
  970. begin
  971. fpc_Val_Real_AnsiStr := 0;
  972. if length(S) > 255 then
  973. code := 256
  974. else
  975. begin
  976. SS := S;
  977. Val(SS,fpc_Val_Real_AnsiStr,code);
  978. end;
  979. end;
  980. {$endif}
  981. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  982. Var
  983. SS : String;
  984. begin
  985. if length(S) > 255 then
  986. begin
  987. fpc_Val_Currency_AnsiStr := 0;
  988. code := 256;
  989. end
  990. else
  991. begin
  992. SS := S;
  993. Val(SS,fpc_Val_Currency_AnsiStr,code);
  994. end;
  995. end;
  996. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  997. Var
  998. SS : ShortString;
  999. begin
  1000. fpc_Val_UInt_AnsiStr := 0;
  1001. if length(S) > 255 then
  1002. code := 256
  1003. else
  1004. begin
  1005. SS := S;
  1006. Val(SS,fpc_Val_UInt_AnsiStr,code);
  1007. end;
  1008. end;
  1009. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  1010. Var
  1011. SS : ShortString;
  1012. begin
  1013. fpc_Val_SInt_AnsiStr:=0;
  1014. if length(S)>255 then
  1015. code:=256
  1016. else
  1017. begin
  1018. SS := S;
  1019. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1020. end;
  1021. end;
  1022. {$ifndef CPU64}
  1023. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  1024. Var
  1025. SS : ShortString;
  1026. begin
  1027. fpc_Val_qword_AnsiStr:=0;
  1028. if length(S)>255 then
  1029. code:=256
  1030. else
  1031. begin
  1032. SS := S;
  1033. Val(SS,fpc_Val_qword_AnsiStr,Code);
  1034. end;
  1035. end;
  1036. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  1037. Var
  1038. SS : ShortString;
  1039. begin
  1040. fpc_Val_int64_AnsiStr:=0;
  1041. if length(S)>255 then
  1042. code:=256
  1043. else
  1044. begin
  1045. SS := s;
  1046. Val(SS,fpc_Val_int64_AnsiStr,Code);
  1047. end;
  1048. end;
  1049. {$endif CPU64}
  1050. {$ifndef FPUNONE}
  1051. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1052. var
  1053. ss: ShortString;
  1054. begin
  1055. str_real(len,fr,d,treal_type(rt),ss);
  1056. s:=ss;
  1057. end;
  1058. {$endif}
  1059. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:ansistring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1060. var ss:shortstring;
  1061. begin
  1062. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1063. s:=ss;
  1064. end;
  1065. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:ansistring);[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1066. var
  1067. ss:shortstring;
  1068. begin
  1069. fpc_shortstr_bool(b,len,ss);
  1070. s:=ss;
  1071. end;
  1072. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:ansistring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  1073. begin
  1074. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1075. end;
  1076. {$ifdef FPC_HAS_STR_CURRENCY}
  1077. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1078. var
  1079. ss: ShortString;
  1080. begin
  1081. str(c:len:fr,ss);
  1082. s:=ss;
  1083. end;
  1084. {$endif FPC_HAS_STR_CURRENCY}
  1085. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1086. Var
  1087. SS : ShortString;
  1088. begin
  1089. str(v:Len,SS);
  1090. S:=SS;
  1091. end;
  1092. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1093. Var
  1094. SS : ShortString;
  1095. begin
  1096. str (v:Len,SS);
  1097. S:=SS;
  1098. end;
  1099. {$ifndef CPU64}
  1100. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1101. Var
  1102. SS : ShortString;
  1103. begin
  1104. str(v:Len,SS);
  1105. S:=SS;
  1106. end;
  1107. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1108. Var
  1109. SS : ShortString;
  1110. begin
  1111. str (v:Len,SS);
  1112. S:=SS;
  1113. end;
  1114. {$endif CPU64}
  1115. Procedure Delete (Var S : RawByteString; Index,Size: SizeInt);
  1116. Var
  1117. LS : SizeInt;
  1118. begin
  1119. ls:=Length(S);
  1120. If (Index>LS) or (Index<=0) or (Size<=0) then
  1121. exit;
  1122. UniqueString (S);
  1123. If (Size>LS-Index) then // Size+Index gives overflow ??
  1124. Size:=LS-Index+1;
  1125. If (Size<=LS-Index) then
  1126. begin
  1127. Dec(Index);
  1128. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  1129. end;
  1130. Setlength(S,LS-Size);
  1131. end;
  1132. Procedure Insert (Const Source : RawByteString; Var S : RawByteString; Index : SizeInt);
  1133. var
  1134. Temp : RawByteString;
  1135. LS : SizeInt;
  1136. begin
  1137. If Length(Source)=0 then
  1138. exit;
  1139. if index <= 0 then
  1140. index := 1;
  1141. Ls:=Length(S);
  1142. if index > LS then
  1143. index := LS+1;
  1144. Dec(Index);
  1145. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  1146. SetLength(Temp,Length(Source)+LS);
  1147. If Index>0 then
  1148. move (Pointer(S)^,Pointer(Temp)^,Index);
  1149. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  1150. If (LS-Index)>0 then
  1151. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  1152. S:=Temp;
  1153. end;
  1154. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  1155. begin
  1156. SetLength(StringOfChar,l);
  1157. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1158. end;
  1159. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  1160. begin
  1161. SetLength(S,Len);
  1162. If (Buf<>Nil) then
  1163. Move (Buf^,Pointer(S)^,Len);
  1164. end;
  1165. Procedure SetString (Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1166. begin
  1167. if (Buf<>nil) and (Len>0) then
  1168. widestringmanager.Wide2AnsiMoveProc(Buf,S,DefaultSystemCodePage,Len)
  1169. else
  1170. SetLength(S, Len);
  1171. end;
  1172. function upcase(const s : ansistring) : ansistring;
  1173. var
  1174. i : SizeInt;
  1175. begin
  1176. Setlength(result,length(s));
  1177. for i := 1 to length (s) do
  1178. result[i] := upcase(s[i]);
  1179. end;
  1180. function lowercase(const s : ansistring) : ansistring;
  1181. var
  1182. i : SizeInt;
  1183. begin
  1184. Setlength(result,length(s));
  1185. for i := 1 to length (s) do
  1186. result[i] := lowercase(s[i]);
  1187. end;
  1188. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1189. begin
  1190. {$ifdef FPC_HAS_CPSTRING}
  1191. if assigned(Pointer(S)) then
  1192. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1193. else
  1194. {$endif FPC_HAS_CPSTRING}
  1195. Result:=DefaultSystemCodePage;
  1196. end;
  1197. function StringElementSize(const S: RawByteString): Word; overload;
  1198. begin
  1199. if assigned(Pointer(S)) then
  1200. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1201. else
  1202. Result:=SizeOf(AnsiChar);
  1203. end;
  1204. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1205. begin
  1206. if assigned(Pointer(S)) then
  1207. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1208. else
  1209. Result:=0;
  1210. end;
  1211. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1212. begin
  1213. if (S='') or (StringCodePage(S)=CodePage) then
  1214. exit
  1215. else if Convert then
  1216. begin
  1217. {$ifdef FPC_HAS_CPSTRING}
  1218. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1219. {$else FPC_HAS_CPSTRING}
  1220. UniqueString(s);
  1221. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1222. {$endif FPC_HAS_CPSTRING}
  1223. end
  1224. else
  1225. begin
  1226. UniqueString(s);
  1227. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1228. end;
  1229. end;
  1230. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1231. begin
  1232. DefaultSystemCodePage:=CodePage;
  1233. end;