astrings.inc 42 KB

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