sstrings.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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)=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_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_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_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_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_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_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_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):
  290. {$IfDef ValInternCompiled}
  291. TMaxSInt;
  292. {$Else ValInternCompiled}
  293. Word;
  294. {$EndIf ValInternCompiled}
  295. var
  296. Code : Longint;
  297. begin
  298. {Skip Spaces and Tab}
  299. code:=1;
  300. while (code<=length(s)) and (s[code] in [' ',#9]) do
  301. inc(code);
  302. {Sign}
  303. negativ:=false;
  304. case s[code] of
  305. '-' : begin
  306. negativ:=true;
  307. inc(code);
  308. end;
  309. '+' : inc(code);
  310. end;
  311. {Base}
  312. base:=10;
  313. if code<=length(s) then
  314. begin
  315. case s[code] of
  316. '$' : begin
  317. base:=16;
  318. repeat
  319. inc(code);
  320. until (code>=length(s)) or (s[code]<>'0');
  321. {The following isn't correct anymore for 64 bit integers! (JM)}
  322. {$IfNDef ValInternCompiled}
  323. if length(s)-code>7 then
  324. code:=code+8;
  325. {$EndIf ValInternCompiled}
  326. end;
  327. '%' : begin
  328. base:=2;
  329. inc(code);
  330. end;
  331. end;
  332. end;
  333. InitVal:=code;
  334. end;
  335. {$IfDef ValInternCompiled}
  336. Function ValSignedInt(DestSize: longint; Const S: ShortString; var Code: TMaxSInt): TMaxSInt; [public, alias:'FPC_VAL_SINT_SSTRING'];
  337. var
  338. u: TMaxSInt;
  339. base : byte;
  340. negative : boolean;
  341. temp, prev: TMaxUInt;
  342. begin
  343. ValSignedInt := 0;
  344. Temp:=0;
  345. Code:=InitVal(s,negative,base);
  346. if Code>length(s) then
  347. exit;
  348. if negative and (s='-2147483648') then
  349. begin
  350. Code:=0;
  351. ValSignedInt:=$80000000;
  352. exit;
  353. end;
  354. while Code<=Length(s) do
  355. begin
  356. case s[Code] of
  357. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  358. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  359. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  360. else
  361. u:=16;
  362. end;
  363. Prev := Temp;
  364. Temp := Temp*TMaxUInt(base);
  365. If ((base = 10) and
  366. (prev > MaxSIntValue div TMaxUInt(Base))) or
  367. (Temp < prev) Then
  368. Begin
  369. ValSignedInt := 0;
  370. Exit
  371. End;
  372. if (u>=base) or
  373. ((base = 10) and
  374. (MaxSIntValue-Temp < u)) or
  375. ((base <> 10) and
  376. (MaxUIntValue-Temp < u)) then
  377. begin
  378. ValSignedInt:=0;
  379. exit;
  380. end;
  381. Temp:=Temp+u;
  382. inc(code);
  383. end;
  384. code := 0;
  385. ValSignedInt := TMaxSInt(Temp);
  386. If Negative Then
  387. ValSignedInt := -ValSignedInt;
  388. If Not(Negative) and (base <> 10) Then
  389. {sign extend the result to allow proper range checking}
  390. Case DestSize of
  391. 1: If (ValSignedInt > High(ShortInt)) and (ValSignedInt <= High(Byte)) Then
  392. ValSignedInt := ValSignedInt or (MaxUIntValue xor High(Byte));
  393. 2: If (ValSignedInt > High(Integer)) and (ValSignedInt <= High(Word)) Then
  394. ValSignedInt := ValSignedInt or (MaxUIntValue xor High(Word));
  395. { Uncomment the folling once full 64bit support is in place
  396. 4: If (ValSignedInt > High(Longint)) and (ValSignedInt <= High(Cardinal)) Then
  397. ValSignedInt := ValSignedInt or (MaxUIntValue xor High(Cardinal));}
  398. End;
  399. end;
  400. Function ValUnsignedInt(Const S: ShortString; var Code: TMaxSInt): TMaxUInt; [public, alias:'FPC_VAL_UINT_SSTRING'];
  401. var
  402. u: TMaxUInt;
  403. base : byte;
  404. negative : boolean;
  405. prev: TMaxUInt;
  406. begin
  407. ValUnSignedInt:=0;
  408. Code:=InitVal(s,negative,base);
  409. If Negative or (Code>length(s)) Then
  410. Exit;
  411. while Code<=Length(s) do
  412. begin
  413. case s[Code] of
  414. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  415. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  416. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  417. else
  418. u:=16;
  419. end;
  420. prev := ValUnsignedInt;
  421. ValUnsignedInt:=ValUnsignedInt*TMaxUInt(base);
  422. If prev > ValUnsignedInt Then
  423. {we've had an overflow. Can't check this with
  424. "If ValUnsignedInt <= (MaxUIntValue div TMaxUInt(Base)) Then"
  425. because this division always overflows! (JM)}
  426. Begin
  427. ValUnsignedInt := 0;
  428. Exit
  429. End;
  430. if (u>=base) or (MaxUIntValue-ValUnsignedInt < u) then
  431. begin
  432. ValUnsignedInt:=0;
  433. exit;
  434. end;
  435. ValUnsignedInt:=ValUnsignedInt+u;
  436. inc(code);
  437. end;
  438. code := 0;
  439. end;
  440. Function ValFloat(const s : shortstring; var code : TMaxSInt): ValReal; [public, alias:'FPC_VAL_REAL_SSTRING'];
  441. var
  442. hd,
  443. esign,sign : valreal;
  444. exponent,i : longint;
  445. flags : byte;
  446. begin
  447. ValFloat:=0.0;
  448. code:=1;
  449. exponent:=0;
  450. esign:=1;
  451. flags:=0;
  452. sign:=1;
  453. while (code<=length(s)) and (s[code] in [' ',#9]) do
  454. inc(code);
  455. case s[code] of
  456. '+' : inc(code);
  457. '-' : begin
  458. sign:=-1.0;
  459. inc(code);
  460. end;
  461. end;
  462. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  463. begin
  464. { Read integer part }
  465. flags:=flags or 1;
  466. valfloat:=valfloat*10;
  467. valfloat:=valfloat+(ord(s[code])-ord('0'));
  468. inc(code);
  469. end;
  470. { Decimal ? }
  471. if (s[code]='.') and (length(s)>=code) then
  472. begin
  473. hd:=0.1;
  474. inc(code);
  475. { After dot, a number is required. }
  476. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  477. begin
  478. valfloat:=0.0;
  479. exit;
  480. end;
  481. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  482. begin
  483. { Read fractional part. }
  484. flags:=flags or 2;
  485. valfloat:=valfloat+hd*(ord(s[code])-ord('0'));
  486. hd:=hd/10.0;
  487. inc(code);
  488. end;
  489. end;
  490. { Again, read integer and fractional part}
  491. if flags=0 then
  492. begin
  493. valfloat:=0.0;
  494. exit;
  495. end;
  496. { Exponent ? }
  497. if (upcase(s[code])='E') and (length(s)>=code) then
  498. begin
  499. inc(code);
  500. if s[code]='+' then
  501. inc(code)
  502. else
  503. if s[code]='-' then
  504. begin
  505. esign:=-1;
  506. inc(code);
  507. end;
  508. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  509. begin
  510. valfloat:=0.0;
  511. exit;
  512. end;
  513. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  514. begin
  515. exponent:=exponent*10;
  516. exponent:=exponent+ord(s[code])-ord('0');
  517. inc(code);
  518. end;
  519. end;
  520. { Calculate Exponent }
  521. if esign>0 then
  522. for i:=1 to exponent do
  523. valfloat:=valfloat*10
  524. else
  525. for i:=1 to exponent do
  526. valfloat:=valfloat/10;
  527. { Not all characters are read ? }
  528. if length(s)>=code then
  529. begin
  530. valfloat:=0.0;
  531. exit;
  532. end;
  533. { evaluate sign }
  534. valfloat:=valfloat*sign;
  535. { success ! }
  536. code:=0;
  537. end;
  538. {$ifdef SUPPORT_FIXED}
  539. Function ValFixed(const s : shortstring;var code : TMaxSInt): Fixed; [public, alias:'FPC_VAL_FIXED_SSTRING'];
  540. begin
  541. ValFixed := Fixed(ValFloat(s,code));
  542. end;
  543. {$endif SUPPORT_FIXED}
  544. {$Else ValInternCompiled}
  545. procedure val(const s : shortstring;var l : longint;var code : word);
  546. var
  547. base,u : byte;
  548. negativ : boolean;
  549. begin
  550. l:=0;
  551. Code:=InitVal(s,negativ,base);
  552. if Code>length(s) then
  553. exit;
  554. if negativ and (s='-2147483648') then
  555. begin
  556. Code:=0;
  557. l:=$80000000;
  558. exit;
  559. end;
  560. while Code<=Length(s) do
  561. begin
  562. u:=ord(s[code]);
  563. case u of
  564. 48..57 : u:=u-48;
  565. 65..70 : u:=u-55;
  566. 97..104 : u:=u-87;
  567. else
  568. u:=16;
  569. end;
  570. l:=l*longint(base);
  571. if (u>=base) or ((base=10) and (2147483647-l<longint(u))) then
  572. begin
  573. l:=0;
  574. exit;
  575. end;
  576. l:=l+u;
  577. inc(code);
  578. end;
  579. code := 0;
  580. if negativ then
  581. l:=0-l;
  582. end;
  583. procedure val(const s : shortstring;var l : longint;var code : integer);
  584. begin
  585. val(s,l,word(code));
  586. end;
  587. procedure val(const s : shortstring;var l : longint;var code : longint);
  588. var
  589. cw : word;
  590. begin
  591. val (s,l,cw);
  592. code:=cw;
  593. end;
  594. procedure val(const s : shortstring;var l : longint);
  595. var
  596. code : word;
  597. begin
  598. val (s,l,code);
  599. end;
  600. procedure val(const s : shortstring;var b : byte);
  601. var
  602. l : longint;
  603. begin
  604. val(s,l);
  605. b:=l;
  606. end;
  607. procedure val(const s : shortstring;var b : byte;var code : word);
  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 : byte;var code : Integer);
  615. begin
  616. val(s,b,word(code));
  617. end;
  618. procedure val(const s : shortstring;var b : byte;var code : longint);
  619. var
  620. l : longint;
  621. begin
  622. val(s,l,code);
  623. b:=l;
  624. end;
  625. procedure val(const s : shortstring;var b : shortint);
  626. var
  627. l : longint;
  628. begin
  629. val(s,l);
  630. b:=l;
  631. end;
  632. procedure val(const s : shortstring;var b : shortint;var code : word);
  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 : shortint;var code : Integer);
  640. begin
  641. val(s,b,word(code));
  642. end;
  643. procedure val(const s : shortstring;var b : shortint;var code : longint);
  644. var
  645. l : longint;
  646. begin
  647. val(s,l,code);
  648. b:=l;
  649. end;
  650. procedure val(const s : shortstring;var b : word);
  651. var
  652. l : longint;
  653. begin
  654. val(s,l);
  655. b:=l;
  656. end;
  657. procedure val(const s : shortstring;var b : word;var code : word);
  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 : word;var code : Integer);
  665. begin
  666. val(s,b,word(code));
  667. end;
  668. procedure val(const s : shortstring;var b : word;var code : longint);
  669. var
  670. l : longint;
  671. begin
  672. val(s,l,code);
  673. b:=l;
  674. end;
  675. procedure val(const s : shortstring;var b : integer);
  676. var
  677. l : longint;
  678. begin
  679. val(s,l);
  680. b:=l;
  681. end;
  682. procedure val(const s : shortstring;var b : integer;var code : word);
  683. var
  684. l : longint;
  685. begin
  686. val(s,l,code);
  687. b:=l;
  688. end;
  689. procedure val(const s : shortstring;var b : integer;var code : Integer);
  690. begin
  691. val(s,b,word(code));
  692. end;
  693. procedure val(const s : shortstring;var b : integer;var code : longint);
  694. var
  695. l : longint;
  696. begin
  697. val(s,l,code);
  698. b:=l;
  699. end;
  700. procedure val(const s : shortstring;var v : cardinal;var code : word);
  701. var
  702. negativ : boolean;
  703. base,u : byte;
  704. begin
  705. v:=0;
  706. code:=InitVal(s,negativ,base);
  707. if (Code>length(s)) or negativ then
  708. exit;
  709. while Code<=Length(s) do
  710. begin
  711. u:=ord(s[code]);
  712. case u of
  713. 48..57 : u:=u-48;
  714. 65..70 : u:=u-55;
  715. 97..104 : u:=u-87;
  716. else
  717. u:=16;
  718. end;
  719. cardinal(v):=cardinal(v)*cardinal(longint(base));
  720. if (u>base) or (cardinal($ffffffff)-cardinal(v)>cardinal(longint(u))) then
  721. begin
  722. v:=0;
  723. exit;
  724. end;
  725. v:=v+u;
  726. inc(code);
  727. end;
  728. code:=0;
  729. end;
  730. procedure val(const s : shortstring;var v : cardinal);
  731. var
  732. code : word;
  733. begin
  734. val(s,v,code);
  735. end;
  736. procedure val(const s : shortstring;var v : cardinal;var code : integer);
  737. begin
  738. val(s,v,word(code));
  739. end;
  740. procedure val(const s : shortstring;var v : cardinal;var code : longint);
  741. var
  742. cw : word;
  743. begin
  744. val(s,v,cw);
  745. code:=cw;
  746. end;
  747. procedure val(const s : shortstring;var d : valreal;var code : word);
  748. var
  749. hd,
  750. esign,sign : valreal;
  751. exponent,i : longint;
  752. flags : byte;
  753. const
  754. i10 = 10;
  755. begin
  756. d:=0;
  757. code:=1;
  758. exponent:=0;
  759. esign:=1;
  760. flags:=0;
  761. sign:=1;
  762. while (code<=length(s)) and (s[code] in [' ',#9]) do
  763. inc(code);
  764. case s[code] of
  765. '+' : inc(code);
  766. '-' : begin
  767. sign:=-1.0;
  768. inc(code);
  769. end;
  770. end;
  771. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  772. begin
  773. { Read integer part }
  774. flags:=flags or 1;
  775. d:=d*i10;
  776. d:=d+(ord(s[code])-ord('0'));
  777. inc(code);
  778. end;
  779. { Decimal ? }
  780. if (s[code]='.') and (length(s)>=code) then
  781. begin
  782. hd:=extended(i1)/extended(i10);
  783. inc(code);
  784. { After dot, a number is required. }
  785. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  786. begin
  787. d:=0.0;
  788. exit;
  789. end;
  790. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  791. begin
  792. { Read fractional part. }
  793. flags:=flags or 2;
  794. d:=d+hd*(ord(s[code])-ord('0'));
  795. hd:=hd/10.0;
  796. inc(code);
  797. end;
  798. end;
  799. { Again, read integer and fractional part}
  800. if flags=0 then
  801. begin
  802. d:=0.0;
  803. exit;
  804. end;
  805. { Exponent ? }
  806. if (upcase(s[code])='E') and (length(s)>=code) then
  807. begin
  808. inc(code);
  809. if s[code]='+' then
  810. inc(code)
  811. else
  812. if s[code]='-' then
  813. begin
  814. esign:=-1;
  815. inc(code);
  816. end;
  817. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  818. begin
  819. d:=0.0;
  820. exit;
  821. end;
  822. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  823. begin
  824. exponent:=exponent*i10;
  825. exponent:=exponent+ord(s[code])-ord('0');
  826. inc(code);
  827. end;
  828. end;
  829. { Calculate Exponent }
  830. if esign>0 then
  831. for i:=1 to exponent do
  832. d:=d*i10
  833. else
  834. for i:=1 to exponent do
  835. d:=d/i10;
  836. { Not all characters are read ? }
  837. if length(s)>=code then
  838. begin
  839. d:=0.0;
  840. exit;
  841. end;
  842. { evalute sign }
  843. d:=d*sign;
  844. { success ! }
  845. code:=0;
  846. end;
  847. procedure val(const s : shortstring;var d : valreal;var code : integer);
  848. begin
  849. val(s,d,word(code));
  850. end;
  851. procedure val(const s : shortstring;var d : valreal;var code : longint);
  852. var
  853. cw : word;
  854. begin
  855. val(s,d,cw);
  856. code:=cw;
  857. end;
  858. procedure val(const s : shortstring;var d : valreal);
  859. var
  860. code : word;
  861. begin
  862. val(s,d,code);
  863. end;
  864. {$ifdef SUPPORT_SINGLE}
  865. procedure val(const s : shortstring;var d : single;var code : word);
  866. var
  867. e : valreal;
  868. begin
  869. val(s,e,code);
  870. d:=e;
  871. end;
  872. procedure val(const s : shortstring;var d : single;var code : integer);
  873. var
  874. e : valreal;
  875. begin
  876. val(s,e,word(code));
  877. d:=e;
  878. end;
  879. procedure val(const s : shortstring;var d : single;var code : longint);
  880. var
  881. cw : word;
  882. e : valreal;
  883. begin
  884. val(s,e,cw);
  885. d:=e;
  886. code:=cw;
  887. end;
  888. procedure val(const s : shortstring;var d : single);
  889. var
  890. code : word;
  891. e : valreal;
  892. begin
  893. val(s,e,code);
  894. d:=e;
  895. end;
  896. {$endif SUPPORT_SINGLE}
  897. {$ifdef DEFAULT_EXTENDED}
  898. { with extended as default the valreal is extended so for real there need
  899. to be a new val }
  900. procedure val(const s : shortstring;var d : real;var code : word);
  901. var
  902. e : valreal;
  903. begin
  904. val(s,e,code);
  905. d:=e;
  906. end;
  907. procedure val(const s : shortstring;var d : real;var code : integer);
  908. var
  909. e : valreal;
  910. begin
  911. val(s,e,word(code));
  912. d:=e;
  913. end;
  914. procedure val(const s : shortstring;var d : real;var code : longint);
  915. var
  916. cw : word;
  917. e : valreal;
  918. begin
  919. val(s,e,cw);
  920. d:=e;
  921. code:=cw;
  922. end;
  923. procedure val(const s : shortstring;var d : real);
  924. var
  925. code : word;
  926. e : valreal;
  927. begin
  928. val(s,e,code);
  929. d:=e;
  930. end;
  931. {$else DEFAULT_EXTENDED}
  932. { when extended is not the default it could still be supported }
  933. {$ifdef SUPPORT_EXTENDED}
  934. procedure val(const s : shortstring;var d : extended;var code : word);
  935. var
  936. e : valreal;
  937. begin
  938. val(s,e,code);
  939. d:=e;
  940. end;
  941. procedure val(const s : shortstring;var d : extended;var code : integer);
  942. var
  943. e : valreal;
  944. begin
  945. val(s,e,word(code));
  946. d:=e;
  947. end;
  948. procedure val(const s : shortstring;var d : extended;var code : longint);
  949. var
  950. cw : word;
  951. e : valreal;
  952. begin
  953. val(s,e,cw);
  954. d:=e;
  955. code:=cw;
  956. end;
  957. procedure val(const s : shortstring;var d : extended);
  958. var
  959. code : word;
  960. e : valreal;
  961. begin
  962. val(s,e,code);
  963. d:=e;
  964. end;
  965. {$endif SUPPORT_EXTENDED}
  966. {$endif DEFAULT_EXTENDED}
  967. {$ifdef SUPPORT_COMP}
  968. procedure val(const s : shortstring;var d : comp;var code : word);
  969. var
  970. e : valreal;
  971. begin
  972. val(s,e,code);
  973. d:=comp(e);
  974. end;
  975. procedure val(const s : shortstring;var d : comp;var code : integer);
  976. var
  977. e : valreal;
  978. begin
  979. val(s,e,word(code));
  980. d:=comp(e);
  981. end;
  982. procedure val(const s : shortstring;var d : comp;var code : longint);
  983. var
  984. cw : word;
  985. e : valreal;
  986. begin
  987. val(s,e,cw);
  988. d:=comp(e);
  989. code:=cw;
  990. end;
  991. procedure val(const s : shortstring;var d : comp);
  992. var
  993. code : word;
  994. e : valreal;
  995. begin
  996. val(s,e,code);
  997. d:=comp(e);
  998. end;
  999. {$endif SUPPORT_COMP}
  1000. {$ifdef SUPPORT_FIXED}
  1001. procedure val(const s : shortstring;var d : fixed;var code : word);
  1002. var
  1003. e : valreal;
  1004. begin
  1005. val(s,e,code);
  1006. d:=fixed(e);
  1007. end;
  1008. procedure val(const s : shortstring;var d : fixed;var code : integer);
  1009. var
  1010. e : valreal;
  1011. begin
  1012. val(s,e,word(code));
  1013. d:=fixed(e);
  1014. end;
  1015. procedure val(const s : shortstring;var d : fixed;var code : longint);
  1016. var
  1017. cw : word;
  1018. e : valreal;
  1019. begin
  1020. val(s,e,cw);
  1021. d:=fixed(e);
  1022. code:=cw;
  1023. end;
  1024. procedure val(const s : shortstring;var d : fixed);
  1025. var
  1026. code : word;
  1027. e : valreal;
  1028. begin
  1029. val(s,e,code);
  1030. d:=fixed(e);
  1031. end;
  1032. {$endif SUPPORT_FIXED}
  1033. {$EndIf ValInternCompiled}
  1034. Procedure SetString (Var S : Shortstring; Buf : PChar; Len : Longint);
  1035. begin
  1036. Move (Buf[0],S[1],Len);
  1037. S[0]:=chr(len);
  1038. end;
  1039. {
  1040. $Log$
  1041. Revision 1.23 1999-03-26 00:24:16 peter
  1042. * last para changed to long for easier pushing with 4 byte aligns
  1043. Revision 1.22 1999/03/16 17:49:36 jonas
  1044. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1045. * in text.inc: changed RTE 106 when read integer values are out of bounds to RTE 201
  1046. * in systemh.inc: disabled "support_fixed" for the i386 because it gave internal errors,
  1047. Revision 1.21 1999/03/10 21:49:03 florian
  1048. * str and val for extended use now int constants to minimize
  1049. rounding error
  1050. Revision 1.20 1999/03/03 15:23:57 michael
  1051. + Added setstring for Delphi compatibility
  1052. Revision 1.19 1999/01/25 20:24:28 peter
  1053. * fixed insert to support again the max string length
  1054. Revision 1.18 1999/01/11 19:26:55 jonas
  1055. * made inster(string,string,index) a bit faster
  1056. + overloaded insert(char,string,index)
  1057. Revision 1.17 1998/12/15 22:43:02 peter
  1058. * removed temp symbols
  1059. Revision 1.16 1998/11/05 10:29:34 pierre
  1060. * fix for length(char) in const expressions
  1061. Revision 1.15 1998/11/04 10:20:50 peter
  1062. * ansistring fixes
  1063. Revision 1.14 1998/10/11 14:30:19 peter
  1064. * small typo :(
  1065. Revision 1.13 1998/10/10 15:28:46 peter
  1066. + read single,fixed
  1067. + val with code:longint
  1068. + val for fixed
  1069. Revision 1.12 1998/09/14 10:48:19 peter
  1070. * FPC_ names
  1071. * Heap manager is now system independent
  1072. Revision 1.11 1998/08/11 21:39:07 peter
  1073. * splitted default_extended from support_extended
  1074. Revision 1.10 1998/08/08 12:28:13 florian
  1075. * a lot small fixes to the extended data type work
  1076. Revision 1.9 1998/07/18 17:14:23 florian
  1077. * strlenint type implemented
  1078. Revision 1.8 1998/07/10 11:02:38 peter
  1079. * support_fixed, becuase fixed is not 100% yet for the m68k
  1080. Revision 1.7 1998/07/02 12:14:19 carl
  1081. * No SINGLE type for non-intel processors!!
  1082. Revision 1.6 1998/06/25 09:44:19 daniel
  1083. + RTLLITE directive to compile minimal RTL.
  1084. Revision 1.5 1998/06/04 23:45:59 peter
  1085. * comp,extended are only i386 added support_comp,support_extended
  1086. Revision 1.4 1998/05/31 14:14:52 peter
  1087. * removed warnings using comp()
  1088. Revision 1.3 1998/05/12 10:42:45 peter
  1089. * moved getopts to inc/, all supported OS's need argc,argv exported
  1090. + strpas, strlen are now exported in the systemunit
  1091. * removed logs
  1092. * removed $ifdef ver_above
  1093. }