Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Port Microsoft.VisualBasic.Conversion class (#37047)
Browse files Browse the repository at this point in the history
  • Loading branch information
cston authored Apr 20, 2019
1 parent 4a25e4b commit c608dda
Show file tree
Hide file tree
Showing 17 changed files with 2,425 additions and 7 deletions.
63 changes: 63 additions & 0 deletions src/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,59 @@ public sealed partial class ControlChars
public ControlChars() { }
}
[Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute]
public sealed partial class Conversion
{
internal Conversion() { }
public static TargetType CTypeDynamic<TargetType>(object Expression) { throw null; }
public static object CTypeDynamic(object Expression, System.Type TargetType) { throw null; }
public static string ErrorToString() { throw null; }
public static string ErrorToString(int ErrorNumber) { throw null; }
public static int Fix(int Number) { throw null; }
public static long Fix(long Number) { throw null; }
public static double Fix(double Number) { throw null; }
public static float Fix(float Number) { throw null; }
public static decimal Fix(decimal Number) { throw null; }
public static object Fix(object Number) { throw null; }
public static short Fix(short Number) { throw null; }
public static string Hex(object Number) { throw null; }
[System.CLSCompliant(false)]
public static string Hex(ulong Number) { throw null; }
public static string Hex(long Number) { throw null; }
public static string Hex(int Number) { throw null; }
public static string Hex(short Number) { throw null; }
public static string Hex(byte Number) { throw null; }
[System.CLSCompliant(false)]
public static string Hex(uint Number) { throw null; }
[System.CLSCompliant(false)]
public static string Hex(ushort Number) { throw null; }
[System.CLSCompliant(false)]
public static string Hex(sbyte Number) { throw null; }
public static short Int(short Number) { throw null; }
public static int Int(int Number) { throw null; }
public static object Int(object Number) { throw null; }
public static double Int(double Number) { throw null; }
public static float Int(float Number) { throw null; }
public static long Int(long Number) { throw null; }
public static decimal Int(decimal Number) { throw null; }
public static string Oct(object Number) { throw null; }
[System.CLSCompliant(false)]
public static string Oct(ulong Number) { throw null; }
[System.CLSCompliant(false)]
public static string Oct(uint Number) { throw null; }
public static string Oct(long Number) { throw null; }
public static string Oct(short Number) { throw null; }
public static string Oct(byte Number) { throw null; }
[System.CLSCompliant(false)]
public static string Oct(sbyte Number) { throw null; }
public static string Oct(int Number) { throw null; }
[System.CLSCompliant(false)]
public static string Oct(ushort Number) { throw null; }
public static string Str(object Number) { throw null; }
public static double Val(string InputStr) { throw null; }
public static int Val(char Expression) { throw null; }
public static double Val(object Expression) { throw null; }
}
[Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute]
public sealed partial class DateAndTime
{
internal DateAndTime() { }
Expand Down Expand Up @@ -133,10 +186,17 @@ internal Information() { }
public static int QBColor(int Color) { throw null; }
public static int RGB(int Red, int Green, int Blue) { throw null; }
public static string SystemTypeName(string VbName) { throw null; }
public static string TypeName(object VarName) { throw null; }
public static int UBound(System.Array Array, int Rank = 1) { throw null; }
public static Microsoft.VisualBasic.VariantType VarType(object VarName) { throw null; }
public static string VbTypeName(string UrtName) { throw null; }
}
[Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute]
public sealed partial class Interaction
{
internal Interaction() { }
public static object CallByName(object ObjectRef, string ProcName, CallType UseCallType, params object[] Args) { throw null; }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
public sealed partial class MyGroupCollectionAttribute : System.Attribute
Expand Down Expand Up @@ -189,6 +249,7 @@ internal Strings() { }
public static string Right(string str, int Length) { throw null; }
public static string RTrim(string str) { throw null; }
public static int StrComp(string String1, string String2, CompareMethod Compare = CompareMethod.Binary) { throw null; }
public static string StrReverse(string Expression) { throw null; }
public static string Trim(string str) { throw null; }
}
public enum VariantType
Expand Down Expand Up @@ -647,8 +708,10 @@ internal Utils() { }
public sealed partial class Versioned
{
internal Versioned() { }
public static object CallByName(object Instance, string MethodName, CallType UseCallType, params object[] Arguments) { throw null; }
public static bool IsNumeric(object Expression) { throw null; }
public static string SystemTypeName(string VbName) { throw null; }
public static string TypeName(object Expression) { throw null; }
public static string VbTypeName(string SystemName) { throw null; }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\System.IO.FileSystem.DriveInfo\ref\System.IO.FileSystem.DriveInfo.csproj" />
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Extensions\ref\System.Runtime.Extensions.csproj" />
<ProjectReference Include="..\..\System.Runtime.InteropServices\ref\System.Runtime.InteropServices.csproj" />
<ProjectReference Include="..\..\System.Threading.Thread\ref\System.Threading.Thread.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="Microsoft\VisualBasic\CompilerServices\Versioned.vb" />
<Compile Include="Microsoft\VisualBasic\Constants.vb" />
<Compile Include="Microsoft\VisualBasic\ControlChars.vb" />
<Compile Include="Microsoft\VisualBasic\Conversion.vb" />
<Compile Include="Microsoft\VisualBasic\DateAndTime.vb" />
<Compile Include="Microsoft\VisualBasic\Devices\NetworkAvailableEventArgs.vb" />
<Compile Include="Microsoft\VisualBasic\ErrObject.vb" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ Namespace Microsoft.VisualBasic.CompilerServices
Dim NewValue As Object

If fi.IsInitOnly Then
'REVIEW: Should this be MissingMember?
Throw New MissingMemberException(GetResourceString(SR.MissingMember_ReadOnlyField2, name, VBFriendlyName(objType, o)))
End If

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3541,8 +3541,6 @@ DecimalExit:

fromType = obj.GetType()

'REVIEW: - Should we handle ByRef in this manner?
' what happens when it's returned as non-byref?
If toType.IsByRef Then
toType = toType.GetElementType()
IsToByRef = True
Expand All @@ -3561,7 +3559,6 @@ DecimalExit:
Return obj
End If
End If
'END REVIEW

Dim toTypeCode As TypeCode = Type.GetTypeCode(toType)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,111 @@ Namespace Microsoft.VisualBasic.CompilerServices
Return SR.Format(resourceKey, args)
End Function

Friend Shared Function StdFormat(ByVal s As String) As String
Dim nfi As NumberFormatInfo
Dim iIndex As Integer
Dim c0, c1, c2 As Char
Dim sb As StringBuilder

nfi = Threading.Thread.CurrentThread.CurrentCulture.NumberFormat
iIndex = s.IndexOf(nfi.NumberDecimalSeparator)

If iIndex = -1 Then
Return s
End If

Try
c0 = s.Chars(0)
c1 = s.Chars(1)
c2 = s.Chars(2)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch ex As System.Threading.ThreadAbortException
Throw ex
Catch
'Ignore, should default to 0 values
End Try

If s.Chars(iIndex) = chPeriod Then
'Optimization: no period replacement needed
'avoids creating stringbuilder and copying string

'If format is "0.xxxx" then replace 0 with space
If c0 = chZero AndAlso c1 = chPeriod Then
Return s.Substring(1)

'If format is "-0.xxxx", "+0.xxxx", " 0.xxxx" then shift everything down over the zero
ElseIf (c0 = chHyphen OrElse c0 = chPlus OrElse c0 = chSpace) AndAlso c1 = chZero AndAlso c2 = chPeriod Then
'Fall down below and use a stringbuilder
Else
'No change
Return s
End If
End If

sb = New StringBuilder(s)
sb.Chars(iIndex) = chPeriod ' change decimal separator to "."

'If format is "0.xxxx" then replace 0 with space
If (c0 = chZero AndAlso c1 = chPeriod) Then
StdFormat = sb.ToString(1, sb.Length - 1)
'If format is "-0.xxxx", "+0.xxxx", " 0.xxxx" then shift everything down over the zero
ElseIf (c0 = chHyphen OrElse c0 = chPlus OrElse c0 = chSpace) AndAlso c1 = chZero AndAlso c2 = chPeriod Then
sb.Remove(1, 1)
StdFormat = sb.ToString()
Else
StdFormat = sb.ToString()
End If
End Function

Friend Shared Function OctFromLong(ByVal Val As Long) As String
'System.Radix is being removed from the .NET platform, so compute this locally.
Dim Buffer As String = ""
Dim ModVal As Integer
Dim CharZero As Integer = Convert.ToInt32(chZero)
Dim Negative As Boolean

If Val < 0 Then
Val = Int64.MaxValue + Val + 1
Negative = True
End If

'Pull apart the number and put the digits (in reverse order) into the buffer.
Do
ModVal = CInt(Val Mod 8)
Val = Val >> 3
Buffer = Buffer & ChrW(ModVal + CharZero)
Loop While Val > 0

Buffer = StrReverse(Buffer)

If Negative Then
Buffer = "1" & Buffer
End If

Return Buffer
End Function

Friend Shared Function OctFromULong(ByVal Val As ULong) As String
'System.Radix is being removed from the .NET platform, so compute this locally.
Dim Buffer As String = ""
Dim ModVal As Integer
Dim CharZero As Integer = Convert.ToInt32(chZero)

'Pull apart the number and put the digits (in reverse order) into the buffer.
Do
ModVal = CInt(Val Mod 8UL)
Val = Val >> 3
Buffer = Buffer & ChrW(ModVal + CharZero)
Loop While Val <> 0UL

Buffer = StrReverse(Buffer)

Return Buffer
End Function

Friend Shared Function GetCultureInfo() As CultureInfo
Return CultureInfo.CurrentCulture
End Function
Expand Down Expand Up @@ -188,7 +293,6 @@ GetSpecialValue:
Return True
End Function


Friend Shared Function VBFriendlyName(ByVal obj As Object) As String
If obj Is Nothing Then
Return "Nothing"
Expand Down Expand Up @@ -541,6 +645,27 @@ GetSpecialValue:
Return resultString
End Function

Friend Shared Function AdjustArraySuffix(ByVal sRank As String) As String
Dim OneChar As Char
Dim RevResult As String = Nothing
Dim length As Integer = sRank.Length
While length > 0
OneChar = sRank.Chars(length - 1)
Select Case OneChar
Case ")"c
RevResult = RevResult + "("c
Case "("c
RevResult = RevResult + ")"c
Case ","c
RevResult = RevResult + OneChar
Case Else
RevResult = OneChar + RevResult
End Select
length = length - 1
End While
Return RevResult
End Function

Friend Shared Function MemberToString(ByVal member As MemberInfo) As String
Select Case member.MemberType
Case MemberTypes.Method, MemberTypes.Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ Namespace Microsoft.VisualBasic.CompilerServices
Private Sub New()
End Sub

Public Shared Function CallByName(ByVal Instance As System.Object, ByVal MethodName As String, ByVal UseCallType As CallType, ByVal ParamArray Arguments() As Object) As Object

Select Case UseCallType

Case CallType.Method
'Need to use LateGet, because we are returning a value
Return CompilerServices.NewLateBinding.LateCall(Instance, Nothing, MethodName, Arguments, Nothing, Nothing, Nothing, False)

Case CallType.Get
Return CompilerServices.NewLateBinding.LateGet(Instance, Nothing, MethodName, Arguments, Nothing, Nothing, Nothing)

Case CallType.Let,
CallType.Set
Dim idmop As IDynamicMetaObjectProvider = IDOUtils.TryCastToIDMOP(Instance)
If idmop IsNot Nothing Then
' UseCallType is used in the late binder to affect the binding behavior for COM Object, but COM Objects
' don't implement IDynamicMetaObjectProvider. Therefore it is safe not to pass on UseCallType here.
IDOBinder.IDOSet(idmop, MethodName, Nothing, Arguments)
Else
CompilerServices.NewLateBinding.LateSet(Instance, Nothing, MethodName, Arguments, Nothing, Nothing, False, False, UseCallType)
End If
Return Nothing

Case Else
Throw New ArgumentException(GetResourceString(SR.Argument_InvalidValue1, "CallType"))
End Select

End Function

Public Shared Function IsNumeric(ByVal Expression As Object) As Boolean

Dim valueInterface As IConvertible = TryCast(Expression, IConvertible)
Expand Down Expand Up @@ -72,6 +101,20 @@ Namespace Microsoft.VisualBasic.CompilerServices

End Function

Public Shared Function TypeName(ByVal Expression As Object) As String

Dim Result As String
Dim typ As System.Type

If Expression Is Nothing Then
Return "Nothing"
End If

typ = Expression.GetType()
Result = VBFriendlyNameOfType(typ)
Return Result
End Function

Public Shared Function SystemTypeName(ByVal VbName As String) As String
Select Case Trim(VbName).ToUpperInvariant()
Case "BOOLEAN" : Return "System.Boolean"
Expand Down
Loading

0 comments on commit c608dda

Please sign in to comment.