astrings.inc 36 KB

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