astrings.inc 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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 (S= SizeOf(SizeInt)):
  21. @-3*S : Code page indicator.
  22. @-3*S+2 : Character size (2 bytes)
  23. @-2*S : SizeInt for reference count;
  24. @-S : 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_utf8_Compare(const S1,S2 : RawByteString): SizeInt;
  577. var
  578. r1,r2 : RawByteString;
  579. begin
  580. r1:=S1;
  581. r2:=S2;
  582. //convert them to utf8 then compare
  583. SetCodePage(r1,65001);
  584. SetCodePage(r2,65001);
  585. Result:=fpc_AnsiStr_Compare(r1,r2);
  586. end;
  587. Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE']; compilerproc;
  588. {
  589. Compares 2 AnsiStrings;
  590. The result is
  591. <0 if S1<S2
  592. 0 if S1=S2
  593. >0 if S1>S2
  594. }
  595. Var
  596. MaxI,Temp : SizeInt;
  597. cp1,cp2 : TSystemCodePage;
  598. r1,r2 : RawByteString;
  599. begin
  600. if pointer(S1)=pointer(S2) then
  601. begin
  602. result:=0;
  603. exit;
  604. end;
  605. if (pointer(S1)=nil) then
  606. begin
  607. result:=-Length(S2);
  608. exit;
  609. end;
  610. if (pointer(S2)=nil) then
  611. begin
  612. result:=Length(S1);
  613. exit;
  614. end;
  615. cp1:=TranslatePlaceholderCP(StringCodePage(S1));
  616. cp2:=TranslatePlaceholderCP(StringCodePage(S2));
  617. if cp1=cp2 then
  618. begin
  619. Maxi:=Length(S1);
  620. temp:=Length(S2);
  621. If MaxI>Temp then
  622. MaxI:=Temp;
  623. if MaxI>0 then
  624. begin
  625. result:=CompareByte(S1[1],S2[1],MaxI);
  626. if result=0 then
  627. result:=Length(S1)-Length(S2);
  628. end
  629. else
  630. result:=Length(S1)-Length(S2);
  631. end
  632. else
  633. Result:=fpc_utf8_compare(s1,s2);
  634. end;
  635. {$endif FPC_HAS_ANSISTR_COMPARE}
  636. {$ifndef FPC_HAS_ANSISTR_COMPARE_EQUAL}
  637. {$define FPC_HAS_ANSISTR_COMPARE_EQUAL}
  638. Function fpc_utf8_Compare_equal(Const S1,S2 : RawByteString): SizeInt;
  639. Var
  640. r1,r2 : RawByteString;
  641. L1,L2 : SizeInt;
  642. begin
  643. r1:=S1;
  644. r2:=S2;
  645. //convert them to utf8 then compare
  646. SetCodePage(r1,65001);
  647. SetCodePage(r2,65001);
  648. L1:=Length(r1);
  649. L2:=Length(r2);
  650. Result:=L1-L2;
  651. if Result = 0 then
  652. if L1>0 then
  653. result:=CompareByte(r1[1],r2[1],L1);
  654. end;
  655. Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt;[Public,Alias : 'FPC_ANSISTR_COMPARE_EQUAL']; compilerproc;
  656. {
  657. Compares 2 AnsiStrings for equality/inequality only;
  658. The result is
  659. 0 if S1=S2
  660. <>0 if S1<>S2
  661. }
  662. Var
  663. MaxI,Temp : SizeInt;
  664. cp1,cp2 : TSystemCodePage;
  665. begin
  666. if pointer(S1)=pointer(S2) then
  667. Exit(0);
  668. { don't compare strings if one of them is empty }
  669. if (pointer(S1)=nil) or (pointer(S2)=Nil) then
  670. Exit(-1);
  671. cp1:=TranslatePlaceholderCP(StringCodePage(S1));
  672. cp2:=TranslatePlaceholderCP(StringCodePage(S2));
  673. if cp1=cp2 then
  674. begin
  675. Maxi:=Length(S1);
  676. temp:=Length(S2);
  677. Result := Maxi - temp;
  678. if Result = 0 then
  679. if MaxI>0 then
  680. result:=CompareByte(S1[1],S2[1],MaxI);
  681. end
  682. else
  683. begin
  684. Result:=fpc_utf8_Compare_equal(S1,S2);
  685. end;
  686. end;
  687. {$endif FPC_HAS_ANSISTR_COMPARE_EQUAL}
  688. {$ifndef FPC_HAS_ANSISTR_RANGECHECK}
  689. {$define FPC_HAS_ANSISTR_RANGECHECK}
  690. Procedure fpc_AnsiStr_RangeCheck(p: Pointer; index: SizeInt);[Public,Alias : 'FPC_ANSISTR_RANGECHECK']; compilerproc;
  691. begin
  692. if (p=nil) or (index>PAnsiRec(p-AnsiFirstOff)^.Len) or (Index<1) then
  693. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  694. end;
  695. {$endif FPC_HAS_ANSISTR_RANGECHECK}
  696. {$ifndef FPC_HAS_ANSISTR_SETLENGTH}
  697. {$define FPC_HAS_ANSISTR_SETLENGTH}
  698. 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;
  699. {
  700. Sets The length of string S to L.
  701. Makes sure S is unique, and contains enough room.
  702. }
  703. Var
  704. Temp : Pointer;
  705. lens, lena,
  706. movelen : SizeInt;
  707. begin
  708. if (l>0) then
  709. begin
  710. if Pointer(S)=nil then
  711. begin
  712. Pointer(S):=NewAnsiString(L);
  713. {$ifdef FPC_HAS_CPSTRING}
  714. cp:=TranslatePlaceholderCP(cp);
  715. PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=cp;
  716. {$else}
  717. PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=DefaultSystemCodePage;
  718. {$endif FPC_HAS_CPSTRING}
  719. end
  720. else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
  721. begin
  722. Temp:=Pointer(s)-AnsiFirstOff;
  723. lens:=MemSize(Temp);
  724. lena:=AnsiFirstOff+L+sizeof(AnsiChar);
  725. { allow shrinking string if that saves at least half of current size }
  726. if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
  727. begin
  728. reallocmem(Temp,lena);
  729. Pointer(S):=Temp+AnsiFirstOff;
  730. end;
  731. end
  732. else
  733. begin
  734. { Reallocation is needed... }
  735. Temp:=NewAnsiString(L);
  736. PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  737. { also move terminating null }
  738. lens:=succ(length(s));
  739. if l<lens then
  740. movelen:=l
  741. else
  742. movelen:=lens;
  743. Move(Pointer(S)^,Temp^,movelen);
  744. fpc_ansistr_decr_ref(Pointer(s));
  745. Pointer(S):=Temp;
  746. end;
  747. { Force nil termination in case it gets shorter }
  748. PByte(Pointer(S)+l)^:=0;
  749. PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
  750. end
  751. else { length=0, deallocate the string }
  752. fpc_ansistr_decr_ref (Pointer(S));
  753. end;
  754. {$endif FPC_HAS_ANSISTR_SETLENGTH}
  755. {$ifdef EXTRAANSISHORT}
  756. Function fpc_AnsiStr_ShortStr_Compare (Var S1 : Pointer; Var S2 : ShortString): SizeInt; compilerproc;
  757. {
  758. Compares a AnsiString with a ShortString;
  759. The result is
  760. <0 if S1<S2
  761. 0 if S1=S2
  762. >0 if S1>S2
  763. }
  764. Var
  765. i,MaxI,Temp : SizeInt;
  766. begin
  767. Temp:=0;
  768. i:=0;
  769. MaxI:=Length(AnsiString(S1));
  770. if MaxI>byte(S2[0]) then
  771. MaxI:=Byte(S2[0]);
  772. While (i<MaxI) and (Temp=0) do
  773. begin
  774. Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
  775. inc(i);
  776. end;
  777. AnsiStr_ShortStr_Compare:=Temp;
  778. end;
  779. {$endif EXTRAANSISHORT}
  780. {*****************************************************************************
  781. Public functions, In interface.
  782. *****************************************************************************}
  783. {$ifndef FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  784. {$define FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  785. function fpc_truely_ansistr_unique(Var S : Pointer): Pointer;
  786. Var
  787. SNew : Pointer;
  788. L : SizeInt;
  789. begin
  790. L:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.len;
  791. SNew:=NewAnsiString (L);
  792. Move (Pointer(S)^,SNew^,L+1);
  793. PAnsiRec(SNew-AnsiFirstOff)^.len:=L;
  794. PAnsiRec(SNew-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  795. fpc_ansistr_decr_ref (Pointer(S)); { Thread safe }
  796. pointer(S):=SNew;
  797. pointer(result):=SNew;
  798. end;
  799. {$endif FPC_SYSTEM_HAS_TRUELY_ANSISTR_UNIQUE}
  800. {$ifndef FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  801. {$define FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  802. // MV: inline the basic checks for case that S is already unique.
  803. // Rest is too complex to inline, so factor that out as a call.
  804. Function fpc_ansistr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_ANSISTR_UNIQUE']; compilerproc; inline;
  805. {
  806. Make sure reference count of S is 1,
  807. using copy-on-write semantics.
  808. }
  809. begin
  810. pointer(result) := pointer(s);
  811. If Pointer(S)=Nil then
  812. exit;
  813. if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref<>1 then
  814. result:=fpc_truely_ansistr_unique(s);
  815. end;
  816. {$endif FPC_SYSTEM_HAS_ANSISTR_UNIQUE}
  817. {$ifndef FPC_HAS_ANSISTR_COPY}
  818. {$define FPC_HAS_ANSISTR_COPY}
  819. Function Fpc_Ansistr_Copy(Const S : RawByteString; Index,Size : SizeInt): RawByteString;compilerproc;
  820. var
  821. ResultAddress : Pointer;
  822. begin
  823. ResultAddress:=Nil;
  824. dec(index);
  825. if Index < 0 then
  826. Index := 0;
  827. { Check Size. Accounts for Zero-length S, the double check is needed because
  828. Size can be maxint and will get <0 when adding index }
  829. if (Size>Length(S)) or
  830. (Index+Size>Length(S)) then
  831. Size:=Length(S)-Index;
  832. If Size>0 then
  833. begin
  834. ResultAddress:=NewAnsiString(Size);
  835. if ResultAddress<>Nil then
  836. begin
  837. Move(Pointer(Pointer(S)+index)^,ResultAddress^,Size);
  838. PByte(ResultAddress+Size)^:=0;
  839. PAnsiRec(ResultAddress-AnsiFirstOff)^.Len:=Size;
  840. PAnsiRec(ResultAddress-AnsiFirstOff)^.CodePage:=PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage;
  841. end;
  842. end;
  843. fpc_ansistr_decr_ref(Pointer(fpc_ansistr_copy));
  844. Pointer(fpc_ansistr_Copy):=ResultAddress;
  845. end;
  846. {$endif FPC_HAS_ANSISTR_COPY}
  847. {$ifndef FPC_HAS_POS_SHORTSTR_ANSISTR}
  848. {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
  849. Function Pos(Const Substr : ShortString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  850. var
  851. i,MaxLen : SizeInt;
  852. pc : PAnsiChar;
  853. begin
  854. Pos:=0;
  855. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  856. begin
  857. MaxLen:=Length(source)-Length(SubStr);
  858. i:=Offset-1;
  859. pc:=@source[Offset];
  860. while (i<=MaxLen) do
  861. begin
  862. inc(i);
  863. if (SubStr[1]=pc^) and
  864. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  865. begin
  866. Pos:=i;
  867. exit;
  868. end;
  869. inc(pc);
  870. end;
  871. end;
  872. end;
  873. {$endif FPC_HAS_POS_SHORTSTR_ANSISTR}
  874. {$ifndef FPC_HAS_POS_ANSISTR_ANSISTR}
  875. {$define FPC_HAS_POS_ANSISTR_ANSISTR}
  876. Function Pos(Const Substr : RawByteString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
  877. var
  878. i,MaxLen : SizeInt;
  879. pc : PAnsiChar;
  880. begin
  881. Pos:=0;
  882. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
  883. begin
  884. MaxLen:=Length(source)-Length(SubStr);
  885. i:=Offset-1;
  886. pc:=@source[Offset];
  887. while (i<=MaxLen) do
  888. begin
  889. inc(i);
  890. if (SubStr[1]=pc^) and
  891. (CompareByte(Substr[1],pc^,Length(SubStr))=0) then
  892. begin
  893. Pos:=i;
  894. exit;
  895. end;
  896. inc(pc);
  897. end;
  898. end;
  899. end;
  900. {$endif FPC_HAS_POS_ANSISTR_ANSISTR}
  901. {$ifndef FPC_HAS_POS_ANSICHAR_ANSISTR}
  902. {$define FPC_HAS_POS_ANSICHAR_ANSISTR}
  903. { Faster version for a char alone. Must be implemented because }
  904. { pos(c: char; const s: shortstring) also exists, so otherwise }
  905. { using pos(char,pchar) will always call the shortstring version }
  906. { (exact match for first argument), also with $h+ (JM) }
  907. Function Pos(c : AnsiChar; Const s : RawByteString; Offset : Sizeint = 1) : SizeInt;
  908. var
  909. i: SizeInt;
  910. pc : PAnsiChar;
  911. begin
  912. Pos:=0;
  913. If (Offset<1) or (Offset>Length(S)) then
  914. exit;
  915. pc:=@s[OffSet];
  916. for i:=Offset to length(s) do
  917. begin
  918. if pc^=c then
  919. begin
  920. pos:=i;
  921. exit;
  922. end;
  923. inc(pc);
  924. end;
  925. pos:=0;
  926. end;
  927. {$endif FPC_HAS_POS_ANSICHAR_ANSISTR}
  928. {$ifndef FPUNONE}
  929. Function fpc_Val_Real_AnsiStr(Const S : RawByteString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_ANSISTR']; compilerproc;
  930. Var
  931. SS : String;
  932. begin
  933. fpc_Val_Real_AnsiStr := 0;
  934. if length(S) > 255 then
  935. code := 256
  936. else
  937. begin
  938. SS := S;
  939. Val(SS,fpc_Val_Real_AnsiStr,code);
  940. end;
  941. end;
  942. {$endif}
  943. Function fpc_Val_Currency_AnsiStr(Const S : RawByteString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_ANSISTR']; compilerproc;
  944. Var
  945. SS : String;
  946. begin
  947. if length(S) > 255 then
  948. begin
  949. fpc_Val_Currency_AnsiStr := 0;
  950. code := 256;
  951. end
  952. else
  953. begin
  954. SS := S;
  955. Val(SS,fpc_Val_Currency_AnsiStr,code);
  956. end;
  957. end;
  958. Function fpc_Val_UInt_AnsiStr (Const S : RawByteString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_ANSISTR']; compilerproc;
  959. Var
  960. SS : ShortString;
  961. begin
  962. fpc_Val_UInt_AnsiStr := 0;
  963. if length(S) > 255 then
  964. code := 256
  965. else
  966. begin
  967. SS := S;
  968. Val(SS,fpc_Val_UInt_AnsiStr,code);
  969. end;
  970. end;
  971. Function fpc_Val_SInt_AnsiStr (DestSize: SizeInt; Const S : RawByteString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_ANSISTR']; compilerproc;
  972. Var
  973. SS : ShortString;
  974. begin
  975. fpc_Val_SInt_AnsiStr:=0;
  976. if length(S)>255 then
  977. code:=256
  978. else
  979. begin
  980. SS := S;
  981. fpc_Val_SInt_AnsiStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  982. end;
  983. end;
  984. {$ifndef CPU64}
  985. Function fpc_Val_qword_AnsiStr (Const S : RawByteString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_ANSISTR']; compilerproc;
  986. Var
  987. SS : ShortString;
  988. begin
  989. fpc_Val_qword_AnsiStr:=0;
  990. if length(S)>255 then
  991. code:=256
  992. else
  993. begin
  994. SS := S;
  995. Val(SS,fpc_Val_qword_AnsiStr,Code);
  996. end;
  997. end;
  998. Function fpc_Val_int64_AnsiStr (Const S : RawByteString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_ANSISTR']; compilerproc;
  999. Var
  1000. SS : ShortString;
  1001. begin
  1002. fpc_Val_int64_AnsiStr:=0;
  1003. if length(S)>255 then
  1004. code:=256
  1005. else
  1006. begin
  1007. SS := s;
  1008. Val(SS,fpc_Val_int64_AnsiStr,Code);
  1009. end;
  1010. end;
  1011. {$endif CPU64}
  1012. {$if defined(CPU16) or defined(CPU8)}
  1013. Function fpc_Val_longword_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_LONGWORD_ANSISTR']; compilerproc;
  1014. Var
  1015. SS : ShortString;
  1016. begin
  1017. fpc_Val_longword_AnsiStr:=0;
  1018. if length(S)>255 then
  1019. code:=256
  1020. else
  1021. begin
  1022. SS := S;
  1023. Val(SS,fpc_Val_longword_AnsiStr,Code);
  1024. end;
  1025. end;
  1026. Function fpc_Val_longint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_LONGINT_ANSISTR']; compilerproc;
  1027. Var
  1028. SS : ShortString;
  1029. begin
  1030. fpc_Val_longint_AnsiStr:=0;
  1031. if length(S)>255 then
  1032. code:=256
  1033. else
  1034. begin
  1035. SS := s;
  1036. Val(SS,fpc_Val_longint_AnsiStr,Code);
  1037. end;
  1038. end;
  1039. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): word; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  1040. Var
  1041. SS : ShortString;
  1042. begin
  1043. fpc_Val_word_AnsiStr:=0;
  1044. if length(S)>255 then
  1045. code:=256
  1046. else
  1047. begin
  1048. SS := S;
  1049. Val(SS,fpc_Val_word_AnsiStr,Code);
  1050. end;
  1051. end;
  1052. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): smallint; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  1053. Var
  1054. SS : ShortString;
  1055. begin
  1056. fpc_Val_smallint_AnsiStr:=0;
  1057. if length(S)>255 then
  1058. code:=256
  1059. else
  1060. begin
  1061. SS := s;
  1062. Val(SS,fpc_Val_smallint_AnsiStr,Code);
  1063. end;
  1064. end;
  1065. {$endif CPU16 or CPU8}
  1066. {$if defined(CPU8)}
  1067. Function fpc_Val_word_AnsiStr (Const S : RawByteString; out Code : ValSInt): longword; [public, alias:'FPC_VAL_WORD_ANSISTR']; compilerproc;
  1068. Var
  1069. SS : ShortString;
  1070. begin
  1071. fpc_Val_longword_AnsiStr:=0;
  1072. if length(S)>255 then
  1073. code:=256
  1074. else
  1075. begin
  1076. SS := S;
  1077. Val(SS,fpc_Val_longword_AnsiStr,Code);
  1078. end;
  1079. end;
  1080. Function fpc_Val_smallint_AnsiStr (Const S : RawByteString; out Code : ValSInt): LongInt; [public, alias:'FPC_VAL_SMALLINT_ANSISTR']; compilerproc;
  1081. Var
  1082. SS : ShortString;
  1083. begin
  1084. fpc_Val_longint_AnsiStr:=0;
  1085. if length(S)>255 then
  1086. code:=256
  1087. else
  1088. begin
  1089. SS := s;
  1090. Val(SS,fpc_Val_longint_AnsiStr,Code);
  1091. end;
  1092. end;
  1093. {$endif CPU8}
  1094. {$ifndef FPUNONE}
  1095. 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;
  1096. var
  1097. ss: ShortString;
  1098. begin
  1099. str_real(len,fr,d,treal_type(rt),ss);
  1100. s:=ss;
  1101. {$ifdef FPC_HAS_CPSTRING}
  1102. SetCodePage(s,cp,false);
  1103. {$endif FPC_HAS_CPSTRING}
  1104. end;
  1105. {$endif}
  1106. {$ifndef FPC_STR_ENUM_INTERN}
  1107. 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;
  1108. var ss:shortstring;
  1109. begin
  1110. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1111. s:=ss;
  1112. {$ifdef FPC_HAS_CPSTRING}
  1113. SetCodePage(s,cp,false);
  1114. {$endif FPC_HAS_CPSTRING}
  1115. end;
  1116. {$endif FPC_STR_ENUM_INTERN}
  1117. 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;
  1118. var
  1119. ss:shortstring;
  1120. begin
  1121. fpc_shortstr_bool(b,len,ss);
  1122. s:=ss;
  1123. {$ifdef FPC_HAS_CPSTRING}
  1124. SetCodePage(s,cp,false);
  1125. {$endif FPC_HAS_CPSTRING}
  1126. end;
  1127. {$ifndef FPC_STR_ENUM_INTERN}
  1128. function fpc_val_enum_ansistr(str2ordindex:pointer;const s:RawByteString;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_ANSISTR']; compilerproc;
  1129. begin
  1130. fpc_val_enum_ansistr:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1131. end;
  1132. {$endif FPC_STR_ENUM_INTERN}
  1133. 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;
  1134. var
  1135. ss: ShortString;
  1136. begin
  1137. str(c:len:fr,ss);
  1138. s:=ss;
  1139. {$ifdef FPC_HAS_CPSTRING}
  1140. SetCodePage(s,cp,false);
  1141. {$endif FPC_HAS_CPSTRING}
  1142. end;
  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 {$ifdef VER3_0}Delete{$else}fpc_ansistr_delete{$endif}(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 {$ifdef VER3_0}Insert{$else}fpc_ansistr_insert{$endif}(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. if length(S)<>0 then
  1260. cp:=TranslatePlaceholderCP(StringCodePage(S))
  1261. else
  1262. cp:=TranslatePlaceholderCP(StringCodePage(Source));
  1263. SetCodePage(Temp,cp,false);
  1264. If Index>0 then
  1265. fpc_pchar_ansistr_intern_charmove(pchar(S),0,Temp,0,Index);
  1266. fpc_pchar_ansistr_intern_charmove(pchar(Source),0,Temp,Index,Length(Source));
  1267. If (LS-Index)>0 then
  1268. fpc_pchar_ansistr_intern_charmove(pchar(S),Index,Temp,Length(Source)+Index,LS-Index);
  1269. S:=Temp;
  1270. end;
  1271. {$ifndef FPC_HAS_ANSISTR_OF_CHAR}
  1272. {$define FPC_HAS_ANSISTR_OF_CHAR}
  1273. Function StringOfChar(c : Ansichar;l : SizeInt) : AnsiString;
  1274. begin
  1275. SetLength(StringOfChar,l);
  1276. FillChar(Pointer(StringOfChar)^,Length(StringOfChar),c);
  1277. end;
  1278. {$endif FPC_HAS_ANSISTR_OF_CHAR}
  1279. {$ifdef FPC_HAS_CPSTRING}
  1280. Procedure fpc_setstring_ansistr_pansichar(out S : RawByteString; Buf : PAnsiChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1281. {$else}
  1282. Procedure SetString(out S : AnsiString; Buf : PAnsiChar; Len : SizeInt);
  1283. {$endif}
  1284. begin
  1285. SetLength(S,Len);
  1286. {$ifdef FPC_HAS_CPSTRING}
  1287. SetCodePage(S,cp,false);
  1288. {$endif}
  1289. If (Buf<>Nil) then
  1290. fpc_pchar_ansistr_intern_charmove(Buf,0,S,0,Len);
  1291. end;
  1292. {$ifdef FPC_HAS_CPSTRING}
  1293. Procedure fpc_setstring_ansistr_pwidechar(out S : RawByteString; Buf : PWideChar; Len : SizeInt; cp: TSystemCodePage); rtlproc; compilerproc;
  1294. {$else}
  1295. Procedure SetString(out S : AnsiString; Buf : PWideChar; Len : SizeInt);
  1296. {$endif}
  1297. begin
  1298. if (Buf<>nil) and (Len>0) then
  1299. widestringmanager.Wide2AnsiMoveProc(Buf,S,{$ifdef FPC_HAS_CPSTRING}cp{$else}DefaultSystemCodePage{$endif},Len)
  1300. else
  1301. begin
  1302. SetLength(S, Len);
  1303. {$ifdef FPC_HAS_CPSTRING}
  1304. SetCodePage(S,cp,false);
  1305. {$endif}
  1306. end;
  1307. end;
  1308. {$ifndef FPC_HAS_UPCASE_ANSISTR}
  1309. {$define FPC_HAS_UPCASE_ANSISTR}
  1310. function upcase(const s : ansistring) : ansistring;
  1311. var
  1312. i : SizeInt;
  1313. begin
  1314. Setlength(result,length(s));
  1315. for i := 1 to length (s) do
  1316. result[i] := upcase(s[i]);
  1317. end;
  1318. {$endif FPC_HAS_UPCASE_ANSISTR}
  1319. {$ifndef FPC_HAS_LOWERCASE_ANSISTR}
  1320. {$define FPC_HAS_LOWERCASE_ANSISTR}
  1321. function lowercase(const s : ansistring) : ansistring;
  1322. var
  1323. i : SizeInt;
  1324. begin
  1325. Setlength(result,length(s));
  1326. for i := 1 to length (s) do
  1327. result[i] := lowercase(s[i]);
  1328. end;
  1329. {$endif FPC_HAS_LOWERCASE_ANSISTR}
  1330. {$ifndef FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1331. {$define FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1332. function StringCodePage(const S: RawByteString): TSystemCodePage; overload;
  1333. begin
  1334. {$ifdef FPC_HAS_CPSTRING}
  1335. if assigned(Pointer(S)) then
  1336. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage
  1337. else
  1338. {$endif FPC_HAS_CPSTRING}
  1339. Result:=DefaultSystemCodePage;
  1340. end;
  1341. {$endif FPC_HAS_ANSISTR_STRINGCODEPAGE}
  1342. {$ifndef FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1343. {$define FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1344. function StringElementSize(const S: RawByteString): Word; overload;
  1345. begin
  1346. if assigned(Pointer(S)) then
  1347. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.ElementSize
  1348. else
  1349. Result:=SizeOf(AnsiChar);
  1350. end;
  1351. {$endif FPC_HAS_ANSISTR_STRINGELEMENTSIZE}
  1352. {$ifndef FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1353. {$define FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1354. function StringRefCount(const S: RawByteString): SizeInt; overload;
  1355. begin
  1356. if assigned(Pointer(S)) then
  1357. Result:=PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref
  1358. else
  1359. Result:=0;
  1360. end;
  1361. {$endif FPC_HAS_ANSISTR_STRINGREFCOUNT}
  1362. {$ifndef FPC_HAS_ANSISTR_SETCODEPAGE}
  1363. {$define FPC_HAS_ANSISTR_SETCODEPAGE}
  1364. procedure InternalSetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1365. begin
  1366. if Convert then
  1367. begin
  1368. {$ifdef FPC_HAS_CPSTRING}
  1369. s:=fpc_AnsiStr_To_AnsiStr(s,CodePage);
  1370. {$else FPC_HAS_CPSTRING}
  1371. UniqueString(s);
  1372. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1373. {$endif FPC_HAS_CPSTRING}
  1374. end
  1375. else
  1376. begin
  1377. UniqueString(s);
  1378. PAnsiRec(pointer(s)-AnsiFirstOff)^.CodePage:=CodePage;
  1379. end;
  1380. end;
  1381. { use this wrapper for the simple case to avoid the generation of a temp. ansistring which causes
  1382. extra exception frames }
  1383. procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);
  1384. var
  1385. OrgCodePage,
  1386. TranslatedCodePage,
  1387. TranslatedCurrentCodePage: TSystemCodePage;
  1388. begin
  1389. if (S='') then
  1390. exit;
  1391. { if the codepage are identical, we don't have to do anything (even if the
  1392. string has multiple references) }
  1393. OrgCodePage:=PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage;
  1394. if OrgCodePage=CodePage then
  1395. exit;
  1396. { if we're just replacing a placeholder code page with its actual value or
  1397. vice versa, we don't have to perform any conversion }
  1398. TranslatedCurrentCodePage:=TranslatePlaceholderCP(OrgCodePage);
  1399. TranslatedCodePage:=TranslatePlaceholderCP(CodePage);
  1400. Convert:=Convert and
  1401. (TranslatedCurrentCodePage<>TranslatedCodePage);
  1402. if not Convert and (PAnsiRec(pointer(S)-AnsiFirstOff)^.Ref=1) then
  1403. PAnsiRec(pointer(S)-AnsiFirstOff)^.CodePage:=CodePage
  1404. else
  1405. InternalSetCodePage(S,CodePage,Convert);
  1406. end;
  1407. {$endif FPC_HAS_ANSISTR_SETCODEPAGE}
  1408. procedure SetMultiByteConversionCodePage(CodePage: TSystemCodePage);
  1409. begin
  1410. DefaultSystemCodePage:=CodePage;
  1411. end;
  1412. procedure SetMultiByteFileSystemCodePage(CodePage: TSystemCodePage);
  1413. begin
  1414. DefaultFileSystemCodePage:=CodePage;
  1415. end;
  1416. procedure SetMultiByteRTLFileSystemCodePage(CodePage: TSystemCodePage);
  1417. begin
  1418. DefaultRTLFileSystemCodePage:=CodePage;
  1419. end;