sstrings.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. subroutines for string handling
  12. ****************************************************************************}
  13. procedure fpc_Shortstr_SetLength(var s:shortstring;len:SizeInt);[Public,Alias : 'FPC_SHORTSTR_SETLENGTH']; compilerproc;
  14. begin
  15. if Len>255 then
  16. Len:=255;
  17. s[0]:=chr(len);
  18. end;
  19. function fpc_shortstr_copy(const s : shortstring;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  20. begin
  21. if count<0 then
  22. count:=0;
  23. if index>1 then
  24. dec(index)
  25. else
  26. index:=0;
  27. if index>length(s) then
  28. count:=0
  29. else
  30. if count>length(s)-index then
  31. count:=length(s)-index;
  32. fpc_shortstr_Copy[0]:=chr(Count);
  33. Move(s[Index+1],fpc_shortstr_Copy[1],Count);
  34. end;
  35. procedure delete(var s : shortstring;index : SizeInt;count : SizeInt);
  36. begin
  37. if index<=0 then
  38. exit;
  39. if (Index<=Length(s)) and (Count>0) then
  40. begin
  41. if Count>length(s)-Index then
  42. Count:=length(s)-Index+1;
  43. s[0]:=Chr(length(s)-Count);
  44. if Index<=Length(s) then
  45. Move(s[Index+Count],s[Index],Length(s)-Index+1);
  46. end;
  47. end;
  48. procedure insert(const source : shortstring;var s : shortstring;index : SizeInt);
  49. var
  50. cut,srclen,indexlen : SizeInt;
  51. begin
  52. if index<1 then
  53. index:=1;
  54. if index>length(s) then
  55. index:=length(s)+1;
  56. indexlen:=Length(s)-Index+1;
  57. srclen:=length(Source);
  58. if sizeInt(length(source))+sizeint(length(s))>=sizeof(s) then
  59. begin
  60. cut:=sizeInt(length(source))+sizeint(length(s))-sizeof(s)+1;
  61. if cut>indexlen then
  62. begin
  63. dec(srclen,cut-indexlen);
  64. indexlen:=0;
  65. end
  66. else
  67. dec(indexlen,cut);
  68. end;
  69. move(s[Index],s[Index+srclen],indexlen);
  70. move(Source[1],s[Index],srclen);
  71. s[0]:=chr(index+srclen+indexlen-1);
  72. end;
  73. procedure insert(source : Char;var s : shortstring;index : SizeInt);
  74. var
  75. indexlen : SizeInt;
  76. begin
  77. if index<1 then
  78. index:=1;
  79. if index>length(s) then
  80. index:=length(s)+1;
  81. indexlen:=Length(s)-Index+1;
  82. if (sizeint(length(s))+1=sizeof(s)) and (indexlen>0) then
  83. dec(indexlen);
  84. move(s[Index],s[Index+1],indexlen);
  85. s[Index]:=Source;
  86. s[0]:=chr(index+indexlen);
  87. end;
  88. function pos(const substr : shortstring;const s : shortstring):SizeInt;
  89. var
  90. i,MaxLen : SizeInt;
  91. pc : pchar;
  92. begin
  93. Pos:=0;
  94. if Length(SubStr)>0 then
  95. begin
  96. MaxLen:=sizeint(Length(s))-Length(SubStr);
  97. i:=0;
  98. pc:=@s[1];
  99. while (i<=MaxLen) do
  100. begin
  101. inc(i);
  102. if (SubStr[1]=pc^) and
  103. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  104. begin
  105. Pos:=i;
  106. exit;
  107. end;
  108. inc(pc);
  109. end;
  110. end;
  111. end;
  112. {Faster when looking for a single char...}
  113. function pos(c:char;const s:shortstring):SizeInt;
  114. var
  115. i : SizeInt;
  116. pc : pchar;
  117. begin
  118. pc:=@s[1];
  119. for i:=1 to length(s) do
  120. begin
  121. if pc^=c then
  122. begin
  123. pos:=i;
  124. exit;
  125. end;
  126. inc(pc);
  127. end;
  128. pos:=0;
  129. end;
  130. function fpc_char_copy(c:char;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  131. begin
  132. if (index=1) and (Count>0) then
  133. fpc_char_Copy:=c
  134. else
  135. fpc_char_Copy:='';
  136. end;
  137. function pos(const substr : shortstring;c:char): SizeInt;
  138. begin
  139. if (length(substr)=1) and (substr[1]=c) then
  140. Pos:=1
  141. else
  142. Pos:=0;
  143. end;
  144. {$ifdef IBM_CHAR_SET}
  145. const
  146. UpCaseTbl : shortstring[7]=#154#142#153#144#128#143#165;
  147. LoCaseTbl : shortstring[7]=#129#132#148#130#135#134#164;
  148. {$endif}
  149. function upcase(c : char) : char;
  150. {$IFDEF IBM_CHAR_SET}
  151. var
  152. i : longint;
  153. {$ENDIF}
  154. begin
  155. if (c in ['a'..'z']) then
  156. upcase:=char(byte(c)-32)
  157. else
  158. {$IFDEF IBM_CHAR_SET}
  159. begin
  160. i:=Pos(c,LoCaseTbl);
  161. if i>0 then
  162. upcase:=UpCaseTbl[i]
  163. else
  164. upcase:=c;
  165. end;
  166. {$ELSE}
  167. upcase:=c;
  168. {$ENDIF}
  169. end;
  170. function upcase(const s : shortstring) : shortstring;
  171. var
  172. i : longint;
  173. begin
  174. upcase[0]:=s[0];
  175. for i := 1 to length (s) do
  176. upcase[i] := upcase (s[i]);
  177. end;
  178. function lowercase(c : char) : char;overload;
  179. {$IFDEF IBM_CHAR_SET}
  180. var
  181. i : longint;
  182. {$ENDIF}
  183. begin
  184. if (c in ['A'..'Z']) then
  185. lowercase:=char(byte(c)+32)
  186. else
  187. {$IFDEF IBM_CHAR_SET}
  188. begin
  189. i:=Pos(c,UpCaseTbl);
  190. if i>0 then
  191. lowercase:=LoCaseTbl[i]
  192. else
  193. lowercase:=c;
  194. end;
  195. {$ELSE}
  196. lowercase:=c;
  197. {$ENDIF}
  198. end;
  199. function lowercase(const s : shortstring) : shortstring; overload;
  200. var
  201. i : longint;
  202. begin
  203. lowercase [0]:=s[0];
  204. for i:=1 to length(s) do
  205. lowercase[i]:=lowercase (s[i]);
  206. end;
  207. const
  208. HexTbl : array[0..15] of char='0123456789ABCDEF';
  209. function hexstr(val : longint;cnt : byte) : shortstring;
  210. var
  211. i : longint;
  212. begin
  213. hexstr[0]:=char(cnt);
  214. for i:=cnt downto 1 do
  215. begin
  216. hexstr[i]:=hextbl[val and $f];
  217. val:=val shr 4;
  218. end;
  219. end;
  220. function octstr(val : longint;cnt : byte) : shortstring;
  221. var
  222. i : longint;
  223. begin
  224. octstr[0]:=char(cnt);
  225. for i:=cnt downto 1 do
  226. begin
  227. octstr[i]:=hextbl[val and 7];
  228. val:=val shr 3;
  229. end;
  230. end;
  231. function binstr(val : longint;cnt : byte) : shortstring;
  232. var
  233. i : longint;
  234. begin
  235. binstr[0]:=char(cnt);
  236. for i:=cnt downto 1 do
  237. begin
  238. binstr[i]:=char(48+val and 1);
  239. val:=val shr 1;
  240. end;
  241. end;
  242. function hexstr(val : int64;cnt : byte) : shortstring;
  243. var
  244. i : longint;
  245. begin
  246. hexstr[0]:=char(cnt);
  247. for i:=cnt downto 1 do
  248. begin
  249. hexstr[i]:=hextbl[val and $f];
  250. val:=val shr 4;
  251. end;
  252. end;
  253. function octstr(val : int64;cnt : byte) : shortstring;
  254. var
  255. i : longint;
  256. begin
  257. octstr[0]:=char(cnt);
  258. for i:=cnt downto 1 do
  259. begin
  260. octstr[i]:=hextbl[val and 7];
  261. val:=val shr 3;
  262. end;
  263. end;
  264. function binstr(val : int64;cnt : byte) : shortstring;
  265. var
  266. i : longint;
  267. begin
  268. binstr[0]:=char(cnt);
  269. for i:=cnt downto 1 do
  270. begin
  271. binstr[i]:=char(48+val and 1);
  272. val:=val shr 1;
  273. end;
  274. end;
  275. Function hexStr(Val:qword;cnt:byte):shortstring;
  276. begin
  277. hexStr:=hexStr(int64(Val),cnt);
  278. end;
  279. Function OctStr(Val:qword;cnt:byte):shortstring;
  280. begin
  281. OctStr:=OctStr(int64(Val),cnt);
  282. end;
  283. Function binStr(Val:qword;cnt:byte):shortstring;
  284. begin
  285. binStr:=binStr(int64(Val),cnt);
  286. end;
  287. function hexstr(val : pointer) : shortstring;
  288. var
  289. i : longint;
  290. v : ptruint;
  291. begin
  292. v:=ptruint(val);
  293. hexstr[0]:=chr(sizeof(pointer)*2);
  294. for i:=sizeof(pointer)*2 downto 1 do
  295. begin
  296. hexstr[i]:=hextbl[v and $f];
  297. v:=v shr 4;
  298. end;
  299. end;
  300. function space (b : byte): shortstring;
  301. begin
  302. space[0] := chr(b);
  303. FillChar (Space[1],b,' ');
  304. end;
  305. {*****************************************************************************
  306. Str() Helpers
  307. *****************************************************************************}
  308. procedure fpc_shortstr_SInt(v : valSInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_SINT']; compilerproc;
  309. begin
  310. int_str(v,s);
  311. if length(s)<len then
  312. s:=space(len-length(s))+s;
  313. end;
  314. procedure fpc_shortstr_UInt(v : valUInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_UINT']; compilerproc;
  315. begin
  316. int_str(v,s);
  317. if length(s)<len then
  318. s:=space(len-length(s))+s;
  319. end;
  320. {$ifndef CPU64}
  321. procedure fpc_shortstr_qword(v : qword;len : longint;out s : shortstring);[public,alias:'FPC_SHORTSTR_QWORD']; compilerproc;
  322. begin
  323. int_str(v,s);
  324. if length(s)<len then
  325. s:=space(len-length(s))+s;
  326. end;
  327. procedure fpc_shortstr_int64(v : int64;len : longint;out s : shortstring);[public,alias:'FPC_SHORTSTR_INT64']; compilerproc;
  328. begin
  329. int_str(v,s);
  330. if length(s)<len then
  331. s:=space(len-length(s))+s;
  332. end;
  333. {$endif CPU64}
  334. { fpc_shortstr_sInt must appear before this file is included, because }
  335. { it's used inside real2str.inc and otherwise the searching via the }
  336. { compilerproc name will fail (JM) }
  337. {$I real2str.inc}
  338. procedure fpc_shortstr_float(d : ValReal;len,fr,rt : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_FLOAT']; compilerproc;
  339. begin
  340. str_real(len,fr,d,treal_type(rt),s);
  341. end;
  342. procedure fpc_shortstr_currency(c : currency; len,f : SizeInt; out s : shortstring);[public,alias:'FPC_SHORTSTR_CURRENCY']; compilerproc;
  343. const
  344. MinLen = 8; { Minimal string length in scientific format }
  345. var
  346. buf : array[1..19] of char;
  347. i,j,k,reslen,tlen,sign,r,point : longint;
  348. ic : qword;
  349. begin
  350. { default value for length is -32767 }
  351. if len=-32767 then
  352. len:=25;
  353. if PInt64(@c)^ >= 0 then
  354. begin
  355. ic:=QWord(PInt64(@c)^);
  356. sign:=0;
  357. end
  358. else
  359. begin
  360. sign:=1;
  361. ic:=QWord(-PInt64(@c)^);
  362. end;
  363. { converting to integer string }
  364. tlen:=0;
  365. repeat
  366. Inc(tlen);
  367. buf[tlen]:=Chr(ic mod 10 + $30);
  368. ic:=ic div 10;
  369. until ic = 0;
  370. { calculating:
  371. reslen - length of result string,
  372. r - rounding or appending zeroes,
  373. point - place of decimal point }
  374. reslen:=tlen;
  375. if f <> 0 then
  376. Inc(reslen); { adding decimal point length }
  377. if f < 0 then
  378. begin
  379. { scientific format }
  380. Inc(reslen,5); { adding length of sign and exponent }
  381. if len < MinLen then
  382. len:=MinLen;
  383. r:=reslen-len;
  384. if reslen < len then
  385. reslen:=len;
  386. if r > 0 then
  387. begin
  388. reslen:=len;
  389. point:=tlen - r;
  390. end
  391. else
  392. point:=tlen;
  393. end
  394. else
  395. begin
  396. { fixed format }
  397. Inc(reslen, sign);
  398. { prepending fractional part with zeroes }
  399. while tlen < 5 do
  400. begin
  401. Inc(reslen);
  402. Inc(tlen);
  403. buf[tlen]:='0';
  404. end;
  405. { Currency have 4 digits in fractional part }
  406. r:=4 - f;
  407. point:=f;
  408. if point <> 0 then
  409. begin
  410. if point > 4 then
  411. point:=4;
  412. Inc(point);
  413. end;
  414. Dec(reslen,r);
  415. end;
  416. { rounding string if r > 0 }
  417. if r > 0 then
  418. begin
  419. i:=1;
  420. k:=0;
  421. for j:=0 to r do
  422. begin
  423. buf[i]:=chr(ord(buf[i]) + k);
  424. if buf[i] >= '5' then
  425. k:=1
  426. else
  427. k:=0;
  428. Inc(i);
  429. if i>tlen then
  430. break;
  431. end;
  432. end;
  433. { preparing result string }
  434. if reslen<len then
  435. reslen:=len;
  436. if reslen>High(s) then
  437. begin
  438. if r < 0 then
  439. Inc(r, reslen - High(s));
  440. reslen:=High(s);
  441. end;
  442. SetLength(s,reslen);
  443. j:=reslen;
  444. if f<0 then
  445. begin
  446. { writing power of 10 part }
  447. if PInt64(@c)^ = 0 then
  448. k:=0
  449. else
  450. k:=tlen-5;
  451. if k >= 0 then
  452. s[j-2]:='+'
  453. else
  454. begin
  455. s[j-2]:='-';
  456. k:=-k;
  457. end;
  458. s[j]:=Chr(k mod 10 + $30);
  459. Dec(j);
  460. s[j]:=Chr(k div 10 + $30);
  461. Dec(j,2);
  462. s[j]:='E';
  463. Dec(j);
  464. end;
  465. { writing extra zeroes if r < 0 }
  466. while r < 0 do
  467. begin
  468. s[j]:='0';
  469. Dec(j);
  470. Inc(r);
  471. end;
  472. { writing digits and decimal point }
  473. for i:=r + 1 to tlen do
  474. begin
  475. Dec(point);
  476. if point = 0 then
  477. begin
  478. s[j]:='.';
  479. Dec(j);
  480. end;
  481. s[j]:=buf[i];
  482. Dec(j);
  483. end;
  484. { writing sign }
  485. if sign = 1 then
  486. begin
  487. s[j]:='-';
  488. Dec(j);
  489. end;
  490. { writing spaces }
  491. while j > 0 do
  492. begin
  493. s[j]:=' ';
  494. Dec(j);
  495. end;
  496. end;
  497. {
  498. Array Of Char Str() helpers
  499. }
  500. procedure fpc_chararray_sint(v : valsint;len : SizeInt;out a:array of char);compilerproc;
  501. var
  502. ss : shortstring;
  503. maxlen : SizeInt;
  504. begin
  505. int_str(v,ss);
  506. if length(ss)<len then
  507. ss:=space(len-length(ss))+ss;
  508. if length(ss)<high(a)+1 then
  509. maxlen:=length(ss)
  510. else
  511. maxlen:=high(a)+1;
  512. move(ss[1],pchar(@a)^,maxlen);
  513. end;
  514. procedure fpc_chararray_uint(v : valuint;len : SizeInt;out a : array of char);compilerproc;
  515. var
  516. ss : shortstring;
  517. maxlen : SizeInt;
  518. begin
  519. int_str(v,ss);
  520. if length(ss)<len then
  521. ss:=space(len-length(ss))+ss;
  522. if length(ss)<high(a)+1 then
  523. maxlen:=length(ss)
  524. else
  525. maxlen:=high(a)+1;
  526. move(ss[1],pchar(@a)^,maxlen);
  527. end;
  528. {$ifndef CPU64}
  529. procedure fpc_chararray_qword(v : qword;len : SizeInt;out a : array of char);compilerproc;
  530. var
  531. ss : shortstring;
  532. maxlen : SizeInt;
  533. begin
  534. int_str(v,ss);
  535. if length(ss)<len then
  536. ss:=space(len-length(ss))+ss;
  537. if length(ss)<high(a)+1 then
  538. maxlen:=length(ss)
  539. else
  540. maxlen:=high(a)+1;
  541. move(ss[1],pchar(@a)^,maxlen);
  542. end;
  543. procedure fpc_chararray_int64(v : int64;len : SizeInt;out a : array of char);compilerproc;
  544. var
  545. ss : shortstring;
  546. maxlen : SizeInt;
  547. begin
  548. int_str(v,ss);
  549. if length(ss)<len then
  550. ss:=space(len-length(ss))+ss;
  551. if length(ss)<high(a)+1 then
  552. maxlen:=length(ss)
  553. else
  554. maxlen:=high(a)+1;
  555. move(ss[1],pchar(@a)^,maxlen);
  556. end;
  557. {$endif CPU64}
  558. procedure fpc_chararray_Float(d : ValReal;len,fr,rt : SizeInt;out a : array of char);compilerproc;
  559. var
  560. ss : shortstring;
  561. maxlen : SizeInt;
  562. begin
  563. str_real(len,fr,d,treal_type(rt),ss);
  564. if length(ss)<high(a)+1 then
  565. maxlen:=length(ss)
  566. else
  567. maxlen:=high(a)+1;
  568. move(ss[1],pchar(@a)^,maxlen);
  569. end;
  570. {$ifdef FPC_HAS_STR_CURRENCY}
  571. procedure fpc_chararray_Currency(c : Currency;len,fr : SizeInt;out a : array of char);compilerproc;
  572. var
  573. ss : shortstring;
  574. maxlen : SizeInt;
  575. begin
  576. str(c:len:fr,ss);
  577. if length(ss)<high(a)+1 then
  578. maxlen:=length(ss)
  579. else
  580. maxlen:=high(a)+1;
  581. move(ss[1],pchar(@a)^,maxlen);
  582. end;
  583. {$endif FPC_HAS_STR_CURRENCY}
  584. {*****************************************************************************
  585. Val() Functions
  586. *****************************************************************************}
  587. Function InitVal(const s:shortstring;out negativ:boolean;out base:byte):ValSInt;
  588. var
  589. Code : SizeInt;
  590. begin
  591. {Skip Spaces and Tab}
  592. code:=1;
  593. while (code<=length(s)) and (s[code] in [' ',#9]) do
  594. inc(code);
  595. {Sign}
  596. negativ:=false;
  597. case s[code] of
  598. '-' : begin
  599. negativ:=true;
  600. inc(code);
  601. end;
  602. '+' : inc(code);
  603. end;
  604. {Base}
  605. base:=10;
  606. if code<=length(s) then
  607. begin
  608. case s[code] of
  609. '$',
  610. 'X',
  611. 'x' : begin
  612. base:=16;
  613. inc(code);
  614. end;
  615. '%' : begin
  616. base:=2;
  617. inc(code);
  618. end;
  619. '&' : begin
  620. Base:=8;
  621. inc(code);
  622. end;
  623. '0' : begin
  624. if (code < length(s)) and (s[code+1] in ['x', 'X']) then
  625. begin
  626. inc(code, 2);
  627. base := 16;
  628. end;
  629. end;
  630. end;
  631. end;
  632. { strip leading zeros }
  633. while ((code < length(s)) and (s[code] = '0')) do begin
  634. inc(code);
  635. end;
  636. InitVal:=code;
  637. end;
  638. Function fpc_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_SHORTSTR']; compilerproc;
  639. var
  640. temp, prev, maxPrevValue, maxNewValue: ValUInt;
  641. base,u : byte;
  642. negative : boolean;
  643. begin
  644. fpc_Val_SInt_ShortStr := 0;
  645. Temp:=0;
  646. Code:=InitVal(s,negative,base);
  647. if Code>length(s) then
  648. exit;
  649. if (s[Code]=#0) then
  650. begin
  651. if (Code>1) and (s[Code-1]='0') then
  652. Code:=0;
  653. exit;
  654. end;
  655. maxPrevValue := ValUInt(MaxUIntValue) div ValUInt(Base);
  656. if (base = 10) then
  657. maxNewValue := MaxSIntValue + ord(negative)
  658. else
  659. maxNewValue := MaxUIntValue;
  660. while Code<=Length(s) do
  661. begin
  662. case s[Code] of
  663. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  664. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  665. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  666. #0 : break;
  667. else
  668. u:=16;
  669. end;
  670. Prev := Temp;
  671. Temp := Temp*ValUInt(base);
  672. If (u >= base) or
  673. (ValUInt(maxNewValue-u) < Temp) or
  674. (prev > maxPrevValue) Then
  675. Begin
  676. fpc_Val_SInt_ShortStr := 0;
  677. Exit
  678. End;
  679. Temp:=Temp+u;
  680. inc(code);
  681. end;
  682. code := 0;
  683. fpc_Val_SInt_ShortStr := ValSInt(Temp);
  684. If Negative Then
  685. fpc_Val_SInt_ShortStr := -fpc_Val_SInt_ShortStr;
  686. If Not(Negative) and (base <> 10) Then
  687. {sign extend the result to allow proper range checking}
  688. Case DestSize of
  689. 1: fpc_Val_SInt_ShortStr := shortint(fpc_Val_SInt_ShortStr);
  690. 2: fpc_Val_SInt_ShortStr := smallint(fpc_Val_SInt_ShortStr);
  691. {$ifdef cpu64}
  692. 4: fpc_Val_SInt_ShortStr := longint(fpc_Val_SInt_ShortStr);
  693. {$endif cpu64}
  694. End;
  695. end;
  696. { we need this for fpc_Val_SInt_Ansistr and fpc_Val_SInt_WideStr because }
  697. { we have to pass the DestSize parameter on (JM) }
  698. Function int_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [external name 'FPC_VAL_SINT_SHORTSTR'];
  699. Function fpc_Val_UInt_Shortstr(Const S: ShortString; out Code: ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_SHORTSTR']; compilerproc;
  700. var
  701. prev : ValUInt;
  702. base,u : byte;
  703. negative : boolean;
  704. begin
  705. fpc_Val_UInt_Shortstr:=0;
  706. Code:=InitVal(s,negative,base);
  707. If Negative or (Code>length(s)) Then
  708. Exit;
  709. if (s[Code]=#0) then
  710. begin
  711. if (Code>1) and (s[Code-1]='0') then
  712. Code:=0;
  713. exit;
  714. end;
  715. while Code<=Length(s) do
  716. begin
  717. case s[Code] of
  718. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  719. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  720. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  721. #0 : break;
  722. else
  723. u:=16;
  724. end;
  725. prev := fpc_Val_UInt_Shortstr;
  726. If (u>=base) or
  727. (ValUInt(MaxUIntValue-u) div ValUInt(Base)<prev) then
  728. begin
  729. fpc_Val_UInt_Shortstr:=0;
  730. exit;
  731. end;
  732. fpc_Val_UInt_Shortstr:=fpc_Val_UInt_Shortstr*ValUInt(base) + u;
  733. inc(code);
  734. end;
  735. code := 0;
  736. end;
  737. {$ifndef CPU64}
  738. Function fpc_val_int64_shortstr(Const S: ShortString; out Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR']; compilerproc;
  739. var u, temp, prev, maxprevvalue, maxnewvalue : qword;
  740. base : byte;
  741. negative : boolean;
  742. const maxint64=qword($7fffffffffffffff);
  743. maxqword=qword($ffffffffffffffff);
  744. begin
  745. fpc_val_int64_shortstr := 0;
  746. Temp:=0;
  747. Code:=InitVal(s,negative,base);
  748. if Code>length(s) then
  749. exit;
  750. if (s[Code]=#0) then
  751. begin
  752. if (Code>1) and (s[Code-1]='0') then
  753. Code:=0;
  754. exit;
  755. end;
  756. maxprevvalue := maxqword div base;
  757. if (base = 10) then
  758. maxnewvalue := maxint64 + ord(negative)
  759. else
  760. maxnewvalue := maxqword;
  761. while Code<=Length(s) do
  762. begin
  763. case s[Code] of
  764. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  765. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  766. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  767. #0 : break;
  768. else
  769. u:=16;
  770. end;
  771. Prev:=Temp;
  772. Temp:=Temp*qword(base);
  773. If (u >= base) or
  774. (qword(maxnewvalue-u) < temp) or
  775. (prev > maxprevvalue) Then
  776. Begin
  777. fpc_val_int64_shortstr := 0;
  778. Exit
  779. End;
  780. Temp:=Temp+u;
  781. inc(code);
  782. end;
  783. code:=0;
  784. fpc_val_int64_shortstr:=int64(Temp);
  785. If Negative Then
  786. fpc_val_int64_shortstr:=-fpc_val_int64_shortstr;
  787. end;
  788. Function fpc_val_qword_shortstr(Const S: ShortString; out Code: ValSInt): QWord; [public, alias:'FPC_VAL_QWORD_SHORTSTR']; compilerproc;
  789. var u, prev: QWord;
  790. base : byte;
  791. negative : boolean;
  792. const maxqword=qword($ffffffffffffffff);
  793. begin
  794. fpc_val_qword_shortstr:=0;
  795. Code:=InitVal(s,negative,base);
  796. If Negative or (Code>length(s)) Then
  797. Exit;
  798. if (s[Code]=#0) then
  799. begin
  800. if (Code>1) and (s[Code-1]='0') then
  801. Code:=0;
  802. exit;
  803. end;
  804. while Code<=Length(s) do
  805. begin
  806. case s[Code] of
  807. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  808. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  809. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  810. #0 : break;
  811. else
  812. u:=16;
  813. end;
  814. prev := fpc_val_qword_shortstr;
  815. If (u>=base) or
  816. ((QWord(maxqword-u) div QWord(base))<prev) then
  817. Begin
  818. fpc_val_qword_shortstr := 0;
  819. Exit
  820. End;
  821. fpc_val_qword_shortstr:=fpc_val_qword_shortstr*QWord(base) + u;
  822. inc(code);
  823. end;
  824. code := 0;
  825. end;
  826. {$endif CPU64}
  827. const
  828. {$ifdef FPC_HAS_TYPE_EXTENDED}
  829. valmaxexpnorm=4932;
  830. {$else}
  831. {$ifdef FPC_HAS_TYPE_DOUBLE}
  832. valmaxexpnorm=308;
  833. {$else}
  834. {$ifdef FPC_HAS_TYPE_SINGLE}
  835. valmaxexpnorm=38;
  836. {$else}
  837. {$error Unknown floating point precision }
  838. {$endif}
  839. {$endif}
  840. {$endif}
  841. Function fpc_Val_Real_ShortStr(const s : shortstring; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_SHORTSTR']; compilerproc;
  842. var
  843. hd,
  844. esign,sign : valreal;
  845. exponent,i : SizeInt;
  846. flags : byte;
  847. begin
  848. fpc_Val_Real_ShortStr:=0.0;
  849. code:=1;
  850. exponent:=0;
  851. esign:=1;
  852. flags:=0;
  853. sign:=1;
  854. while (code<=length(s)) and (s[code] in [' ',#9]) do
  855. inc(code);
  856. if code<=length(s) then
  857. case s[code] of
  858. '+' : inc(code);
  859. '-' : begin
  860. sign:=-1;
  861. inc(code);
  862. end;
  863. end;
  864. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  865. begin
  866. { Read integer part }
  867. flags:=flags or 1;
  868. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  869. inc(code);
  870. end;
  871. { Decimal ? }
  872. if (length(s)>=code) and (s[code]='.') then
  873. begin
  874. hd:=1.0;
  875. inc(code);
  876. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  877. begin
  878. { Read fractional part. }
  879. flags:=flags or 2;
  880. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*10+(ord(s[code])-ord('0'));
  881. hd:=hd*10.0;
  882. inc(code);
  883. end;
  884. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  885. end;
  886. { Again, read integer and fractional part}
  887. if flags=0 then
  888. begin
  889. fpc_Val_Real_ShortStr:=0.0;
  890. exit;
  891. end;
  892. { Exponent ? }
  893. if (length(s)>=code) and (upcase(s[code])='E') then
  894. begin
  895. inc(code);
  896. if Length(s) >= code then
  897. if s[code]='+' then
  898. inc(code)
  899. else
  900. if s[code]='-' then
  901. begin
  902. esign:=-1;
  903. inc(code);
  904. end;
  905. if (length(s)<code) or not(s[code] in ['0'..'9']) then
  906. begin
  907. fpc_Val_Real_ShortStr:=0.0;
  908. exit;
  909. end;
  910. while (length(s)>=code) and (s[code] in ['0'..'9']) do
  911. begin
  912. exponent:=exponent*10;
  913. exponent:=exponent+ord(s[code])-ord('0');
  914. inc(code);
  915. end;
  916. end;
  917. { evaluate sign }
  918. { (before exponent, because the exponent may turn it into a denormal) }
  919. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*sign;
  920. { Calculate Exponent }
  921. hd:=1.0;
  922. { the magnitude range maximum (normal) is lower in absolute value than the }
  923. { the magnitude range minimum (denormal). E.g. an extended value can go }
  924. { up to 1E4932, but "down" to 1E-4951. So make sure that we don't try to }
  925. { calculate 1E4951 as factor, since that would overflow and result in 0. }
  926. if (exponent>valmaxexpnorm-2) then
  927. begin
  928. for i:=1 to valmaxexpnorm-2 do
  929. hd:=hd*10.0;
  930. if esign>0 then
  931. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  932. else
  933. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  934. dec(exponent,valmaxexpnorm-2);
  935. hd:=1.0;
  936. end;
  937. for i:=1 to exponent do
  938. hd:=hd*10.0;
  939. if esign>0 then
  940. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr*hd
  941. else
  942. fpc_Val_Real_ShortStr:=fpc_Val_Real_ShortStr/hd;
  943. { Not all characters are read ? }
  944. if length(s)>=code then
  945. begin
  946. fpc_Val_Real_ShortStr:=0.0;
  947. exit;
  948. end;
  949. { success ! }
  950. code:=0;
  951. end;
  952. function fpc_Val_Currency_ShortStr(const s : shortstring; out Code : ValSInt): currency; [public, alias:'FPC_VAL_CURRENCY_SHORTSTR']; compilerproc;
  953. const
  954. MaxInt64 : Int64 = $7FFFFFFFFFFFFFFF;
  955. Int64Edge : Int64 = ($7FFFFFFFFFFFFFFF - 10) div 10;
  956. Int64Edge2 : Int64 = $7FFFFFFFFFFFFFFF div 10;
  957. var
  958. res : Int64;
  959. i,j,power,sign,len : longint;
  960. FracOverflow : boolean;
  961. begin
  962. fpc_Val_Currency_ShortStr:=0;
  963. res:=0;
  964. len:=Length(s);
  965. Code:=1;
  966. sign:=1;
  967. power:=0;
  968. while True do
  969. if Code > len then
  970. exit
  971. else
  972. if s[Code] in [' ', #9] then
  973. Inc(Code)
  974. else
  975. break;
  976. { Read sign }
  977. case s[Code] of
  978. '+' : Inc(Code);
  979. '-' : begin
  980. sign:=-1;
  981. inc(code);
  982. end;
  983. end;
  984. { Read digits }
  985. FracOverflow:=False;
  986. i:=0;
  987. while Code <= len do
  988. begin
  989. case s[Code] of
  990. '0'..'9':
  991. begin
  992. j:=Ord(s[code])-Ord('0');
  993. { check overflow }
  994. if (res <= Int64Edge) or (res <= (MaxInt64 - j) div 10) then
  995. begin
  996. res:=res*10 + j;
  997. Inc(i);
  998. end
  999. else
  1000. if power = 0 then
  1001. { exit if integer part overflow }
  1002. exit
  1003. else
  1004. begin
  1005. if not FracOverflow and (j >= 5) and (res < MaxInt64) then
  1006. { round if first digit of fractional part overflow }
  1007. Inc(res);
  1008. FracOverflow:=True;
  1009. end;
  1010. end;
  1011. '.':
  1012. begin
  1013. if power = 0 then
  1014. begin
  1015. power:=1;
  1016. i:=0;
  1017. end
  1018. else
  1019. exit;
  1020. end;
  1021. else
  1022. break;
  1023. end;
  1024. Inc(Code);
  1025. end;
  1026. if (i = 0) and (power = 0) then
  1027. exit;
  1028. if power <> 0 then
  1029. power:=i;
  1030. power:=4 - power;
  1031. { Exponent? }
  1032. if Code <= len then
  1033. if s[Code] in ['E', 'e'] then
  1034. begin
  1035. Inc(Code);
  1036. if Code > len then
  1037. exit;
  1038. i:=1;
  1039. case s[Code] of
  1040. '+':
  1041. Inc(Code);
  1042. '-':
  1043. begin
  1044. i:=-1;
  1045. Inc(Code);
  1046. end;
  1047. end;
  1048. { read exponent }
  1049. j:=0;
  1050. while Code <= len do
  1051. if s[Code] in ['0'..'9'] then
  1052. begin
  1053. if j > 4951 then
  1054. exit;
  1055. j:=j*10 + (Ord(s[code])-Ord('0'));
  1056. Inc(Code);
  1057. end
  1058. else
  1059. exit;
  1060. power:=power + j*i;
  1061. end
  1062. else
  1063. exit;
  1064. if power > 0 then
  1065. begin
  1066. for i:=1 to power do
  1067. if res <= Int64Edge2 then
  1068. res:=res*10
  1069. else
  1070. exit;
  1071. end
  1072. else
  1073. for i:=1 to -power do
  1074. begin
  1075. if res <= MaxInt64 - 5 then
  1076. Inc(res, 5);
  1077. res:=res div 10;
  1078. end;
  1079. res:=res*sign;
  1080. fpc_Val_Currency_ShortStr:=PCurrency(@res)^;
  1081. Code:=0;
  1082. end;
  1083. Procedure SetString (Out S : Shortstring; Buf : PChar; Len : SizeInt);
  1084. begin
  1085. If Len > High(S) then
  1086. Len := High(S);
  1087. SetLength(S,Len);
  1088. If Buf<>Nil then
  1089. begin
  1090. Move (Buf[0],S[1],Len);
  1091. end;
  1092. end;