astrings.inc 42 KB

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