astrings.inc 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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 : AnsiChar;
  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 : PAnsiChar;
  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:=PAnsiChar(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 : PAnsiChar;
  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:=PAnsiChar(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(PAnsiChar(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) or (cp=$ffff) 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) or (cp=$ffff) 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 : AnsiChar{$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) or (cp=$ffff) 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 : PAnsiChar{$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) or (cp=$ffff) 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) or (cp=$ffff) 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. {$push}{$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. {$pop}
  556. end;
  557. {$else ndef FPC_STRTOCHARARRAYPROC}
  558. procedure fpc_ansistr_to_chararray(out res: array of AnsiChar; const src: RawByteString); compilerproc;
  559. var
  560. len: SizeInt;
  561. begin
  562. len := length(src);
  563. if len > length(res) then
  564. len := length(res);
  565. {$push}{$r-}
  566. { make sure we don't try to access element 1 of the ansistring if it's nil }
  567. if len > 0 then
  568. move(src[1],res[0],len);
  569. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  570. fillchar(res[len],length(res)-len,0);
  571. {$pop}
  572. end;
  573. {$endif ndef FPC_STRTOCHARARRAYPROC}
  574. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  575. {
  576. Compares 2 AnsiStrings;
  577. The result is
  578. <0 if S1<S2
  579. 0 if S1=S2
  580. >0 if S1>S2
  581. }
  582. Var
  583. MaxI,Temp : SizeInt;
  584. cp1,cp2 : TSystemCodePage;
  585. r1,r2 : RawByteString;
  586. u1,u2 : UTF8String;
  587. begin
  588. if pointer(S1)=pointer(S2) then
  589. begin
  590. result:=0;
  591. exit;
  592. end;
  593. if (pointer(S1)=nil) then
  594. begin
  595. result:=-Length(S2);
  596. exit;
  597. end;
  598. if (pointer(S2)=nil) then
  599. begin
  600. result:=Length(S1);
  601. exit;
  602. end;
  603. cp1:=StringCodePage(S1);
  604. cp2:=StringCodePage(S2);
  605. if (cp1=cp2) then
  606. begin
  607. Maxi:=Length(S1);
  608. temp:=Length(S2);
  609. If MaxI>Temp then
  610. MaxI:=Temp;
  611. if MaxI>0 then
  612. begin
  613. result:=CompareByte(S1[1],S2[1],MaxI);
  614. if result=0 then
  615. result:=Length(S1)-Length(S2);
  616. end
  617. else
  618. result:=Length(S1)-Length(S2);
  619. end
  620. else
  621. begin
  622. r1:=S1;
  623. if (cp1=$ffff) or (cp1=0) then
  624. SetCodePage(r1,DefaultSystemCodePage,false);
  625. r2:=S2;
  626. if (cp2=$ffff) or (cp2=0) then
  627. SetCodePage(r2,DefaultSystemCodePage,false);
  628. //convert them to utf8 then compare
  629. SetCodePage(r1,65001);
  630. SetCodePage(r2,65001);
  631. Result := fpc_AnsiStr_Compare(r1,r2);
  632. end;
  633. end;
  634. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  635. {
  636. Compares 2 AnsiStrings for equality/inequality only;
  637. The result is
  638. 0 if S1=S2
  639. <>0 if S1<>S2
  640. }
  641. Var
  642. MaxI,Temp : SizeInt;
  643. cp1,cp2 : TSystemCodePage;
  644. r1,r2 : RawByteString;
  645. begin
  646. if pointer(S1)=pointer(S2) then
  647. begin
  648. result:=0;
  649. exit;
  650. end;
  651. cp1:=StringCodePage(S1);
  652. cp2:=StringCodePage(S2);
  653. if (cp1=cp2) then
  654. begin
  655. Maxi:=Length(S1);
  656. temp:=Length(S2);
  657. Result := Maxi - temp;
  658. if Result = 0 then
  659. if MaxI>0 then
  660. result:=CompareByte(S1[1],S2[1],MaxI);
  661. end
  662. else
  663. begin
  664. r1:=S1;
  665. if (cp1=$ffff) or (cp1=0) then
  666. SetCodePage(r1,DefaultSystemCodePage,false);
  667. r2:=S2;
  668. if (cp2=$ffff) or (cp2=0) then
  669. SetCodePage(r2,DefaultSystemCodePage,false);
  670. //convert them to utf8 then compare
  671. SetCodePage(r1,65001);
  672. SetCodePage(r2,65001);
  673. Maxi:=Length(r1);
  674. temp:=Length(r2);
  675. Result := Maxi - temp;
  676. if Result = 0 then
  677. if MaxI>0 then
  678. result:=CompareByte(r1[1],r2[1],MaxI);
  679. end;
  680. end;
  681. {$ifdef VER2_4}
  682. // obsolete but needed for boostrapping with 2.4
  683. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  684. begin
  685. if p=nil then
  686. HandleErrorFrame(201,get_frame);
  687. end;
  688. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  689. begin
  690. if (index>len) or (Index<1) then
  691. HandleErrorFrame(201,get_frame);
  692. end;
  693. {$else VER2_4}
  694. Procedure fpc_AnsiStr_CheckRange(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  695. begin
  696. if (p=nil) or (index>PAnsiRec(p-AnsiFirstOff)^.Len) or (Index<1) then
  697. HandleErrorFrame(201,get_frame);
  698. end;
  699. {$endif VER2_4}
  700. 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;
  701. {
  702. Sets The length of string S to L.
  703. Makes sure S is unique, and contains enough room.
  704. }
  705. Var
  706. Temp : Pointer;
  707. lens, lena,
  708. movelen : SizeInt;
  709. begin
  710. if (l>0) then
  711. begin
  712. if Pointer(S)=nil then
  713. begin
  714. GetMem(Pointer(S),AnsiRecLen+L);
  715. PAnsiRec(S)^.Ref:=1;
  716. {$ifdef FPC_HAS_CPSTRING}
  717. if (cp=0) or (cp=$ffff) then
  718. cp:=DefaultSystemCodePage;
  719. PAnsiRec(S)^.CodePage:=cp;
  720. {$else}
  721. PAnsiRec(S)^.CodePage:=DefaultSystemCodePage;
  722. {$endif FPC_HAS_CPSTRING}
  723. PAnsiRec(S)^.ElementSize:=1;
  724. inc(Pointer(S),AnsiFirstOff);
  725. end
  726. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  727. begin
  728. Dec(Pointer(S),AnsiFirstOff);
  729. lens:=MemSize(Pointer(s));
  730. lena:=AnsiRecLen+L;
  731. { allow shrinking string if that saves at least half of current size }
  732. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  733. reallocmem(pointer(S),AnsiRecLen+L);
  734. Inc(Pointer(S),AnsiFirstOff);
  735. end
  736. else
  737. begin
  738. { Reallocation is needed... }
  739. Temp:=Pointer(NewAnsiString(L));
  740. {$ifdef FPC_HAS_CPSTRING}
  741. PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=cp;
  742. {$endif FPC_HAS_CPSTRING}
  743. { also move terminating null }
  744. lens:=succ(length(s));
  745. if l<lens then
  746. movelen:=l
  747. else
  748. movelen:=lens;
  749. Move(Pointer(S)^,Temp^,movelen);
  750. { ref count dropped to zero in the mean time? }
  751. If (PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref>0) and
  752. declocked(PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref) then
  753. freemem(PAnsiRec(Pointer(s)-AnsiFirstOff));
  754. Pointer(S):=Temp;
  755. end;
  756. { Force nil termination in case it gets shorter }
  757. PByte(Pointer(S)+l)^:=0;
  758. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  759. end
  760. else
  761. begin
  762. { Length=0 }
  763. if Pointer(S)<>nil then
  764. fpc_ansistr_decr_ref (Pointer(S));
  765. Pointer(S):=Nil;
  766. end;
  767. end;
  768. {$ifdef EXTRAANSISHORT}
  769. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  770. {
  771. Compares a AnsiString with a ShortString;
  772. The result is
  773. <0 if S1<S2
  774. 0 if S1=S2
  775. >0 if S1>S2
  776. }
  777. Var
  778. i,MaxI,Temp : SizeInt;
  779. begin
  780. Temp:=0;
  781. i:=0;
  782. MaxI:=Length(AnsiString(S1));
  783. if MaxI>byte(S2[0]) then
  784. MaxI:=Byte(S2[0]);
  785. While (i<MaxI) and (Temp=0) do
  786. begin
  787. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  788. inc(i);
  789. end;
  790. AnsiStr_ShortStr_Compare:=Temp;
  791. end;
  792. {$endif EXTRAANSISHORT}
  793. {*****************************************************************************
  794. Public functions, In interface.
  795. *****************************************************************************}
  796. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  797. Var
  798. SNew : Pointer;
  799. L : SizeInt;
  800. begin
  801. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  802. SNew:=NewAnsiString (L);
  803. Move (Pointer(S)^,SNew^,L+1);
  804. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  805. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  806. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  807. pointer(S):=SNew;
  808. pointer(result):=SNew;
  809. end;
  810. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  811. // MV: inline the basic checks for case that S is already unique.
  812. // Rest is too complex to inline, so factor that out as a call.
  813. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  814. {
  815. Make sure reference count of S is 1,
  816. using copy-on-write semantics.
  817. }
  818. begin
  819. pointer(result) := pointer(s);
  820. If Pointer(S)=Nil then
  821. exit;
  822. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  823. result:=fpc_truely_ansistr_unique(s);
  824. end;
  825. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  826. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt) : RawByteString;compilerproc;
  827. var
  828. ResultAddress : Pointer;
  829. begin
  830. ResultAddress:=Nil;
  831. dec(index);
  832. if Index < 0 then
  833. Index := 0;
  834. { Check Size. Accounts for Zero-length S, the double check is needed because
  835. Size can be maxint and will get <0 when adding index }
  836. if (Size>Length(S)) or
  837. (Index+Size>Length(S)) then
  838. Size:=Length(S)-Index;
  839. If Size>0 then
  840. begin
  841. If Index<0 Then
  842. Index:=0;
  843. ResultAddress:=Pointer(NewAnsiString (Size));
  844. if ResultAddress<>Nil then
  845. begin
  846. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  847. PByte(ResultAddress+Size)^:=0;
  848. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  849. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  850. end;
  851. end;
  852. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  853. Pointer(fpc_ansistr_Copy):=ResultAddress;
  854. end;
  855. Function Pos(Const Substr : ShortString; Const Source : RawByteString) : SizeInt;
  856. var
  857. i,MaxLen : SizeInt;
  858. pc : PAnsiChar;
  859. begin
  860. Pos:=0;
  861. if Length(SubStr)>0 then
  862. begin
  863. MaxLen:=Length(source)-Length(SubStr);
  864. i:=0;
  865. pc:=@source[1];
  866. while (i<=MaxLen) do
  867. begin
  868. inc(i);
  869. if (SubStr[1]=pc^) and
  870. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  871. begin
  872. Pos:=i;
  873. exit;
  874. end;
  875. inc(pc);
  876. end;
  877. end;
  878. end;
  879. Function Pos(Const Substr : RawByteString; Const Source : RawByteString) : SizeInt;
  880. var
  881. i,MaxLen : SizeInt;
  882. pc : PAnsiChar;
  883. begin
  884. Pos:=0;
  885. if Length(SubStr)>0 then
  886. begin
  887. MaxLen:=Length(source)-Length(SubStr);
  888. i:=0;
  889. pc:=@source[1];
  890. while (i<=MaxLen) do
  891. begin
  892. inc(i);
  893. if (SubStr[1]=pc^) and
  894. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  895. begin
  896. Pos:=i;
  897. exit;
  898. end;
  899. inc(pc);
  900. end;
  901. end;
  902. end;
  903. { Faster version for a char alone. Must be implemented because }
  904. { pos(c: char; const s: shortstring) also exists, so otherwise }
  905. { using pos(char,pchar) will always call the shortstring version }
  906. { (exact match for first argument), also with $h+ (JM) }
  907. Function Pos(c : AnsiChar; Const s : RawByteString) : SizeInt;
  908. var
  909. i: SizeInt;
  910. pc : PAnsiChar;
  911. begin
  912. pc:=@s[1];
  913. for i:=1 to length(s) do
  914. begin
  915. if pc^=c then
  916. begin
  917. pos:=i;
  918. exit;
  919. end;
  920. inc(pc);
  921. end;
  922. pos:=0;
  923. end;
  924. {$ifndef FPUNONE}
  925. Function fpc_Val_Real_AnsiStr(Const S : RawByteString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  926. Var
  927. SS : String;
  928. begin
  929. fpc_Val_Real_AnsiStr := 0;
  930. if length(S) > 255 then
  931. code := 256
  932. else
  933. begin
  934. SS := S;
  935. Val(SS,fpc_Val_Real_AnsiStr,code);
  936. end;
  937. end;
  938. {$endif}
  939. Function fpc_Val_Currency_AnsiStr(Const S : RawByteString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  940. Var
  941. SS : String;
  942. begin
  943. if length(S) > 255 then
  944. begin
  945. fpc_Val_Currency_AnsiStr := 0;
  946. code := 256;
  947. end
  948. else
  949. begin
  950. SS := S;
  951. Val(SS,fpc_Val_Currency_AnsiStr,code);
  952. end;
  953. end;
  954. Function fpc_Val_UInt_AnsiStr (Const S : RawByteString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  955. Var
  956. SS : ShortString;
  957. begin
  958. fpc_Val_UInt_AnsiStr := 0;
  959. if length(S) > 255 then
  960. code := 256
  961. else
  962. begin
  963. SS := S;
  964. Val(SS,fpc_Val_UInt_AnsiStr,code);
  965. end;
  966. end;
  967. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : RawByteString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  968. Var
  969. SS : ShortString;
  970. begin
  971. fpc_Val_SInt_AnsiStr:=0;
  972. if length(S)>255 then
  973. code:=256
  974. else
  975. begin
  976. SS := S;
  977. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  978. end;
  979. end;
  980. {$ifndef CPU64}
  981. Function fpc_Val_qword_AnsiStr (Const S : RawByteString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  982. Var
  983. SS : ShortString;
  984. begin
  985. fpc_Val_qword_AnsiStr:=0;
  986. if length(S)>255 then
  987. code:=256
  988. else
  989. begin
  990. SS := S;
  991. Val(SS,fpc_Val_qword_AnsiStr,Code);
  992. end;
  993. end;
  994. Function fpc_Val_int64_AnsiStr (Const S : RawByteString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  995. Var
  996. SS : ShortString;
  997. begin
  998. fpc_Val_int64_AnsiStr:=0;
  999. if length(S)>255 then
  1000. code:=256
  1001. else
  1002. begin
  1003. SS := s;
  1004. Val(SS,fpc_Val_int64_AnsiStr,Code);
  1005. end;
  1006. end;
  1007. {$endif CPU64}
  1008. {$ifndef FPUNONE}
  1009. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1010. var
  1011. ss: ShortString;
  1012. begin
  1013. str_real(len,fr,d,treal_type(rt),ss);
  1014. s:=ss;
  1015. {$ifdef FPC_HAS_CPSTRING}
  1016. SetCodePage(s,cp,false);
  1017. {$endif FPC_HAS_CPSTRING}
  1018. end;
  1019. {$endif}
  1020. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1021. var ss:shortstring;
  1022. begin
  1023. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1024. s:=ss;
  1025. {$ifdef FPC_HAS_CPSTRING}
  1026. SetCodePage(s,cp,false);
  1027. {$endif FPC_HAS_CPSTRING}
  1028. end;
  1029. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1030. var
  1031. ss:shortstring;
  1032. begin
  1033. fpc_shortstr_bool(b,len,ss);
  1034. s:=ss;
  1035. {$ifdef FPC_HAS_CPSTRING}
  1036. SetCodePage(s,cp,false);
  1037. {$endif FPC_HAS_CPSTRING}
  1038. end;
  1039. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:RawByteString;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  1040. begin
  1041. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1042. end;
  1043. {$ifdef FPC_HAS_STR_CURRENCY}
  1044. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1045. var
  1046. ss: ShortString;
  1047. begin
  1048. str(c:len:fr,ss);
  1049. s:=ss;
  1050. {$ifdef FPC_HAS_CPSTRING}
  1051. SetCodePage(s,cp,false);
  1052. {$endif FPC_HAS_CPSTRING}
  1053. end;
  1054. {$endif FPC_HAS_STR_CURRENCY}
  1055. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1056. Var
  1057. SS : ShortString;
  1058. begin
  1059. str(v:Len,SS);
  1060. S:=SS;
  1061. {$ifdef FPC_HAS_CPSTRING}
  1062. SetCodePage(s,cp,false);
  1063. {$endif FPC_HAS_CPSTRING}
  1064. end;
  1065. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1066. Var
  1067. SS : ShortString;
  1068. begin
  1069. str (v:Len,SS);
  1070. S:=SS;
  1071. {$ifdef FPC_HAS_CPSTRING}
  1072. SetCodePage(s,cp,false);
  1073. {$endif FPC_HAS_CPSTRING}
  1074. end;
  1075. {$ifndef CPU64}
  1076. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1077. Var
  1078. SS : ShortString;
  1079. begin
  1080. str(v:Len,SS);
  1081. S:=SS;
  1082. {$ifdef FPC_HAS_CPSTRING}
  1083. SetCodePage(s,cp,false);
  1084. {$endif FPC_HAS_CPSTRING}
  1085. end;
  1086. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1087. Var
  1088. SS : ShortString;
  1089. begin
  1090. str (v:Len,SS);
  1091. S:=SS;
  1092. {$ifdef FPC_HAS_CPSTRING}
  1093. SetCodePage(s,cp,false);
  1094. {$endif FPC_HAS_CPSTRING}
  1095. end;
  1096. {$endif CPU64}
  1097. Procedure Delete(Var S : RawByteString; Index,Size: SizeInt);
  1098. Var
  1099. LS : SizeInt;
  1100. begin
  1101. ls:=Length(S);
  1102. If (Index>LS) or (Index<=0) or (Size<=0) then
  1103. exit;
  1104. UniqueString(S);
  1105. If (Size>LS-Index) then // Size+Index gives overflow ??
  1106. Size:=LS-Index+1;
  1107. If (Size<=LS-Index) then
  1108. begin
  1109. Dec(Index);
  1110. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  1111. end;
  1112. Setlength(S,LS-Size);
  1113. end;
  1114. Procedure Insert(Const Source : RawByteString; Var S : RawByteString; Index : SizeInt);
  1115. var
  1116. Temp : RawByteString;
  1117. LS : SizeInt;
  1118. cp : TSystemCodePage;
  1119. begin
  1120. If Length(Source)=0 then
  1121. exit;
  1122. if index <= 0 then
  1123. index := 1;
  1124. Ls:=Length(S);
  1125. if index > LS then
  1126. index := LS+1;
  1127. Dec(Index);
  1128. SetLength(Temp,Length(Source)+LS);
  1129. cp:=StringCodePage(S);
  1130. if (cp=0) or (cp=$ffff) then
  1131. cp:=DefaultSystemCodePage;
  1132. SetCodePage(Temp,cp,false);
  1133. If Index>0 then
  1134. Move(Pointer(S)^,Pointer(Temp)^,Index);
  1135. Move(Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  1136. If (LS-Index)>0 then
  1137. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  1138. S:=Temp;
  1139. end;
  1140. Function StringOfChar(c : Ansichar;l : SizeInt) : AnsiString;
  1141. begin
  1142. SetLength(StringOfChar,l);
  1143. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1144. end;
  1145. Procedure SetString(Out S : AnsiString; Buf : PAnsiChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  1146. begin
  1147. SetLength(S,Len);
  1148. If (Buf<>Nil) then
  1149. Move(Buf^,Pointer(S)^,Len);
  1150. end;
  1151. Procedure SetString(Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1152. begin
  1153. if (Buf<>nil) and (Len>0) then
  1154. widestringmanager.Wide2AnsiMoveProc(Buf,S,DefaultSystemCodePage,Len)
  1155. else
  1156. SetLength(S, Len);
  1157. end;
  1158. function upcase(const s : ansistring) : ansistring;
  1159. var
  1160. i : SizeInt;
  1161. begin
  1162. Setlength(result,length(s));
  1163. for i := 1 to length (s) do
  1164. result[i] := upcase(s[i]);
  1165. end;
  1166. function lowercase(const s : ansistring) : ansistring;
  1167. var
  1168. i : SizeInt;
  1169. begin
  1170. Setlength(result,length(s));
  1171. for i := 1 to length (s) do
  1172. result[i] := lowercase(s[i]);
  1173. end;
  1174. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1175. begin
  1176. {$ifdef FPC_HAS_CPSTRING}
  1177. if assigned(Pointer(S)) then
  1178. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1179. else
  1180. {$endif FPC_HAS_CPSTRING}
  1181. Result:=DefaultSystemCodePage;
  1182. end;
  1183. function StringElementSize(const S: RawByteString): Word; overload;
  1184. begin
  1185. if assigned(Pointer(S)) then
  1186. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1187. else
  1188. Result:=SizeOf(AnsiChar);
  1189. end;
  1190. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1191. begin
  1192. if assigned(Pointer(S)) then
  1193. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1194. else
  1195. Result:=0;
  1196. end;
  1197. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1198. begin
  1199. if (S='') or (StringCodePage(S)=CodePage) then
  1200. exit
  1201. else if Convert then
  1202. begin
  1203. {$ifdef FPC_HAS_CPSTRING}
  1204. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1205. {$else FPC_HAS_CPSTRING}
  1206. UniqueString(s);
  1207. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1208. {$endif FPC_HAS_CPSTRING}
  1209. end
  1210. else
  1211. begin
  1212. UniqueString(s);
  1213. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1214. end;
  1215. end;
  1216. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1217. begin
  1218. DefaultSystemCodePage:=CodePage;
  1219. end;