astrings.inc 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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 : Pointer;
  722. begin
  723. ResultAddress:=Nil;
  724. if Index < 1 then
  725. Index := 1;
  726. dec(index);
  727. Lim:=Length(S)-Index; { Cannot overflow as both Length(S) and Index are non-negative. }
  728. if Size>Lim then
  729. Size:=Lim;
  730. If Size>0 then
  731. begin
  732. ResultAddress:=GetMem(Size+(AnsiFirstOff+1))+AnsiFirstOff;
  733. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  734. PAnsiRec(ResultAddress-AnsiFirstOff)^.ElementSize:=1;
  735. PAnsiRec(ResultAddress-AnsiFirstOff)^.Ref:=1;
  736. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  737. Move(Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  738. PByte(ResultAddress)[Size]:=0;
  739. end;
  740. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  741. Pointer(fpc_ansistr_Copy):=ResultAddress;
  742. end;
  743. {$endif FPC_HAS_ANSISTR_COPY}
  744. {$ifndef FPC_HAS_POS_SHORTSTR_ANSISTR}
  745. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  746. Function Pos(Const Substr : ShortString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  747. var
  748. nsource : SizeInt;
  749. begin
  750. nsource:=Length(Source);
  751. result:=0;
  752. dec(Offset);
  753. if SizeUint(Offset)<SizeUint(nsource) then { (Offset >= 0) and (Offset < nsource) }
  754. result:=MemPos(PByte(@Substr[1]),length(Substr),PByte(Source)+Offset,nsource-Offset)+1;
  755. if result>0 then
  756. inc(result,Offset);
  757. end;
  758. {$endif FPC_HAS_POS_SHORTSTR_ANSISTR}
  759. {$ifndef FPC_HAS_POS_ANSISTR_ANSISTR}
  760. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  761. Function Pos(Const Substr : RawByteString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  762. var
  763. nsource : SizeInt;
  764. begin
  765. nsource:=Length(Source);
  766. result:=0;
  767. dec(Offset);
  768. if SizeUint(Offset)<SizeUint(nsource) then { (Offset >= 0) and (Offset < nsource) }
  769. result:=MemPos(PByte(Substr),length(Substr),PByte(Source)+Offset,nsource-Offset)+1;
  770. if result>0 then
  771. inc(result,Offset);
  772. end;
  773. {$endif FPC_HAS_POS_ANSISTR_ANSISTR}
  774. {$ifndef FPC_HAS_POS_ANSICHAR_ANSISTR}
  775. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  776. { Faster version for a AnsiChar alone. Must be implemented because }
  777. { pos(c: AnsiChar; const s: shortstring) also exists, so otherwise }
  778. { using pos(AnsiChar,pansichar) will always call the shortstring version }
  779. { (exact match for first argument), also with $h+ (JM) }
  780. Function Pos(c : AnsiChar; Const s : RawByteString; Offset : Sizeint = 1) : SizeInt;
  781. var
  782. ns,idx: SizeInt;
  783. begin
  784. pos:=0;
  785. ns:=length(s);
  786. if (Offset>0) and (Offset<=ns) then
  787. begin
  788. idx:=IndexByte(s[Offset],ns-Offset+1,byte(c));
  789. if idx>=0 then
  790. pos:=Offset+idx;
  791. end;
  792. end;
  793. {$endif FPC_HAS_POS_ANSICHAR_ANSISTR}
  794. {$ifndef FPUNONE}
  795. Function fpc_Val_Real_AnsiStr(Const S : RawByteString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  796. Var
  797. SS : ShortString;
  798. begin
  799. fpc_Val_Real_AnsiStr := 0;
  800. if length(S) > 255 then
  801. code := 256
  802. else
  803. begin
  804. SS := S;
  805. Val(SS,fpc_Val_Real_AnsiStr,code);
  806. end;
  807. end;
  808. {$endif}
  809. Function fpc_Val_Currency_AnsiStr(Const S : RawByteString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  810. Var
  811. SS : ShortString;
  812. begin
  813. if length(S) > 255 then
  814. begin
  815. fpc_Val_Currency_AnsiStr := 0;
  816. code := 256;
  817. end
  818. else
  819. begin
  820. SS := S;
  821. Val(SS,fpc_Val_Currency_AnsiStr,code);
  822. end;
  823. end;
  824. 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;
  825. Var
  826. SS : ShortString;
  827. begin
  828. fpc_Val_UInt_AnsiStr := 0;
  829. if length(S) > 255 then
  830. code := 256
  831. else
  832. begin
  833. SS := S;
  834. Val(SS,fpc_Val_UInt_AnsiStr,code);
  835. end;
  836. end;
  837. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : RawByteString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  838. Var
  839. SS : ShortString;
  840. begin
  841. fpc_Val_SInt_AnsiStr:=0;
  842. if length(S)>255 then
  843. code:=256
  844. else
  845. begin
  846. SS := S;
  847. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  848. end;
  849. end;
  850. {$ifndef CPU64}
  851. Function fpc_Val_qword_AnsiStr (Const S : RawByteString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  852. Var
  853. SS : ShortString;
  854. begin
  855. fpc_Val_qword_AnsiStr:=0;
  856. if length(S)>255 then
  857. code:=256
  858. else
  859. begin
  860. SS := S;
  861. Val(SS,fpc_Val_qword_AnsiStr,Code);
  862. end;
  863. end;
  864. Function fpc_Val_int64_AnsiStr (Const S : RawByteString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  865. Var
  866. SS : ShortString;
  867. begin
  868. fpc_Val_int64_AnsiStr:=0;
  869. if length(S)>255 then
  870. code:=256
  871. else
  872. begin
  873. SS := s;
  874. Val(SS,fpc_Val_int64_AnsiStr,Code);
  875. end;
  876. end;
  877. {$endif CPU64}
  878. {$if defined(CPU16) or defined(CPU8)}
  879. Function fpc_Val_longword_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_LONGWORD_ANSISTR']; compilerproc;
  880. Var
  881. SS : ShortString;
  882. begin
  883. fpc_Val_longword_AnsiStr:=0;
  884. if length(S)>255 then
  885. code:=256
  886. else
  887. begin
  888. SS := S;
  889. Val(SS,fpc_Val_longword_AnsiStr,Code);
  890. end;
  891. end;
  892. Function fpc_Val_longint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_LONGINT_ANSISTR']; compilerproc;
  893. Var
  894. SS : ShortString;
  895. begin
  896. fpc_Val_longint_AnsiStr:=0;
  897. if length(S)>255 then
  898. code:=256
  899. else
  900. begin
  901. SS := s;
  902. Val(SS,fpc_Val_longint_AnsiStr,Code);
  903. end;
  904. end;
  905. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): word; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  906. Var
  907. SS : ShortString;
  908. begin
  909. fpc_Val_word_AnsiStr:=0;
  910. if length(S)>255 then
  911. code:=256
  912. else
  913. begin
  914. SS := S;
  915. Val(SS,fpc_Val_word_AnsiStr,Code);
  916. end;
  917. end;
  918. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): smallint; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  919. Var
  920. SS : ShortString;
  921. begin
  922. fpc_Val_smallint_AnsiStr:=0;
  923. if length(S)>255 then
  924. code:=256
  925. else
  926. begin
  927. SS := s;
  928. Val(SS,fpc_Val_smallint_AnsiStr,Code);
  929. end;
  930. end;
  931. {$endif CPU16 or CPU8}
  932. {$if defined(CPU8)}
  933. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  934. Var
  935. SS : ShortString;
  936. begin
  937. fpc_Val_longword_AnsiStr:=0;
  938. if length(S)>255 then
  939. code:=256
  940. else
  941. begin
  942. SS := S;
  943. Val(SS,fpc_Val_longword_AnsiStr,Code);
  944. end;
  945. end;
  946. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  947. Var
  948. SS : ShortString;
  949. begin
  950. fpc_Val_longint_AnsiStr:=0;
  951. if length(S)>255 then
  952. code:=256
  953. else
  954. begin
  955. SS := s;
  956. Val(SS,fpc_Val_longint_AnsiStr,Code);
  957. end;
  958. end;
  959. {$endif CPU8}
  960. {$ifndef FPUNONE}
  961. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : RawByteString;cp : TSystemCodePage);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; inline;
  962. var
  963. ss: ShortString;
  964. begin
  965. str_real(len,fr,d,treal_type(rt),ss);
  966. s:=ss;
  967. SetCodePage(s,cp,false);
  968. end;
  969. {$endif}
  970. {$ifndef FPC_STR_ENUM_INTERN}
  971. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:RawByteString;cp : TSystemCodePage);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; inline;
  972. var ss:shortstring;
  973. begin
  974. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  975. s:=ss;
  976. SetCodePage(s,cp,false);
  977. end;
  978. {$endif FPC_STR_ENUM_INTERN}
  979. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:RawByteString;cp : TSystemCodePage);[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; inline;
  980. var
  981. ss:shortstring;
  982. begin
  983. fpc_shortstr_bool(b,len,ss);
  984. s:=ss;
  985. SetCodePage(s,cp,false);
  986. end;
  987. {$ifndef FPC_STR_ENUM_INTERN}
  988. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:RawByteString;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  989. begin
  990. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  991. end;
  992. {$endif FPC_STR_ENUM_INTERN}
  993. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : RawByteString;cp : TSystemCodePage);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; inline;
  994. var
  995. ss: ShortString;
  996. begin
  997. str(c:len:fr,ss);
  998. s:=ss;
  999. SetCodePage(s,cp,false);
  1000. end;
  1001. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; inline;
  1002. Var
  1003. SS : ShortString;
  1004. begin
  1005. str(v:Len,SS);
  1006. S:=SS;
  1007. SetCodePage(s,cp,false);
  1008. end;
  1009. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; inline;
  1010. Var
  1011. SS : ShortString;
  1012. begin
  1013. str (v:Len,SS);
  1014. S:=SS;
  1015. SetCodePage(s,cp,false);
  1016. end;
  1017. {$ifndef CPU64}
  1018. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; inline;
  1019. Var
  1020. SS : ShortString;
  1021. begin
  1022. str(v:Len,SS);
  1023. S:=SS;
  1024. SetCodePage(s,cp,false);
  1025. end;
  1026. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; inline;
  1027. Var
  1028. SS : ShortString;
  1029. begin
  1030. str (v:Len,SS);
  1031. S:=SS;
  1032. SetCodePage(s,cp,false);
  1033. end;
  1034. {$endif CPU64}
  1035. {$if defined(CPU16) or defined(CPU8)}
  1036. Procedure fpc_AnsiStr_LongWord(v : LongWord;Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_LONGWORD']; compilerproc; inline;
  1037. Var
  1038. SS : ShortString;
  1039. begin
  1040. str(v:Len,SS);
  1041. S:=SS;
  1042. SetCodePage(s,cp,false);
  1043. end;
  1044. Procedure fpc_AnsiStr_LongInt(v : LongInt; Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_LONGINT']; compilerproc; inline;
  1045. Var
  1046. SS : ShortString;
  1047. begin
  1048. str (v:Len,SS);
  1049. S:=SS;
  1050. SetCodePage(s,cp,false);
  1051. end;
  1052. Procedure fpc_AnsiStr_Word(v : Word;Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_WORD']; 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_SmallInt(v : SmallInt; Len : SizeInt; out S : RawByteString;cp : TSystemCodePage);[Public,Alias : 'FPC_ANSISTR_SMALLINT']; 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 CPU16 or CPU8}
  1069. Procedure fpc_ansistr_delete(Var S : RawByteString; Index,Size: SizeInt);
  1070. Var
  1071. LS : SizeInt;
  1072. begin
  1073. ls:=Length(S);
  1074. If (Index>LS) or (Index<=0) or (Size<=0) then
  1075. exit;
  1076. UniqueString(S);
  1077. If (Size>LS-Index) then // Size+Index gives overflow ??
  1078. Size:=LS-Index+1;
  1079. If (Size<=LS-Index) then
  1080. begin
  1081. Dec(Index);
  1082. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S),Index+Size,S,Index,LS-Index-Size+1);
  1083. end;
  1084. Setlength(S,LS-Size);
  1085. end;
  1086. Procedure fpc_ansistr_insert(Const Source : RawByteString; Var S : RawByteString; Index : SizeInt);
  1087. var
  1088. LS,LSource : SizeInt;
  1089. {$ifdef jvm}
  1090. Temp : RawByteString;
  1091. {$else}
  1092. selfinsert : boolean;
  1093. srcp : PAnsiChar;
  1094. {$endif}
  1095. begin
  1096. If Source='' then
  1097. exit;
  1098. if S='' then
  1099. begin
  1100. S:=Source;
  1101. exit;
  1102. end;
  1103. LSource:={$ifdef jvm}Length(Source){$else}PAnsiRec(Pointer(Source)-AnsiFirstOff)^.Len{$endif};
  1104. LS:={$ifdef jvm}Length(S){$else}PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len{$endif};
  1105. if index < 1 then
  1106. index := 1;
  1107. Dec(Index);
  1108. if index > LS then
  1109. index := LS;
  1110. {$ifdef jvm}
  1111. SetLength(Temp,LSource+LS);
  1112. SetCodePage(Temp,StringCodePage(S),false);
  1113. If Index>0 then
  1114. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S),0,Temp,0,Index);
  1115. fpc_pchar_ansistr_intern_charmove(PAnsiChar(Source),0,Temp,Index,LSource);
  1116. If (LS-Index)>0 then
  1117. fpc_pchar_ansistr_intern_charmove(PAnsiChar(S),Index,Temp,LSource+Index,LS-Index);
  1118. S:=Temp;
  1119. {$else}
  1120. selfinsert:=Pointer(Source)=Pointer(S);
  1121. SetLength(S,LS+LSource);
  1122. Move(PAnsiChar(Pointer(S))[Index],PAnsiChar(Pointer(S))[Index+LSource],(LS-Index)*SizeOf(AnsiChar));
  1123. srcp:=Pointer(Source);
  1124. if selfinsert then
  1125. srcp:=Pointer(S);
  1126. Move(srcp^,PAnsiChar(Pointer(S))[Index],LSource*SizeOf(AnsiChar));
  1127. {$endif}
  1128. end;
  1129. {$ifndef FPC_HAS_ANSISTR_OF_CHAR}
  1130. {$define FPC_HAS_ANSISTR_OF_CHAR}
  1131. Function StringOfChar(c : Ansichar;l : SizeInt) : AnsiString;
  1132. begin
  1133. SetLength(StringOfChar,l);
  1134. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1135. end;
  1136. {$endif FPC_HAS_ANSISTR_OF_CHAR}
  1137. Procedure fpc_setstring_ansistr_pansichar(out S : RawByteString; Buf : PAnsiChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1138. begin
  1139. SetLength(S,Len);
  1140. SetCodePage(S,cp,false);
  1141. If (Buf<>Nil) then
  1142. fpc_pchar_ansistr_intern_charmove(Buf,0,S,0,Len);
  1143. end;
  1144. Procedure fpc_setstring_ansistr_pwidechar(out S : RawByteString; Buf : PWideChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1145. begin
  1146. cp:=TranslatePlaceholderCP(cp);
  1147. if (Buf<>nil) and (Len>0) then
  1148. widestringmanager.Wide2AnsiMoveProc(Buf,S,cp,Len)
  1149. else
  1150. begin
  1151. SetLength(S, Len);
  1152. SetCodePage(S,cp,false);
  1153. end;
  1154. end;
  1155. {$ifndef FPC_HAS_UPCASE_ANSISTR}
  1156. {$define FPC_HAS_UPCASE_ANSISTR}
  1157. function upcase(const s : ansistring) : ansistring;
  1158. var
  1159. i : SizeInt;
  1160. begin
  1161. Setlength(result,length(s));
  1162. for i := 1 to length (s) do
  1163. result[i] := upcase(s[i]);
  1164. end;
  1165. {$endif FPC_HAS_UPCASE_ANSISTR}
  1166. {$ifndef FPC_HAS_LOWERCASE_ANSISTR}
  1167. {$define FPC_HAS_LOWERCASE_ANSISTR}
  1168. function lowercase(const s : ansistring) : ansistring;
  1169. var
  1170. i : SizeInt;
  1171. begin
  1172. Setlength(result,length(s));
  1173. for i := 1 to length (s) do
  1174. result[i] := lowercase(s[i]);
  1175. end;
  1176. {$endif FPC_HAS_LOWERCASE_ANSISTR}
  1177. {$ifndef FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1178. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1179. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1180. begin
  1181. if assigned(Pointer(S)) then
  1182. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1183. else
  1184. Result:=DefaultSystemCodePage;
  1185. end;
  1186. {$endif FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1187. {$ifndef FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1188. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1189. function StringElementSize(const S: RawByteString): Word; overload;
  1190. begin
  1191. if assigned(Pointer(S)) then
  1192. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1193. else
  1194. Result:=SizeOf(AnsiChar);
  1195. end;
  1196. {$endif FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1197. {$ifndef FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1198. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1199. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1200. begin
  1201. if assigned(Pointer(S)) then
  1202. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1203. else
  1204. Result:=0;
  1205. end;
  1206. {$endif FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1207. {$ifndef FPC_HAS_ANSISTR_SETCODEPAGE}
  1208. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  1209. procedure InternalSetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1210. begin
  1211. if Convert then
  1212. begin
  1213. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1214. end
  1215. else
  1216. begin
  1217. UniqueString(s);
  1218. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1219. end;
  1220. end;
  1221. { use this wrapper for the simple case to avoid the generation of a temp. ansistring which causes
  1222. extra exception frames }
  1223. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1224. var
  1225. OrgCodePage,
  1226. TranslatedCodePage,
  1227. TranslatedCurrentCodePage: TSystemCodePage;
  1228. begin
  1229. if Length(S)=0 then
  1230. exit;
  1231. { if the codepage are identical, we don't have to do anything (even if the
  1232. string has multiple references) }
  1233. OrgCodePage:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage;
  1234. if OrgCodePage=CodePage then
  1235. exit;
  1236. { if we're just replacing a placeholder code page with its actual value or
  1237. vice versa, we don't have to perform any conversion }
  1238. TranslatedCurrentCodePage:=TranslatePlaceholderCP(OrgCodePage);
  1239. TranslatedCodePage:=TranslatePlaceholderCP(CodePage);
  1240. Convert:=Convert and
  1241. (TranslatedCurrentCodePage<>TranslatedCodePage);
  1242. if not Convert and (PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref=1) then
  1243. PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage:=CodePage
  1244. else
  1245. InternalSetCodePage(S,CodePage,Convert);
  1246. end;
  1247. {$endif FPC_HAS_ANSISTR_SETCODEPAGE}
  1248. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1249. begin
  1250. DefaultSystemCodePage:=CodePage;
  1251. end;
  1252. procedure SetMultiByteFileSystemCodePage(CodePage: TSystemCodePage);
  1253. begin
  1254. DefaultFileSystemCodePage:=CodePage;
  1255. end;
  1256. procedure SetMultiByteRTLFileSystemCodePage(CodePage: TSystemCodePage);
  1257. begin
  1258. DefaultRTLFileSystemCodePage:=CodePage;
  1259. end;