astrings.inc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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. Procedure fpc_AnsiStr_ZeroBased_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_ZEROBASED_RANGECHECK']; compilerproc;
  700. begin
  701. if (p=nil) or (index>=PAnsiRec(p-AnsiFirstOff)^.Len) or (Index<0) then
  702. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  703. end;
  704. {$endif FPC_HAS_ANSISTR_RANGECHECK}
  705. {$ifndef FPC_HAS_ANSISTR_SETLENGTH}
  706. {$define FPC_HAS_ANSISTR_SETLENGTH}
  707. 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;
  708. {
  709. Sets The length of string S to L.
  710. Makes sure S is unique, and contains enough room.
  711. }
  712. Var
  713. Temp : Pointer;
  714. lens, lena : SizeInt;
  715. begin
  716. if (l>0) then
  717. begin
  718. if Pointer(S)=nil then
  719. begin
  720. Pointer(S):=NewAnsiString(L);
  721. {$ifdef FPC_HAS_CPSTRING}
  722. cp:=TranslatePlaceholderCP(cp);
  723. PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=cp;
  724. {$else}
  725. PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=DefaultSystemCodePage;
  726. {$endif FPC_HAS_CPSTRING}
  727. end
  728. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  729. begin
  730. Temp:=Pointer(s)-AnsiFirstOff;
  731. lens:=MemSize(Temp);
  732. lena:=AnsiFirstOff+L+sizeof(AnsiChar);
  733. { allow shrinking string if that saves at least half of current size }
  734. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  735. begin
  736. reallocmem(Temp,lena);
  737. Pointer(S):=Temp+AnsiFirstOff;
  738. end;
  739. end
  740. else
  741. begin
  742. { Reallocation is needed... }
  743. Temp:=NewAnsiString(L);
  744. PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  745. lens:=length(s);
  746. if l<=lens then
  747. lens:=l
  748. else
  749. { Also copy a trailing implicit #0 of the original string
  750. to the new larger string }
  751. Inc(lens);
  752. Move(Pointer(S)^,Temp^,lens);
  753. fpc_ansistr_decr_ref(Pointer(s));
  754. Pointer(S):=Temp;
  755. end;
  756. { Force nil termination in case it gets shorter }
  757. PByte(Pointer(S)+l)^:=0;
  758. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  759. end
  760. else { length=0, deallocate the string }
  761. fpc_ansistr_decr_ref (Pointer(S));
  762. end;
  763. {$endif FPC_HAS_ANSISTR_SETLENGTH}
  764. {$ifdef EXTRAANSISHORT}
  765. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  766. {
  767. Compares a AnsiString with a ShortString;
  768. The result is
  769. <0 if S1<S2
  770. 0 if S1=S2
  771. >0 if S1>S2
  772. }
  773. Var
  774. i,MaxI,Temp : SizeInt;
  775. begin
  776. Temp:=0;
  777. i:=0;
  778. MaxI:=Length(AnsiString(S1));
  779. if MaxI>byte(S2[0]) then
  780. MaxI:=Byte(S2[0]);
  781. While (i<MaxI) and (Temp=0) do
  782. begin
  783. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  784. inc(i);
  785. end;
  786. AnsiStr_ShortStr_Compare:=Temp;
  787. end;
  788. {$endif EXTRAANSISHORT}
  789. {*****************************************************************************
  790. Public functions, In interface.
  791. *****************************************************************************}
  792. {$ifndef FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  793. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  794. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  795. Var
  796. SNew : Pointer;
  797. L : SizeInt;
  798. begin
  799. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  800. SNew:=NewAnsiString (L);
  801. Move (Pointer(S)^,SNew^,L+1);
  802. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  803. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  804. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  805. pointer(S):=SNew;
  806. pointer(result):=SNew;
  807. end;
  808. {$endif FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  809. Function fpc_ansistr_Unique_func(Var S : RawByteString): Pointer; external name 'FPC_ANSISTR_UNIQUE';
  810. Procedure UniqueString(var S : RawByteString);{$ifdef FPC_HAS_CPSTRING}rtlproc;{$endif FPC_HAS_CPSTRING}{$ifdef SYSTEMINLINE}inline;{$endif}
  811. begin
  812. fpc_ansistr_Unique_func(S);
  813. end;
  814. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  815. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  816. // MV: inline the basic checks for case that S is already unique.
  817. // Rest is too complex to inline, so factor that out as a call.
  818. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; inline;
  819. {
  820. Make sure reference count of S is 1,
  821. using copy-on-write semantics.
  822. }
  823. begin
  824. pointer(result) := pointer(s);
  825. If Pointer(S)=Nil then
  826. exit;
  827. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  828. result:=fpc_truely_ansistr_unique(s);
  829. end;
  830. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  831. {$ifndef FPC_HAS_ANSISTR_COPY}
  832. {$define FPC_HAS_ANSISTR_COPY}
  833. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt): RawByteString;compilerproc;
  834. var
  835. ResultAddress : Pointer;
  836. begin
  837. ResultAddress:=Nil;
  838. dec(index);
  839. if Index < 0 then
  840. Index := 0;
  841. { Check Size. Accounts for Zero-length S, the double check is needed because
  842. Size can be maxint and will get <0 when adding index }
  843. if (Size>Length(S)) or
  844. (Index+Size>Length(S)) then
  845. Size:=Length(S)-Index;
  846. If Size>0 then
  847. begin
  848. ResultAddress:=NewAnsiString(Size);
  849. if ResultAddress<>Nil then
  850. begin
  851. Move(Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  852. PByte(ResultAddress+Size)^:=0;
  853. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  854. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  855. end;
  856. end;
  857. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  858. Pointer(fpc_ansistr_Copy):=ResultAddress;
  859. end;
  860. {$endif FPC_HAS_ANSISTR_COPY}
  861. {$ifndef FPC_HAS_POS_SHORTSTR_ANSISTR}
  862. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  863. Function Pos(Const Substr : ShortString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  864. var
  865. i,MaxLen : SizeInt;
  866. pc : PAnsiChar;
  867. begin
  868. Pos:=0;
  869. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  870. begin
  871. MaxLen:=Length(source)-Length(SubStr);
  872. i:=Offset-1;
  873. pc:=@source[Offset];
  874. while (i<=MaxLen) do
  875. begin
  876. inc(i);
  877. if (SubStr[1]=pc^) and
  878. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  879. begin
  880. Pos:=i;
  881. exit;
  882. end;
  883. inc(pc);
  884. end;
  885. end;
  886. end;
  887. {$endif FPC_HAS_POS_SHORTSTR_ANSISTR}
  888. {$ifndef FPC_HAS_POS_ANSISTR_ANSISTR}
  889. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  890. Function Pos(Const Substr : RawByteString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  891. var
  892. i,MaxLen : SizeInt;
  893. pc : PAnsiChar;
  894. begin
  895. Pos:=0;
  896. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  897. begin
  898. MaxLen:=Length(source)-Length(SubStr);
  899. i:=Offset-1;
  900. pc:=@source[Offset];
  901. while (i<=MaxLen) do
  902. begin
  903. inc(i);
  904. if (SubStr[1]=pc^) and
  905. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  906. begin
  907. Pos:=i;
  908. exit;
  909. end;
  910. inc(pc);
  911. end;
  912. end;
  913. end;
  914. {$endif FPC_HAS_POS_ANSISTR_ANSISTR}
  915. {$ifndef FPC_HAS_POS_ANSICHAR_ANSISTR}
  916. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  917. { Faster version for a char alone. Must be implemented because }
  918. { pos(c: char; const s: shortstring) also exists, so otherwise }
  919. { using pos(char,pchar) will always call the shortstring version }
  920. { (exact match for first argument), also with $h+ (JM) }
  921. Function Pos(c : AnsiChar; Const s : RawByteString; Offset : Sizeint = 1) : SizeInt;
  922. var
  923. i: SizeInt;
  924. pc : PAnsiChar;
  925. begin
  926. Pos:=0;
  927. If (Offset<1) or (Offset>Length(S)) then
  928. exit;
  929. pc:=@s[OffSet];
  930. for i:=Offset to length(s) do
  931. begin
  932. if pc^=c then
  933. begin
  934. pos:=i;
  935. exit;
  936. end;
  937. inc(pc);
  938. end;
  939. pos:=0;
  940. end;
  941. {$endif FPC_HAS_POS_ANSICHAR_ANSISTR}
  942. {$ifndef FPUNONE}
  943. Function fpc_Val_Real_AnsiStr(Const S : RawByteString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  944. Var
  945. SS : String;
  946. begin
  947. fpc_Val_Real_AnsiStr := 0;
  948. if length(S) > 255 then
  949. code := 256
  950. else
  951. begin
  952. SS := S;
  953. Val(SS,fpc_Val_Real_AnsiStr,code);
  954. end;
  955. end;
  956. {$endif}
  957. Function fpc_Val_Currency_AnsiStr(Const S : RawByteString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  958. Var
  959. SS : String;
  960. begin
  961. if length(S) > 255 then
  962. begin
  963. fpc_Val_Currency_AnsiStr := 0;
  964. code := 256;
  965. end
  966. else
  967. begin
  968. SS := S;
  969. Val(SS,fpc_Val_Currency_AnsiStr,code);
  970. end;
  971. end;
  972. Function fpc_Val_UInt_AnsiStr (Const S : RawByteString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  973. Var
  974. SS : ShortString;
  975. begin
  976. fpc_Val_UInt_AnsiStr := 0;
  977. if length(S) > 255 then
  978. code := 256
  979. else
  980. begin
  981. SS := S;
  982. Val(SS,fpc_Val_UInt_AnsiStr,code);
  983. end;
  984. end;
  985. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : RawByteString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  986. Var
  987. SS : ShortString;
  988. begin
  989. fpc_Val_SInt_AnsiStr:=0;
  990. if length(S)>255 then
  991. code:=256
  992. else
  993. begin
  994. SS := S;
  995. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  996. end;
  997. end;
  998. {$ifndef CPU64}
  999. Function fpc_Val_qword_AnsiStr (Const S : RawByteString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  1000. Var
  1001. SS : ShortString;
  1002. begin
  1003. fpc_Val_qword_AnsiStr:=0;
  1004. if length(S)>255 then
  1005. code:=256
  1006. else
  1007. begin
  1008. SS := S;
  1009. Val(SS,fpc_Val_qword_AnsiStr,Code);
  1010. end;
  1011. end;
  1012. Function fpc_Val_int64_AnsiStr (Const S : RawByteString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  1013. Var
  1014. SS : ShortString;
  1015. begin
  1016. fpc_Val_int64_AnsiStr:=0;
  1017. if length(S)>255 then
  1018. code:=256
  1019. else
  1020. begin
  1021. SS := s;
  1022. Val(SS,fpc_Val_int64_AnsiStr,Code);
  1023. end;
  1024. end;
  1025. {$endif CPU64}
  1026. {$if defined(CPU16) or defined(CPU8)}
  1027. Function fpc_Val_longword_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_LONGWORD_ANSISTR']; compilerproc;
  1028. Var
  1029. SS : ShortString;
  1030. begin
  1031. fpc_Val_longword_AnsiStr:=0;
  1032. if length(S)>255 then
  1033. code:=256
  1034. else
  1035. begin
  1036. SS := S;
  1037. Val(SS,fpc_Val_longword_AnsiStr,Code);
  1038. end;
  1039. end;
  1040. Function fpc_Val_longint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_LONGINT_ANSISTR']; compilerproc;
  1041. Var
  1042. SS : ShortString;
  1043. begin
  1044. fpc_Val_longint_AnsiStr:=0;
  1045. if length(S)>255 then
  1046. code:=256
  1047. else
  1048. begin
  1049. SS := s;
  1050. Val(SS,fpc_Val_longint_AnsiStr,Code);
  1051. end;
  1052. end;
  1053. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): word; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  1054. Var
  1055. SS : ShortString;
  1056. begin
  1057. fpc_Val_word_AnsiStr:=0;
  1058. if length(S)>255 then
  1059. code:=256
  1060. else
  1061. begin
  1062. SS := S;
  1063. Val(SS,fpc_Val_word_AnsiStr,Code);
  1064. end;
  1065. end;
  1066. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): smallint; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  1067. Var
  1068. SS : ShortString;
  1069. begin
  1070. fpc_Val_smallint_AnsiStr:=0;
  1071. if length(S)>255 then
  1072. code:=256
  1073. else
  1074. begin
  1075. SS := s;
  1076. Val(SS,fpc_Val_smallint_AnsiStr,Code);
  1077. end;
  1078. end;
  1079. {$endif CPU16 or CPU8}
  1080. {$if defined(CPU8)}
  1081. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  1082. Var
  1083. SS : ShortString;
  1084. begin
  1085. fpc_Val_longword_AnsiStr:=0;
  1086. if length(S)>255 then
  1087. code:=256
  1088. else
  1089. begin
  1090. SS := S;
  1091. Val(SS,fpc_Val_longword_AnsiStr,Code);
  1092. end;
  1093. end;
  1094. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  1095. Var
  1096. SS : ShortString;
  1097. begin
  1098. fpc_Val_longint_AnsiStr:=0;
  1099. if length(S)>255 then
  1100. code:=256
  1101. else
  1102. begin
  1103. SS := s;
  1104. Val(SS,fpc_Val_longint_AnsiStr,Code);
  1105. end;
  1106. end;
  1107. {$endif CPU8}
  1108. {$ifndef FPUNONE}
  1109. 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;
  1110. var
  1111. ss: ShortString;
  1112. begin
  1113. str_real(len,fr,d,treal_type(rt),ss);
  1114. s:=ss;
  1115. {$ifdef FPC_HAS_CPSTRING}
  1116. SetCodePage(s,cp,false);
  1117. {$endif FPC_HAS_CPSTRING}
  1118. end;
  1119. {$endif}
  1120. {$ifndef FPC_STR_ENUM_INTERN}
  1121. 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;
  1122. var ss:shortstring;
  1123. begin
  1124. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1125. s:=ss;
  1126. {$ifdef FPC_HAS_CPSTRING}
  1127. SetCodePage(s,cp,false);
  1128. {$endif FPC_HAS_CPSTRING}
  1129. end;
  1130. {$endif FPC_STR_ENUM_INTERN}
  1131. 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;
  1132. var
  1133. ss:shortstring;
  1134. begin
  1135. fpc_shortstr_bool(b,len,ss);
  1136. s:=ss;
  1137. {$ifdef FPC_HAS_CPSTRING}
  1138. SetCodePage(s,cp,false);
  1139. {$endif FPC_HAS_CPSTRING}
  1140. end;
  1141. {$ifndef FPC_STR_ENUM_INTERN}
  1142. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:RawByteString;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  1143. begin
  1144. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1145. end;
  1146. {$endif FPC_STR_ENUM_INTERN}
  1147. 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;
  1148. var
  1149. ss: ShortString;
  1150. begin
  1151. str(c:len:fr,ss);
  1152. s:=ss;
  1153. {$ifdef FPC_HAS_CPSTRING}
  1154. SetCodePage(s,cp,false);
  1155. {$endif FPC_HAS_CPSTRING}
  1156. end;
  1157. 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;
  1158. Var
  1159. SS : ShortString;
  1160. begin
  1161. str(v:Len,SS);
  1162. S:=SS;
  1163. {$ifdef FPC_HAS_CPSTRING}
  1164. SetCodePage(s,cp,false);
  1165. {$endif FPC_HAS_CPSTRING}
  1166. end;
  1167. 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;
  1168. Var
  1169. SS : ShortString;
  1170. begin
  1171. str (v:Len,SS);
  1172. S:=SS;
  1173. {$ifdef FPC_HAS_CPSTRING}
  1174. SetCodePage(s,cp,false);
  1175. {$endif FPC_HAS_CPSTRING}
  1176. end;
  1177. {$ifndef CPU64}
  1178. 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;
  1179. Var
  1180. SS : ShortString;
  1181. begin
  1182. str(v:Len,SS);
  1183. S:=SS;
  1184. {$ifdef FPC_HAS_CPSTRING}
  1185. SetCodePage(s,cp,false);
  1186. {$endif FPC_HAS_CPSTRING}
  1187. end;
  1188. 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;
  1189. Var
  1190. SS : ShortString;
  1191. begin
  1192. str (v:Len,SS);
  1193. S:=SS;
  1194. {$ifdef FPC_HAS_CPSTRING}
  1195. SetCodePage(s,cp,false);
  1196. {$endif FPC_HAS_CPSTRING}
  1197. end;
  1198. {$endif CPU64}
  1199. {$if defined(CPU16) or defined(CPU8)}
  1200. 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;
  1201. Var
  1202. SS : ShortString;
  1203. begin
  1204. str(v:Len,SS);
  1205. S:=SS;
  1206. {$ifdef FPC_HAS_CPSTRING}
  1207. SetCodePage(s,cp,false);
  1208. {$endif FPC_HAS_CPSTRING}
  1209. end;
  1210. 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;
  1211. Var
  1212. SS : ShortString;
  1213. begin
  1214. str (v:Len,SS);
  1215. S:=SS;
  1216. {$ifdef FPC_HAS_CPSTRING}
  1217. SetCodePage(s,cp,false);
  1218. {$endif FPC_HAS_CPSTRING}
  1219. end;
  1220. 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;
  1221. Var
  1222. SS : ShortString;
  1223. begin
  1224. str(v:Len,SS);
  1225. S:=SS;
  1226. {$ifdef FPC_HAS_CPSTRING}
  1227. SetCodePage(s,cp,false);
  1228. {$endif FPC_HAS_CPSTRING}
  1229. end;
  1230. 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;
  1231. Var
  1232. SS : ShortString;
  1233. begin
  1234. str (v:Len,SS);
  1235. S:=SS;
  1236. {$ifdef FPC_HAS_CPSTRING}
  1237. SetCodePage(s,cp,false);
  1238. {$endif FPC_HAS_CPSTRING}
  1239. end;
  1240. {$endif CPU16 or CPU8}
  1241. Procedure {$ifdef VER3_0}Delete{$else}fpc_ansistr_delete{$endif}(Var S : RawByteString; Index,Size: SizeInt);
  1242. Var
  1243. LS : SizeInt;
  1244. begin
  1245. ls:=Length(S);
  1246. If (Index>LS) or (Index<=0) or (Size<=0) then
  1247. exit;
  1248. UniqueString(S);
  1249. If (Size>LS-Index) then // Size+Index gives overflow ??
  1250. Size:=LS-Index+1;
  1251. If (Size<=LS-Index) then
  1252. begin
  1253. Dec(Index);
  1254. fpc_pchar_ansistr_intern_charmove(pchar(S),Index+Size,S,Index,LS-Index-Size+1);
  1255. end;
  1256. Setlength(S,LS-Size);
  1257. end;
  1258. Procedure {$ifdef VER3_0}Insert{$else}fpc_ansistr_insert{$endif}(Const Source : RawByteString; Var S : RawByteString; Index : SizeInt);
  1259. var
  1260. Temp : RawByteString;
  1261. LS : SizeInt;
  1262. cp : TSystemCodePage;
  1263. begin
  1264. If Length(Source)=0 then
  1265. exit;
  1266. if index <= 0 then
  1267. index := 1;
  1268. Ls:=Length(S);
  1269. if index > LS then
  1270. index := LS+1;
  1271. Dec(Index);
  1272. SetLength(Temp,Length(Source)+LS);
  1273. if length(S)<>0 then
  1274. cp:=TranslatePlaceholderCP(StringCodePage(S))
  1275. else
  1276. cp:=TranslatePlaceholderCP(StringCodePage(Source));
  1277. SetCodePage(Temp,cp,false);
  1278. If Index>0 then
  1279. fpc_pchar_ansistr_intern_charmove(pchar(S),0,Temp,0,Index);
  1280. fpc_pchar_ansistr_intern_charmove(pchar(Source),0,Temp,Index,Length(Source));
  1281. If (LS-Index)>0 then
  1282. fpc_pchar_ansistr_intern_charmove(pchar(S),Index,Temp,Length(Source)+Index,LS-Index);
  1283. S:=Temp;
  1284. end;
  1285. {$ifndef FPC_HAS_ANSISTR_OF_CHAR}
  1286. {$define FPC_HAS_ANSISTR_OF_CHAR}
  1287. Function StringOfChar(c : Ansichar;l : SizeInt) : AnsiString;
  1288. begin
  1289. SetLength(StringOfChar,l);
  1290. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1291. end;
  1292. {$endif FPC_HAS_ANSISTR_OF_CHAR}
  1293. {$ifdef FPC_HAS_CPSTRING}
  1294. Procedure fpc_setstring_ansistr_pansichar(out S : RawByteString; Buf : PAnsiChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1295. {$else}
  1296. Procedure SetString(out S : AnsiString; Buf : PAnsiChar; Len : SizeInt);
  1297. {$endif}
  1298. begin
  1299. SetLength(S,Len);
  1300. {$ifdef FPC_HAS_CPSTRING}
  1301. SetCodePage(S,cp,false);
  1302. {$endif}
  1303. If (Buf<>Nil) then
  1304. fpc_pchar_ansistr_intern_charmove(Buf,0,S,0,Len);
  1305. end;
  1306. {$ifdef FPC_HAS_CPSTRING}
  1307. Procedure fpc_setstring_ansistr_pwidechar(out S : RawByteString; Buf : PWideChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1308. {$else}
  1309. Procedure SetString(out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1310. {$endif}
  1311. begin
  1312. if (Buf<>nil) and (Len>0) then
  1313. widestringmanager.Wide2AnsiMoveProc(Buf,S,{$ifdef FPC_HAS_CPSTRING}cp{$else}DefaultSystemCodePage{$endif},Len)
  1314. else
  1315. begin
  1316. SetLength(S, Len);
  1317. {$ifdef FPC_HAS_CPSTRING}
  1318. SetCodePage(S,cp,false);
  1319. {$endif}
  1320. end;
  1321. end;
  1322. {$ifndef FPC_HAS_UPCASE_ANSISTR}
  1323. {$define FPC_HAS_UPCASE_ANSISTR}
  1324. function upcase(const s : ansistring) : ansistring;
  1325. var
  1326. i : SizeInt;
  1327. begin
  1328. Setlength(result,length(s));
  1329. for i := 1 to length (s) do
  1330. result[i] := upcase(s[i]);
  1331. end;
  1332. {$endif FPC_HAS_UPCASE_ANSISTR}
  1333. {$ifndef FPC_HAS_LOWERCASE_ANSISTR}
  1334. {$define FPC_HAS_LOWERCASE_ANSISTR}
  1335. function lowercase(const s : ansistring) : ansistring;
  1336. var
  1337. i : SizeInt;
  1338. begin
  1339. Setlength(result,length(s));
  1340. for i := 1 to length (s) do
  1341. result[i] := lowercase(s[i]);
  1342. end;
  1343. {$endif FPC_HAS_LOWERCASE_ANSISTR}
  1344. {$ifndef FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1345. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1346. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1347. begin
  1348. {$ifdef FPC_HAS_CPSTRING}
  1349. if assigned(Pointer(S)) then
  1350. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1351. else
  1352. {$endif FPC_HAS_CPSTRING}
  1353. Result:=DefaultSystemCodePage;
  1354. end;
  1355. {$endif FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1356. {$ifndef FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1357. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1358. function StringElementSize(const S: RawByteString): Word; overload;
  1359. begin
  1360. if assigned(Pointer(S)) then
  1361. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1362. else
  1363. Result:=SizeOf(AnsiChar);
  1364. end;
  1365. {$endif FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1366. {$ifndef FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1367. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1368. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1369. begin
  1370. if assigned(Pointer(S)) then
  1371. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1372. else
  1373. Result:=0;
  1374. end;
  1375. {$endif FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1376. {$ifndef FPC_HAS_ANSISTR_SETCODEPAGE}
  1377. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  1378. procedure InternalSetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1379. begin
  1380. if Convert then
  1381. begin
  1382. {$ifdef FPC_HAS_CPSTRING}
  1383. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1384. {$else FPC_HAS_CPSTRING}
  1385. UniqueString(s);
  1386. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1387. {$endif FPC_HAS_CPSTRING}
  1388. end
  1389. else
  1390. begin
  1391. UniqueString(s);
  1392. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1393. end;
  1394. end;
  1395. { use this wrapper for the simple case to avoid the generation of a temp. ansistring which causes
  1396. extra exception frames }
  1397. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1398. var
  1399. OrgCodePage,
  1400. TranslatedCodePage,
  1401. TranslatedCurrentCodePage: TSystemCodePage;
  1402. begin
  1403. if (S='') then
  1404. exit;
  1405. { if the codepage are identical, we don't have to do anything (even if the
  1406. string has multiple references) }
  1407. OrgCodePage:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage;
  1408. if OrgCodePage=CodePage then
  1409. exit;
  1410. { if we're just replacing a placeholder code page with its actual value or
  1411. vice versa, we don't have to perform any conversion }
  1412. TranslatedCurrentCodePage:=TranslatePlaceholderCP(OrgCodePage);
  1413. TranslatedCodePage:=TranslatePlaceholderCP(CodePage);
  1414. Convert:=Convert and
  1415. (TranslatedCurrentCodePage<>TranslatedCodePage);
  1416. if not Convert and (PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref=1) then
  1417. PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage:=CodePage
  1418. else
  1419. InternalSetCodePage(S,CodePage,Convert);
  1420. end;
  1421. {$endif FPC_HAS_ANSISTR_SETCODEPAGE}
  1422. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1423. begin
  1424. DefaultSystemCodePage:=CodePage;
  1425. end;
  1426. procedure SetMultiByteFileSystemCodePage(CodePage: TSystemCodePage);
  1427. begin
  1428. DefaultFileSystemCodePage:=CodePage;
  1429. end;
  1430. procedure SetMultiByteRTLFileSystemCodePage(CodePage: TSystemCodePage);
  1431. begin
  1432. DefaultRTLFileSystemCodePage:=CodePage;
  1433. end;