sstrings.inc 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. subroutines for string handling
  13. ****************************************************************************}
  14. {$I real2str.inc}
  15. function copy(const s : shortstring;index : StrLenInt;count : StrLenInt): shortstring;
  16. begin
  17. if count<0 then
  18. count:=0;
  19. if index>1 then
  20. dec(index)
  21. else
  22. index:=0;
  23. if index>length(s) then
  24. count:=0
  25. else
  26. if index+count>length(s) then
  27. count:=length(s)-index;
  28. Copy[0]:=chr(Count);
  29. Move(s[Index+1],Copy[1],Count);
  30. end;
  31. procedure delete(var s : shortstring;index : StrLenInt;count : StrLenInt);
  32. begin
  33. if index<=0 then
  34. begin
  35. inc(count,index-1);
  36. index:=1;
  37. end;
  38. if (Index<=Length(s)) and (Count>0) then
  39. begin
  40. if Count+Index>length(s) then
  41. Count:=length(s)-Index+1;
  42. s[0]:=Chr(length(s)-Count);
  43. if Index<=Length(s) then
  44. Move(s[Index+Count],s[Index],Length(s)-Index+1);
  45. end;
  46. end;
  47. procedure insert(const source : shortstring;var s : shortstring;index : StrLenInt);
  48. var
  49. cut,srclen,indexlen : longint;
  50. begin
  51. if index<1 then
  52. index:=1;
  53. if index>length(s) then
  54. index:=length(s)+1;
  55. indexlen:=Length(s)-Index+1;
  56. srclen:=length(Source);
  57. if length(source)+length(s)>=sizeof(s) then
  58. begin
  59. cut:=length(source)+length(s)-sizeof(s)+1;
  60. if cut>indexlen then
  61. begin
  62. dec(srclen,cut-indexlen);
  63. indexlen:=0;
  64. end
  65. else
  66. dec(indexlen,cut);
  67. end;
  68. move(s[Index],s[Index+srclen],indexlen);
  69. move(Source[1],s[Index],srclen);
  70. s[0]:=chr(index+srclen+indexlen-1);
  71. end;
  72. procedure insert(source : Char;var s : shortstring;index : StrLenInt);
  73. var
  74. indexlen : longint;
  75. begin
  76. if index<1 then
  77. index:=1;
  78. if index>length(s) then
  79. index:=length(s)+1;
  80. indexlen:=Length(s)-Index+1;
  81. if (length(s)+1=sizeof(s)) and (indexlen>0) then
  82. dec(indexlen);
  83. move(s[Index],s[Index+1],indexlen);
  84. s[Index]:=Source;
  85. s[0]:=chr(index+indexlen);
  86. end;
  87. function pos(const substr : shortstring;const s : shortstring):StrLenInt;
  88. var
  89. i,j : StrLenInt;
  90. e : boolean;
  91. begin
  92. i := 0;
  93. j := 0;
  94. e:=(length(SubStr)>0);
  95. while e and (i<=Length(s)-Length(SubStr)) do
  96. begin
  97. inc(i);
  98. if (SubStr[1]=s[i]) and (Substr=Copy(s,i,Length(SubStr))) then
  99. begin
  100. j:=i;
  101. e:=false;
  102. end;
  103. end;
  104. Pos:=j;
  105. end;
  106. {Faster when looking for a single char...}
  107. function pos(c:char;const s:shortstring):StrLenInt;
  108. var
  109. i : StrLenInt;
  110. begin
  111. for i:=1 to length(s) do
  112. if s[i]=c then
  113. begin
  114. pos:=i;
  115. exit;
  116. end;
  117. pos:=0;
  118. end;
  119. procedure SetLength(var s:shortstring;len:StrLenInt);
  120. begin
  121. if Len>255 then
  122. Len:=255;
  123. s[0]:=chr(len);
  124. end;
  125. function copy(c:char;index : StrLenInt;count : StrLenInt): shortstring;
  126. begin
  127. if (index=1) and (Count>0) then
  128. Copy:=c
  129. else
  130. Copy:='';
  131. end;
  132. function pos(const substr : shortstring;c:char): StrLenInt;
  133. begin
  134. if (length(substr)=1) and (substr[1]=c) then
  135. Pos:=1
  136. else
  137. Pos:=0;
  138. end;
  139. { removed must be internal to be accepted in const expr !! PM
  140. function length(c:char):StrLenInt;
  141. begin
  142. Length:=1;
  143. end;
  144. }
  145. {$ifdef IBM_CHAR_SET}
  146. const
  147. UpCaseTbl : shortstring[7]=#154#142#153#144#128#143#165;
  148. LoCaseTbl : shortstring[7]=#129#132#148#130#135#134#164;
  149. {$endif}
  150. function upcase(c : char) : char;
  151. {$IFDEF IBM_CHAR_SET}
  152. var
  153. i : longint;
  154. {$ENDIF}
  155. begin
  156. if (c in ['a'..'z']) then
  157. upcase:=char(byte(c)-32)
  158. else
  159. {$IFDEF IBM_CHAR_SET}
  160. begin
  161. i:=Pos(c,LoCaseTbl);
  162. if i>0 then
  163. upcase:=UpCaseTbl[i]
  164. else
  165. upcase:=c;
  166. end;
  167. {$ELSE}
  168. upcase:=c;
  169. {$ENDIF}
  170. end;
  171. function upcase(const s : shortstring) : shortstring;
  172. var
  173. i : longint;
  174. begin
  175. upcase[0]:=s[0];
  176. for i := 1 to length (s) do
  177. upcase[i] := upcase (s[i]);
  178. end;
  179. {$ifndef RTLLITE}
  180. function lowercase(c : char) : char;
  181. {$IFDEF IBM_CHAR_SET}
  182. var
  183. i : longint;
  184. {$ENDIF}
  185. begin
  186. if (c in ['A'..'Z']) then
  187. lowercase:=char(byte(c)+32)
  188. else
  189. {$IFDEF IBM_CHAR_SET}
  190. begin
  191. i:=Pos(c,UpCaseTbl);
  192. if i>0 then
  193. lowercase:=LoCaseTbl[i]
  194. else
  195. lowercase:=c;
  196. end;
  197. {$ELSE}
  198. lowercase:=c;
  199. {$ENDIF}
  200. end;
  201. function lowercase(const s : shortstring) : shortstring;
  202. var
  203. i : longint;
  204. begin
  205. lowercase [0]:=s[0];
  206. for i:=1 to length(s) do
  207. lowercase[i]:=lowercase (s[i]);
  208. end;
  209. function hexstr(val : longint;cnt : byte) : shortstring;
  210. const
  211. HexTbl : array[0..15] of char='0123456789ABCDEF';
  212. var
  213. i : longint;
  214. begin
  215. hexstr[0]:=char(cnt);
  216. for i:=cnt downto 1 do
  217. begin
  218. hexstr[i]:=hextbl[val and $f];
  219. val:=val shr 4;
  220. end;
  221. end;
  222. function binstr(val : longint;cnt : byte) : shortstring;
  223. var
  224. i : longint;
  225. begin
  226. binstr[0]:=char(cnt);
  227. for i:=cnt downto 1 do
  228. begin
  229. binstr[i]:=char(48+val and 1);
  230. val:=val shr 1;
  231. end;
  232. end;
  233. {$endif RTLLITE}
  234. function space (b : byte): shortstring;
  235. begin
  236. space[0] := chr(b);
  237. FillChar (Space[1],b,' ');
  238. end;
  239. {*****************************************************************************
  240. Str() Helpers
  241. *****************************************************************************}
  242. procedure int_str_real(d : real;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_REAL'];
  243. begin
  244. {$ifdef i386}
  245. str_real(len,fr,d,rt_s64real,s);
  246. {$else}
  247. str_real(len,fr,d,rt_s32real,s);
  248. {$endif}
  249. end;
  250. {$ifdef SUPPORT_SINGLE}
  251. procedure int_str_single(d : single;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_SINGLE'];
  252. begin
  253. str_real(len,fr,d,rt_s32real,s);
  254. end;
  255. {$endif SUPPORT_SINGLE}
  256. {$ifdef SUPPORT_EXTENDED}
  257. procedure int_str_extended(d : extended;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_EXTENDED'];
  258. begin
  259. str_real(len,fr,d,rt_s80real,s);
  260. end;
  261. {$endif SUPPORT_EXTENDED}
  262. {$ifdef SUPPORT_COMP}
  263. procedure int_str_comp(d : comp;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_COMP'];
  264. begin
  265. str_real(len,fr,d,rt_s64bit,s);
  266. end;
  267. {$endif SUPPORT_COMP}
  268. {$ifdef SUPPORT_FIXED}
  269. procedure int_str_fixed(d : fixed;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_FIXED'];
  270. begin
  271. str_real(len,fr,d,rt_f32bit,s);
  272. end;
  273. {$endif SUPPORT_FIXED}
  274. procedure int_str_longint(v : longint;len : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_LONGINT'];
  275. begin
  276. int_str(v,s);
  277. if length(s)<len then
  278. s:=space(len-length(s))+s;
  279. end;
  280. procedure int_str_cardinal(v : cardinal;len : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_CARDINAL'];
  281. begin
  282. int_str(v,s);
  283. if length(s)<len then
  284. s:=space(len-length(s))+s;
  285. end;
  286. {*****************************************************************************
  287. Val() Functions
  288. *****************************************************************************}
  289. Function InitVal(const s:shortstring;var negativ:boolean;var base:byte):ValSInt;
  290. var
  291. Code : Longint;
  292. begin
  293. {Skip Spaces and Tab}
  294. code:=1;
  295. while (code<=length(s)) and (s[code] in [' ',#9]) do
  296. inc(code);
  297. {Sign}
  298. negativ:=false;
  299. case s[code] of
  300. '-' : begin
  301. negativ:=true;
  302. inc(code);
  303. end;
  304. '+' : inc(code);
  305. end;
  306. {Base}
  307. base:=10;
  308. if code<=length(s) then
  309. begin
  310. case s[code] of
  311. '$' : begin
  312. base:=16;
  313. repeat
  314. inc(code);
  315. until (code>=length(s)) or (s[code]<>'0');
  316. {The following isn't correct anymore for 64 bit integers! (JM)}
  317. {$IfNDef ValInternCompiled}
  318. if length(s)-code>7 then
  319. code:=code+8;
  320. {$EndIf ValInternCompiled}
  321. end;
  322. '%' : begin
  323. base:=2;
  324. inc(code);
  325. end;
  326. end;
  327. end;
  328. InitVal:=code;
  329. end;
  330. {$IfDef ValInternCompiled}
  331. Function ValSignedInt(DestSize: longint; Const S: ShortString; var Code: ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_SHORTSTR'];
  332. var
  333. u: ValSInt;
  334. base : byte;
  335. negative : boolean;
  336. temp, prev: ValUInt;
  337. begin
  338. ValSignedInt := 0;
  339. Temp:=0;
  340. Code:=InitVal(s,negative,base);
  341. if Code>length(s) then
  342. exit;
  343. if negative and (s='-2147483648') then
  344. begin
  345. Code:=0;
  346. ValSignedInt:=$80000000;
  347. exit;
  348. end;
  349. while Code<=Length(s) do
  350. begin
  351. case s[Code] of
  352. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  353. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  354. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  355. else
  356. u:=16;
  357. end;
  358. Prev := Temp;
  359. Temp := Temp*ValUInt(base);
  360. If ((base = 10) and
  361. (prev > MaxSIntValue div ValUInt(Base))) or
  362. (Temp < prev) Then
  363. Begin
  364. ValSignedInt := 0;
  365. Exit
  366. End;
  367. if (u>=base) or
  368. ((base = 10) and
  369. (MaxSIntValue-Temp < u)) or
  370. ((base <> 10) and
  371. (MaxUIntValue-Temp < u)) then
  372. begin
  373. ValSignedInt:=0;
  374. exit;
  375. end;
  376. Temp:=Temp+u;
  377. inc(code);
  378. end;
  379. code := 0;
  380. ValSignedInt := ValSInt(Temp);
  381. If Negative Then
  382. ValSignedInt := -ValSignedInt;
  383. If Not(Negative) and (base <> 10) Then
  384. {sign extend the result to allow proper range checking}
  385. Case DestSize of
  386. 1: If (ValSignedInt > High(ShortInt)) and (ValSignedInt <= High(Byte)) Then
  387. ValSignedInt := ValSignedInt or (MaxUIntValue xor High(Byte));
  388. 2: If (ValSignedInt > High(Integer)) and (ValSignedInt <= High(Word)) Then
  389. ValSignedInt := ValSignedInt or (MaxUIntValue xor High(Word));
  390. { Uncomment the folling once full 64bit support is in place
  391. 4: If (ValSignedInt > High(Longint)) and (ValSignedInt <= High(Cardinal)) Then
  392. ValSignedInt := ValSignedInt or (MaxUIntValue xor High(Cardinal));}
  393. End;
  394. end;
  395. Function ValUnsignedInt(Const S: ShortString; var Code: ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_SHORTSTR'];
  396. var
  397. u: ValUInt;
  398. base : byte;
  399. negative : boolean;
  400. prev: ValUInt;
  401. begin
  402. ValUnSignedInt:=0;
  403. Code:=InitVal(s,negative,base);
  404. If Negative or (Code>length(s)) Then
  405. Exit;
  406. while Code<=Length(s) do
  407. begin
  408. case s[Code] of
  409. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  410. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  411. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  412. else
  413. u:=16;
  414. end;
  415. prev := ValUnsignedInt;
  416. ValUnsignedInt:=ValUnsignedInt*ValUInt(base);
  417. If prev > ValUnsignedInt Then
  418. {we've had an overflow. Can't check this with
  419. "If ValUnsignedInt <= (MaxUIntValue div ValUInt(Base)) Then"
  420. because this division always overflows! (JM)}
  421. Begin
  422. ValUnsignedInt := 0;
  423. Exit
  424. End;
  425. if (u>=base) or (MaxUIntValue-ValUnsignedInt < u) then
  426. begin
  427. ValUnsignedInt:=0;
  428. exit;
  429. end;
  430. ValUnsignedInt:=ValUnsignedInt+u;
  431. inc(code);
  432. end;
  433. code := 0;
  434. end;
  435. Function ValFloat(const s : shortstring; var code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_SHORTSTR'];
  436. var
  437. hd,
  438. esign,sign : valreal;
  439. exponent,i : longint;
  440. flags : byte;
  441. begin
  442. ValFloat:=0.0;
  443. code:=1;
  444. exponent:=0;
  445. esign:=1;
  446. flags:=0;
  447. sign:=1;
  448. while (code<=length(s)) and (s[code] in [' ',#9]) do
  449. inc(code);
  450. case s[code] of
  451. '+' : inc(code);
  452. '-' : begin
  453. sign:=-1;
  454. inc(code);
  455. end;
  456. end;
  457. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  458. begin
  459. { Read integer part }
  460. flags:=flags or 1;
  461. valfloat:=valfloat*10;
  462. valfloat:=valfloat+(ord(s[code])-ord('0'));
  463. inc(code);
  464. end;
  465. { Decimal ? }
  466. if (s[code]='.') and (length(s)>=code) then
  467. begin
  468. hd:=0.1;
  469. inc(code);
  470. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  471. begin
  472. { Read fractional part. }
  473. flags:=flags or 2;
  474. valfloat:=valfloat+hd*(ord(s[code])-ord('0'));
  475. hd:=hd/10.0;
  476. inc(code);
  477. end;
  478. end;
  479. { Again, read integer and fractional part}
  480. if flags=0 then
  481. begin
  482. valfloat:=0.0;
  483. exit;
  484. end;
  485. { Exponent ? }
  486. if (upcase(s[code])='E') and (length(s)>=code) then
  487. begin
  488. inc(code);
  489. if s[code]='+' then
  490. inc(code)
  491. else
  492. if s[code]='-' then
  493. begin
  494. esign:=-1;
  495. inc(code);
  496. end;
  497. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  498. begin
  499. valfloat:=0.0;
  500. exit;
  501. end;
  502. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  503. begin
  504. exponent:=exponent*10;
  505. exponent:=exponent+ord(s[code])-ord('0');
  506. inc(code);
  507. end;
  508. end;
  509. { Calculate Exponent }
  510. if esign>0 then
  511. for i:=1 to exponent do
  512. valfloat:=valfloat*10
  513. else
  514. for i:=1 to exponent do
  515. valfloat:=valfloat/10;
  516. { Not all characters are read ? }
  517. if length(s)>=code then
  518. begin
  519. valfloat:=0.0;
  520. exit;
  521. end;
  522. { evaluate sign }
  523. valfloat:=valfloat*sign;
  524. { success ! }
  525. code:=0;
  526. end;
  527. {$ifdef SUPPORT_FIXED}
  528. Function ValFixed(const s : shortstring;var code : ValSInt): Fixed; [public, alias:'FPC_VAL_FIXED_SHORTSTR'];
  529. begin
  530. ValFixed := Fixed(ValFloat(s,code));
  531. end;
  532. {$endif SUPPORT_FIXED}
  533. {$Else ValInternCompiled}
  534. procedure val(const s : shortstring;var l : longint;var code : word);
  535. var
  536. base,u : byte;
  537. negativ : boolean;
  538. begin
  539. l:=0;
  540. Code:=InitVal(s,negativ,base);
  541. if Code>length(s) then
  542. exit;
  543. if negativ and (s='-2147483648') then
  544. begin
  545. Code:=0;
  546. l:=$80000000;
  547. exit;
  548. end;
  549. while Code<=Length(s) do
  550. begin
  551. u:=ord(s[code]);
  552. case u of
  553. 48..57 : u:=u-48;
  554. 65..70 : u:=u-55;
  555. 97..104 : u:=u-87;
  556. else
  557. u:=16;
  558. end;
  559. l:=l*longint(base);
  560. if (u>=base) or ((base=10) and (2147483647-l<longint(u))) then
  561. begin
  562. l:=0;
  563. exit;
  564. end;
  565. l:=l+u;
  566. inc(code);
  567. end;
  568. code := 0;
  569. if negativ then
  570. l:=0-l;
  571. end;
  572. procedure val(const s : shortstring;var l : longint;var code : integer);
  573. begin
  574. val(s,l,word(code));
  575. end;
  576. procedure val(const s : shortstring;var l : longint;var code : longint);
  577. var
  578. cw : word;
  579. begin
  580. val (s,l,cw);
  581. code:=cw;
  582. end;
  583. procedure val(const s : shortstring;var l : longint);
  584. var
  585. code : word;
  586. begin
  587. val (s,l,code);
  588. end;
  589. procedure val(const s : shortstring;var b : byte);
  590. var
  591. l : longint;
  592. begin
  593. val(s,l);
  594. b:=l;
  595. end;
  596. procedure val(const s : shortstring;var b : byte;var code : word);
  597. var
  598. l : longint;
  599. begin
  600. val(s,l,code);
  601. b:=l;
  602. end;
  603. procedure val(const s : shortstring;var b : byte;var code : Integer);
  604. begin
  605. val(s,b,word(code));
  606. end;
  607. procedure val(const s : shortstring;var b : byte;var code : longint);
  608. var
  609. l : longint;
  610. begin
  611. val(s,l,code);
  612. b:=l;
  613. end;
  614. procedure val(const s : shortstring;var b : shortint);
  615. var
  616. l : longint;
  617. begin
  618. val(s,l);
  619. b:=l;
  620. end;
  621. procedure val(const s : shortstring;var b : shortint;var code : word);
  622. var
  623. l : longint;
  624. begin
  625. val(s,l,code);
  626. b:=l;
  627. end;
  628. procedure val(const s : shortstring;var b : shortint;var code : Integer);
  629. begin
  630. val(s,b,word(code));
  631. end;
  632. procedure val(const s : shortstring;var b : shortint;var code : longint);
  633. var
  634. l : longint;
  635. begin
  636. val(s,l,code);
  637. b:=l;
  638. end;
  639. procedure val(const s : shortstring;var b : word);
  640. var
  641. l : longint;
  642. begin
  643. val(s,l);
  644. b:=l;
  645. end;
  646. procedure val(const s : shortstring;var b : word;var code : word);
  647. var
  648. l : longint;
  649. begin
  650. val(s,l,code);
  651. b:=l;
  652. end;
  653. procedure val(const s : shortstring;var b : word;var code : Integer);
  654. begin
  655. val(s,b,word(code));
  656. end;
  657. procedure val(const s : shortstring;var b : word;var code : longint);
  658. var
  659. l : longint;
  660. begin
  661. val(s,l,code);
  662. b:=l;
  663. end;
  664. procedure val(const s : shortstring;var b : integer);
  665. var
  666. l : longint;
  667. begin
  668. val(s,l);
  669. b:=l;
  670. end;
  671. procedure val(const s : shortstring;var b : integer;var code : word);
  672. var
  673. l : longint;
  674. begin
  675. val(s,l,code);
  676. b:=l;
  677. end;
  678. procedure val(const s : shortstring;var b : integer;var code : Integer);
  679. begin
  680. val(s,b,word(code));
  681. end;
  682. procedure val(const s : shortstring;var b : integer;var code : longint);
  683. var
  684. l : longint;
  685. begin
  686. val(s,l,code);
  687. b:=l;
  688. end;
  689. procedure val(const s : shortstring;var v : cardinal;var code : word);
  690. var
  691. negativ : boolean;
  692. base,u : byte;
  693. begin
  694. v:=0;
  695. code:=InitVal(s,negativ,base);
  696. if (Code>length(s)) or negativ then
  697. exit;
  698. while Code<=Length(s) do
  699. begin
  700. u:=ord(s[code]);
  701. case u of
  702. 48..57 : u:=u-48;
  703. 65..70 : u:=u-55;
  704. 97..104 : u:=u-87;
  705. else
  706. u:=16;
  707. end;
  708. cardinal(v):=cardinal(v)*cardinal(longint(base));
  709. if (u>base) or (cardinal($ffffffff)-cardinal(v)>cardinal(longint(u))) then
  710. begin
  711. v:=0;
  712. exit;
  713. end;
  714. v:=v+u;
  715. inc(code);
  716. end;
  717. code:=0;
  718. end;
  719. procedure val(const s : shortstring;var v : cardinal);
  720. var
  721. code : word;
  722. begin
  723. val(s,v,code);
  724. end;
  725. procedure val(const s : shortstring;var v : cardinal;var code : integer);
  726. begin
  727. val(s,v,word(code));
  728. end;
  729. procedure val(const s : shortstring;var v : cardinal;var code : longint);
  730. var
  731. cw : word;
  732. begin
  733. val(s,v,cw);
  734. code:=cw;
  735. end;
  736. procedure val(const s : shortstring;var d : valreal;var code : word);
  737. var
  738. hd,
  739. esign,sign : valreal;
  740. exponent,i : longint;
  741. flags : byte;
  742. const
  743. i10 = 10;
  744. begin
  745. d:=0;
  746. code:=1;
  747. exponent:=0;
  748. esign:=1;
  749. flags:=0;
  750. sign:=1;
  751. while (code<=length(s)) and (s[code] in [' ',#9]) do
  752. inc(code);
  753. case s[code] of
  754. '+' : inc(code);
  755. '-' : begin
  756. sign:=-1;
  757. inc(code);
  758. end;
  759. end;
  760. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  761. begin
  762. { Read integer part }
  763. flags:=flags or 1;
  764. d:=d*i10;
  765. d:=d+(ord(s[code])-ord('0'));
  766. inc(code);
  767. end;
  768. { Decimal ? }
  769. if (s[code]='.') and (length(s)>=code) then
  770. begin
  771. hd:=extended(i1)/extended(i10);
  772. inc(code);
  773. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  774. begin
  775. { Read fractional part. }
  776. flags:=flags or 2;
  777. d:=d+hd*(ord(s[code])-ord('0'));
  778. hd:=hd/i10;
  779. inc(code);
  780. end;
  781. end;
  782. { Again, read integer and fractional part}
  783. if flags=0 then
  784. begin
  785. d:=0;
  786. exit;
  787. end;
  788. { Exponent ? }
  789. if (upcase(s[code])='E') and (length(s)>=code) then
  790. begin
  791. inc(code);
  792. if s[code]='+' then
  793. inc(code)
  794. else
  795. if s[code]='-' then
  796. begin
  797. esign:=-1;
  798. inc(code);
  799. end;
  800. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  801. begin
  802. d:=0;
  803. exit;
  804. end;
  805. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  806. begin
  807. exponent:=exponent*i10;
  808. exponent:=exponent+ord(s[code])-ord('0');
  809. inc(code);
  810. end;
  811. end;
  812. { Calculate Exponent }
  813. if esign>0 then
  814. for i:=1 to exponent do
  815. d:=d*i10
  816. else
  817. for i:=1 to exponent do
  818. d:=d/i10;
  819. { Not all characters are read ? }
  820. if length(s)>=code then
  821. begin
  822. d:=0.0;
  823. exit;
  824. end;
  825. { evalute sign }
  826. d:=d*sign;
  827. { success ! }
  828. code:=0;
  829. end;
  830. procedure val(const s : shortstring;var d : valreal;var code : integer);
  831. begin
  832. val(s,d,word(code));
  833. end;
  834. procedure val(const s : shortstring;var d : valreal;var code : longint);
  835. var
  836. cw : word;
  837. begin
  838. val(s,d,cw);
  839. code:=cw;
  840. end;
  841. procedure val(const s : shortstring;var d : valreal);
  842. var
  843. code : word;
  844. begin
  845. val(s,d,code);
  846. end;
  847. {$ifdef SUPPORT_SINGLE}
  848. procedure val(const s : shortstring;var d : single;var code : word);
  849. var
  850. e : valreal;
  851. begin
  852. val(s,e,code);
  853. d:=e;
  854. end;
  855. procedure val(const s : shortstring;var d : single;var code : integer);
  856. var
  857. e : valreal;
  858. begin
  859. val(s,e,word(code));
  860. d:=e;
  861. end;
  862. procedure val(const s : shortstring;var d : single;var code : longint);
  863. var
  864. cw : word;
  865. e : valreal;
  866. begin
  867. val(s,e,cw);
  868. d:=e;
  869. code:=cw;
  870. end;
  871. procedure val(const s : shortstring;var d : single);
  872. var
  873. code : word;
  874. e : valreal;
  875. begin
  876. val(s,e,code);
  877. d:=e;
  878. end;
  879. {$endif SUPPORT_SINGLE}
  880. {$ifdef DEFAULT_EXTENDED}
  881. { with extended as default the valreal is extended so for real there need
  882. to be a new val }
  883. procedure val(const s : shortstring;var d : real;var code : word);
  884. var
  885. e : valreal;
  886. begin
  887. val(s,e,code);
  888. d:=e;
  889. end;
  890. procedure val(const s : shortstring;var d : real;var code : integer);
  891. var
  892. e : valreal;
  893. begin
  894. val(s,e,word(code));
  895. d:=e;
  896. end;
  897. procedure val(const s : shortstring;var d : real;var code : longint);
  898. var
  899. cw : word;
  900. e : valreal;
  901. begin
  902. val(s,e,cw);
  903. d:=e;
  904. code:=cw;
  905. end;
  906. procedure val(const s : shortstring;var d : real);
  907. var
  908. code : word;
  909. e : valreal;
  910. begin
  911. val(s,e,code);
  912. d:=e;
  913. end;
  914. {$else DEFAULT_EXTENDED}
  915. { when extended is not the default it could still be supported }
  916. {$ifdef SUPPORT_EXTENDED}
  917. procedure val(const s : shortstring;var d : extended;var code : word);
  918. var
  919. e : valreal;
  920. begin
  921. val(s,e,code);
  922. d:=e;
  923. end;
  924. procedure val(const s : shortstring;var d : extended;var code : integer);
  925. var
  926. e : valreal;
  927. begin
  928. val(s,e,word(code));
  929. d:=e;
  930. end;
  931. procedure val(const s : shortstring;var d : extended;var code : longint);
  932. var
  933. cw : word;
  934. e : valreal;
  935. begin
  936. val(s,e,cw);
  937. d:=e;
  938. code:=cw;
  939. end;
  940. procedure val(const s : shortstring;var d : extended);
  941. var
  942. code : word;
  943. e : valreal;
  944. begin
  945. val(s,e,code);
  946. d:=e;
  947. end;
  948. {$endif SUPPORT_EXTENDED}
  949. {$endif DEFAULT_EXTENDED}
  950. {$ifdef SUPPORT_COMP}
  951. procedure val(const s : shortstring;var d : comp;var code : word);
  952. var
  953. e : valreal;
  954. begin
  955. val(s,e,code);
  956. d:=comp(e);
  957. end;
  958. procedure val(const s : shortstring;var d : comp;var code : integer);
  959. var
  960. e : valreal;
  961. begin
  962. val(s,e,word(code));
  963. d:=comp(e);
  964. end;
  965. procedure val(const s : shortstring;var d : comp;var code : longint);
  966. var
  967. cw : word;
  968. e : valreal;
  969. begin
  970. val(s,e,cw);
  971. d:=comp(e);
  972. code:=cw;
  973. end;
  974. procedure val(const s : shortstring;var d : comp);
  975. var
  976. code : word;
  977. e : valreal;
  978. begin
  979. val(s,e,code);
  980. d:=comp(e);
  981. end;
  982. {$endif SUPPORT_COMP}
  983. {$ifdef SUPPORT_FIXED}
  984. procedure val(const s : shortstring;var d : fixed;var code : word);
  985. var
  986. e : valreal;
  987. begin
  988. val(s,e,code);
  989. d:=fixed(e);
  990. end;
  991. procedure val(const s : shortstring;var d : fixed;var code : integer);
  992. var
  993. e : valreal;
  994. begin
  995. val(s,e,word(code));
  996. d:=fixed(e);
  997. end;
  998. procedure val(const s : shortstring;var d : fixed;var code : longint);
  999. var
  1000. cw : word;
  1001. e : valreal;
  1002. begin
  1003. val(s,e,cw);
  1004. d:=fixed(e);
  1005. code:=cw;
  1006. end;
  1007. procedure val(const s : shortstring;var d : fixed);
  1008. var
  1009. code : word;
  1010. e : valreal;
  1011. begin
  1012. val(s,e,code);
  1013. d:=fixed(e);
  1014. end;
  1015. {$endif SUPPORT_FIXED}
  1016. {$EndIf ValInternCompiled}
  1017. Procedure SetString (Var S : Shortstring; Buf : PChar; Len : Longint);
  1018. begin
  1019. Move (Buf[0],S[1],Len);
  1020. S[0]:=chr(len);
  1021. end;
  1022. {
  1023. $Log$
  1024. Revision 1.27 1999-04-08 15:57:54 peter
  1025. + subrange checking for readln()
  1026. Revision 1.26 1999/04/05 12:28:27 michael
  1027. + Fixed insert with char. length byte wrapped around in some cases.
  1028. Revision 1.25 1999/04/01 22:11:50 peter
  1029. * fixed '1.' parsing of val
  1030. Revision 1.24 1999/04/01 22:00:49 peter
  1031. * universal names for str/val (ansistr instead of stransi)
  1032. * '1.' support for val() this is compatible with tp7
  1033. Revision 1.23 1999/03/26 00:24:16 peter
  1034. * last para changed to long for easier pushing with 4 byte aligns
  1035. Revision 1.22 1999/03/16 17:49:36 jonas
  1036. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1037. * in text.inc: changed RTE 106 when read integer values are out of bounds to RTE 201
  1038. * in systemh.inc: disabled "support_fixed" for the i386 because it gave internal errors,
  1039. Revision 1.21 1999/03/10 21:49:03 florian
  1040. * str and val for extended use now int constants to minimize
  1041. rounding error
  1042. Revision 1.20 1999/03/03 15:23:57 michael
  1043. + Added setstring for Delphi compatibility
  1044. Revision 1.19 1999/01/25 20:24:28 peter
  1045. * fixed insert to support again the max string length
  1046. Revision 1.18 1999/01/11 19:26:55 jonas
  1047. * made inster(string,string,index) a bit faster
  1048. + overloaded insert(char,string,index)
  1049. Revision 1.17 1998/12/15 22:43:02 peter
  1050. * removed temp symbols
  1051. Revision 1.16 1998/11/05 10:29:34 pierre
  1052. * fix for length(char) in const expressions
  1053. Revision 1.15 1998/11/04 10:20:50 peter
  1054. * ansistring fixes
  1055. Revision 1.14 1998/10/11 14:30:19 peter
  1056. * small typo :(
  1057. Revision 1.13 1998/10/10 15:28:46 peter
  1058. + read single,fixed
  1059. + val with code:longint
  1060. + val for fixed
  1061. Revision 1.12 1998/09/14 10:48:19 peter
  1062. * FPC_ names
  1063. * Heap manager is now system independent
  1064. Revision 1.11 1998/08/11 21:39:07 peter
  1065. * splitted default_extended from support_extended
  1066. Revision 1.10 1998/08/08 12:28:13 florian
  1067. * a lot small fixes to the extended data type work
  1068. Revision 1.9 1998/07/18 17:14:23 florian
  1069. * strlenint type implemented
  1070. Revision 1.8 1998/07/10 11:02:38 peter
  1071. * support_fixed, becuase fixed is not 100% yet for the m68k
  1072. Revision 1.7 1998/07/02 12:14:19 carl
  1073. * No SINGLE type for non-intel processors!!
  1074. Revision 1.6 1998/06/25 09:44:19 daniel
  1075. + RTLLITE directive to compile minimal RTL.
  1076. Revision 1.5 1998/06/04 23:45:59 peter
  1077. * comp,extended are only i386 added support_comp,support_extended
  1078. Revision 1.4 1998/05/31 14:14:52 peter
  1079. * removed warnings using comp()
  1080. Revision 1.3 1998/05/12 10:42:45 peter
  1081. * moved getopts to inc/, all supported OS's need argc,argv exported
  1082. + strpas, strlen are now exported in the systemunit
  1083. * removed logs
  1084. * removed $ifdef ver_above
  1085. }