astrings.inc 42 KB

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