astrings.inc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  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 (S= SizeOf(SizeInt)):
  21. @-3*S : Code page indicator.
  22. @-3*S+2 : Character size (2 bytes)
  23. @-2*S : SizeInt for reference count;
  24. @-S : 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 or keep the code page if the other string is empty }
  206. if cp=CP_NONE then
  207. begin
  208. if (S1CP=S2CP) or (S2='') then
  209. DestCP:=S1CP
  210. else if S1='' then
  211. DestCP:=S2CP;
  212. end;
  213. {$endif FPC_HAS_CPSTRING}
  214. if ((S1CP<>DestCP) and (s1<>'')) or ((S2CP<>DestCP) and (s2<>'')) then
  215. begin
  216. ansistr_concat_complex(DestS,S1,S2,DestCP);
  217. exit;
  218. end;
  219. { only assign if s1 or s2 is empty }
  220. if (S1='') then
  221. begin
  222. DestS:=s2;
  223. exit;
  224. end;
  225. if (S2='') then
  226. begin
  227. DestS:=s1;
  228. exit;
  229. end;
  230. S1Len:=Length(S1);
  231. S2Len:=length(S2);
  232. { Use Pointer() typecasts to prevent extra conversion code }
  233. if Pointer(DestS)=Pointer(S1) then
  234. begin
  235. same:=Pointer(S1)=Pointer(S2);
  236. SetLength(DestS,S1Len+S2Len);
  237. if same then
  238. fpc_pchar_ansistr_intern_charmove(PAnsiChar(DestS),0,DestS,S1Len,S2Len)
  239. else
  240. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S2),0,DestS,S1Len,S2Len+1)
  241. end
  242. else if Pointer(DestS)=Pointer(S2) then
  243. begin
  244. SetLength(DestS,S1Len+S2Len);
  245. fpc_pchar_ansistr_intern_charmove(PAnsiChar(DestS),0,DestS,S1Len,S2Len+1);
  246. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S1),0,DestS,0,S1Len);
  247. end
  248. else
  249. begin
  250. SetLength(DestS,S1Len+S2Len);
  251. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S1),0,DestS,0,S1Len);
  252. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S2),0,DestS,S1Len,S2Len+1);
  253. end;
  254. SetCodePage(DestS,DestCP,false);
  255. end;
  256. {$endif FPC_HAS_ANSISTR_CONCAT}
  257. {$ifndef FPC_HAS_ANSISTR_CONCAT_MULTI}
  258. {$define FPC_HAS_ANSISTR_CONCAT_MULTI}
  259. procedure fpc_AnsiStr_Concat_multi (var DestS:RawByteString;const sarr:array of RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); compilerproc;
  260. Var
  261. lowstart,
  262. nonemptystart,
  263. i : ObjpasInt;
  264. p,pc : pointer;
  265. Size,NewLen,
  266. OldDestLen : SizeInt;
  267. destcopy : pointer;
  268. U : UnicodeString;
  269. DestCP,
  270. tmpCP : TSystemCodePage;
  271. sameCP : Boolean;
  272. begin
  273. if high(sarr)=0 then
  274. begin
  275. DestS:='';
  276. exit;
  277. end;
  278. {$ifdef FPC_HAS_CPSTRING}
  279. DestCP:=cp;
  280. if DestCp=CP_NONE then
  281. DestCP:=DefaultSystemCodePage;
  282. {$else FPC_HAS_CPSTRING}
  283. DestCP:=StringCodePage(DestS);
  284. {$endif FPC_HAS_CPSTRING}
  285. lowstart:=low(sarr);
  286. { skip empty strings }
  287. while (lowstart<=high(sarr)) and
  288. (sarr[lowstart]='') do
  289. inc(lowstart);
  290. if lowstart>high(sarr) then
  291. begin
  292. DestS:=''; { All source strings empty }
  293. exit;
  294. end;
  295. DestCP:=TranslatePlaceholderCP(DestCP);
  296. sameCP:=true;
  297. tmpCP:=TranslatePlaceholderCP(StringCodePage(sarr[lowstart]));
  298. for i:=lowstart+1 to high(sarr) do
  299. begin
  300. { ignore the code page of empty strings, it will always be
  301. DefaultSystemCodePage but it doesn't matter for the outcome }
  302. if (sarr[i]<>'') and
  303. (tmpCP<>TranslatePlaceholderCP(StringCodePage(sarr[i]))) then
  304. begin
  305. sameCP:=false;
  306. break;
  307. end;
  308. end;
  309. if not sameCP then
  310. begin
  311. U:='';
  312. for i:=lowstart to high(sarr) do
  313. if sarr[i]<>'' then
  314. U:=U+UnicodeString(sarr[i]);
  315. DestS:='';
  316. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(U)),DestS,DestCP,Length(U));
  317. exit;
  318. end;
  319. {$ifdef FPC_HAS_CPSTRING}
  320. { if the result is rawbytestring and all strings have the same code page,
  321. keep that code page }
  322. if cp=CP_NONE then
  323. DestCP:=tmpCP;
  324. {$endif FPC_HAS_CPSTRING}
  325. destcopy:=nil;
  326. nonemptystart:=lowstart;
  327. { Check for another reuse, then we can't use
  328. the append optimization }
  329. if DestS<>'' then
  330. begin
  331. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  332. inc(lowstart);
  333. for i:=lowstart to high(sarr) do
  334. begin
  335. if Pointer(DestS)=Pointer(sarr[i]) then
  336. begin
  337. { if DestS is used somewhere in the middle of the expression,
  338. we need to make sure the original string still exists after
  339. we empty/modify DestS }
  340. destcopy:=pointer(dests);
  341. fpc_AnsiStr_Incr_Ref(destcopy);
  342. lowstart:=nonemptystart;
  343. break;
  344. end;
  345. end;
  346. end;
  347. { Start with empty DestS if we start with concatting
  348. the first (non-empty) array element }
  349. if lowstart=nonemptystart then
  350. DestS:='';
  351. OldDestLen:=length(DestS);
  352. { Calculate size of the result so we can do
  353. a single call to SetLength() }
  354. NewLen:=0;
  355. for i:=nonemptystart to high(sarr) do
  356. inc(NewLen,length(sarr[i]));
  357. SetLength(DestS,NewLen);
  358. { Concat all strings, except the string we already
  359. copied in DestS }
  360. pc:=Pointer(DestS)+OldDestLen;
  361. for i:=lowstart to high(sarr) do
  362. begin
  363. p:=pointer(sarr[i]);
  364. if assigned(p) then
  365. begin
  366. Size:=length(ansistring(p));
  367. Move(p^,pc^,Size+1);
  368. inc(pc,size);
  369. end;
  370. end;
  371. SetCodePage(DestS,tmpCP,False);
  372. SetCodePage(DestS,DestCP,True);
  373. fpc_AnsiStr_Decr_Ref(destcopy);
  374. end;
  375. {$endif FPC_HAS_ANSISTR_CONCAT_MULTI}
  376. {$ifdef EXTRAANSISHORT}
  377. Procedure AnsiStr_ShortStr_Concat (Var S1: AnsiString; Var S2 : ShortString);
  378. {
  379. Concatenates a Ansi with a short string; : S2 + S2
  380. }
  381. Var
  382. Size,Location : SizeInt;
  383. begin
  384. Size:=Length(S2);
  385. Location:=Length(S1);
  386. If Size=0 then
  387. exit;
  388. { Setlength takes case of uniqueness
  389. and alllocated memory. We need to use length,
  390. to take into account possibility of S1=Nil }
  391. SetLength (S1,Size+Length(S1));
  392. Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
  393. PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
  394. end;
  395. {$endif EXTRAANSISHORT}
  396. {$ifdef FPC_HAS_CPSTRING}
  397. {$ifndef FPC_HAS_ANSISTR_TO_ANSISTR}
  398. {$define FPC_HAS_ANSISTR_TO_ANSISTR}
  399. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [Public, alias: 'FPC_ANSISTR_TO_ANSISTR']; compilerproc;
  400. {
  401. Converts an AnsiString to an AnsiString taking code pages into care
  402. }
  403. Var
  404. Size : SizeInt;
  405. temp : UnicodeString;
  406. orgcp: TSystemCodePage;
  407. begin
  408. result:='';
  409. Size:=Length(S);
  410. if Size>0 then
  411. begin
  412. cp:=TranslatePlaceholderCP(cp);
  413. orgcp:=TranslatePlaceholderCP(StringCodePage(S));
  414. if (orgcp=cp) or (orgcp=CP_NONE) then
  415. begin
  416. SetLength(result,Size);
  417. Move(S[1],result[1],Size);
  418. PAnsiRec(Pointer(result)-AnsiFirstOff)^.CodePage:=cp;
  419. end
  420. else
  421. begin
  422. temp:=UnicodeString(S);
  423. Size:=Length(temp);
  424. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Pointer(temp)),result,cp,Size);
  425. end;
  426. end;
  427. end;
  428. Function fpc_AnsiStr_To_AnsiStr (const S : RawByteString;cp : TSystemCodePage): RawByteString; [external name 'FPC_ANSISTR_TO_ANSISTR'];
  429. {$endif FPC_HAS_CPSTRING}
  430. {$endif FPC_HAS_ANSISTR_TO_ANSISTR}
  431. {$ifndef FPC_HAS_ANSISTR_TO_SHORTSTR}
  432. {$define FPC_HAS_ANSISTR_TO_SHORTSTR}
  433. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  434. {
  435. Converts a AnsiString to a ShortString;
  436. }
  437. Var
  438. Size : SizeInt;
  439. begin
  440. if S2='' then
  441. res:=''
  442. else
  443. begin
  444. Size:=Length(S2);
  445. If Size>high(res) then
  446. Size:=high(res);
  447. Move (S2[1],res[1],Size);
  448. byte(res[0]):=byte(Size);
  449. end;
  450. end;
  451. {$endif FPC_HAS_ANSISTR_TO_SHORTSTR}
  452. {$ifndef FPC_HAS_SHORTSTR_TO_ANSISTR}
  453. {$define FPC_HAS_SHORTSTR_TO_ANSISTR}
  454. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  455. {
  456. Converts a ShortString to a AnsiString;
  457. }
  458. Var
  459. Size : SizeInt;
  460. {$ifndef FPC_HAS_CPSTRING}
  461. cp : TSystemCodePage;
  462. {$endif FPC_HAS_CPSTRING}
  463. begin
  464. {$ifdef FPC_HAS_CPSTRING}
  465. cp:=TranslatePlaceholderCP(cp);
  466. {$else FPC_HAS_CPSTRING}
  467. cp:=DefaultSystemCodePage;
  468. {$endif FPC_HAS_CPSTRING}
  469. Size:=Length(S2);
  470. Setlength(fpc_ShortStr_To_AnsiStr,Size);
  471. if Size>0 then
  472. begin
  473. fpc_shortstr_ansistr_intern_charmove(S2,1,fpc_ShortStr_To_AnsiStr,0,Size);
  474. SetCodePage(fpc_ShortStr_To_AnsiStr,cp,False);
  475. end
  476. end;
  477. {$endif FPC_HAS_SHORTSTR_TO_ANSISTR}
  478. {$ifndef FPC_HAS_CHAR_TO_ANSISTR}
  479. {$define FPC_HAS_CHAR_TO_ANSISTR}
  480. Function fpc_Char_To_AnsiStr(const c : AnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  481. {
  482. Converts a Char to a AnsiString;
  483. }
  484. {$ifndef FPC_HAS_CPSTRING}
  485. var
  486. cp : TSystemCodePage;
  487. {$endif FPC_HAS_CPSTRING}
  488. begin
  489. {$ifdef FPC_HAS_CPSTRING}
  490. cp:=TranslatePlaceholderCP(cp);
  491. {$else FPC_HAS_CPSTRING}
  492. cp:=DefaultSystemCodePage;
  493. {$endif FPC_HAS_CPSTRING}
  494. Setlength (fpc_Char_To_AnsiStr,1);
  495. PAnsiChar(fpc_Char_To_AnsiStr)^:=c;
  496. { Terminating Zero already set by SetLength above }
  497. SetCodePage(fpc_Char_To_AnsiStr,cp,False);
  498. end;
  499. {$endif FPC_HAS_CHAR_TO_ANSISTR}
  500. {$ifndef FPC_HAS_PCHAR_TO_ANSISTR}
  501. {$define FPC_HAS_PCHAR_TO_ANSISTR}
  502. Function fpc_PChar_To_AnsiStr(const p : PAnsiChar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  503. Var
  504. L : SizeInt;
  505. {$ifndef FPC_HAS_CPSTRING}
  506. cp : TSystemCodePage;
  507. {$endif FPC_HAS_CPSTRING}
  508. begin
  509. if (not assigned(p)) or (p[0]=#0) Then
  510. L := 0
  511. else
  512. L:=IndexChar(p^,-1,#0);
  513. SetLength(fpc_PChar_To_AnsiStr,L);
  514. if L > 0 then
  515. begin
  516. {$ifdef FPC_HAS_CPSTRING}
  517. cp:=TranslatePlaceholderCP(cp);
  518. {$else FPC_HAS_CPSTRING}
  519. cp:=DefaultSystemCodePage;
  520. {$endif FPC_HAS_CPSTRING}
  521. fpc_pchar_ansistr_intern_charmove(p,0,fpc_PChar_To_AnsiStr,0,L);
  522. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  523. end;
  524. end;
  525. {$endif FPC_HAS_PCHAR_TO_ANSISTR}
  526. {$ifndef FPC_HAS_CHARARRAY_TO_ANSISTR}
  527. {$define FPC_HAS_CHARARRAY_TO_ANSISTR}
  528. 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;
  529. var
  530. i : SizeInt;
  531. {$ifndef FPC_HAS_CPSTRING}
  532. cp : TSystemCodePage;
  533. {$endif FPC_HAS_CPSTRING}
  534. begin
  535. if (zerobased) then
  536. begin
  537. if (arr[0]=#0) Then
  538. i := 0
  539. else
  540. begin
  541. i:=IndexChar(arr,high(arr)+1,#0);
  542. if i = -1 then
  543. i := high(arr)+1;
  544. end;
  545. end
  546. else
  547. i := high(arr)+1;
  548. SetLength(fpc_CharArray_To_AnsiStr,i);
  549. if i > 0 then
  550. begin
  551. {$ifdef FPC_HAS_CPSTRING}
  552. cp:=TranslatePlaceholderCP(cp);
  553. {$else FPC_HAS_CPSTRING}
  554. cp:=DefaultSystemCodePage;
  555. {$endif FPC_HAS_CPSTRING}
  556. fpc_pchar_ansistr_intern_charmove(pansichar(@arr),0,fpc_CharArray_To_AnsiStr,0,i);
  557. SetCodePage(fpc_CharArray_To_AnsiStr,cp,False);
  558. end;
  559. end;
  560. {$endif FPC_HAS_ANSISTR_TO_CHARARRAY}
  561. {$ifndef FPC_HAS_ANSISTR_TO_CHARARRAY}
  562. {$define FPC_HAS_ANSISTR_TO_CHARARRAY}
  563. procedure fpc_ansistr_to_chararray(out res: array of AnsiChar; const src: RawByteString); compilerproc;
  564. var
  565. len: SizeInt;
  566. begin
  567. len := length(src);
  568. if len > length(res) then
  569. len := length(res);
  570. {$push}{$r-}
  571. { make sure we don't try to access element 1 of the ansistring if it's nil }
  572. if len > 0 then
  573. move(src[1],res[0],len);
  574. fillchar(res[len],length(res)-len,0);
  575. {$pop}
  576. end;
  577. {$endif FPC_HAS_ANSISTR_TO_CHARARRAY}
  578. {$ifndef FPC_HAS_ANSISTR_COMPARE}
  579. {$define FPC_HAS_ANSISTR_COMPARE}
  580. Function fpc_utf8_Compare(const S1,S2 : RawByteString): SizeInt;
  581. var
  582. r1,r2 : RawByteString;
  583. begin
  584. r1:=S1;
  585. r2:=S2;
  586. //convert them to utf8 then compare
  587. SetCodePage(r1,65001);
  588. SetCodePage(r2,65001);
  589. Result:=fpc_AnsiStr_Compare(r1,r2);
  590. end;
  591. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  592. {
  593. Compares 2 AnsiStrings;
  594. The result is
  595. <0 if S1<S2
  596. 0 if S1=S2
  597. >0 if S1>S2
  598. }
  599. Var
  600. MaxI,Temp : SizeInt;
  601. cp1,cp2 : TSystemCodePage;
  602. r1,r2 : RawByteString;
  603. begin
  604. if pointer(S1)=pointer(S2) then
  605. begin
  606. result:=0;
  607. exit;
  608. end;
  609. if (pointer(S1)=nil) then
  610. begin
  611. result:=-Length(S2);
  612. exit;
  613. end;
  614. if (pointer(S2)=nil) then
  615. begin
  616. result:=Length(S1);
  617. exit;
  618. end;
  619. cp1:=TranslatePlaceholderCP(StringCodePage(S1));
  620. cp2:=TranslatePlaceholderCP(StringCodePage(S2));
  621. if cp1=cp2 then
  622. begin
  623. Maxi:=Length(S1);
  624. temp:=Length(S2);
  625. If MaxI>Temp then
  626. MaxI:=Temp;
  627. if MaxI>0 then
  628. begin
  629. result:=CompareByte(S1[1],S2[1],MaxI);
  630. if result=0 then
  631. result:=Length(S1)-Length(S2);
  632. end
  633. else
  634. result:=Length(S1)-Length(S2);
  635. end
  636. else
  637. Result:=fpc_utf8_compare(s1,s2);
  638. end;
  639. {$endif FPC_HAS_ANSISTR_COMPARE}
  640. {$ifndef FPC_HAS_ANSISTR_COMPARE_EQUAL}
  641. {$define FPC_HAS_ANSISTR_COMPARE_EQUAL}
  642. Function fpc_utf8_Compare_equal(Const S1,S2 : RawByteString): SizeInt;
  643. Var
  644. r1,r2 : RawByteString;
  645. L1,L2 : SizeInt;
  646. begin
  647. r1:=S1;
  648. r2:=S2;
  649. //convert them to utf8 then compare
  650. SetCodePage(r1,65001);
  651. SetCodePage(r2,65001);
  652. L1:=Length(r1);
  653. L2:=Length(r2);
  654. Result:=L1-L2;
  655. if Result = 0 then
  656. if L1>0 then
  657. result:=CompareByte(r1[1],r2[1],L1);
  658. end;
  659. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  660. {
  661. Compares 2 AnsiStrings for equality/inequality only;
  662. The result is
  663. 0 if S1=S2
  664. <>0 if S1<>S2
  665. }
  666. Var
  667. MaxI,Temp : SizeInt;
  668. cp1,cp2 : TSystemCodePage;
  669. begin
  670. if pointer(S1)=pointer(S2) then
  671. Exit(0);
  672. { don't compare strings if one of them is empty }
  673. if (pointer(S1)=nil) or (pointer(S2)=Nil) then
  674. Exit(-1);
  675. cp1:=TranslatePlaceholderCP(StringCodePage(S1));
  676. cp2:=TranslatePlaceholderCP(StringCodePage(S2));
  677. if cp1=cp2 then
  678. begin
  679. Maxi:=Length(S1);
  680. temp:=Length(S2);
  681. Result := Maxi - temp;
  682. if Result = 0 then
  683. if MaxI>0 then
  684. result:=CompareByte(S1[1],S2[1],MaxI);
  685. end
  686. else
  687. begin
  688. Result:=fpc_utf8_Compare_equal(S1,S2);
  689. end;
  690. end;
  691. {$endif FPC_HAS_ANSISTR_COMPARE_EQUAL}
  692. {$ifndef FPC_HAS_ANSISTR_RANGECHECK}
  693. {$define FPC_HAS_ANSISTR_RANGECHECK}
  694. Procedure fpc_AnsiStr_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  695. begin
  696. if (p=nil) or (index>PAnsiRec(p-AnsiFirstOff)^.Len) or (Index<1) then
  697. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  698. end;
  699. {$endif FPC_HAS_ANSISTR_RANGECHECK}
  700. {$ifndef FPC_HAS_ANSISTR_SETLENGTH}
  701. {$define FPC_HAS_ANSISTR_SETLENGTH}
  702. 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;
  703. {
  704. Sets The length of string S to L.
  705. Makes sure S is unique, and contains enough room.
  706. }
  707. Var
  708. Temp : Pointer;
  709. lens, lena : SizeInt;
  710. begin
  711. if (l>0) then
  712. begin
  713. if Pointer(S)=nil then
  714. begin
  715. Pointer(S):=NewAnsiString(L);
  716. {$ifdef FPC_HAS_CPSTRING}
  717. cp:=TranslatePlaceholderCP(cp);
  718. PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=cp;
  719. {$else}
  720. PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=DefaultSystemCodePage;
  721. {$endif FPC_HAS_CPSTRING}
  722. end
  723. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  724. begin
  725. Temp:=Pointer(s)-AnsiFirstOff;
  726. lens:=MemSize(Temp);
  727. lena:=AnsiFirstOff+L+sizeof(AnsiChar);
  728. { allow shrinking string if that saves at least half of current size }
  729. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  730. begin
  731. reallocmem(Temp,lena);
  732. Pointer(S):=Temp+AnsiFirstOff;
  733. end;
  734. end
  735. else
  736. begin
  737. { Reallocation is needed... }
  738. Temp:=NewAnsiString(L);
  739. PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  740. lens:=length(s);
  741. if l<=lens then
  742. lens:=l
  743. else
  744. { Also copy a trailing implicit #0 of the original string
  745. to the new larger string }
  746. Inc(lens);
  747. Move(Pointer(S)^,Temp^,lens);
  748. fpc_ansistr_decr_ref(Pointer(s));
  749. Pointer(S):=Temp;
  750. end;
  751. { Force nil termination in case it gets shorter }
  752. PByte(Pointer(S)+l)^:=0;
  753. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  754. end
  755. else { length=0, deallocate the string }
  756. fpc_ansistr_decr_ref (Pointer(S));
  757. end;
  758. {$endif FPC_HAS_ANSISTR_SETLENGTH}
  759. {$ifdef EXTRAANSISHORT}
  760. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  761. {
  762. Compares a AnsiString with a ShortString;
  763. The result is
  764. <0 if S1<S2
  765. 0 if S1=S2
  766. >0 if S1>S2
  767. }
  768. Var
  769. i,MaxI,Temp : SizeInt;
  770. begin
  771. Temp:=0;
  772. i:=0;
  773. MaxI:=Length(AnsiString(S1));
  774. if MaxI>byte(S2[0]) then
  775. MaxI:=Byte(S2[0]);
  776. While (i<MaxI) and (Temp=0) do
  777. begin
  778. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  779. inc(i);
  780. end;
  781. AnsiStr_ShortStr_Compare:=Temp;
  782. end;
  783. {$endif EXTRAANSISHORT}
  784. {*****************************************************************************
  785. Public functions, In interface.
  786. *****************************************************************************}
  787. {$ifndef FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  788. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  789. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  790. Var
  791. SNew : Pointer;
  792. L : SizeInt;
  793. begin
  794. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  795. SNew:=NewAnsiString (L);
  796. Move (Pointer(S)^,SNew^,L+1);
  797. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  798. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  799. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  800. pointer(S):=SNew;
  801. pointer(result):=SNew;
  802. end;
  803. {$endif FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  804. Function fpc_ansistr_Unique_func(Var S : RawByteString): Pointer; external name 'FPC_ANSISTR_UNIQUE';
  805. Procedure UniqueString(var S : RawByteString);{$ifdef FPC_HAS_CPSTRING}rtlproc;{$endif FPC_HAS_CPSTRING}{$ifdef SYSTEMINLINE}inline;{$endif}
  806. begin
  807. fpc_ansistr_Unique_func(S);
  808. end;
  809. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  810. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  811. // MV: inline the basic checks for case that S is already unique.
  812. // Rest is too complex to inline, so factor that out as a call.
  813. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; inline;
  814. {
  815. Make sure reference count of S is 1,
  816. using copy-on-write semantics.
  817. }
  818. begin
  819. pointer(result) := pointer(s);
  820. If Pointer(S)=Nil then
  821. exit;
  822. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  823. result:=fpc_truely_ansistr_unique(s);
  824. end;
  825. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  826. {$ifndef FPC_HAS_ANSISTR_COPY}
  827. {$define FPC_HAS_ANSISTR_COPY}
  828. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt): RawByteString;compilerproc;
  829. var
  830. ResultAddress : Pointer;
  831. begin
  832. ResultAddress:=Nil;
  833. dec(index);
  834. if Index < 0 then
  835. Index := 0;
  836. { Check Size. Accounts for Zero-length S, the double check is needed because
  837. Size can be maxint and will get <0 when adding index }
  838. if (Size>Length(S)) or
  839. (Index+Size>Length(S)) then
  840. Size:=Length(S)-Index;
  841. If Size>0 then
  842. begin
  843. ResultAddress:=NewAnsiString(Size);
  844. if ResultAddress<>Nil then
  845. begin
  846. Move(Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  847. PByte(ResultAddress+Size)^:=0;
  848. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  849. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  850. end;
  851. end;
  852. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  853. Pointer(fpc_ansistr_Copy):=ResultAddress;
  854. end;
  855. {$endif FPC_HAS_ANSISTR_COPY}
  856. {$ifndef FPC_HAS_POS_SHORTSTR_ANSISTR}
  857. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  858. Function Pos(Const Substr : ShortString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  859. var
  860. i,MaxLen : SizeInt;
  861. pc : PAnsiChar;
  862. begin
  863. Pos:=0;
  864. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  865. begin
  866. MaxLen:=Length(source)-Length(SubStr);
  867. i:=Offset-1;
  868. pc:=@source[Offset];
  869. while (i<=MaxLen) do
  870. begin
  871. inc(i);
  872. if (SubStr[1]=pc^) and
  873. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  874. begin
  875. Pos:=i;
  876. exit;
  877. end;
  878. inc(pc);
  879. end;
  880. end;
  881. end;
  882. {$endif FPC_HAS_POS_SHORTSTR_ANSISTR}
  883. {$ifndef FPC_HAS_POS_ANSISTR_ANSISTR}
  884. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  885. Function Pos(Const Substr : RawByteString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  886. var
  887. i,MaxLen : SizeInt;
  888. pc : PAnsiChar;
  889. begin
  890. Pos:=0;
  891. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  892. begin
  893. MaxLen:=Length(source)-Length(SubStr);
  894. i:=Offset-1;
  895. pc:=@source[Offset];
  896. while (i<=MaxLen) do
  897. begin
  898. inc(i);
  899. if (SubStr[1]=pc^) and
  900. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  901. begin
  902. Pos:=i;
  903. exit;
  904. end;
  905. inc(pc);
  906. end;
  907. end;
  908. end;
  909. {$endif FPC_HAS_POS_ANSISTR_ANSISTR}
  910. {$ifndef FPC_HAS_POS_ANSICHAR_ANSISTR}
  911. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  912. { Faster version for a char alone. Must be implemented because }
  913. { pos(c: char; const s: shortstring) also exists, so otherwise }
  914. { using pos(char,pchar) will always call the shortstring version }
  915. { (exact match for first argument), also with $h+ (JM) }
  916. Function Pos(c : AnsiChar; Const s : RawByteString; Offset : Sizeint = 1) : SizeInt;
  917. var
  918. i: SizeInt;
  919. pc : PAnsiChar;
  920. begin
  921. Pos:=0;
  922. If (Offset<1) or (Offset>Length(S)) then
  923. exit;
  924. pc:=@s[OffSet];
  925. for i:=Offset to length(s) do
  926. begin
  927. if pc^=c then
  928. begin
  929. pos:=i;
  930. exit;
  931. end;
  932. inc(pc);
  933. end;
  934. pos:=0;
  935. end;
  936. {$endif FPC_HAS_POS_ANSICHAR_ANSISTR}
  937. {$ifndef FPUNONE}
  938. Function fpc_Val_Real_AnsiStr(Const S : RawByteString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  939. Var
  940. SS : String;
  941. begin
  942. fpc_Val_Real_AnsiStr := 0;
  943. if length(S) > 255 then
  944. code := 256
  945. else
  946. begin
  947. SS := S;
  948. Val(SS,fpc_Val_Real_AnsiStr,code);
  949. end;
  950. end;
  951. {$endif}
  952. Function fpc_Val_Currency_AnsiStr(Const S : RawByteString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  953. Var
  954. SS : String;
  955. begin
  956. if length(S) > 255 then
  957. begin
  958. fpc_Val_Currency_AnsiStr := 0;
  959. code := 256;
  960. end
  961. else
  962. begin
  963. SS := S;
  964. Val(SS,fpc_Val_Currency_AnsiStr,code);
  965. end;
  966. end;
  967. Function fpc_Val_UInt_AnsiStr (Const S : RawByteString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  968. Var
  969. SS : ShortString;
  970. begin
  971. fpc_Val_UInt_AnsiStr := 0;
  972. if length(S) > 255 then
  973. code := 256
  974. else
  975. begin
  976. SS := S;
  977. Val(SS,fpc_Val_UInt_AnsiStr,code);
  978. end;
  979. end;
  980. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : RawByteString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  981. Var
  982. SS : ShortString;
  983. begin
  984. fpc_Val_SInt_AnsiStr:=0;
  985. if length(S)>255 then
  986. code:=256
  987. else
  988. begin
  989. SS := S;
  990. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  991. end;
  992. end;
  993. {$ifndef CPU64}
  994. Function fpc_Val_qword_AnsiStr (Const S : RawByteString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  995. Var
  996. SS : ShortString;
  997. begin
  998. fpc_Val_qword_AnsiStr:=0;
  999. if length(S)>255 then
  1000. code:=256
  1001. else
  1002. begin
  1003. SS := S;
  1004. Val(SS,fpc_Val_qword_AnsiStr,Code);
  1005. end;
  1006. end;
  1007. Function fpc_Val_int64_AnsiStr (Const S : RawByteString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  1008. Var
  1009. SS : ShortString;
  1010. begin
  1011. fpc_Val_int64_AnsiStr:=0;
  1012. if length(S)>255 then
  1013. code:=256
  1014. else
  1015. begin
  1016. SS := s;
  1017. Val(SS,fpc_Val_int64_AnsiStr,Code);
  1018. end;
  1019. end;
  1020. {$endif CPU64}
  1021. {$if defined(CPU16) or defined(CPU8)}
  1022. Function fpc_Val_longword_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_LONGWORD_ANSISTR']; compilerproc;
  1023. Var
  1024. SS : ShortString;
  1025. begin
  1026. fpc_Val_longword_AnsiStr:=0;
  1027. if length(S)>255 then
  1028. code:=256
  1029. else
  1030. begin
  1031. SS := S;
  1032. Val(SS,fpc_Val_longword_AnsiStr,Code);
  1033. end;
  1034. end;
  1035. Function fpc_Val_longint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_LONGINT_ANSISTR']; compilerproc;
  1036. Var
  1037. SS : ShortString;
  1038. begin
  1039. fpc_Val_longint_AnsiStr:=0;
  1040. if length(S)>255 then
  1041. code:=256
  1042. else
  1043. begin
  1044. SS := s;
  1045. Val(SS,fpc_Val_longint_AnsiStr,Code);
  1046. end;
  1047. end;
  1048. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): word; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  1049. Var
  1050. SS : ShortString;
  1051. begin
  1052. fpc_Val_word_AnsiStr:=0;
  1053. if length(S)>255 then
  1054. code:=256
  1055. else
  1056. begin
  1057. SS := S;
  1058. Val(SS,fpc_Val_word_AnsiStr,Code);
  1059. end;
  1060. end;
  1061. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): smallint; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  1062. Var
  1063. SS : ShortString;
  1064. begin
  1065. fpc_Val_smallint_AnsiStr:=0;
  1066. if length(S)>255 then
  1067. code:=256
  1068. else
  1069. begin
  1070. SS := s;
  1071. Val(SS,fpc_Val_smallint_AnsiStr,Code);
  1072. end;
  1073. end;
  1074. {$endif CPU16 or CPU8}
  1075. {$if defined(CPU8)}
  1076. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  1077. Var
  1078. SS : ShortString;
  1079. begin
  1080. fpc_Val_longword_AnsiStr:=0;
  1081. if length(S)>255 then
  1082. code:=256
  1083. else
  1084. begin
  1085. SS := S;
  1086. Val(SS,fpc_Val_longword_AnsiStr,Code);
  1087. end;
  1088. end;
  1089. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  1090. Var
  1091. SS : ShortString;
  1092. begin
  1093. fpc_Val_longint_AnsiStr:=0;
  1094. if length(S)>255 then
  1095. code:=256
  1096. else
  1097. begin
  1098. SS := s;
  1099. Val(SS,fpc_Val_longint_AnsiStr,Code);
  1100. end;
  1101. end;
  1102. {$endif CPU8}
  1103. {$ifndef FPUNONE}
  1104. 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;
  1105. var
  1106. ss: ShortString;
  1107. begin
  1108. str_real(len,fr,d,treal_type(rt),ss);
  1109. s:=ss;
  1110. {$ifdef FPC_HAS_CPSTRING}
  1111. SetCodePage(s,cp,false);
  1112. {$endif FPC_HAS_CPSTRING}
  1113. end;
  1114. {$endif}
  1115. {$ifndef FPC_STR_ENUM_INTERN}
  1116. 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;
  1117. var ss:shortstring;
  1118. begin
  1119. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1120. s:=ss;
  1121. {$ifdef FPC_HAS_CPSTRING}
  1122. SetCodePage(s,cp,false);
  1123. {$endif FPC_HAS_CPSTRING}
  1124. end;
  1125. {$endif FPC_STR_ENUM_INTERN}
  1126. 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;
  1127. var
  1128. ss:shortstring;
  1129. begin
  1130. fpc_shortstr_bool(b,len,ss);
  1131. s:=ss;
  1132. {$ifdef FPC_HAS_CPSTRING}
  1133. SetCodePage(s,cp,false);
  1134. {$endif FPC_HAS_CPSTRING}
  1135. end;
  1136. {$ifndef FPC_STR_ENUM_INTERN}
  1137. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:RawByteString;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  1138. begin
  1139. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1140. end;
  1141. {$endif FPC_STR_ENUM_INTERN}
  1142. 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;
  1143. var
  1144. ss: ShortString;
  1145. begin
  1146. str(c:len:fr,ss);
  1147. s:=ss;
  1148. {$ifdef FPC_HAS_CPSTRING}
  1149. SetCodePage(s,cp,false);
  1150. {$endif FPC_HAS_CPSTRING}
  1151. end;
  1152. 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;
  1153. Var
  1154. SS : ShortString;
  1155. begin
  1156. str(v:Len,SS);
  1157. S:=SS;
  1158. {$ifdef FPC_HAS_CPSTRING}
  1159. SetCodePage(s,cp,false);
  1160. {$endif FPC_HAS_CPSTRING}
  1161. end;
  1162. 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;
  1163. Var
  1164. SS : ShortString;
  1165. begin
  1166. str (v:Len,SS);
  1167. S:=SS;
  1168. {$ifdef FPC_HAS_CPSTRING}
  1169. SetCodePage(s,cp,false);
  1170. {$endif FPC_HAS_CPSTRING}
  1171. end;
  1172. {$ifndef CPU64}
  1173. 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;
  1174. Var
  1175. SS : ShortString;
  1176. begin
  1177. str(v:Len,SS);
  1178. S:=SS;
  1179. {$ifdef FPC_HAS_CPSTRING}
  1180. SetCodePage(s,cp,false);
  1181. {$endif FPC_HAS_CPSTRING}
  1182. end;
  1183. 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;
  1184. Var
  1185. SS : ShortString;
  1186. begin
  1187. str (v:Len,SS);
  1188. S:=SS;
  1189. {$ifdef FPC_HAS_CPSTRING}
  1190. SetCodePage(s,cp,false);
  1191. {$endif FPC_HAS_CPSTRING}
  1192. end;
  1193. {$endif CPU64}
  1194. {$if defined(CPU16) or defined(CPU8)}
  1195. 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;
  1196. Var
  1197. SS : ShortString;
  1198. begin
  1199. str(v:Len,SS);
  1200. S:=SS;
  1201. {$ifdef FPC_HAS_CPSTRING}
  1202. SetCodePage(s,cp,false);
  1203. {$endif FPC_HAS_CPSTRING}
  1204. end;
  1205. 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;
  1206. Var
  1207. SS : ShortString;
  1208. begin
  1209. str (v:Len,SS);
  1210. S:=SS;
  1211. {$ifdef FPC_HAS_CPSTRING}
  1212. SetCodePage(s,cp,false);
  1213. {$endif FPC_HAS_CPSTRING}
  1214. end;
  1215. Procedure fpc_AnsiStr_Word(v : Word;Len : SizeInt; out S : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_WORD']; compilerproc; inline;
  1216. Var
  1217. SS : ShortString;
  1218. begin
  1219. str(v:Len,SS);
  1220. S:=SS;
  1221. {$ifdef FPC_HAS_CPSTRING}
  1222. SetCodePage(s,cp,false);
  1223. {$endif FPC_HAS_CPSTRING}
  1224. end;
  1225. Procedure fpc_AnsiStr_SmallInt(v : SmallInt; Len : SizeInt; out S : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING});[Public,Alias : 'FPC_ANSISTR_SMALLINT']; compilerproc; inline;
  1226. Var
  1227. SS : ShortString;
  1228. begin
  1229. str (v:Len,SS);
  1230. S:=SS;
  1231. {$ifdef FPC_HAS_CPSTRING}
  1232. SetCodePage(s,cp,false);
  1233. {$endif FPC_HAS_CPSTRING}
  1234. end;
  1235. {$endif CPU16 or CPU8}
  1236. Procedure {$ifdef VER3_0}Delete{$else}fpc_ansistr_delete{$endif}(Var S : RawByteString; Index,Size: SizeInt);
  1237. Var
  1238. LS : SizeInt;
  1239. begin
  1240. ls:=Length(S);
  1241. If (Index>LS) or (Index<=0) or (Size<=0) then
  1242. exit;
  1243. UniqueString(S);
  1244. If (Size>LS-Index) then // Size+Index gives overflow ??
  1245. Size:=LS-Index+1;
  1246. If (Size<=LS-Index) then
  1247. begin
  1248. Dec(Index);
  1249. fpc_pchar_ansistr_intern_charmove(pchar(S),Index+Size,S,Index,LS-Index-Size+1);
  1250. end;
  1251. Setlength(S,LS-Size);
  1252. end;
  1253. Procedure {$ifdef VER3_0}Insert{$else}fpc_ansistr_insert{$endif}(Const Source : RawByteString; Var S : RawByteString; Index : SizeInt);
  1254. var
  1255. Temp : RawByteString;
  1256. LS : SizeInt;
  1257. cp : TSystemCodePage;
  1258. begin
  1259. If Length(Source)=0 then
  1260. exit;
  1261. if index <= 0 then
  1262. index := 1;
  1263. Ls:=Length(S);
  1264. if index > LS then
  1265. index := LS+1;
  1266. Dec(Index);
  1267. SetLength(Temp,Length(Source)+LS);
  1268. if length(S)<>0 then
  1269. cp:=TranslatePlaceholderCP(StringCodePage(S))
  1270. else
  1271. cp:=TranslatePlaceholderCP(StringCodePage(Source));
  1272. SetCodePage(Temp,cp,false);
  1273. If Index>0 then
  1274. fpc_pchar_ansistr_intern_charmove(pchar(S),0,Temp,0,Index);
  1275. fpc_pchar_ansistr_intern_charmove(pchar(Source),0,Temp,Index,Length(Source));
  1276. If (LS-Index)>0 then
  1277. fpc_pchar_ansistr_intern_charmove(pchar(S),Index,Temp,Length(Source)+Index,LS-Index);
  1278. S:=Temp;
  1279. end;
  1280. {$ifndef FPC_HAS_ANSISTR_OF_CHAR}
  1281. {$define FPC_HAS_ANSISTR_OF_CHAR}
  1282. Function StringOfChar(c : Ansichar;l : SizeInt) : AnsiString;
  1283. begin
  1284. SetLength(StringOfChar,l);
  1285. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1286. end;
  1287. {$endif FPC_HAS_ANSISTR_OF_CHAR}
  1288. {$ifdef FPC_HAS_CPSTRING}
  1289. Procedure fpc_setstring_ansistr_pansichar(out S : RawByteString; Buf : PAnsiChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1290. {$else}
  1291. Procedure SetString(out S : AnsiString; Buf : PAnsiChar; Len : SizeInt);
  1292. {$endif}
  1293. begin
  1294. SetLength(S,Len);
  1295. {$ifdef FPC_HAS_CPSTRING}
  1296. SetCodePage(S,cp,false);
  1297. {$endif}
  1298. If (Buf<>Nil) then
  1299. fpc_pchar_ansistr_intern_charmove(Buf,0,S,0,Len);
  1300. end;
  1301. {$ifdef FPC_HAS_CPSTRING}
  1302. Procedure fpc_setstring_ansistr_pwidechar(out S : RawByteString; Buf : PWideChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1303. {$else}
  1304. Procedure SetString(out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1305. {$endif}
  1306. begin
  1307. if (Buf<>nil) and (Len>0) then
  1308. widestringmanager.Wide2AnsiMoveProc(Buf,S,{$ifdef FPC_HAS_CPSTRING}cp{$else}DefaultSystemCodePage{$endif},Len)
  1309. else
  1310. begin
  1311. SetLength(S, Len);
  1312. {$ifdef FPC_HAS_CPSTRING}
  1313. SetCodePage(S,cp,false);
  1314. {$endif}
  1315. end;
  1316. end;
  1317. {$ifndef FPC_HAS_UPCASE_ANSISTR}
  1318. {$define FPC_HAS_UPCASE_ANSISTR}
  1319. function upcase(const s : ansistring) : ansistring;
  1320. var
  1321. i : SizeInt;
  1322. begin
  1323. Setlength(result,length(s));
  1324. for i := 1 to length (s) do
  1325. result[i] := upcase(s[i]);
  1326. end;
  1327. {$endif FPC_HAS_UPCASE_ANSISTR}
  1328. {$ifndef FPC_HAS_LOWERCASE_ANSISTR}
  1329. {$define FPC_HAS_LOWERCASE_ANSISTR}
  1330. function lowercase(const s : ansistring) : ansistring;
  1331. var
  1332. i : SizeInt;
  1333. begin
  1334. Setlength(result,length(s));
  1335. for i := 1 to length (s) do
  1336. result[i] := lowercase(s[i]);
  1337. end;
  1338. {$endif FPC_HAS_LOWERCASE_ANSISTR}
  1339. {$ifndef FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1340. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1341. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1342. begin
  1343. {$ifdef FPC_HAS_CPSTRING}
  1344. if assigned(Pointer(S)) then
  1345. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1346. else
  1347. {$endif FPC_HAS_CPSTRING}
  1348. Result:=DefaultSystemCodePage;
  1349. end;
  1350. {$endif FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1351. {$ifndef FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1352. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1353. function StringElementSize(const S: RawByteString): Word; overload;
  1354. begin
  1355. if assigned(Pointer(S)) then
  1356. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1357. else
  1358. Result:=SizeOf(AnsiChar);
  1359. end;
  1360. {$endif FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1361. {$ifndef FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1362. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1363. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1364. begin
  1365. if assigned(Pointer(S)) then
  1366. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1367. else
  1368. Result:=0;
  1369. end;
  1370. {$endif FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1371. {$ifndef FPC_HAS_ANSISTR_SETCODEPAGE}
  1372. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  1373. procedure InternalSetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1374. begin
  1375. if Convert then
  1376. begin
  1377. {$ifdef FPC_HAS_CPSTRING}
  1378. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1379. {$else FPC_HAS_CPSTRING}
  1380. UniqueString(s);
  1381. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1382. {$endif FPC_HAS_CPSTRING}
  1383. end
  1384. else
  1385. begin
  1386. UniqueString(s);
  1387. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1388. end;
  1389. end;
  1390. { use this wrapper for the simple case to avoid the generation of a temp. ansistring which causes
  1391. extra exception frames }
  1392. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1393. var
  1394. OrgCodePage,
  1395. TranslatedCodePage,
  1396. TranslatedCurrentCodePage: TSystemCodePage;
  1397. begin
  1398. if (S='') then
  1399. exit;
  1400. { if the codepage are identical, we don't have to do anything (even if the
  1401. string has multiple references) }
  1402. OrgCodePage:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage;
  1403. if OrgCodePage=CodePage then
  1404. exit;
  1405. { if we're just replacing a placeholder code page with its actual value or
  1406. vice versa, we don't have to perform any conversion }
  1407. TranslatedCurrentCodePage:=TranslatePlaceholderCP(OrgCodePage);
  1408. TranslatedCodePage:=TranslatePlaceholderCP(CodePage);
  1409. Convert:=Convert and
  1410. (TranslatedCurrentCodePage<>TranslatedCodePage);
  1411. if not Convert and (PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref=1) then
  1412. PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage:=CodePage
  1413. else
  1414. InternalSetCodePage(S,CodePage,Convert);
  1415. end;
  1416. {$endif FPC_HAS_ANSISTR_SETCODEPAGE}
  1417. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1418. begin
  1419. DefaultSystemCodePage:=CodePage;
  1420. end;
  1421. procedure SetMultiByteFileSystemCodePage(CodePage: TSystemCodePage);
  1422. begin
  1423. DefaultFileSystemCodePage:=CodePage;
  1424. end;
  1425. procedure SetMultiByteRTLFileSystemCodePage(CodePage: TSystemCodePage);
  1426. begin
  1427. DefaultRTLFileSystemCodePage:=CodePage;
  1428. end;