Skip to content

Commit

Permalink
Extend hex FromString support to other numeric types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Esme Povirk committed Dec 1, 2020
1 parent 609437d commit 36284c5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
Expand Up @@ -29,6 +29,7 @@
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'
Imports System
Imports System.Globalization

Namespace Microsoft.VisualBasic.CompilerServices
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _
Expand All @@ -41,6 +42,12 @@ Namespace Microsoft.VisualBasic.CompilerServices
Try
If value Is Nothing Then Return 0

Value = Value.TrimStart(Nothing)

If Value.StartsWith("&H", StringComparison.CurrentCultureIgnoreCase) Then
Return Byte.Parse(Value.Substring(2), NumberStyles.AllowHexSpecifier)
End If

Return Byte.Parse(value)
Catch ex As FormatException
Throw New InvalidCastException(String.Format(Utils.GetResourceString("CastFromStringToType"), value, "Byte"), ex)
Expand All @@ -58,4 +65,4 @@ Namespace Microsoft.VisualBasic.CompilerServices
Return Convert.ToByte(Value)
End Function
End Class
End Namespace
End Namespace
Expand Up @@ -30,6 +30,7 @@
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Imports System
Imports System.Globalization
Imports System.Runtime.InteropServices

Namespace Microsoft.VisualBasic.CompilerServices
Expand Down Expand Up @@ -89,6 +90,12 @@ Namespace Microsoft.VisualBasic.CompilerServices
Return 0
End If

Value = Value.TrimStart(Nothing)

If Value.StartsWith("&H", StringComparison.CurrentCultureIgnoreCase) Then
Return Int64.Parse(Value.Substring(2), NumberStyles.AllowHexSpecifier)
End If

Return Double.Parse(Value, NumberFormat)
End Function

Expand Down
Expand Up @@ -91,8 +91,10 @@ Namespace Microsoft.VisualBasic.CompilerServices
#End If

Try
If Value.TrimStart(Nothing).StartsWith("&H", StringComparison.CurrentCultureIgnoreCase) Then
Return Int32.Parse(Value.TrimStart(Nothing).Substring(2), NumberStyles.AllowHexSpecifier)
Value = Value.TrimStart(Nothing)

If Value.StartsWith("&H", StringComparison.CurrentCultureIgnoreCase) Then
Return Int32.Parse(Value.Substring(2), NumberStyles.AllowHexSpecifier)
End If

#If TRACE Then
Expand Down
Expand Up @@ -29,6 +29,7 @@
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Imports System
Imports System.Globalization

Namespace Microsoft.VisualBasic.CompilerServices
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _
Expand Down Expand Up @@ -58,6 +59,12 @@ Namespace Microsoft.VisualBasic.CompilerServices
Return 0
End If
Try
Value = Value.TrimStart(Nothing)

If Value.StartsWith("&H", StringComparison.CurrentCultureIgnoreCase) Then
Return Int64.Parse(Value.Substring(2), NumberStyles.AllowHexSpecifier)
End If

Return Convert.ToInt64(DecimalType.FromString(value))
Catch ex As Exception
Throw New InvalidCastException(String.Format(Utils.GetResourceString("CastFromStringToType"), value, "Long"), ex)
Expand Down
Expand Up @@ -29,6 +29,7 @@
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Imports System
Imports System.Globalization
Namespace Microsoft.VisualBasic.CompilerServices
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _
Public NotInheritable Class ShortType
Expand Down Expand Up @@ -63,10 +64,16 @@ Namespace Microsoft.VisualBasic.CompilerServices
End If

Try
Value = Value.TrimStart(Nothing)

If Value.StartsWith("&H", StringComparison.CurrentCultureIgnoreCase) Then
Return Short.Parse(Value.Substring(2), NumberStyles.AllowHexSpecifier)
End If

Return Short.Parse(value)
Catch ex As Exception
Throw New InvalidCastException(String.Format(Utils.GetResourceString("CastFromStringToType"), value, "Short"), ex)
End Try
End Function
End Class
End Namespace
End Namespace
Expand Up @@ -74,6 +74,18 @@ public void FromString1()
b = Microsoft.VisualBasic.CompilerServices.BooleanType.FromString("0");
Assert.AreEqual (false.ToString(), b.ToString(), "FromString1#3");

// test string = "&H0"
b = Microsoft.VisualBasic.CompilerServices.BooleanType.FromString("&H0");
Assert.AreEqual (false.ToString(), b.ToString(), "FromString1#4");

// test string = "&H1"
b = Microsoft.VisualBasic.CompilerServices.BooleanType.FromString("&H1");
Assert.AreEqual (true.ToString(), b.ToString(), "FromString1#5");

// test string = "&Ha"
b = Microsoft.VisualBasic.CompilerServices.BooleanType.FromString("&Ha");
Assert.AreEqual (true.ToString(), b.ToString(), "FromString1#6");

}

[Test]
Expand Down

0 comments on commit 36284c5

Please sign in to comment.