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 some missing methods from the Strings class (#31086)
Browse files Browse the repository at this point in the history
* building

* Asc, Chr tests

* Trim tests

* Remaining tests

* Fix test param names

* Fix netfx tests

* Spacing
  • Loading branch information
danmoseley committed Jul 17, 2018
1 parent 9e21bd9 commit 5a6f230
Show file tree
Hide file tree
Showing 7 changed files with 1,536 additions and 32 deletions.
10 changes: 10 additions & 0 deletions src/Microsoft.VisualBasic/ref/Microsoft.VisualBasic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ public HideModuleNameAttribute() { }
public sealed partial class Strings
{
internal Strings() { }
public static int Asc(char String) { throw null; }
public static int Asc(string String) { throw null; }
public static int AscW(char String) { throw null; }
public static int AscW(string String) { throw null; }
public static char Chr(int CharCode) { throw null; }
public static char ChrW(int CharCode) { throw null; }
public static string Left(string str, int Length) { throw null; }
public static string LTrim(string str) { throw null; }
public static string Mid(string str, int Start) { throw null; }
public static string Mid(string str, int Start, int Length) { throw null; }
public static string Right(string str, int Length) { throw null; }
public static string RTrim(string str) { throw null; }
public static string Trim(string str) { throw null; }
}
}
namespace Microsoft.VisualBasic.CompilerServices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,165 @@
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.

Option Strict Off

Imports System
Imports Microsoft.VisualBasic.CompilerServices.Utils

Namespace Microsoft.VisualBasic.CompilerServices

Friend Enum vbErrors
None = 0
ReturnWOGoSub = 3
IllegalFuncCall = 5
Overflow = 6
OutOfMemory = 7
OutOfBounds = 9
ArrayLocked = 10
DivByZero = 11
TypeMismatch = 13
OutOfStrSpace = 14
ExprTooComplex = 16
CantContinue = 17
UserInterrupt = 18
ResumeWOErr = 20
OutOfStack = 28
UNDONE = 29
UndefinedProc = 35
TooManyClients = 47
DLLLoadErr = 48
DLLBadCallingConv = 49
InternalError = 51
BadFileNameOrNumber = 52
FileNotFound = 53
BadFileMode = 54
FileAlreadyOpen = 55
IOError = 57
FileAlreadyExists = 58
BadRecordLen = 59
DiskFull = 61
EndOfFile = 62
BadRecordNum = 63
TooManyFiles = 67
DevUnavailable = 68
PermissionDenied = 70
DiskNotReady = 71
DifferentDrive = 74
PathFileAccess = 75
PathNotFound = 76
ObjNotSet = 91
IllegalFor = 92
BadPatStr = 93
CantUseNull = 94
UserDefined = 95
AdviseLimit = 96
BadCallToFriendFunction = 97
CantPassPrivateObject = 98
DLLCallException = 99
DoesntImplementICollection = 100
Abort = 287
InvalidFileFormat = 321
CantCreateTmpFile = 322
InvalidResourceFormat = 325
InvalidPropertyValue = 380
InvalidPropertyArrayIndex = 381
SetNotSupportedAtRuntime = 382
SetNotSupported = 383
NeedPropertyArrayIndex = 385
SetNotPermitted = 387
GetNotSupportedAtRuntime = 393
GetNotSupported = 394
PropertyNotFound = 422
NoSuchControlOrProperty = 423
NotObject = 424
CantCreateObject = 429
OLENotSupported = 430
OLEFileNotFound = 432
OLENoPropOrMethod = 438
OLEAutomationError = 440
LostTLB = 442
OLENoDefault = 443
ActionNotSupported = 445
NamedArgsNotSupported = 446
LocaleSettingNotSupported = 447
NamedParamNotFound = 448
ParameterNotOptional = 449
FuncArityMismatch = 450
NotEnum = 451
InvalidOrdinal = 452
InvalidDllFunctionName = 453
CodeResourceNotFound = 454
CodeResourceLockError = 455
DuplicateKey = 457
InvalidTypeLibVariable = 458
ObjDoesNotSupportEvents = 459
InvalidClipboardFormat = 460
IdentNotMember = 461
ServerNotFound = 462
ObjNotRegistered = 463
InvalidPicture = 481
PrinterError = 482
CantSaveFileToTemp = 735
SearchTextNotFound = 744
ReplacementsTooLong = 746

