astrings.inc 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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=CP_NONE) or (S1CP=0) then
  180. S1CP:=DefaultSystemCodePage;
  181. S2CP:=StringCodePage(S2);
  182. if (S2CP=CP_NONE) 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=CP_NONE) 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. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  235. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  236. end;
  237. SetCodePage(DestS,DestCP,false);
  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=CP_NONE) 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=CP_NONE) 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. orgcp: TSystemCodePage;
  372. begin
  373. result:='';
  374. Size:=Length(S);
  375. if Size>0 then
  376. begin
  377. if (cp=0) or (cp=CP_NONE) then
  378. cp:=DefaultSystemCodePage;
  379. orgcp:=StringCodePage(S);
  380. if (orgcp=cp) or (orgcp=CP_NONE) then
  381. begin
  382. SetLength(result,Size);
  383. Move(S[1],result[1],Size);
  384. PAnsiRec(Pointer(result)-AnsiFirstOff)^.CodePage:=cp;
  385. end
  386. else
  387. begin
  388. temp:=S;
  389. Size:=Length(temp);
  390. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(temp)),result,cp,Size);
  391. end;
  392. end;
  393. end;
  394. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [external name 'FPC_ANSISTR_TO_ANSISTR'];
  395. {$endif FPC_HAS_CPSTRING}
  396. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  397. { the following declaration has exactly the same effect as }
  398. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  399. { which is what the old helper was, so we don't need an extra implementation }
  400. { of the old helper (JM) }
  401. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring; [Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  402. {
  403. Converts a AnsiString to a ShortString;
  404. }
  405. Var
  406. Size : SizeInt;
  407. begin
  408. if S2='' then
  409. fpc_AnsiStr_To_ShortStr:=''
  410. else
  411. begin
  412. Size:=Length(S2);
  413. If Size>high_of_res then
  414. Size:=high_of_res;
  415. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  416. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  417. end;
  418. end;
  419. {$else FPC_STRTOSHORTSTRINGPROC}
  420. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  421. {
  422. Converts a AnsiString to a ShortString;
  423. }
  424. Var
  425. Size : SizeInt;
  426. begin
  427. if S2='' then
  428. res:=''
  429. else
  430. begin
  431. Size:=Length(S2);
  432. If Size>high(res) then
  433. Size:=high(res);
  434. Move (S2[1],res[1],Size);
  435. byte(res[0]):=byte(Size);
  436. end;
  437. end;
  438. {$endif FPC_STRTOSHORTSTRINGPROC}
  439. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  440. {
  441. Converts a ShortString to a AnsiString;
  442. }
  443. Var
  444. Size : SizeInt;
  445. {$ifndef FPC_HAS_CPSTRING}
  446. cp : TSystemCodePage;
  447. {$endif FPC_HAS_CPSTRING}
  448. begin
  449. {$ifdef FPC_HAS_CPSTRING}
  450. if (cp=0) or (cp=CP_NONE) then
  451. cp:=DefaultSystemCodePage;
  452. {$else FPC_HAS_CPSTRING}
  453. cp:=DefaultSystemCodePage;
  454. {$endif FPC_HAS_CPSTRING}
  455. Size:=Length(S2);
  456. Setlength(fpc_ShortStr_To_AnsiStr,Size);
  457. if Size>0 then
  458. begin
  459. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  460. SetCodePage(fpc_ShortStr_To_AnsiStr,cp,False);
  461. end
  462. end;
  463. Function fpc_Char_To_AnsiStr(const c : AnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  464. {
  465. Converts a Char to a AnsiString;
  466. }
  467. {$ifndef FPC_HAS_CPSTRING}
  468. var
  469. cp : TSystemCodePage;
  470. {$endif FPC_HAS_CPSTRING}
  471. begin
  472. {$ifdef FPC_HAS_CPSTRING}
  473. if (cp=0) or (cp=CP_NONE) then
  474. cp:=DefaultSystemCodePage;
  475. {$else FPC_HAS_CPSTRING}
  476. cp:=DefaultSystemCodePage;
  477. {$endif FPC_HAS_CPSTRING}
  478. Setlength (fpc_Char_To_AnsiStr,1);
  479. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  480. { Terminating Zero }
  481. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  482. SetCodePage(fpc_Char_To_AnsiStr,cp,False);
  483. end;
  484. Function fpc_PChar_To_AnsiStr(const p : PAnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  485. Var
  486. L : SizeInt;
  487. {$ifndef FPC_HAS_CPSTRING}
  488. cp : TSystemCodePage;
  489. {$endif FPC_HAS_CPSTRING}
  490. begin
  491. if (not assigned(p)) or (p[0]=#0) Then
  492. L := 0
  493. else
  494. l:=IndexChar(p^,-1,#0);
  495. SetLength(fpc_PChar_To_AnsiStr,L);
  496. if L > 0 then
  497. begin
  498. {$ifdef FPC_HAS_CPSTRING}
  499. if (cp=0) or (cp=CP_NONE) then
  500. cp:=DefaultSystemCodePage;
  501. {$else FPC_HAS_CPSTRING}
  502. cp:=DefaultSystemCodePage;
  503. {$endif FPC_HAS_CPSTRING}
  504. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L);
  505. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  506. end;
  507. end;
  508. 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;
  509. var
  510. i : SizeInt;
  511. {$ifndef FPC_HAS_CPSTRING}
  512. cp : TSystemCodePage;
  513. {$endif FPC_HAS_CPSTRING}
  514. begin
  515. if (zerobased) then
  516. begin
  517. if (arr[0]=#0) Then
  518. i := 0
  519. else
  520. begin
  521. i:=IndexChar(arr,high(arr)+1,#0);
  522. if i = -1 then
  523. i := high(arr)+1;
  524. end;
  525. end
  526. else
  527. i := high(arr)+1;
  528. SetLength(fpc_CharArray_To_AnsiStr,i);
  529. if i > 0 then
  530. begin
  531. {$ifdef FPC_HAS_CPSTRING}
  532. if (cp=0) or (cp=CP_NONE) then
  533. cp:=DefaultSystemCodePage;
  534. {$else FPC_HAS_CPSTRING}
  535. cp:=DefaultSystemCodePage;
  536. {$endif FPC_HAS_CPSTRING}
  537. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  538. SetCodePage(fpc_CharArray_To_AnsiStr,cp,False);
  539. end;
  540. end;
  541. {$ifndef FPC_STRTOCHARARRAYPROC}
  542. { note: inside the compiler, the resulttype is modified to be the length }
  543. { of the actual chararray to which we convert (JM) }
  544. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  545. var
  546. len: SizeInt;
  547. begin
  548. len := length(src);
  549. if len > arraysize then
  550. len := arraysize;
  551. {$push}{$r-}
  552. { make sure we don't try to access element 1 of the ansistring if it's nil }
  553. if len > 0 then
  554. move(src[1],fpc_ansistr_to_chararray[0],len);
  555. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  556. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  557. {$pop}
  558. end;
  559. {$else ndef FPC_STRTOCHARARRAYPROC}
  560. procedure fpc_ansistr_to_chararray(out res: array of AnsiChar; 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. {$push}{$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. {$pop}
  574. end;
  575. {$endif ndef FPC_STRTOCHARARRAYPROC}
  576. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  577. {
  578. Compares 2 AnsiStrings;
  579. The result is
  580. <0 if S1<S2
  581. 0 if S1=S2
  582. >0 if S1>S2
  583. }
  584. Var
  585. MaxI,Temp : SizeInt;
  586. cp1,cp2 : TSystemCodePage;
  587. r1,r2 : RawByteString;
  588. begin
  589. if pointer(S1)=pointer(S2) then
  590. begin
  591. result:=0;
  592. exit;
  593. end;
  594. if (pointer(S1)=nil) then
  595. begin
  596. result:=-Length(S2);
  597. exit;
  598. end;
  599. if (pointer(S2)=nil) then
  600. begin
  601. result:=Length(S1);
  602. exit;
  603. end;
  604. cp1:=StringCodePage(S1);
  605. cp2:=StringCodePage(S2);
  606. if (cp1=cp2) then
  607. begin
  608. Maxi:=Length(S1);
  609. temp:=Length(S2);
  610. If MaxI>Temp then
  611. MaxI:=Temp;
  612. if MaxI>0 then
  613. begin
  614. result:=CompareByte(S1[1],S2[1],MaxI);
  615. if result=0 then
  616. result:=Length(S1)-Length(S2);
  617. end
  618. else
  619. result:=Length(S1)-Length(S2);
  620. end
  621. else
  622. begin
  623. r1:=S1;
  624. if (cp1=CP_NONE) or (cp1=0) then
  625. SetCodePage(r1,DefaultSystemCodePage,false);
  626. r2:=S2;
  627. if (cp2=CP_NONE) or (cp2=0) then
  628. SetCodePage(r2,DefaultSystemCodePage,false);
  629. //convert them to utf8 then compare
  630. SetCodePage(r1,65001);
  631. SetCodePage(r2,65001);
  632. Result := fpc_AnsiStr_Compare(r1,r2);
  633. end;
  634. end;
  635. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  636. {
  637. Compares 2 AnsiStrings for equality/inequality only;
  638. The result is
  639. 0 if S1=S2
  640. <>0 if S1<>S2
  641. }
  642. Var
  643. MaxI,Temp : SizeInt;
  644. cp1,cp2 : TSystemCodePage;
  645. r1,r2 : RawByteString;
  646. begin
  647. if pointer(S1)=pointer(S2) then
  648. begin
  649. result:=0;
  650. exit;
  651. end;
  652. { don't compare strings if one of them is empty }
  653. if (pointer(S1)=nil) then
  654. begin
  655. result:=-Length(S2);
  656. exit;
  657. end;
  658. if (pointer(S2)=nil) then
  659. begin
  660. result:=Length(S1);
  661. exit;
  662. end;
  663. cp1:=StringCodePage(S1);
  664. cp2:=StringCodePage(S2);
  665. if (cp1=cp2) then
  666. begin
  667. Maxi:=Length(S1);
  668. temp:=Length(S2);
  669. Result := Maxi - temp;
  670. if Result = 0 then
  671. if MaxI>0 then
  672. result:=CompareByte(S1[1],S2[1],MaxI);
  673. end
  674. else
  675. begin
  676. r1:=S1;
  677. if (cp1=CP_NONE) or (cp1=0) then
  678. SetCodePage(r1,DefaultSystemCodePage,false);
  679. r2:=S2;
  680. if (cp2=CP_NONE) or (cp2=0) then
  681. SetCodePage(r2,DefaultSystemCodePage,false);
  682. //convert them to utf8 then compare
  683. SetCodePage(r1,65001);
  684. SetCodePage(r2,65001);
  685. Maxi:=Length(r1);
  686. temp:=Length(r2);
  687. Result := Maxi - temp;
  688. if Result = 0 then
  689. if MaxI>0 then
  690. result:=CompareByte(r1[1],r2[1],MaxI);
  691. end;
  692. end;
  693. {$ifdef VER2_4}
  694. // obsolete but needed for boostrapping with 2.4
  695. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  696. begin
  697. if p=nil then
  698. HandleErrorFrame(201,get_frame);
  699. end;
  700. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  701. begin
  702. if (index>len) or (Index<1) then
  703. HandleErrorFrame(201,get_frame);
  704. end;
  705. {$else VER2_4}
  706. Procedure fpc_AnsiStr_CheckRange(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  707. begin
  708. if (p=nil) or (index>PAnsiRec(p-AnsiFirstOff)^.Len) or (Index<1) then
  709. HandleErrorFrame(201,get_frame);
  710. end;
  711. {$endif VER2_4}
  712. 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;
  713. {
  714. Sets The length of string S to L.
  715. Makes sure S is unique, and contains enough room.
  716. }
  717. Var
  718. Temp : Pointer;
  719. lens, lena,
  720. movelen : SizeInt;
  721. begin
  722. if (l>0) then
  723. begin
  724. if Pointer(S)=nil then
  725. begin
  726. GetMem(Pointer(S),AnsiRecLen+L);
  727. PAnsiRec(S)^.Ref:=1;
  728. {$ifdef FPC_HAS_CPSTRING}
  729. if (cp=0) or (cp=CP_NONE) then
  730. cp:=DefaultSystemCodePage;
  731. PAnsiRec(S)^.CodePage:=cp;
  732. {$else}
  733. PAnsiRec(S)^.CodePage:=DefaultSystemCodePage;
  734. {$endif FPC_HAS_CPSTRING}
  735. PAnsiRec(S)^.ElementSize:=1;
  736. inc(Pointer(S),AnsiFirstOff);
  737. end
  738. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  739. begin
  740. Dec(Pointer(S),AnsiFirstOff);
  741. lens:=MemSize(Pointer(s));
  742. lena:=AnsiRecLen+L;
  743. { allow shrinking string if that saves at least half of current size }
  744. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  745. reallocmem(pointer(S),AnsiRecLen+L);
  746. Inc(Pointer(S),AnsiFirstOff);
  747. end
  748. else
  749. begin
  750. { Reallocation is needed... }
  751. Temp:=Pointer(NewAnsiString(L));
  752. {$ifdef FPC_HAS_CPSTRING}
  753. PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=cp;
  754. {$endif FPC_HAS_CPSTRING}
  755. { also move terminating null }
  756. lens:=succ(length(s));
  757. if l<lens then
  758. movelen:=l
  759. else
  760. movelen:=lens;
  761. Move(Pointer(S)^,Temp^,movelen);
  762. { ref count dropped to zero in the mean time? }
  763. If (PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref>0) and
  764. declocked(PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref) then
  765. freemem(PAnsiRec(Pointer(s)-AnsiFirstOff));
  766. Pointer(S):=Temp;
  767. end;
  768. { Force nil termination in case it gets shorter }
  769. PByte(Pointer(S)+l)^:=0;
  770. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  771. end
  772. else
  773. begin
  774. { Length=0 }
  775. if Pointer(S)<>nil then
  776. fpc_ansistr_decr_ref (Pointer(S));
  777. Pointer(S):=Nil;
  778. end;
  779. end;
  780. {$ifdef EXTRAANSISHORT}
  781. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  782. {
  783. Compares a AnsiString with a ShortString;
  784. The result is
  785. <0 if S1<S2
  786. 0 if S1=S2
  787. >0 if S1>S2
  788. }
  789. Var
  790. i,MaxI,Temp : SizeInt;
  791. begin
  792. Temp:=0;
  793. i:=0;
  794. MaxI:=Length(AnsiString(S1));
  795. if MaxI>byte(S2[0]) then
  796. MaxI:=Byte(S2[0]);
  797. While (i<MaxI) and (Temp=0) do
  798. begin
  799. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  800. inc(i);
  801. end;
  802. AnsiStr_ShortStr_Compare:=Temp;
  803. end;
  804. {$endif EXTRAANSISHORT}
  805. {*****************************************************************************
  806. Public functions, In interface.
  807. *****************************************************************************}
  808. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  809. Var
  810. SNew : Pointer;
  811. L : SizeInt;
  812. begin
  813. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  814. SNew:=NewAnsiString (L);
  815. Move (Pointer(S)^,SNew^,L+1);
  816. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  817. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  818. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  819. pointer(S):=SNew;
  820. pointer(result):=SNew;
  821. end;
  822. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  823. // MV: inline the basic checks for case that S is already unique.
  824. // Rest is too complex to inline, so factor that out as a call.
  825. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  826. {
  827. Make sure reference count of S is 1,
  828. using copy-on-write semantics.
  829. }
  830. begin
  831. pointer(result) := pointer(s);
  832. If Pointer(S)=Nil then
  833. exit;
  834. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  835. result:=fpc_truely_ansistr_unique(s);
  836. end;
  837. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  838. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt) : RawByteString;compilerproc;
  839. var
  840. ResultAddress : Pointer;
  841. begin
  842. ResultAddress:=Nil;
  843. dec(index);
  844. if Index < 0 then
  845. Index := 0;
  846. { Check Size. Accounts for Zero-length S, the double check is needed because
  847. Size can be maxint and will get <0 when adding index }
  848. if (Size>Length(S)) or
  849. (Index+Size>Length(S)) then
  850. Size:=Length(S)-Index;
  851. If Size>0 then
  852. begin
  853. If Index<0 Then
  854. Index:=0;
  855. ResultAddress:=Pointer(NewAnsiString (Size));
  856. if ResultAddress<>Nil then
  857. begin
  858. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  859. PByte(ResultAddress+Size)^:=0;
  860. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  861. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  862. end;
  863. end;
  864. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  865. Pointer(fpc_ansistr_Copy):=ResultAddress;
  866. end;
  867. Function Pos(Const Substr : ShortString; Const Source : RawByteString) : SizeInt;
  868. var
  869. i,MaxLen : SizeInt;
  870. pc : PAnsiChar;
  871. begin
  872. Pos:=0;
  873. if Length(SubStr)>0 then
  874. begin
  875. MaxLen:=Length(source)-Length(SubStr);
  876. i:=0;
  877. pc:=@source[1];
  878. while (i<=MaxLen) do
  879. begin
  880. inc(i);
  881. if (SubStr[1]=pc^) and
  882. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  883. begin
  884. Pos:=i;
  885. exit;
  886. end;
  887. inc(pc);
  888. end;
  889. end;
  890. end;
  891. Function Pos(Const Substr : RawByteString; Const Source : RawByteString) : SizeInt;
  892. var
  893. i,MaxLen : SizeInt;
  894. pc : PAnsiChar;
  895. begin
  896. Pos:=0;
  897. if Length(SubStr)>0 then
  898. begin
  899. MaxLen:=Length(source)-Length(SubStr);
  900. i:=0;
  901. pc:=@source[1];
  902. while (i<=MaxLen) do
  903. begin
  904. inc(i);
  905. if (SubStr[1]=pc^) and
  906. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  907. begin
  908. Pos:=i;
  909. exit;
  910. end;
  911. inc(pc);
  912. end;
  913. end;
  914. end;
  915. { Faster version for a char alone. Must be implemented because }
  916. { pos(c: char; const s: shortstring) also exists, so otherwise }
  917. { using pos(char,pchar) will always call the shortstring version }
  918. { (exact match for first argument), also with $h+ (JM) }
  919. Function Pos(c : AnsiChar; Const s : RawByteString) : SizeInt;
  920. var
  921. i: SizeInt;
  922. pc : PAnsiChar;
  923. begin
  924. pc:=@s[1];
  925. for i:=1 to length(s) do
  926. begin
  927. if pc^=c then
  928. begin
  929. pos:=i;
  930. exit;
  931. end;
  932. inc(pc);
  933. end;
  934. pos:=0;
  935. end;
  936. {$ifndef FPUNONE}
  937. Function fpc_Val_Real_AnsiStr(Const S : RawByteString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  938. Var
  939. SS : String;
  940. begin
  941. fpc_Val_Real_AnsiStr := 0;
  942. if length(S) > 255 then
  943. code := 256
  944. else
  945. begin
  946. SS := S;
  947. Val(SS,fpc_Val_Real_AnsiStr,code);
  948. end;
  949. end;
  950. {$endif}
  951. Function fpc_Val_Currency_AnsiStr(Const S : RawByteString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  952. Var
  953. SS : String;
  954. begin
  955. if length(S) > 255 then
  956. begin
  957. fpc_Val_Currency_AnsiStr := 0;
  958. code := 256;
  959. end
  960. else
  961. begin
  962. SS := S;
  963. Val(SS,fpc_Val_Currency_AnsiStr,code);
  964. end;
  965. end;
  966. Function fpc_Val_UInt_AnsiStr (Const S : RawByteString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  967. Var
  968. SS : ShortString;
  969. begin
  970. fpc_Val_UInt_AnsiStr := 0;
  971. if length(S) > 255 then
  972. code := 256
  973. else
  974. begin
  975. SS := S;
  976. Val(SS,fpc_Val_UInt_AnsiStr,code);
  977. end;
  978. end;
  979. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : RawByteString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  980. Var
  981. SS : ShortString;
  982. begin
  983. fpc_Val_SInt_AnsiStr:=0;
  984. if length(S)>255 then
  985. code:=256
  986. else
  987. begin
  988. SS := S;
  989. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  990. end;
  991. end;
  992. {$ifndef CPU64}
  993. Function fpc_Val_qword_AnsiStr (Const S : RawByteString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  994. Var
  995. SS : ShortString;
  996. begin
  997. fpc_Val_qword_AnsiStr:=0;
  998. if length(S)>255 then
  999. code:=256
  1000. else
  1001. begin
  1002. SS := S;
  1003. Val(SS,fpc_Val_qword_AnsiStr,Code);
  1004. end;
  1005. end;
  1006. Function fpc_Val_int64_AnsiStr (Const S : RawByteString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  1007. Var
  1008. SS : ShortString;
  1009. begin
  1010. fpc_Val_int64_AnsiStr:=0;
  1011. if length(S)>255 then
  1012. code:=256
  1013. else
  1014. begin
  1015. SS := s;
  1016. Val(SS,fpc_Val_int64_AnsiStr,Code);
  1017. end;
  1018. end;
  1019. {$endif CPU64}
  1020. {$ifndef FPUNONE}
  1021. 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}
  1022. var
  1023. ss: ShortString;
  1024. begin
  1025. str_real(len,fr,d,treal_type(rt),ss);
  1026. s:=ss;
  1027. {$ifdef FPC_HAS_CPSTRING}
  1028. SetCodePage(s,cp,false);
  1029. {$endif FPC_HAS_CPSTRING}
  1030. end;
  1031. {$endif}
  1032. 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}
  1033. var ss:shortstring;
  1034. begin
  1035. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1036. s:=ss;
  1037. {$ifdef FPC_HAS_CPSTRING}
  1038. SetCodePage(s,cp,false);
  1039. {$endif FPC_HAS_CPSTRING}
  1040. end;
  1041. 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}
  1042. var
  1043. ss:shortstring;
  1044. begin
  1045. fpc_shortstr_bool(b,len,ss);
  1046. s:=ss;
  1047. {$ifdef FPC_HAS_CPSTRING}
  1048. SetCodePage(s,cp,false);
  1049. {$endif FPC_HAS_CPSTRING}
  1050. end;
  1051. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:RawByteString;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  1052. begin
  1053. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1054. end;
  1055. {$ifdef FPC_HAS_STR_CURRENCY}
  1056. 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}
  1057. var
  1058. ss: ShortString;
  1059. begin
  1060. str(c:len:fr,ss);
  1061. s:=ss;
  1062. {$ifdef FPC_HAS_CPSTRING}
  1063. SetCodePage(s,cp,false);
  1064. {$endif FPC_HAS_CPSTRING}
  1065. end;
  1066. {$endif FPC_HAS_STR_CURRENCY}
  1067. 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}
  1068. Var
  1069. SS : ShortString;
  1070. begin
  1071. str(v:Len,SS);
  1072. S:=SS;
  1073. {$ifdef FPC_HAS_CPSTRING}
  1074. SetCodePage(s,cp,false);
  1075. {$endif FPC_HAS_CPSTRING}
  1076. end;
  1077. 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}
  1078. Var
  1079. SS : ShortString;
  1080. begin
  1081. str (v:Len,SS);
  1082. S:=SS;
  1083. {$ifdef FPC_HAS_CPSTRING}
  1084. SetCodePage(s,cp,false);
  1085. {$endif FPC_HAS_CPSTRING}
  1086. end;
  1087. {$ifndef CPU64}
  1088. 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}
  1089. Var
  1090. SS : ShortString;
  1091. begin
  1092. str(v:Len,SS);
  1093. S:=SS;
  1094. {$ifdef FPC_HAS_CPSTRING}
  1095. SetCodePage(s,cp,false);
  1096. {$endif FPC_HAS_CPSTRING}
  1097. end;
  1098. 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}
  1099. Var
  1100. SS : ShortString;
  1101. begin
  1102. str (v:Len,SS);
  1103. S:=SS;
  1104. {$ifdef FPC_HAS_CPSTRING}
  1105. SetCodePage(s,cp,false);
  1106. {$endif FPC_HAS_CPSTRING}
  1107. end;
  1108. {$endif CPU64}
  1109. Procedure Delete(Var S : RawByteString; Index,Size: SizeInt);
  1110. Var
  1111. LS : SizeInt;
  1112. begin
  1113. ls:=Length(S);
  1114. If (Index>LS) or (Index<=0) or (Size<=0) then
  1115. exit;
  1116. UniqueString(S);
  1117. If (Size>LS-Index) then // Size+Index gives overflow ??
  1118. Size:=LS-Index+1;
  1119. If (Size<=LS-Index) then
  1120. begin
  1121. Dec(Index);
  1122. Move(PByte(Pointer(S))[Index+Size],PByte(Pointer(S))[Index],LS-Index-Size+1);
  1123. end;
  1124. Setlength(S,LS-Size);
  1125. end;
  1126. Procedure Insert(Const Source : RawByteString; Var S : RawByteString; Index : SizeInt);
  1127. var
  1128. Temp : RawByteString;
  1129. LS : SizeInt;
  1130. cp : TSystemCodePage;
  1131. begin
  1132. If Length(Source)=0 then
  1133. exit;
  1134. if index <= 0 then
  1135. index := 1;
  1136. Ls:=Length(S);
  1137. if index > LS then
  1138. index := LS+1;
  1139. Dec(Index);
  1140. SetLength(Temp,Length(Source)+LS);
  1141. cp:=StringCodePage(S);
  1142. if (cp=0) or (cp=CP_NONE) then
  1143. cp:=DefaultSystemCodePage;
  1144. SetCodePage(Temp,cp,false);
  1145. If Index>0 then
  1146. Move(Pointer(S)^,Pointer(Temp)^,Index);
  1147. Move(Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  1148. If (LS-Index)>0 then
  1149. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  1150. S:=Temp;
  1151. end;
  1152. Function StringOfChar(c : Ansichar;l : SizeInt) : AnsiString;
  1153. begin
  1154. SetLength(StringOfChar,l);
  1155. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1156. end;
  1157. Procedure SetString(Out S : AnsiString; Buf : PAnsiChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  1158. begin
  1159. SetLength(S,Len);
  1160. If (Buf<>Nil) then
  1161. Move(Buf^,Pointer(S)^,Len);
  1162. end;
  1163. Procedure SetString(Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1164. begin
  1165. if (Buf<>nil) and (Len>0) then
  1166. widestringmanager.Wide2AnsiMoveProc(Buf,S,DefaultSystemCodePage,Len)
  1167. else
  1168. SetLength(S, Len);
  1169. end;
  1170. function upcase(const s : ansistring) : ansistring;
  1171. var
  1172. i : SizeInt;
  1173. begin
  1174. Setlength(result,length(s));
  1175. for i := 1 to length (s) do
  1176. result[i] := upcase(s[i]);
  1177. end;
  1178. function lowercase(const s : ansistring) : ansistring;
  1179. var
  1180. i : SizeInt;
  1181. begin
  1182. Setlength(result,length(s));
  1183. for i := 1 to length (s) do
  1184. result[i] := lowercase(s[i]);
  1185. end;
  1186. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1187. begin
  1188. {$ifdef FPC_HAS_CPSTRING}
  1189. if assigned(Pointer(S)) then
  1190. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1191. else
  1192. {$endif FPC_HAS_CPSTRING}
  1193. Result:=DefaultSystemCodePage;
  1194. end;
  1195. function StringElementSize(const S: RawByteString): Word; overload;
  1196. begin
  1197. if assigned(Pointer(S)) then
  1198. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1199. else
  1200. Result:=SizeOf(AnsiChar);
  1201. end;
  1202. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1203. begin
  1204. if assigned(Pointer(S)) then
  1205. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1206. else
  1207. Result:=0;
  1208. end;
  1209. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1210. begin
  1211. if (S='') or (StringCodePage(S)=CodePage) then
  1212. exit
  1213. else if Convert then
  1214. begin
  1215. {$ifdef FPC_HAS_CPSTRING}
  1216. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1217. {$else FPC_HAS_CPSTRING}
  1218. UniqueString(s);
  1219. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1220. {$endif FPC_HAS_CPSTRING}
  1221. end
  1222. else
  1223. begin
  1224. UniqueString(s);
  1225. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1226. end;
  1227. end;
  1228. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1229. begin
  1230. DefaultSystemCodePage:=CodePage;
  1231. end;