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 : Char;
  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 : pchar;
  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:=pchar(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 : pchar;
  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:=pchar(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(pchar(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=$ffff) or (S1CP=0) then
  180. S1CP:=DefaultSystemCodePage;
  181. S2CP:=StringCodePage(S2);
  182. if (S2CP=$ffff) or (S2CP=0) 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=$ffff) or (DestCP=0) 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. SetCodePage(DestS,DestCP,false);
  235. Move(Pointer(S1)^,Pointer(DestS)^,Location);
  236. Move(Pointer(S2)^,(Pointer(DestS)+Location)^,Size+1);
  237. end;
  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=$ffff) or (DestCP=0) 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=$ffff) or (tmpCP=0) 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. begin
  372. result:='';
  373. Size:=Length(S);
  374. if Size>0 then
  375. begin
  376. if (cp=0) then
  377. cp:=DefaultSystemCodePage;
  378. if (StringCodePage(S)=cp) 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:=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. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  395. { the following declaration has exactly the same effect as }
  396. { procedure fpc_AnsiStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer); }
  397. { which is what the old helper was, so we don't need an extra implementation }
  398. { of the old helper (JM) }
  399. function fpc_AnsiStr_To_ShortStr (high_of_res: SizeInt;const S2 : Ansistring): shortstring; [Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  400. {
  401. Converts a AnsiString to a ShortString;
  402. }
  403. Var
  404. Size : SizeInt;
  405. begin
  406. if S2='' then
  407. fpc_AnsiStr_To_ShortStr:=''
  408. else
  409. begin
  410. Size:=Length(S2);
  411. If Size>high_of_res then
  412. Size:=high_of_res;
  413. Move (S2[1],fpc_AnsiStr_To_ShortStr[1],Size);
  414. byte(fpc_AnsiStr_To_ShortStr[0]):=byte(Size);
  415. end;
  416. end;
  417. {$else FPC_STRTOSHORTSTRINGPROC}
  418. procedure fpc_AnsiStr_To_ShortStr (out res: shortstring; const S2 : RawByteString);[Public, alias: 'FPC_ANSISTR_TO_SHORTSTR']; compilerproc;
  419. {
  420. Converts a AnsiString to a ShortString;
  421. }
  422. Var
  423. Size : SizeInt;
  424. begin
  425. if S2='' then
  426. res:=''
  427. else
  428. begin
  429. Size:=Length(S2);
  430. If Size>high(res) then
  431. Size:=high(res);
  432. Move (S2[1],res[1],Size);
  433. byte(res[0]):=byte(Size);
  434. end;
  435. end;
  436. {$endif FPC_STRTOSHORTSTRINGPROC}
  437. Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  438. {
  439. Converts a ShortString to a AnsiString;
  440. }
  441. Var
  442. Size : SizeInt;
  443. {$ifndef FPC_HAS_CPSTRING}
  444. cp : TSystemCodePage;
  445. {$endif FPC_HAS_CPSTRING}
  446. begin
  447. {$ifdef FPC_HAS_CPSTRING}
  448. if (cp=0) then
  449. cp:=DefaultSystemCodePage;
  450. {$else FPC_HAS_CPSTRING}
  451. cp:=DefaultSystemCodePage;
  452. {$endif FPC_HAS_CPSTRING}
  453. Size:=Length(S2);
  454. Setlength (fpc_ShortStr_To_AnsiStr,Size);
  455. if Size>0 then
  456. begin
  457. Move(S2[1],Pointer(fpc_ShortStr_To_AnsiStr)^,Size);
  458. SetCodePage(fpc_ShortStr_To_AnsiStr,cp,False);
  459. end
  460. end;
  461. Function fpc_Char_To_AnsiStr(const c : Char{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  462. {
  463. Converts a Char to a AnsiString;
  464. }
  465. {$ifndef FPC_HAS_CPSTRING}
  466. var
  467. cp : TSystemCodePage;
  468. {$endif FPC_HAS_CPSTRING}
  469. begin
  470. {$ifdef FPC_HAS_CPSTRING}
  471. if (cp=0) then
  472. cp:=DefaultSystemCodePage;
  473. {$else FPC_HAS_CPSTRING}
  474. cp:=DefaultSystemCodePage;
  475. {$endif FPC_HAS_CPSTRING}
  476. Setlength (fpc_Char_To_AnsiStr,1);
  477. PByte(Pointer(fpc_Char_To_AnsiStr))^:=byte(c);
  478. { Terminating Zero }
  479. PByte(Pointer(fpc_Char_To_AnsiStr)+1)^:=0;
  480. SetCodePage(fpc_Char_To_AnsiStr,cp,False);
  481. end;
  482. Function fpc_PChar_To_AnsiStr(const p : pchar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
  483. Var
  484. L : SizeInt;
  485. {$ifndef FPC_HAS_CPSTRING}
  486. cp : TSystemCodePage;
  487. {$endif FPC_HAS_CPSTRING}
  488. begin
  489. if (not assigned(p)) or (p[0]=#0) Then
  490. L := 0
  491. else
  492. l:=IndexChar(p^,-1,#0);
  493. SetLength(fpc_PChar_To_AnsiStr,L);
  494. if L > 0 then
  495. begin
  496. {$ifdef FPC_HAS_CPSTRING}
  497. if (cp=0) then
  498. cp:=DefaultSystemCodePage;
  499. {$else FPC_HAS_CPSTRING}
  500. cp:=DefaultSystemCodePage;
  501. {$endif FPC_HAS_CPSTRING}
  502. Move (P[0],Pointer(fpc_PChar_To_AnsiStr)^,L);
  503. SetCodePage(fpc_PChar_To_AnsiStr,cp,False);
  504. end;
  505. end;
  506. 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;
  507. var
  508. i : SizeInt;
  509. {$ifndef FPC_HAS_CPSTRING}
  510. cp : TSystemCodePage;
  511. {$endif FPC_HAS_CPSTRING}
  512. begin
  513. if (zerobased) then
  514. begin
  515. if (arr[0]=#0) Then
  516. i := 0
  517. else
  518. begin
  519. i:=IndexChar(arr,high(arr)+1,#0);
  520. if i = -1 then
  521. i := high(arr)+1;
  522. end;
  523. end
  524. else
  525. i := high(arr)+1;
  526. SetLength(fpc_CharArray_To_AnsiStr,i);
  527. if i > 0 then
  528. begin
  529. {$ifdef FPC_HAS_CPSTRING}
  530. if (cp=0) then
  531. cp:=DefaultSystemCodePage;
  532. {$else FPC_HAS_CPSTRING}
  533. cp:=DefaultSystemCodePage;
  534. {$endif FPC_HAS_CPSTRING}
  535. Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  536. SetCodePage(fpc_CharArray_To_AnsiStr,cp,False);
  537. end;
  538. end;
  539. {$ifndef FPC_STRTOCHARARRAYPROC}
  540. { note: inside the compiler, the resulttype is modified to be the length }
  541. { of the actual chararray to which we convert (JM) }
  542. function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; [public, alias: 'FPC_ANSISTR_TO_CHARARRAY']; compilerproc;
  543. var
  544. len: SizeInt;
  545. begin
  546. len := length(src);
  547. if len > arraysize then
  548. len := arraysize;
  549. {$push}{$r-}
  550. { make sure we don't try to access element 1 of the ansistring if it's nil }
  551. if len > 0 then
  552. move(src[1],fpc_ansistr_to_chararray[0],len);
  553. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  554. fillchar(fpc_ansistr_to_chararray[len],arraysize-len,0);
  555. {$pop}
  556. end;
  557. {$else ndef FPC_STRTOCHARARRAYPROC}
  558. procedure fpc_ansistr_to_chararray(out res: array of char; const src: RawByteString); compilerproc;
  559. var
  560. len: SizeInt;
  561. begin
  562. len := length(src);
  563. if len > length(res) then
  564. len := length(res);
  565. {$push}{$r-}
  566. { make sure we don't try to access element 1 of the ansistring if it's nil }
  567. if len > 0 then
  568. move(src[1],res[0],len);
  569. { fpc_big_chararray is defined as array[0..0], see compproc.inc why }
  570. fillchar(res[len],length(res)-len,0);
  571. {$pop}
  572. end;
  573. {$endif ndef FPC_STRTOCHARARRAYPROC}
  574. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  575. {
  576. Compares 2 AnsiStrings;
  577. The result is
  578. <0 if S1<S2
  579. 0 if S1=S2
  580. >0 if S1>S2
  581. }
  582. Var
  583. MaxI,Temp : SizeInt;
  584. cp1,cp2 : TSystemCodePage;
  585. r1,r2 : RawByteString;
  586. u1,u2 : UTF8String;
  587. begin
  588. if pointer(S1)=pointer(S2) then
  589. begin
  590. result:=0;
  591. exit;
  592. end;
  593. if (pointer(S1)=nil) then
  594. begin
  595. result:=-Length(S2);
  596. exit;
  597. end;
  598. if (pointer(S2)=nil) then
  599. begin
  600. result:=Length(S1);
  601. exit;
  602. end;
  603. cp1:=StringCodePage(S1);
  604. cp2:=StringCodePage(S2);
  605. if (cp1=cp2) then
  606. begin
  607. Maxi:=Length(S1);
  608. temp:=Length(S2);
  609. If MaxI>Temp then
  610. MaxI:=Temp;
  611. if MaxI>0 then
  612. begin
  613. result:=CompareByte(S1[1],S2[1],MaxI);
  614. if result=0 then
  615. result:=Length(S1)-Length(S2);
  616. end
  617. else
  618. result:=Length(S1)-Length(S2);
  619. end
  620. else
  621. begin
  622. r1:=S1;
  623. if (cp1=$ffff) or (cp1=0) then
  624. SetCodePage(r1,DefaultSystemCodePage,false);
  625. r2:=S2;
  626. if (cp2=$ffff) or (cp2=0) then
  627. SetCodePage(r2,DefaultSystemCodePage,false);
  628. //convert them to utf8 then compare
  629. SetCodePage(r1,65001);
  630. SetCodePage(r2,65001);
  631. Result := fpc_AnsiStr_Compare(r1,r2);
  632. end;
  633. end;
  634. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  635. {
  636. Compares 2 AnsiStrings for equality/inequality only;
  637. The result is
  638. 0 if S1=S2
  639. <>0 if S1<>S2
  640. }
  641. Var
  642. MaxI,Temp : SizeInt;
  643. cp1,cp2 : TSystemCodePage;
  644. r1,r2 : RawByteString;
  645. begin
  646. if pointer(S1)=pointer(S2) then
  647. begin
  648. result:=0;
  649. exit;
  650. end;
  651. cp1:=StringCodePage(S1);
  652. cp2:=StringCodePage(S2);
  653. if (cp1=cp2) then
  654. begin
  655. Maxi:=Length(S1);
  656. temp:=Length(S2);
  657. Result := Maxi - temp;
  658. if Result = 0 then
  659. if MaxI>0 then
  660. result:=CompareByte(S1[1],S2[1],MaxI);
  661. end
  662. else
  663. begin
  664. r1:=S1;
  665. if (cp1=$ffff) or (cp1=0) then
  666. SetCodePage(r1,DefaultSystemCodePage,false);
  667. r2:=S2;
  668. if (cp2=$ffff) or (cp2=0) then
  669. SetCodePage(r2,DefaultSystemCodePage,false);
  670. //convert them to utf8 then compare
  671. SetCodePage(r1,65001);
  672. SetCodePage(r2,65001);
  673. Maxi:=Length(r1);
  674. temp:=Length(r2);
  675. Result := Maxi - temp;
  676. if Result = 0 then
  677. if MaxI>0 then
  678. result:=CompareByte(r1[1],r2[1],MaxI);
  679. end;
  680. end;
  681. {$ifdef VER2_4}
  682. // obsolete but needed for boostrapping with 2.4
  683. Procedure fpc_AnsiStr_CheckZero(p : pointer);[Public,Alias : 'FPC_ANSISTR_CHECKZERO']; compilerproc;
  684. begin
  685. if p=nil then
  686. HandleErrorFrame(201,get_frame);
  687. end;
  688. Procedure fpc_AnsiStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  689. begin
  690. if (index>len) or (Index<1) then
  691. HandleErrorFrame(201,get_frame);
  692. end;
  693. {$else VER2_4}
  694. Procedure fpc_AnsiStr_CheckRange(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  695. begin
  696. if (p=nil) or (index>PAnsiRec(p-AnsiFirstOff)^.Len) or (Index<1) then
  697. HandleErrorFrame(201,get_frame);
  698. end;
  699. {$endif VER2_4}
  700. 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;
  701. {
  702. Sets The length of string S to L.
  703. Makes sure S is unique, and contains enough room.
  704. }
  705. Var
  706. Temp : Pointer;
  707. lens, lena,
  708. movelen : SizeInt;
  709. begin
  710. if (l>0) then
  711. begin
  712. if Pointer(S)=nil then
  713. begin
  714. GetMem(Pointer(S),AnsiRecLen+L);
  715. PAnsiRec(S)^.Ref:=1;
  716. {$ifdef FPC_HAS_CPSTRING}
  717. if (cp=0) then
  718. cp:=DefaultSystemCodePage;
  719. PAnsiRec(S)^.CodePage:=cp;
  720. {$else}
  721. PAnsiRec(S)^.CodePage:=DefaultSystemCodePage;
  722. {$endif FPC_HAS_CPSTRING}
  723. PAnsiRec(S)^.ElementSize:=1;
  724. inc(Pointer(S),AnsiFirstOff);
  725. end
  726. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  727. begin
  728. Dec(Pointer(S),AnsiFirstOff);
  729. lens:=MemSize(Pointer(s));
  730. lena:=AnsiRecLen+L;
  731. { allow shrinking string if that saves at least half of current size }
  732. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  733. reallocmem(pointer(S),AnsiRecLen+L);
  734. Inc(Pointer(S),AnsiFirstOff);
  735. end
  736. else
  737. begin
  738. { Reallocation is needed... }
  739. Temp:=Pointer(NewAnsiString(L));
  740. {$ifdef FPC_HAS_CPSTRING}
  741. PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=cp;
  742. {$endif FPC_HAS_CPSTRING}
  743. { also move terminating null }
  744. lens:=succ(length(s));
  745. if l<lens then
  746. movelen:=l
  747. else
  748. movelen:=lens;
  749. Move(Pointer(S)^,Temp^,movelen);
  750. { ref count dropped to zero in the mean time? }
  751. If (PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref>0) and
  752. declocked(PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref) then
  753. freemem(PAnsiRec(Pointer(s)-AnsiFirstOff));
  754. Pointer(S):=Temp;
  755. end;
  756. { Force nil termination in case it gets shorter }
  757. PByte(Pointer(S)+l)^:=0;
  758. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  759. end
  760. else
  761. begin
  762. { Length=0 }
  763. if Pointer(S)<>nil then
  764. fpc_ansistr_decr_ref (Pointer(S));
  765. Pointer(S):=Nil;
  766. end;
  767. end;
  768. {$ifdef EXTRAANSISHORT}
  769. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  770. {
  771. Compares a AnsiString with a ShortString;
  772. The result is
  773. <0 if S1<S2
  774. 0 if S1=S2
  775. >0 if S1>S2
  776. }
  777. Var
  778. i,MaxI,Temp : SizeInt;
  779. begin
  780. Temp:=0;
  781. i:=0;
  782. MaxI:=Length(AnsiString(S1));
  783. if MaxI>byte(S2[0]) then
  784. MaxI:=Byte(S2[0]);
  785. While (i<MaxI) and (Temp=0) do
  786. begin
  787. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  788. inc(i);
  789. end;
  790. AnsiStr_ShortStr_Compare:=Temp;
  791. end;
  792. {$endif EXTRAANSISHORT}
  793. {*****************************************************************************
  794. Public functions, In interface.
  795. *****************************************************************************}
  796. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  797. Var
  798. SNew : Pointer;
  799. L : SizeInt;
  800. begin
  801. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  802. SNew:=NewAnsiString (L);
  803. Move (Pointer(S)^,SNew^,L+1);
  804. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  805. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  806. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  807. pointer(S):=SNew;
  808. pointer(result):=SNew;
  809. end;
  810. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  811. // MV: inline the basic checks for case that S is already unique.
  812. // Rest is too complex to inline, so factor that out as a call.
  813. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  814. {
  815. Make sure reference count of S is 1,
  816. using copy-on-write semantics.
  817. }
  818. begin
  819. pointer(result) := pointer(s);
  820. If Pointer(S)=Nil then
  821. exit;
  822. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  823. result:=fpc_truely_ansistr_unique(s);
  824. end;
  825. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  826. Procedure fpc_ansistr_append_char(Var S : AnsiString;c : char); [Public,Alias : 'FPC_ANSISTR_APPEND_CHAR']; compilerproc;
  827. begin
  828. SetLength(S,length(S)+1);
  829. // avoid unique call
  830. PChar(Pointer(S)+length(S)-1)^:=c;
  831. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  832. end;
  833. Procedure fpc_ansistr_append_shortstring(Var S : AnsiString;const Str : ShortString); [Public,Alias : 'FPC_ANSISTR_APPEND_SHORTSTRING']; compilerproc;
  834. var
  835. ofs : SizeInt;
  836. begin
  837. if Str='' then
  838. exit;
  839. ofs:=Length(S);
  840. SetLength(S,ofs+length(Str));
  841. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  842. move(Str[1],(pointer(S)+ofs)^,length(Str));
  843. PByte(Pointer(S)+length(S))^:=0; { Terminating Zero }
  844. end;
  845. Procedure fpc_ansistr_append_ansistring(Var S : AnsiString;const Str : AnsiString); [Public,Alias : 'FPC_ANSISTR_APPEND_ANSISTRING']; compilerproc;
  846. var
  847. ofs, strlength: SizeInt;
  848. samestring: boolean;
  849. begin
  850. if Str='' then
  851. exit;
  852. samestring := pointer(s) = pointer(str);
  853. { needed in case s and str are the same string }
  854. strlength := length(str);
  855. ofs:=Length(S);
  856. SetLength(S,ofs+strlength);
  857. { the pbyte cast avoids an unique call which isn't necessary because SetLength was just called }
  858. if not(samestring) then
  859. move(Str[1],(pointer(S)+ofs)^,strlength+1)
  860. else
  861. { the setlength may have relocated the string, so str may no longer be valid }
  862. move(S[1],(pointer(S)+ofs)^,strlength+1)
  863. end;
  864. Function Fpc_Ansistr_Copy (Const S : RawByteString; Index,Size : SizeInt) : AnsiString;compilerproc;
  865. var
  866. ResultAddress : Pointer;
  867. begin
  868. ResultAddress:=Nil;
  869. dec(index);
  870. if Index < 0 then
  871. Index := 0;
  872. { Check Size. Accounts for Zero-length S, the double check is needed because
  873. Size can be maxint and will get <0 when adding index }
  874. if (Size>Length(S)) or
  875. (Index+Size>Length(S)) then
  876. Size:=Length(S)-Index;
  877. If Size>0 then
  878. begin
  879. If Index<0 Then
  880. Index:=0;
  881. ResultAddress:=Pointer(NewAnsiString (Size));
  882. if ResultAddress<>Nil then
  883. begin
  884. Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  885. PByte(ResultAddress+Size)^:=0;
  886. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  887. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  888. end;
  889. end;
  890. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  891. Pointer(fpc_ansistr_Copy):=ResultAddress;
  892. end;
  893. Function Pos (Const Substr : ShortString; Const Source : RawByteString) : SizeInt;
  894. var
  895. i,MaxLen : SizeInt;
  896. pc : pchar;
  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. Function Pos (Const Substr : RawByteString; Const Source : RawByteString) : SizeInt;
  918. var
  919. i,MaxLen : SizeInt;
  920. pc : pchar;
  921. begin
  922. Pos:=0;
  923. if Length(SubStr)>0 then
  924. begin
  925. MaxLen:=Length(source)-Length(SubStr);
  926. i:=0;
  927. pc:=@source[1];
  928. while (i<=MaxLen) do
  929. begin
  930. inc(i);
  931. if (SubStr[1]=pc^) and
  932. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  933. begin
  934. Pos:=i;
  935. exit;
  936. end;
  937. inc(pc);
  938. end;
  939. end;
  940. end;
  941. { Faster version for a char alone. Must be implemented because }
  942. { pos(c: char; const s: shortstring) also exists, so otherwise }
  943. { using pos(char,pchar) will always call the shortstring version }
  944. { (exact match for first argument), also with $h+ (JM) }
  945. Function Pos (c : Char; Const s : RawByteString) : SizeInt;
  946. var
  947. i: SizeInt;
  948. pc : pchar;
  949. begin
  950. pc:=@s[1];
  951. for i:=1 to length(s) do
  952. begin
  953. if pc^=c then
  954. begin
  955. pos:=i;
  956. exit;
  957. end;
  958. inc(pc);
  959. end;
  960. pos:=0;
  961. end;
  962. {$ifndef FPUNONE}
  963. Function fpc_Val_Real_AnsiStr(Const S : AnsiString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  964. Var
  965. SS : String;
  966. begin
  967. fpc_Val_Real_AnsiStr := 0;
  968. if length(S) > 255 then
  969. code := 256
  970. else
  971. begin
  972. SS := S;
  973. Val(SS,fpc_Val_Real_AnsiStr,code);
  974. end;
  975. end;
  976. {$endif}
  977. Function fpc_Val_Currency_AnsiStr(Const S : AnsiString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  978. Var
  979. SS : String;
  980. begin
  981. if length(S) > 255 then
  982. begin
  983. fpc_Val_Currency_AnsiStr := 0;
  984. code := 256;
  985. end
  986. else
  987. begin
  988. SS := S;
  989. Val(SS,fpc_Val_Currency_AnsiStr,code);
  990. end;
  991. end;
  992. Function fpc_Val_UInt_AnsiStr (Const S : AnsiString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  993. Var
  994. SS : ShortString;
  995. begin
  996. fpc_Val_UInt_AnsiStr := 0;
  997. if length(S) > 255 then
  998. code := 256
  999. else
  1000. begin
  1001. SS := S;
  1002. Val(SS,fpc_Val_UInt_AnsiStr,code);
  1003. end;
  1004. end;
  1005. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : AnsiString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  1006. Var
  1007. SS : ShortString;
  1008. begin
  1009. fpc_Val_SInt_AnsiStr:=0;
  1010. if length(S)>255 then
  1011. code:=256
  1012. else
  1013. begin
  1014. SS := S;
  1015. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1016. end;
  1017. end;
  1018. {$ifndef CPU64}
  1019. Function fpc_Val_qword_AnsiStr (Const S : AnsiString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  1020. Var
  1021. SS : ShortString;
  1022. begin
  1023. fpc_Val_qword_AnsiStr:=0;
  1024. if length(S)>255 then
  1025. code:=256
  1026. else
  1027. begin
  1028. SS := S;
  1029. Val(SS,fpc_Val_qword_AnsiStr,Code);
  1030. end;
  1031. end;
  1032. Function fpc_Val_int64_AnsiStr (Const S : AnsiString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  1033. Var
  1034. SS : ShortString;
  1035. begin
  1036. fpc_Val_int64_AnsiStr:=0;
  1037. if length(S)>255 then
  1038. code:=256
  1039. else
  1040. begin
  1041. SS := s;
  1042. Val(SS,fpc_Val_int64_AnsiStr,Code);
  1043. end;
  1044. end;
  1045. {$endif CPU64}
  1046. {$ifndef FPUNONE}
  1047. procedure fpc_AnsiStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_FLOAT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1048. var
  1049. ss: ShortString;
  1050. begin
  1051. str_real(len,fr,d,treal_type(rt),ss);
  1052. s:=ss;
  1053. end;
  1054. {$endif}
  1055. procedure fpc_ansistr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:ansistring);[public,alias:'FPC_ANSISTR_ENUM'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1056. var ss:shortstring;
  1057. begin
  1058. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1059. s:=ss;
  1060. end;
  1061. procedure fpc_ansistr_bool(b : boolean;len:sizeint;out s:ansistring);[public,alias:'FPC_ANSISTR_BOOL'];compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1062. var
  1063. ss:shortstring;
  1064. begin
  1065. fpc_shortstr_bool(b,len,ss);
  1066. s:=ss;
  1067. end;
  1068. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:ansistring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  1069. begin
  1070. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1071. end;
  1072. {$ifdef FPC_HAS_STR_CURRENCY}
  1073. procedure fpc_AnsiStr_Currency(c : currency;len,fr : SizeInt;out s : ansistring);[public,alias:'FPC_ANSISTR_CURRENCY']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1074. var
  1075. ss: ShortString;
  1076. begin
  1077. str(c:len:fr,ss);
  1078. s:=ss;
  1079. end;
  1080. {$endif FPC_HAS_STR_CURRENCY}
  1081. Procedure fpc_AnsiStr_UInt(v : ValUInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALUINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1082. Var
  1083. SS : ShortString;
  1084. begin
  1085. str(v:Len,SS);
  1086. S:=SS;
  1087. end;
  1088. Procedure fpc_AnsiStr_SInt(v : ValSInt;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_VALSINT']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1089. Var
  1090. SS : ShortString;
  1091. begin
  1092. str (v:Len,SS);
  1093. S:=SS;
  1094. end;
  1095. {$ifndef CPU64}
  1096. Procedure fpc_AnsiStr_QWord(v : QWord;Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_QWORD']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1097. Var
  1098. SS : ShortString;
  1099. begin
  1100. str(v:Len,SS);
  1101. S:=SS;
  1102. end;
  1103. Procedure fpc_AnsiStr_Int64(v : Int64; Len : SizeInt; out S : AnsiString);[Public,Alias : 'FPC_ANSISTR_INT64']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
  1104. Var
  1105. SS : ShortString;
  1106. begin
  1107. str (v:Len,SS);
  1108. S:=SS;
  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. begin
  1133. If Length(Source)=0 then
  1134. exit;
  1135. if index <= 0 then
  1136. index := 1;
  1137. Ls:=Length(S);
  1138. if index > LS then
  1139. index := LS+1;
  1140. Dec(Index);
  1141. Pointer(Temp) := NewAnsiString(Length(Source)+LS);
  1142. SetLength(Temp,Length(Source)+LS);
  1143. If Index>0 then
  1144. move (Pointer(S)^,Pointer(Temp)^,Index);
  1145. Move (Pointer(Source)^,PByte(Temp)[Index],Length(Source));
  1146. If (LS-Index)>0 then
  1147. Move(PByte(Pointer(S))[Index],PByte(temp)[Length(Source)+index],LS-Index);
  1148. S:=Temp;
  1149. end;
  1150. Function StringOfChar(c : char;l : SizeInt) : AnsiString;
  1151. begin
  1152. SetLength(StringOfChar,l);
  1153. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1154. end;
  1155. Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); {$IFNDEF VER2_0} Inline; {$ENDIF}
  1156. begin
  1157. SetLength(S,Len);
  1158. If (Buf<>Nil) then
  1159. Move (Buf^,Pointer(S)^,Len);
  1160. end;
  1161. Procedure SetString (Out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1162. begin
  1163. if (Buf<>nil) and (Len>0) then
  1164. widestringmanager.Wide2AnsiMoveProc(Buf,S,DefaultSystemCodePage,Len)
  1165. else
  1166. SetLength(S, Len);
  1167. end;
  1168. function upcase(const s : ansistring) : ansistring;
  1169. var
  1170. i : SizeInt;
  1171. begin
  1172. Setlength(result,length(s));
  1173. for i := 1 to length (s) do
  1174. result[i] := upcase(s[i]);
  1175. end;
  1176. function lowercase(const s : ansistring) : ansistring;
  1177. var
  1178. i : SizeInt;
  1179. begin
  1180. Setlength(result,length(s));
  1181. for i := 1 to length (s) do
  1182. result[i] := lowercase(s[i]);
  1183. end;
  1184. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1185. begin
  1186. {$ifdef FPC_HAS_CPSTRING}
  1187. if assigned(Pointer(S)) then
  1188. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1189. else
  1190. {$endif FPC_HAS_CPSTRING}
  1191. Result:=DefaultSystemCodePage;
  1192. end;
  1193. function StringElementSize(const S: RawByteString): Word; overload;
  1194. begin
  1195. if assigned(Pointer(S)) then
  1196. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1197. else
  1198. Result:=SizeOf(AnsiChar);
  1199. end;
  1200. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1201. begin
  1202. if assigned(Pointer(S)) then
  1203. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1204. else
  1205. Result:=0;
  1206. end;
  1207. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1208. begin
  1209. if (S='') or (StringCodePage(S)=CodePage) then
  1210. exit
  1211. else if Convert then
  1212. begin
  1213. {$ifdef FPC_HAS_CPSTRING}
  1214. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1215. {$else FPC_HAS_CPSTRING}
  1216. UniqueString(s);
  1217. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1218. {$endif FPC_HAS_CPSTRING}
  1219. end
  1220. else
  1221. begin
  1222. UniqueString(s);
  1223. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1224. end;
  1225. end;
  1226. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1227. begin
  1228. DefaultSystemCodePage:=CodePage;
  1229. end;