NotYetImplemented = 32768
FileNotFoundWithName = 40243
CantFindDllEntryPoint = 59201

SeekErr = 32771
ReadFault = 32772
WriteFault = 32773
BadFunctionId = 32774
FileLockViolation = 32775
ShareRequired = 32789
BufferTooSmall = 32790
InvDataRead = 32792
UnsupFormat = 32793
RegistryAccess = 32796
LibNotRegistered = 32797
Usage = 32799
UndefinedType = 32807
QualifiedNameDisallowed = 32808
InvalidState = 32809
WrongTypeKind = 32810
ElementNotFound = 32811
AmbiguousName = 32812
ModNameConflict = 32813
UnknownLcid = 32814
BadModuleKind = 35005
NoContainingLib = 35009
BadTypeId = 35010
BadLibId = 35011
Eof = 35012
SizeTooBig = 35013
ExpectedFuncNotModule = 35015
ExpectedFuncNotRecord = 35016
ExpectedFuncNotProject = 35017
ExpectedFuncNotVar = 35018
ExpectedTypeNotProj = 35019
UnsuitableFuncPropMatch = 35020
BrokenLibRef = 35021
UnsupportedTypeLibFeature = 35022
ModuleAsType = 35024
InvalidTypeInfoKind = 35025
InvalidTypeLibFunction = 35026
OperationNotAllowedInDll = 40035
CompileError = 40036
CantEvalWatch = 40037
MissingVbaTypeLib = 40038
UserReset = 40040
MissingEndBrack = 40041
IncorrectTypeChar = 40042
InvalidNumLit = 40043
IllegalChar = 40044
IdTooLong = 40045
StatementTooComplex = 40046
ExpectedTokens = 40047
InconsistentPropFuncs = 40067
CircularType = 40068
AccessViolation = &H80004003 'This is E_POINTER. This is what VB6 returns from err.Number when calling into a .NET assembly that throws an AccessViolation
LastTrappable = ReplacementsTooLong
End Enum

' Implements error utilities for Basic
Expand All @@ -27,6 +178,23 @@ Namespace Microsoft.VisualBasic.CompilerServices
Return VbMakeExceptionEx(vbErrors.ObjNotSet, GetResourceString(SR.ID91)) ' 91 - ObjNotSet
End Function

Friend Shared Function VbMakeException(ByVal hr As Integer) As System.Exception
Dim sMsg As String

If hr > 0 AndAlso hr <= &HFFFFI Then
sMsg = GetResourceString(CType(hr, vbErrors))
Else
sMsg = ""
End If
VbMakeException = VbMakeExceptionEx(hr, sMsg)
End Function

Friend Shared Function VbMakeException(ByVal ex As Exception, ByVal hr As Integer) As System.Exception
' UNDONE - Err() requires port of Information.vb, ProjectData.vb
'Err().SetUnmappedError(hr)
Return ex
End Function

Private Shared Function VbMakeExceptionEx(ByVal number As Integer, ByVal sMsg As String) As System.Exception
Dim vBDefinedError As Boolean

Expand All @@ -37,23 +205,98 @@ Namespace Microsoft.VisualBasic.CompilerServices

End Function

Friend Shared Function BuildException(ByVal Number As Integer, ByVal Description As String, ByRef VBDefinedError As Boolean) As System.Exception

VBDefinedError = True

Select Case Number

Case vbErrors.None

Case vbErrors.ReturnWOGoSub, _
vbErrors.ResumeWOErr, _
vbErrors.CantUseNull, _
vbErrors.DoesntImplementICollection
Return New InvalidOperationException(Description)

