dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
290 stars 66 forks source link

(Documentation) Bug: AscW(ChrW(x)) <> x #509

Open tverweij opened 4 years ago

tverweij commented 4 years ago

Form the documentation (https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.strings.chrw?view=netframework-4.8):

Returns the character associated with the specified character code. public static char ChrW (int CharCode); Parameters CharCode Int32 Required. An Integer expression representing the code point, or character code, for the character. Returns Char The character associated with the specified character code. Exceptions

ArgumentException CharCode < -32768 or > 65535 for ChrW.

And AscW is the opposite of that. That means that AscW(ChrW(x)) = x

So, the range to check is -32678 to 65535 Test code:

    Public Function test() As Integer
        For i As Integer = -32768 To 65535
            If i <> Microsoft.VisualBasic.AscW(Microsoft.VisualBasic.ChrW(i)) Then
                Return i
            End If
        Next
        Return -99999
    End Function

And this test function fails for every negative input. All positive input (and zero) works fine.

tverweij commented 4 years ago

It looks like a documentation bug as Unicode = UTF16 and 16 bit conforms to 0 to 65535 or -32768 to 32767 and not -32768 to 65535

KathleenDollard commented 4 years ago

Docs are OSS! I'm going to leave this as "great first issue" for the moment

tverweij commented 4 years ago

@KathleenDollard The parameter check in VB is according to the docs. But the negative values are meaningless. In (almost) all cases AscW(ChrW(negative value)) returns -1.

I think this error is a leftover from VB6 where Integer was 16 bit and the ranges was - 32768 to 32767.