sstrings.inc 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. {$ifdef INTERNDOUBLE}
  243. procedure ShortStr_Float(d : ValReal;len,fr,rt : longint;var s : shortstring);[public,alias:'FPC_SHORTSTR_FLOAT'];
  244. begin
  245. str_real(len,fr,d,treal_type(rt),s);
  246. end;
  247. {$else}
  248. {$ifdef SUPPORT_SINGLE}
  249. procedure ShortStr_Single(d : single;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_'+{$ifdef INTERNDOUBLE}'S32REAL'{$else}'SINGLE'{$endif}];
  250. begin
  251. str_real(len,fr,d,rt_s32real,s);
  252. end;
  253. {$endif}
  254. {$ifdef SUPPORT_DOUBLE}
  255. procedure ShortStr_Real(d : real;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_'+{$ifdef INTERNDOUBLE}'S64'+{$endif}'REAL'];
  256. begin
  257. str_real(len,fr,d,rt_s64real,s);
  258. end;
  259. {$endif SUPPORT_S64REAL}
  260. {$ifdef SUPPORT_EXTENDED}
  261. procedure ShortStr_Extended(d : extended;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_'+{$ifdef INTERNDOUBLE}'S80REAL'{$else}'EXTENDED'{$endif}];
  262. begin
  263. str_real(len,fr,d,rt_s80real,s);
  264. end;
  265. {$endif SUPPORT_S80REAL}
  266. {$ifdef SUPPORT_COMP}
  267. procedure ShortStr_Comp(d : comp;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_'+{$ifdef INTERNDOUBLE}'C64BIT'{$else}'COMP'{$endif}];
  268. begin
  269. str_real(len,fr,d,rt_c64bit,s);
  270. end;
  271. {$endif SUPPORT_C64BIT}
  272. {$ifdef SUPPORT_FIXED}
  273. procedure ShortStr_Fixed(d : fixed;len,fr : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_'+{$ifdef INTERNDOUBLE}'F16BIT'{$else}'FIXED'{$endif}];
  274. begin
  275. str_real(len,fr,d,rt_f32bit,s);
  276. end;
  277. {$endif SUPPORT_F16BIT}
  278. {$endif}
  279. procedure int_str_longint(v : longint;len : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_LONGINT'];
  280. begin
  281. int_str(v,s);
  282. if length(s)<len then
  283. s:=space(len-length(s))+s;
  284. end;
  285. procedure int_str_cardinal(v : cardinal;len : longint;var s : shortstring);[public,alias:'FPC_'+{$ifdef NOSTRANSI}'SHORT'+{$endif}'STR_CARDINAL'];
  286. begin
  287. int_str(v,s);
  288. if length(s)<len then
  289. s:=space(len-length(s))+s;
  290. end;
  291. {*****************************************************************************
  292. Val() Functions
  293. *****************************************************************************}
  294. Function InitVal(const s:shortstring;var negativ:boolean;var base:byte):ValSInt;
  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: ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_SHORTSTR'];
  337. var
  338. u: ValSInt;
  339. base : byte;
  340. negative : boolean;
  341. temp, prev: ValUInt;
  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*ValUInt(base);
  365. If ((base = 10) and
  366. (prev > MaxSIntValue div ValUInt(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 := ValSInt(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: ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_SHORTSTR'];
  401. var
  402. u: ValUInt;
  403. base : byte;
  404. negative : boolean;
  405. prev: ValUInt;
  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*ValUInt(base);
  422. If prev > ValUnsignedInt Then
  423. {we've had an overflow. Can't check this with
  424. "If ValUnsignedInt <= (MaxUIntValue div ValUInt(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 : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_SHORTSTR'];
  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;
  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. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  476. begin
  477. { Read fractional part. }
  478. flags:=flags or 2;
  479. valfloat:=valfloat+hd*(ord(s[code])-ord('0'));
  480. hd:=hd/10.0;
  481. inc(code);
  482. end;
  483. end;
  484. { Again, read integer and fractional part}
  485. if flags=0 then
  486. begin
  487. valfloat:=0.0;
  488. exit;
  489. end;
  490. { Exponent ? }
  491. if (upcase(s[code])='E') and (length(s)>=code) then
  492. begin
  493. inc(code);
  494. if s[code]='+' then
  495. inc(code)
  496. else
  497. if s[code]='-' then
  498. begin
  499. esign:=-1;
  500. inc(code);
  501. end;
  502. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  503. begin
  504. valfloat:=0.0;
  505. exit;
  506. end;
  507. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  508. begin
  509. exponent:=exponent*10;
  510. exponent:=exponent+ord(s[code])-ord('0');
  511. inc(code);
  512. end;
  513. end;
  514. { Calculate Exponent }
  515. if esign>0 then
  516. for i:=1 to exponent do
  517. valfloat:=valfloat*10
  518. else
  519. for i:=1 to exponent do
  520. valfloat:=valfloat/10;
  521. { Not all characters are read ? }
  522. if length(s)>=code then
  523. begin
  524. valfloat:=0.0;
  525. exit;
  526. end;
  527. { evaluate sign }
  528. valfloat:=valfloat*sign;
  529. { success ! }
  530. code:=0;
  531. end;
  532. {$ifdef SUPPORT_FIXED}
  533. Function ValFixed(const s : shortstring;var code : ValSInt): Fixed; [public, alias:'FPC_VAL_FIXED_SHORTSTR'];
  534. begin
  535. ValFixed := Fixed(ValFloat(s,code));
  536. end;
  537. {$endif SUPPORT_FIXED}
  538. {$Else ValInternCompiled}
  539. procedure val(const s : shortstring;var l : longint;var code : word);
  540. var
  541. base,u : byte;
  542. negativ : boolean;
  543. begin
  544. l:=0;
  545. Code:=InitVal(s,negativ,base);
  546. if Code>length(s) then
  547. exit;
  548. if negativ and (s='-2147483648') then
  549. begin
  550. Code:=0;
  551. l:=$80000000;
  552. exit;
  553. end;
  554. while Code<=Length(s) do
  555. begin
  556. u:=ord(s[code]);
  557. case u of
  558. 48..57 : u:=u-48;
  559. 65..70 : u:=u-55;
  560. 97..104 : u:=u-87;
  561. else
  562. u:=16;
  563. end;
  564. l:=l*longint(base);
  565. if (u>=base) or ((base=10) and (2147483647-l<longint(u))) then
  566. begin
  567. l:=0;
  568. exit;
  569. end;
  570. l:=l+u;
  571. inc(code);
  572. end;
  573. code := 0;
  574. if negativ then
  575. l:=0-l;
  576. end;
  577. procedure val(const s : shortstring;var l : longint;var code : integer);
  578. begin
  579. val(s,l,word(code));
  580. end;
  581. procedure val(const s : shortstring;var l : longint;var code : longint);
  582. var
  583. cw : word;
  584. begin
  585. val (s,l,cw);
  586. code:=cw;
  587. end;
  588. procedure val(const s : shortstring;var l : longint);
  589. var
  590. code : word;
  591. begin
  592. val (s,l,code);
  593. end;
  594. procedure val(const s : shortstring;var b : byte);
  595. var
  596. l : longint;
  597. begin
  598. val(s,l);
  599. b:=l;
  600. end;
  601. procedure val(const s : shortstring;var b : byte;var code : word);
  602. var
  603. l : longint;
  604. begin
  605. val(s,l,code);
  606. b:=l;
  607. end;
  608. procedure val(const s : shortstring;var b : byte;var code : Integer);
  609. begin
  610. val(s,b,word(code));
  611. end;
  612. procedure val(const s : shortstring;var b : byte;var code : longint);
  613. var
  614. l : longint;
  615. begin
  616. val(s,l,code);
  617. b:=l;
  618. end;
  619. procedure val(const s : shortstring;var b : shortint);
  620. var
  621. l : longint;
  622. begin
  623. val(s,l);
  624. b:=l;
  625. end;
  626. procedure val(const s : shortstring;var b : shortint;var code : word);
  627. var
  628. l : longint;
  629. begin
  630. val(s,l,code);
  631. b:=l;
  632. end;
  633. procedure val(const s : shortstring;var b : shortint;var code : Integer);
  634. begin
  635. val(s,b,word(code));
  636. end;
  637. procedure val(const s : shortstring;var b : shortint;var code : longint);
  638. var
  639. l : longint;
  640. begin
  641. val(s,l,code);
  642. b:=l;
  643. end;
  644. procedure val(const s : shortstring;var b : word);
  645. var
  646. l : longint;
  647. begin
  648. val(s,l);
  649. b:=l;
  650. end;
  651. procedure val(const s : shortstring;var b : word;var code : word);
  652. var
  653. l : longint;
  654. begin
  655. val(s,l,code);
  656. b:=l;
  657. end;
  658. procedure val(const s : shortstring;var b : word;var code : Integer);
  659. begin
  660. val(s,b,word(code));
  661. end;
  662. procedure val(const s : shortstring;var b : word;var code : longint);
  663. var
  664. l : longint;
  665. begin
  666. val(s,l,code);
  667. b:=l;
  668. end;
  669. procedure val(const s : shortstring;var b : integer);
  670. var
  671. l : longint;
  672. begin
  673. val(s,l);
  674. b:=l;
  675. end;
  676. procedure val(const s : shortstring;var b : integer;var code : word);
  677. var
  678. l : longint;
  679. begin
  680. val(s,l,code);
  681. b:=l;
  682. end;
  683. procedure val(const s : shortstring;var b : integer;var code : Integer);
  684. begin
  685. val(s,b,word(code));
  686. end;
  687. procedure val(const s : shortstring;var b : integer;var code : longint);
  688. var
  689. l : longint;
  690. begin
  691. val(s,l,code);
  692. b:=l;
  693. end;
  694. procedure val(const s : shortstring;var v : cardinal;var code : word);
  695. var
  696. negativ : boolean;
  697. base,u : byte;
  698. begin
  699. v:=0;
  700. code:=InitVal(s,negativ,base);
  701. if (Code>length(s)) or negativ then
  702. exit;
  703. while Code<=Length(s) do
  704. begin
  705. u:=ord(s[code]);
  706. case u of
  707. 48..57 : u:=u-48;
  708. 65..70 : u:=u-55;
  709. 97..104 : u:=u-87;
  710. else
  711. u:=16;
  712. end;
  713. cardinal(v):=cardinal(v)*cardinal(longint(base));
  714. if (u>base) or (cardinal($ffffffff)-cardinal(v)>cardinal(longint(u))) then
  715. begin
  716. v:=0;
  717. exit;
  718. end;
  719. v:=v+u;
  720. inc(code);
  721. end;
  722. code:=0;
  723. end;
  724. procedure val(const s : shortstring;var v : cardinal);
  725. var
  726. code : word;
  727. begin
  728. val(s,v,code);
  729. end;
  730. procedure val(const s : shortstring;var v : cardinal;var code : integer);
  731. begin
  732. val(s,v,word(code));
  733. end;
  734. procedure val(const s : shortstring;var v : cardinal;var code : longint);
  735. var
  736. cw : word;
  737. begin
  738. val(s,v,cw);
  739. code:=cw;
  740. end;
  741. procedure val(const s : shortstring;var d : valreal;var code : word);
  742. var
  743. hd,
  744. esign,sign : valreal;
  745. exponent,i : longint;
  746. flags : byte;
  747. const
  748. i10 = 10;
  749. begin
  750. d:=0;
  751. code:=1;
  752. exponent:=0;
  753. esign:=1;
  754. flags:=0;
  755. sign:=1;
  756. while (code<=length(s)) and (s[code] in [' ',#9]) do
  757. inc(code);
  758. case s[code] of
  759. '+' : inc(code);
  760. '-' : begin
  761. sign:=-1;
  762. inc(code);
  763. end;
  764. end;
  765. while (Code<=Length(s)) and (s[code] in ['0'..'9']) do
  766. begin
  767. { Read integer part }
  768. flags:=flags or 1;
  769. d:=d*i10;
  770. d:=d+(ord(s[code])-ord('0'));
  771. inc(code);
  772. end;
  773. { Decimal ? }
  774. if (s[code]='.') and (length(s)>=code) then
  775. begin
  776. hd:=extended(i1)/extended(i10);
  777. inc(code);
  778. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  779. begin
  780. { Read fractional part. }
  781. flags:=flags or 2;
  782. d:=d+hd*(ord(s[code])-ord('0'));
  783. hd:=hd/i10;
  784. inc(code);
  785. end;
  786. end;
  787. { Again, read integer and fractional part}
  788. if flags=0 then
  789. begin
  790. d:=0;
  791. exit;
  792. end;
  793. { Exponent ? }
  794. if (upcase(s[code])='E') and (length(s)>=code) then
  795. begin
  796. inc(code);
  797. if s[code]='+' then
  798. inc(code)
  799. else
  800. if s[code]='-' then
  801. begin
  802. esign:=-1;
  803. inc(code);
  804. end;
  805. if not(s[code] in ['0'..'9']) or (length(s)<code) then
  806. begin
  807. d:=0;
  808. exit;
  809. end;
  810. while (s[code] in ['0'..'9']) and (length(s)>=code) do
  811. begin
  812. exponent:=exponent*i10;
  813. exponent:=exponent+ord(s[code])-ord('0');
  814. inc(code);
  815. end;
  816. end;
  817. { Calculate Exponent }
  818. if esign>0 then
  819. for i:=1 to exponent do
  820. d:=d*i10
  821. else
  822. for i:=1 to exponent do
  823. d:=d/i10;
  824. { Not all characters are read ? }
  825. if length(s)>=code then
  826. begin
  827. d:=0.0;
  828. exit;
  829. end;
  830. { evalute sign }
  831. d:=d*sign;
  832. { success ! }
  833. code:=0;
  834. end;
  835. procedure val(const s : shortstring;var d : valreal;var code : integer);
  836. begin
  837. val(s,d,word(code));
  838. end;
  839. procedure val(const s : shortstring;var d : valreal;var code : longint);
  840. var
  841. cw : word;
  842. begin
  843. val(s,d,cw);
  844. code:=cw;
  845. end;
  846. procedure val(const s : shortstring;var d : valreal);
  847. var
  848. code : word;
  849. begin
  850. val(s,d,code);
  851. end;
  852. {$ifdef SUPPORT_SINGLE}
  853. procedure val(const s : shortstring;var d : single;var code : word);
  854. var
  855. e : valreal;
  856. begin
  857. val(s,e,code);
  858. d:=e;
  859. end;
  860. procedure val(const s : shortstring;var d : single;var code : integer);
  861. var
  862. e : valreal;
  863. begin
  864. val(s,e,word(code));
  865. d:=e;
  866. end;
  867. procedure val(const s : shortstring;var d : single;var code : longint);
  868. var
  869. cw : word;
  870. e : valreal;
  871. begin
  872. val(s,e,cw);
  873. d:=e;
  874. code:=cw;
  875. end;
  876. procedure val(const s : shortstring;var d : single);
  877. var
  878. code : word;
  879. e : valreal;
  880. begin
  881. val(s,e,code);
  882. d:=e;
  883. end;
  884. {$endif SUPPORT_SINGLE}
  885. {$ifdef DEFAULT_EXTENDED}
  886. { with extended as default the valreal is extended so for real there need
  887. to be a new val }
  888. procedure val(const s : shortstring;var d : real;var code : word);
  889. var
  890. e : valreal;
  891. begin
  892. val(s,e,code);
  893. d:=e;
  894. end;
  895. procedure val(const s : shortstring;var d : real;var code : integer);
  896. var
  897. e : valreal;
  898. begin
  899. val(s,e,word(code));
  900. d:=e;
  901. end;
  902. procedure val(const s : shortstring;var d : real;var code : longint);
  903. var
  904. cw : word;
  905. e : valreal;
  906. begin
  907. val(s,e,cw);
  908. d:=e;
  909. code:=cw;
  910. end;
  911. procedure val(const s : shortstring;var d : real);
  912. var
  913. code : word;
  914. e : valreal;
  915. begin
  916. val(s,e,code);
  917. d:=e;
  918. end;
  919. {$else DEFAULT_EXTENDED}
  920. { when extended is not the default it could still be supported }
  921. {$ifdef SUPPORT_EXTENDED}
  922. procedure val(const s : shortstring;var d : extended;var code : word);
  923. var
  924. e : valreal;
  925. begin
  926. val(s,e,code);
  927. d:=e;
  928. end;
  929. procedure val(const s : shortstring;var d : extended;var code : integer);
  930. var
  931. e : valreal;
  932. begin
  933. val(s,e,word(code));
  934. d:=e;
  935. end;
  936. procedure val(const s : shortstring;var d : extended;var code : longint);
  937. var
  938. cw : word;
  939. e : valreal;
  940. begin
  941. val(s,e,cw);
  942. d:=e;
  943. code:=cw;
  944. end;
  945. procedure val(const s : shortstring;var d : extended);
  946. var
  947. code : word;
  948. e : valreal;
  949. begin
  950. val(s,e,code);
  951. d:=e;
  952. end;
  953. {$endif SUPPORT_EXTENDED}
  954. {$endif DEFAULT_EXTENDED}
  955. {$ifdef SUPPORT_COMP}
  956. procedure val(const s : shortstring;var d : comp;var code : word);
  957. var
  958. e : valreal;
  959. begin
  960. val(s,e,code);
  961. d:=comp(e);
  962. end;
  963. procedure val(const s : shortstring;var d : comp;var code : integer);
  964. var
  965. e : valreal;
  966. begin
  967. val(s,e,word(code));
  968. d:=comp(e);
  969. end;
  970. procedure val(const s : shortstring;var d : comp;var code : longint);
  971. var
  972. cw : word;
  973. e : valreal;
  974. begin
  975. val(s,e,cw);
  976. d:=comp(e);
  977. code:=cw;
  978. end;
  979. procedure val(const s : shortstring;var d : comp);
  980. var
  981. code : word;
  982. e : valreal;
  983. begin
  984. val(s,e,code);
  985. d:=comp(e);
  986. end;
  987. {$endif SUPPORT_COMP}
  988. {$ifdef SUPPORT_FIXED}
  989. procedure val(const s : shortstring;var d : fixed;var code : word);
  990. var
  991. e : valreal;
  992. begin
  993. val(s,e,code);
  994. d:=fixed(e);
  995. end;
  996. procedure val(const s : shortstring;var d : fixed;var code : integer);
  997. var
  998. e : valreal;
  999. begin
  1000. val(s,e,word(code));
  1001. d:=fixed(e);
  1002. end;
  1003. procedure val(const s : shortstring;var d : fixed;var code : longint);
  1004. var
  1005. cw : word;
  1006. e : valreal;
  1007. begin
  1008. val(s,e,cw);
  1009. d:=fixed(e);
  1010. code:=cw;
  1011. end;
  1012. procedure val(const s : shortstring;var d : fixed);
  1013. var
  1014. code : word;
  1015. e : valreal;
  1016. begin
  1017. val(s,e,code);
  1018. d:=fixed(e);
  1019. end;
  1020. {$endif SUPPORT_FIXED}
  1021. {$EndIf ValInternCompiled}
  1022. Procedure SetString (Var S : Shortstring; Buf : PChar; Len : Longint);
  1023. begin
  1024. Move (Buf[0],S[1],Len);
  1025. S[0]:=chr(len);
  1026. end;
  1027. {
  1028. $Log$
  1029. Revision 1.28 1999-05-06 09:05:13 peter
  1030. * generic write_float str_float
  1031. Revision 1.27 1999/04/08 15:57:54 peter
  1032. + subrange checking for readln()
  1033. Revision 1.26 1999/04/05 12:28:27 michael
  1034. + Fixed insert with char. length byte wrapped around in some cases.
  1035. Revision 1.25 1999/04/01 22:11:50 peter
  1036. * fixed '1.' parsing of val
  1037. Revision 1.24 1999/04/01 22:00:49 peter
  1038. * universal names for str/val (ansistr instead of stransi)
  1039. * '1.' support for val() this is compatible with tp7
  1040. Revision 1.23 1999/03/26 00:24:16 peter
  1041. * last para changed to long for easier pushing with 4 byte aligns
  1042. Revision 1.22 1999/03/16 17:49:36 jonas
  1043. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1044. * in text.inc: changed RTE 106 when read integer values are out of bounds to RTE 201
  1045. * in systemh.inc: disabled "support_fixed" for the i386 because it gave internal errors,
  1046. Revision 1.21 1999/03/10 21:49:03 florian
  1047. * str and val for extended use now int constants to minimize
  1048. rounding error
  1049. Revision 1.20 1999/03/03 15:23:57 michael
  1050. + Added setstring for Delphi compatibility
  1051. Revision 1.19 1999/01/25 20:24:28 peter
  1052. * fixed insert to support again the max string length
  1053. Revision 1.18 1999/01/11 19:26:55 jonas
  1054. * made inster(string,string,index) a bit faster
  1055. + overloaded insert(char,string,index)
  1056. Revision 1.17 1998/12/15 22:43:02 peter
  1057. * removed temp symbols
  1058. Revision 1.16 1998/11/05 10:29:34 pierre
  1059. * fix for length(char) in const expressions
  1060. Revision 1.15 1998/11/04 10:20:50 peter
  1061. * ansistring fixes
  1062. Revision 1.14 1998/10/11 14:30:19 peter
  1063. * small typo :(
  1064. Revision 1.13 1998/10/10 15:28:46 peter
  1065. + read single,fixed
  1066. + val with code:longint
  1067. + val for fixed
  1068. Revision 1.12 1998/09/14 10:48:19 peter
  1069. * FPC_ names
  1070. * Heap manager is now system independent
  1071. Revision 1.11 1998/08/11 21:39:07 peter
  1072. * splitted default_extended from support_extended
  1073. Revision 1.10 1998/08/08 12:28:13 florian
  1074. * a lot small fixes to the extended data type work
  1075. Revision 1.9 1998/07/18 17:14:23 florian
  1076. * strlenint type implemented
  1077. Revision 1.8 1998/07/10 11:02:38 peter
  1078. * support_fixed, becuase fixed is not 100% yet for the m68k
  1079. Revision 1.7 1998/07/02 12:14:19 carl
  1080. * No SINGLE type for non-intel processors!!
  1081. Revision 1.6 1998/06/25 09:44:19 daniel
  1082. + RTLLITE directive to compile minimal RTL.
  1083. Revision 1.5 1998/06/04 23:45:59 peter
  1084. * comp,extended are only i386 added support_comp,support_extended
  1085. Revision 1.4 1998/05/31 14:14:52 peter
  1086. * removed warnings using comp()
  1087. Revision 1.3 1998/05/12 10:42:45 peter
  1088. * moved getopts to inc/, all supported OS's need argc,argv exported
  1089. + strpas, strlen are now exported in the systemunit
  1090. * removed logs
  1091. * removed $ifdef ver_above
  1092. }