mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-06-02 13:22:37 +02:00
* use val() for decoding integers
This commit is contained in:
parent
04c97cc129
commit
79c20ebf7d
@ -77,7 +77,6 @@ interface
|
||||
function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
|
||||
function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
|
||||
function DStr(l:longint):string;
|
||||
procedure valint(S : string;var V : longint;var code : integer);
|
||||
{# Returns true if the string s is a number }
|
||||
function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
|
||||
{# Returns true if value is a power of 2, the actual
|
||||
@ -604,45 +603,6 @@ uses
|
||||
end;
|
||||
|
||||
|
||||
procedure valint(S : string;var V : longint;var code : integer);
|
||||
{
|
||||
val() with support for octal, which is not supported under tp7
|
||||
}
|
||||
{$ifndef FPC}
|
||||
var
|
||||
vs : longint;
|
||||
c : byte;
|
||||
begin
|
||||
if s[1]='%' then
|
||||
begin
|
||||
vs:=0;
|
||||
longint(v):=0;
|
||||
for c:=2 to length(s) do
|
||||
begin
|
||||
if s[c]='0' then
|
||||
vs:=vs shl 1
|
||||
else
|
||||
if s[c]='1' then
|
||||
vs:=vs shl 1+1
|
||||
else
|
||||
begin
|
||||
code:=c;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
code:=0;
|
||||
longint(v):=vs;
|
||||
end
|
||||
else
|
||||
system.val(S,V,code);
|
||||
end;
|
||||
{$else not FPC}
|
||||
begin
|
||||
system.val(S,V,code);
|
||||
end;
|
||||
{$endif not FPC}
|
||||
|
||||
|
||||
function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
|
||||
{
|
||||
is string a correct number ?
|
||||
@ -651,7 +611,7 @@ uses
|
||||
w : integer;
|
||||
l : longint;
|
||||
begin
|
||||
valint(s,l,w);
|
||||
val(s,l,w);
|
||||
is_number:=(w=0);
|
||||
end;
|
||||
|
||||
@ -1264,7 +1224,10 @@ initialization
|
||||
end.
|
||||
{
|
||||
$Log$
|
||||
Revision 1.47 2004-11-15 23:35:31 peter
|
||||
Revision 1.48 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.47 2004/11/15 23:35:31 peter
|
||||
* tparaitem removed, use tparavarsym instead
|
||||
* parameter order is now calculated from paranr value in tparavarsym
|
||||
|
||||
|
@ -515,7 +515,7 @@ Unit Ra386int;
|
||||
actasmpattern:=actasmpattern + c;
|
||||
c:=current_scanner.asmgetchar;
|
||||
end;
|
||||
actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,16));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end;
|
||||
@ -615,7 +615,7 @@ Unit Ra386int;
|
||||
Begin
|
||||
{ Delete the last binary specifier }
|
||||
delete(actasmpattern,length(actasmpattern),1);
|
||||
actasmpattern:=tostr(ValBinary(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,2));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end
|
||||
@ -624,21 +624,21 @@ Unit Ra386int;
|
||||
case c of
|
||||
'O' :
|
||||
Begin
|
||||
actasmpattern:=tostr(ValOctal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,8));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
c:=current_scanner.asmgetchar;
|
||||
exit;
|
||||
end;
|
||||
'H' :
|
||||
Begin
|
||||
actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,16));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
c:=current_scanner.asmgetchar;
|
||||
exit;
|
||||
end;
|
||||
else { must be an integer number }
|
||||
begin
|
||||
actasmpattern:=tostr(ValDecimal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,10));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end;
|
||||
@ -2036,7 +2036,10 @@ begin
|
||||
end.
|
||||
{
|
||||
$Log$
|
||||
Revision 1.85 2005-01-19 22:19:41 peter
|
||||
Revision 1.86 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.85 2005/01/19 22:19:41 peter
|
||||
* unit mapping rewrite
|
||||
* new derefmap added
|
||||
|
||||
|
@ -591,7 +591,7 @@ begin
|
||||
error:=0;
|
||||
if l>0 then
|
||||
begin
|
||||
valint(copy(dllversion,l+1,255),minor,error);
|
||||
val(copy(dllversion,l+1,255),minor,error);
|
||||
if (error=0) and
|
||||
(minor>=0) and (minor<=$ffff) then
|
||||
dllminor:=minor
|
||||
@ -603,7 +603,7 @@ begin
|
||||
l:=256;
|
||||
dllmajor:=1;
|
||||
if error=0 then
|
||||
valint(copy(dllversion,1,l-1),major,error);
|
||||
val(copy(dllversion,1,l-1),major,error);
|
||||
if (error=0) and (major>=0) and (major<=$ffff) then
|
||||
dllmajor:=major
|
||||
else
|
||||
@ -2089,7 +2089,10 @@ finalization
|
||||
end.
|
||||
{
|
||||
$Log$
|
||||
Revision 1.161 2005-01-09 20:24:43 olle
|
||||
Revision 1.162 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.161 2005/01/09 20:24:43 olle
|
||||
* rework of macro subsystem
|
||||
+ exportable macros for mode macpas
|
||||
|
||||
|
@ -2017,7 +2017,7 @@ implementation
|
||||
else
|
||||
begin
|
||||
{ then longint }
|
||||
valint(pattern,l,code);
|
||||
val(pattern,l,code);
|
||||
if code = 0 then
|
||||
begin
|
||||
consume(_INTCONST);
|
||||
@ -2527,7 +2527,10 @@ implementation
|
||||
end.
|
||||
{
|
||||
$Log$
|
||||
Revision 1.178 2005-01-04 16:38:07 peter
|
||||
Revision 1.179 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.178 2005/01/04 16:38:07 peter
|
||||
* don't allow properties in C style operators
|
||||
|
||||
Revision 1.177 2004/12/26 16:22:01 peter
|
||||
|
@ -400,7 +400,7 @@ unit raatt;
|
||||
c:=current_scanner.asmgetchar;
|
||||
end;
|
||||
actasmpattern[0]:=chr(len);
|
||||
actasmpattern:=tostr(ValDecimal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,10));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end;
|
||||
@ -417,7 +417,7 @@ unit raatt;
|
||||
actasmpattern:=actasmpattern + c;
|
||||
c:=current_scanner.asmgetchar;
|
||||
end;
|
||||
actasmpattern:=tostr(ValBinary(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,2));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end;
|
||||
@ -478,7 +478,7 @@ unit raatt;
|
||||
actasmpattern:=actasmpattern + c;
|
||||
c:=current_scanner.asmgetchar;
|
||||
end;
|
||||
actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,16));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end;
|
||||
@ -490,13 +490,13 @@ unit raatt;
|
||||
actasmpattern:=actasmpattern + c;
|
||||
c:=current_scanner.asmgetchar;
|
||||
end;
|
||||
actasmpattern:=tostr(ValOctal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,8));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end;
|
||||
else { octal number zero value...}
|
||||
Begin
|
||||
actasmpattern:=tostr(ValOctal(actasmpattern));
|
||||
actasmpattern:=tostr(ParseVal(actasmpattern,8));
|
||||
actasmtoken:=AS_INTNUM;
|
||||
exit;
|
||||
end;
|
||||
@ -1523,7 +1523,10 @@ end.
|
||||
|
||||
{
|
||||
$Log$
|
||||
Revision 1.17 2005-01-19 22:19:41 peter
|
||||
Revision 1.18 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.17 2005/01/19 22:19:41 peter
|
||||
* unit mapping rewrite
|
||||
* new derefmap added
|
||||
|
||||
|
@ -175,10 +175,7 @@ type
|
||||
{ String routines }
|
||||
{---------------------------------------------------------------------}
|
||||
|
||||
Function ValDecimal(const S:String):aint;
|
||||
Function ValOctal(const S:String):aint;
|
||||
Function ValBinary(const S:String):aint;
|
||||
Function ValHexaDecimal(const S:String):aint;
|
||||
Function ParseVal(const S:String;base:byte):aint;
|
||||
Function PadZero(Var s: String; n: byte): Boolean;
|
||||
Function EscapeToPascal(const s:string): string;
|
||||
|
||||
@ -543,14 +540,14 @@ Begin
|
||||
temp:=temp+s[i+1];
|
||||
temp:=temp+s[i+2];
|
||||
inc(i,2);
|
||||
c:=chr(ValOctal(temp));
|
||||
c:=chr(ParseVal(temp,8));
|
||||
end;
|
||||
'x':
|
||||
Begin
|
||||
temp:=s[i+1];
|
||||
temp:=temp+s[i+2];
|
||||
inc(i,2);
|
||||
c:=chr(ValHexaDecimal(temp));
|
||||
c:=chr(ParseVal(temp,16));
|
||||
end;
|
||||
else
|
||||
Begin
|
||||
@ -569,101 +566,47 @@ Begin
|
||||
end;
|
||||
|
||||
|
||||
Function ValDecimal(const S:String):aint;
|
||||
Function ParseVal(const S:String;base:byte):aint;
|
||||
{ Converts a decimal string to aint }
|
||||
var
|
||||
vs : aint;
|
||||
c : longint;
|
||||
code : integer;
|
||||
errmsg : word;
|
||||
prefix : string[2];
|
||||
Begin
|
||||
vs:=0;
|
||||
for c:=1 to length(s) do
|
||||
begin
|
||||
vs:=vs*10;
|
||||
if s[c] in ['0'..'9'] then
|
||||
inc(vs,ord(s[c])-ord('0'))
|
||||
else
|
||||
case base of
|
||||
2 :
|
||||
begin
|
||||
Message1(asmr_e_error_converting_decimal,s);
|
||||
ValDecimal:=0;
|
||||
exit;
|
||||
errmsg:=asmr_e_error_converting_binary;
|
||||
prefix:='%';
|
||||
end;
|
||||
end;
|
||||
ValDecimal:=vs;
|
||||
end;
|
||||
|
||||
|
||||
Function ValOctal(const S:String):aint;
|
||||
{ Converts an octal string to aint }
|
||||
var
|
||||
vs : aint;
|
||||
c : longint;
|
||||
Begin
|
||||
vs:=0;
|
||||
for c:=1 to length(s) do
|
||||
begin
|
||||
vs:=vs shl 3;
|
||||
if s[c] in ['0'..'7'] then
|
||||
inc(vs,ord(s[c])-ord('0'))
|
||||
else
|
||||
8 :
|
||||
begin
|
||||
Message1(asmr_e_error_converting_octal,s);
|
||||
ValOctal:=0;
|
||||
exit;
|
||||
errmsg:=asmr_e_error_converting_octal;
|
||||
prefix:='&';
|
||||
end;
|
||||
end;
|
||||
ValOctal:=vs;
|
||||
end;
|
||||
|
||||
|
||||
Function ValBinary(const S:String):aint;
|
||||
{ Converts a binary string to aint }
|
||||
var
|
||||
vs : aint;
|
||||
c : longint;
|
||||
Begin
|
||||
vs:=0;
|
||||
for c:=1 to length(s) do
|
||||
begin
|
||||
vs:=vs shl 1;
|
||||
if s[c] in ['0'..'1'] then
|
||||
inc(vs,ord(s[c])-ord('0'))
|
||||
else
|
||||
10 :
|
||||
begin
|
||||
Message1(asmr_e_error_converting_binary,s);
|
||||
ValBinary:=0;
|
||||
exit;
|
||||
errmsg:=asmr_e_error_converting_decimal;
|
||||
prefix:='';
|
||||
end;
|
||||
end;
|
||||
ValBinary:=vs;
|
||||
end;
|
||||
|
||||
|
||||
Function ValHexadecimal(const S:String):aint;
|
||||
{ Converts a binary string to aint }
|
||||
var
|
||||
vs : aint;
|
||||
c : longint;
|
||||
Begin
|
||||
vs:=0;
|
||||
for c:=1 to length(s) do
|
||||
begin
|
||||
vs:=vs shl 4;
|
||||
case s[c] of
|
||||
'0'..'9' :
|
||||
inc(vs,ord(s[c])-ord('0'));
|
||||
'A'..'F' :
|
||||
inc(vs,ord(s[c])-ord('A')+10);
|
||||
'a'..'f' :
|
||||
inc(vs,ord(s[c])-ord('a')+10);
|
||||
else
|
||||
begin
|
||||
Message1(asmr_e_error_converting_hexadecimal,s);
|
||||
ValHexadecimal:=0;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
ValHexadecimal:=vs;
|
||||
16 :
|
||||
begin
|
||||
errmsg:=asmr_e_error_converting_hexadecimal;
|
||||
prefix:='$';
|
||||
end;
|
||||
else
|
||||
internalerror(200501202);
|
||||
end;
|
||||
val(prefix+s,result,code);
|
||||
if code<>0 then
|
||||
begin
|
||||
val(prefix+s,aword(result),code);
|
||||
if code<>0 then
|
||||
begin
|
||||
Message1(errmsg,s);
|
||||
result:=0;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
@ -1626,7 +1569,10 @@ end;
|
||||
end.
|
||||
{
|
||||
$Log$
|
||||
Revision 1.101 2005-01-19 22:19:41 peter
|
||||
Revision 1.102 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.101 2005/01/19 22:19:41 peter
|
||||
* unit mapping rewrite
|
||||
* new derefmap added
|
||||
|
||||
|
@ -958,7 +958,7 @@ implementation
|
||||
major:=0;
|
||||
minor:=0;
|
||||
revision:=0;
|
||||
valint(pattern,major,error);
|
||||
val(pattern,major,error);
|
||||
if (error<>0) or (major > high(word)) or (major < 0) then
|
||||
begin
|
||||
Message1(scan_w_wrong_version_ignored,pattern);
|
||||
@ -968,7 +968,7 @@ implementation
|
||||
begin
|
||||
current_scanner.readchar;
|
||||
current_scanner.readnumber;
|
||||
valint(pattern,minor,error);
|
||||
val(pattern,minor,error);
|
||||
if (error<>0) or (minor > high(word)) or (minor < 0) then
|
||||
begin
|
||||
Message1(scan_w_wrong_version_ignored,tostr(major)+'.'+pattern);
|
||||
@ -979,7 +979,7 @@ implementation
|
||||
begin
|
||||
current_scanner.readchar;
|
||||
current_scanner.readnumber;
|
||||
valint(pattern,revision,error);
|
||||
val(pattern,revision,error);
|
||||
if (error<>0) or (revision > high(word)) or (revision < 0) then
|
||||
begin
|
||||
Message1(scan_w_wrong_version_ignored,tostr(revision)+'.'+pattern);
|
||||
@ -1183,7 +1183,10 @@ begin
|
||||
end.
|
||||
{
|
||||
$Log$
|
||||
Revision 1.52 2005-01-18 15:44:43 peter
|
||||
Revision 1.53 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.52 2005/01/18 15:44:43 peter
|
||||
* ignore more delphi directives
|
||||
|
||||
Revision 1.51 2005/01/09 20:24:43 olle
|
||||
|
@ -607,7 +607,7 @@ implementation
|
||||
begin
|
||||
preproc_consume(_ID);
|
||||
hs:=read_factor();
|
||||
valint(hs,l,w);
|
||||
val(hs,l,w);
|
||||
if l<>0 then
|
||||
read_factor:='0'
|
||||
else
|
||||
@ -656,8 +656,8 @@ implementation
|
||||
break;
|
||||
preproc_consume(_ID);
|
||||
hs2:=read_factor;
|
||||
valint(hs1,l1,w);
|
||||
valint(hs2,l2,w);
|
||||
val(hs1,l1,w);
|
||||
val(hs2,l2,w);
|
||||
if (l1<>0) and (l2<>0) then
|
||||
hs1:='1'
|
||||
else
|
||||
@ -681,8 +681,8 @@ implementation
|
||||
break;
|
||||
preproc_consume(_ID);
|
||||
hs2:=read_term;
|
||||
valint(hs1,l1,w);
|
||||
valint(hs2,l2,w);
|
||||
val(hs1,l1,w);
|
||||
val(hs2,l2,w);
|
||||
if (l1<>0) or (l2<>0) then
|
||||
hs1:='1'
|
||||
else
|
||||
@ -710,8 +710,8 @@ implementation
|
||||
hs2:=read_simple_expr;
|
||||
if is_number(hs1) and is_number(hs2) then
|
||||
begin
|
||||
valint(hs1,l1,w);
|
||||
valint(hs2,l2,w);
|
||||
val(hs1,l1,w);
|
||||
val(hs2,l2,w);
|
||||
case t of
|
||||
_EQUAL : b:=l1=l2;
|
||||
_UNEQUAL : b:=l1<>l2;
|
||||
@ -1876,7 +1876,7 @@ implementation
|
||||
w : integer;
|
||||
begin
|
||||
readnumber;
|
||||
valint(pattern,l,w);
|
||||
val(pattern,l,w);
|
||||
readval:=l;
|
||||
end;
|
||||
|
||||
@ -2803,7 +2803,7 @@ implementation
|
||||
readchar;
|
||||
end;
|
||||
end;
|
||||
valint(asciinr,m,code);
|
||||
val(asciinr,m,code);
|
||||
if (asciinr='') or (code<>0) then
|
||||
Message(scan_e_illegal_char_const)
|
||||
else if (m<0) or (m>255) or (length(asciinr)>3) then
|
||||
@ -3259,7 +3259,10 @@ exit_label:
|
||||
end.
|
||||
{
|
||||
$Log$
|
||||
Revision 1.98 2005-01-09 20:24:43 olle
|
||||
Revision 1.99 2005-01-20 17:05:53 peter
|
||||
* use val() for decoding integers
|
||||
|
||||
Revision 1.98 2005/01/09 20:24:43 olle
|
||||
* rework of macro subsystem
|
||||
+ exportable macros for mode macpas
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user