Private Shared Function BuildException(ByVal number As Integer, ByVal description As String, ByRef vBDefinedError As Boolean) As System.Exception
Case vbErrors.IllegalFuncCall, _
vbErrors.NamedParamNotFound, _
vbErrors.NamedArgsNotSupported, _
vbErrors.ParameterNotOptional
Return New ArgumentException(Description)

vBDefinedError = True
Case vbErrors.OLENoPropOrMethod
Return New MissingMemberException(Description)

Select Case number
Case vbErrors.Overflow
Return New OverflowException(Description)

Case vbErrors.OutOfMemory, vbErrors.OutOfStrSpace
Return New OutOfMemoryException(Description)

Case vbErrors.OutOfBounds
Return New IndexOutOfRangeException(Description)

Case vbErrors.DivByZero
Return New DivideByZeroException(Description)

Case vbErrors.TypeMismatch
Return New InvalidCastException(Description)

Case vbErrors.OutOfStack
Return New StackOverflowException(Description)

Case vbErrors.DLLLoadErr
Return New TypeLoadException(Description)

Case vbErrors.FileNotFound
Return New IO.FileNotFoundException(Description)

Case vbErrors.EndOfFile
Return New IO.EndOfStreamException(Description)

Case vbErrors.IOError, _
vbErrors.BadFileNameOrNumber, _
vbErrors.BadFileMode, _
vbErrors.FileAlreadyOpen, _
vbErrors.FileAlreadyExists, _
vbErrors.BadRecordLen, _
vbErrors.DiskFull, _
vbErrors.BadRecordNum, _
vbErrors.TooManyFiles, _
vbErrors.DevUnavailable, _
vbErrors.PermissionDenied, _
vbErrors.DiskNotReady, _
vbErrors.DifferentDrive, _
vbErrors.PathFileAccess
Return New IO.IOException(Description)

Case vbErrors.PathNotFound, _
vbErrors.OLEFileNotFound
Return New IO.FileNotFoundException(Description)

Case vbErrors.ObjNotSet
Return New NullReferenceException(description)
Return New NullReferenceException(Description)

Case vbErrors.PropertyNotFound
Return New MissingFieldException(Description)

Case vbErrors.CantCreateObject, _
vbErrors.ServerNotFound
Return New Exception(Description)

Case vbErrors.AccessViolation
Return New AccessViolationException() 'We never want a custom description here. Use the localized message that comes for free inside the exception

Case Else
vBDefinedError = False
Return New Exception(description)
'Fall below to default
VBDefinedError = False
Return New Exception(Description)
End Select

vBDefinedError = False
Return New Exception(description)
VBDefinedError = False
Return New Exception(Description)

End Function

End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ Namespace Microsoft.VisualBasic.CompilerServices
' Param: Args - An array of params used to replace placeholders.
'Returns: The resource string if found or an error message string
'*****************************************************************************
Friend Shared Function GetResourceString(ByVal ResourceId As vbErrors) As String
Dim id as String = "ID" & CStr(ResourceId)
Return SR.GetResourceString(id, id)
End Function

Friend Shared Function GetResourceString(ByVal resourceKey As String, ByVal ParamArray args() As String) As String
Return SR.Format(resourceKey, args)
End Function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Imports System
Imports System.Diagnostics
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Text
Imports System.Reflection

Namespace Global.Microsoft.VisualBasic.CompilerServices
Expand Down Expand Up @@ -52,6 +53,15 @@ Namespace Global.Microsoft.VisualBasic.CompilerServices
End If
Return aryDest
End Function

Friend Shared Function GetFileIOEncoding() As Encoding
Return System.Text.Encoding.Default
End Function

Friend Shared Function GetLocaleCodePage() As Integer
Return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ANSICodePage
End Function

End Class

Friend Module ReflectionExtensions
Expand Down
Loading

0 comments on commit 5a6f230

Please sign in to comment.