astrings.inc 38 KB

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