Browse Source

* fixed declaration of val_int64 (removed destsize parameter)
* fixed val_int64 and val_qword so they reject invalid input
(u >= base)
* when reading a number, invalid input is removed from the input
buffer (+ it should be faster as well)

Jonas Maebe 25 years ago
parent
commit
49b6de7c9e
2 changed files with 34 additions and 77 deletions
  1. 12 5
      rtl/inc/int64.inc
  2. 22 72
      rtl/inc/text.inc

+ 12 - 5
rtl/inc/int64.inc

@@ -159,7 +159,7 @@
          signed : boolean;
          r,nq,zq : qword;
 
-      begin    
+      begin
          if n=0 then
            HandleErrorFrame(200,get_frame);
          if n<0 then
@@ -373,7 +373,7 @@
        s:=ss;
     end;
 
-  Function ValInt64(DestSize: longint; Const S: ShortString; var Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR'];
+  Function ValInt64(Const S: ShortString; var Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR'];
 
     var
        u, temp, prev : Int64;
@@ -404,7 +404,7 @@
        end;
        Prev:=Temp;
        Temp:=Temp*Int64(base);
-       if (Temp<prev) Then
+       if (Temp<prev) or (u >= base) Then
          Begin
            ValInt64:=0;
            Exit
@@ -446,7 +446,7 @@
        end;
        prev := ValQWord;
        ValQWord:=ValQWord*QWord(base);
-       If (prev>ValQWord) or (u>base) Then
+       If (prev>ValQWord) or (u>=base) Then
          Begin
            ValQWord := 0;
            Exit
@@ -466,7 +466,14 @@
 
 {
   $Log$
-  Revision 1.19  2000-02-09 22:19:24  florian
+  Revision 1.20  2000-03-17 21:27:56  jonas
+    * fixed declaration of val_int64 (removed destsize parameter)
+    * fixed val_int64 and val_qword so they reject invalid input
+      (u >= base)
+    * when reading a number, invalid input is removed from the input
+      buffer (+ it should be faster as well)
+
+  Revision 1.19  2000/02/09 22:19:24  florian
     + helper routine for <int64> mod <in64> added
 
   Revision 1.18  2000/02/09 16:59:30  peter

+ 22 - 72
rtl/inc/text.inc

@@ -659,48 +659,15 @@ begin
 end;
 
 
-Function ReadSign(var f:TextRec;var s:string):Boolean;
-{
-  Read + and - sign, return true if buffer is empty
-}
-begin
-  ReadSign:=(not (f.Bufptr^[f.BufPos] in ['-','+'])) or NextChar(f,s);
-end;
-
-
-Function ReadBase(var f:TextRec;var s:string;var Base:longint):boolean;
-{
-  Read the base $ For 16 and % For 2, if buffer is empty return true
-}
-begin
-  case f.BufPtr^[f.BufPos] of
-   '$' : Base:=16;
-   '%' : Base:=2;
-  else
-   Base:=10;
-  end;
-  ReadBase:=(Base=10) or NextChar(f,s);
-end;
-
-
-Function ReadNumeric(var f:TextRec;var s:string;base:longint):Boolean;
+procedure ReadNumeric(var f:TextRec;var s:string);
 {
   Read numeric input, if buffer is empty then return True
 }
-var
-  c : char;
 begin
-  ReadNumeric:=false;
-  c:=f.BufPtr^[f.BufPos];
-  while ((base>=10) and (c in ['0'..'9'])) or
-        ((base=16) and (c in ['A'..'F','a'..'f'])) or
-        ((base=2) and (c in ['0'..'1'])) do
-   begin
-     if not NextChar(f,s) then
+  repeat
+    if not NextChar(f,s) then
       exit;
-     c:=f.BufPtr^[f.BufPos];
-   end;
-  ReadNumeric:=true;
+  until f.BufPtr^[f.BufPos] in [#9,#10,#13,' '];
 end;
 
 
@@ -879,7 +846,6 @@ Function Read_SInt(var f : TextRec):ValSInt;[Public,Alias:'FPC_READ_TEXT_SINT'];
 var
   hs   : String;
   code : Longint;
-  base : longint;
 Begin
   Read_SInt:=0;
 { Leave if error or not open file, else check for empty buf }
@@ -896,8 +862,8 @@ Begin
   If f.BufPos>=f.BufEnd Then
    FileFunc(f.InOutFunc)(f);
   hs:='';
-  if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
-   ReadNumeric(f,hs,Base);
+  if IgnoreSpaces(f) then
+   ReadNumeric(f,hs);
   Val(hs,Read_SInt,code);
   If code<>0 Then
    InOutRes:=106;
@@ -908,7 +874,6 @@ Function Read_UInt(var f : TextRec):ValUInt;[Public,Alias:'FPC_READ_TEXT_UINT'];
 var
   hs   : String;
   code : longint;
-  base : longint;
 Begin
   Read_UInt:=0;
 { Leave if error or not open file, else check for empty buf }
@@ -925,8 +890,8 @@ Begin
   If f.BufPos>=f.BufEnd Then
    FileFunc(f.InOutFunc)(f);
   hs:='';
-  if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
-   ReadNumeric(f,hs,Base);
+  if IgnoreSpaces(f) then
+   ReadNumeric(f,hs);
   val(hs,Read_UInt,code);
   If code<>0 Then
    InOutRes:=106;
@@ -953,28 +918,8 @@ begin
   If f.BufPos>=f.BufEnd Then
    FileFunc(f.InOutFunc)(f);
   hs:='';
-  if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
-   begin
-   { First check for a . }
-     if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
-      begin
-        hs:=hs+'.';
-        Inc(f.BufPos);
-        If f.BufPos>=f.BufEnd Then
-         FileFunc(f.InOutFunc)(f);
-        ReadNumeric(f,hs,10);
-      end;
-   { Also when a point is found check for a E }
-     if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
-      begin
-        hs:=hs+'E';
-        Inc(f.BufPos);
-        If f.BufPos>=f.BufEnd Then
-         FileFunc(f.InOutFunc)(f);
-        if ReadSign(f,hs) then
-         ReadNumeric(f,hs,10);
-      end;
-   end;
+  if IgnoreSpaces(f) then
+    ReadNumeric(f,hs);
   val(hs,Read_Float,code);
   If code<>0 Then
    InOutRes:=106;
@@ -986,7 +931,6 @@ function Read_QWord(var f : textrec) : qword;[public,alias:'FPC_READ_TEXT_QWORD'
 var
   hs   : String;
   code : longint;
-  base : longint;
 Begin
   Read_QWord:=0;
   { Leave if error or not open file, else check for empty buf }
@@ -1003,8 +947,8 @@ Begin
   If f.BufPos>=f.BufEnd Then
    FileFunc(f.InOutFunc)(f);
   hs:='';
-  if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
-   ReadNumeric(f,hs,Base);
+  if IgnoreSpaces(f) then
+   ReadNumeric(f,hs);
   val(hs,Read_QWord,code);
   If code<>0 Then
    InOutRes:=106;
@@ -1014,7 +958,6 @@ function Read_Int64(var f : textrec) : int64;[public,alias:'FPC_READ_TEXT_INT64'
 var
   hs   : String;
   code : Longint;
-  base : longint;
 Begin
   Read_Int64:=0;
 { Leave if error or not open file, else check for empty buf }
@@ -1031,8 +974,8 @@ Begin
   If f.BufPos>=f.BufEnd Then
    FileFunc(f.InOutFunc)(f);
   hs:='';
-  if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
-   ReadNumeric(f,hs,Base);
+  if IgnoreSpaces(f) then
+   ReadNumeric(f,hs);
   Val(hs,Read_Int64,code);
   If code<>0 Then
    InOutRes:=106;
@@ -1066,7 +1009,14 @@ end;
 
 {
   $Log$
-  Revision 1.69  2000-02-09 16:59:31  peter
+  Revision 1.70  2000-03-17 21:27:56  jonas
+    * fixed declaration of val_int64 (removed destsize parameter)
+    * fixed val_int64 and val_qword so they reject invalid input
+      (u >= base)
+    * when reading a number, invalid input is removed from the input
+      buffer (+ it should be faster as well)
+
+  Revision 1.69  2000/02/09 16:59:31  peter
     * truncated log
 
   Revision 1.68  2000/01/31 12:11:53  jonas