astrings.inc 38 KB

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