@@ -28,7 +28,7 @@ public class NumberConverter : ICustomConverter
28
28
/// <param name="str">The non-decimal string.</param>
29
29
/// <param name="atBase">The number base.</param>
30
30
/// <returns>The converted string as a double.</returns>
31
- public static double GetDoubleAtBase ( string str , int atBase )
31
+ public static Value GetDoubleAtBase ( string str , int atBase )
32
32
{
33
33
var regex = atBase == 16 ? s_hexDoubleParserRegex : s_nonHexDoubleParserRegex ;
34
34
var mantissaExponent = regex . Match ( str . Replace ( "_" , "" ) ) ;
@@ -40,10 +40,33 @@ public static double GetDoubleAtBase(string str, int atBase)
40
40
if ( mantissaExponent . Groups . Count > 2 && mantissaExponent . Groups [ 3 ] . Success )
41
41
{
42
42
var base_ = mantissaExponent . Groups [ 2 ] . Value . ToLower ( ) [ 0 ] == 'e' ? 10 : 2 ;
43
- var exponentent = double . Parse ( mantissaExponent . Groups [ 3 ] . Value ) ;
44
- return mantissa * Math . Pow ( base_ , exponentent ) ;
43
+ var exponent = double . Parse ( mantissaExponent . Groups [ 3 ] . Value ) ;
44
+ return new Value ( mantissa * Math . Pow ( base_ , exponent ) ) ;
45
45
}
46
- return mantissa ;
46
+ return new Value ( mantissa ) ;
47
+ }
48
+
49
+ /// <summary>
50
+ /// Convert a <see cref="double"/> to an <see cref="int"/> or
51
+ /// <see cref="uint"/> if the converted value is able to be converted.
52
+ /// Otherwise, the returned value is the original value itself.
53
+ /// </summary>
54
+ /// <param name="value">The <see cref="double"/> as an <see cref="IValue"/>.
55
+ /// </param>
56
+ /// <returns>The converted <see cref="int"/> or <see cref="uint"/> as an
57
+ /// <see cref="Value"/> if conversion was successful, otherwise the
58
+ /// original value itself.</returns>
59
+ public static Value ConvertToIntegral ( Value value )
60
+ {
61
+ if ( value . ToDouble ( ) >= int . MinValue && value . ToDouble ( ) <= uint . MaxValue )
62
+ {
63
+ if ( value . ToDouble ( ) <= int . MaxValue )
64
+ {
65
+ return new Value ( unchecked ( ( int ) ( value . ToLong ( ) & 0xFFFF_FFFF ) ) ) ;
66
+ }
67
+ return new Value ( ( uint ) ( value . ToLong ( ) & 0xFFFF_FFFF ) ) ;
68
+ }
69
+ return value ;
47
70
}
48
71
49
72
public Value Convert ( string str )
@@ -53,15 +76,15 @@ public Value Convert(string str)
53
76
{
54
77
if ( ! char . IsDigit ( str [ 1 ] ) )
55
78
{
56
- return Evaluator . ConvertToIntegral ( new Value ( System . Convert . ToInt64 ( str [ 2 ..] , 8 ) ) ) ;
79
+ return ConvertToIntegral ( new Value ( System . Convert . ToInt64 ( str [ 2 ..] , 8 ) ) ) ;
57
80
}
58
- return Evaluator . ConvertToIntegral ( new Value ( System . Convert . ToInt64 ( str , 8 ) ) ) ;
81
+ return ConvertToIntegral ( new Value ( System . Convert . ToInt64 ( str , 8 ) ) ) ;
59
82
}
60
83
var numVal = new Value ( System . Convert . ToDouble ( str ) ) ;
61
84
bool isDouble = str . IndexOf ( '.' ) > - 1 || str . IndexOf ( 'e' ) > - 1 || str . IndexOf ( 'E' ) > - 1 ;
62
85
if ( ! isDouble )
63
86
{
64
- return Evaluator . ConvertToIntegral ( numVal ) ;
87
+ return ConvertToIntegral ( numVal ) ;
65
88
}
66
89
return numVal ;
67
90
}
@@ -76,7 +99,7 @@ public class OctalDoubleConverter : ICustomConverter
76
99
public Value Convert ( string str )
77
100
{
78
101
string octalStr = ! char . IsDigit ( str [ 1 ] ) ? str [ 2 ..] : str [ 1 ..] ;
79
- return new Value ( NumberConverter . GetDoubleAtBase ( octalStr , 8 ) ) ;
102
+ return NumberConverter . GetDoubleAtBase ( octalStr , 8 ) ;
80
103
}
81
104
}
82
105
}
0 commit comments