astrings.inc 39 KB

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