astrings.inc 40 KB

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