Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| ' <Snippet7> | |
| Imports System.Text | |
| Imports System.Collections.Generic | |
| Module Module1 | |
| Sub Main() | |
| Dim enc As Encoding = Encoding.GetEncoding("us-ascii", New CustomMapper(), New DecoderExceptionFallback()) | |
| Dim str1 As String = String.Format("{0} {1} {2}", ChrW(&H24C8), ChrW(&H2075), ChrW(&H221E)) | |
| Console.WriteLine(str1) | |
| For ctr As Integer = 0 To str1.Length - 1 | |
| Console.Write("{0} ", Convert.ToUInt16(str1(ctr)).ToString("X4")) | |
| If ctr = str1.Length - 1 Then Console.WriteLine() | |
| Next | |
| Console.WriteLine() | |
| ' Encode the original string using the ASCII encoder. | |
| Dim bytes() As Byte = enc.GetBytes(str1) | |
| Console.Write("Encoded bytes: ") | |
| For Each byt In bytes | |
| Console.Write("{0:X2} ", byt) | |
| Next | |
| Console.WriteLine() | |
| Console.WriteLine() | |
| ' Decode the ASCII bytes. | |
| Dim str2 As String = enc.GetString(bytes) | |
| Console.WriteLine("Round-trip: {0}", str1.Equals(str2)) | |
| If Not str1.Equals(str2) Then | |
| Console.WriteLine(str2) | |
| For Each ch In str2 | |
| Console.Write("{0} ", Convert.ToUInt16(ch).ToString("X4")) | |
| Next | |
| Console.WriteLine() | |
| End If | |
| End Sub | |
| End Module | |
| ' </Snippet7> | |
| ' <Snippet5> | |
| Public Class CustomMapper : Inherits EncoderFallback | |
| Public DefaultString As String | |
| Friend mapping As Dictionary(Of UShort, ULong) | |
| Public Sub New() | |
| Me.New("?") | |
| End Sub | |
| Public Sub New(ByVal defaultString As String) | |
| Me.DefaultString = defaultString | |
| ' Create table of mappings | |
| mapping = New Dictionary(Of UShort, ULong) | |
| mapping.Add(&H24C8, &H53) | |
| mapping.Add(&H2075, &H35) | |
| mapping.Add(&H221E, &H49004E0046) | |
| End Sub | |
| Public Overrides Function CreateFallbackBuffer() As System.Text.EncoderFallbackBuffer | |
| Return New CustomMapperFallbackBuffer(Me) | |
| End Function | |
| Public Overrides ReadOnly Property MaxCharCount As Integer | |
| Get | |
| Return 3 | |
| End Get | |
| End Property | |
| End Class | |
| ' </Snippet5> | |
| ' <Snippet6> | |
| Public Class CustomMapperFallbackBuffer : Inherits EncoderFallbackBuffer | |
| Dim count As Integer = -1 ' Number of characters to return | |
| Dim index As Integer = -1 ' Index of character to return | |
| Dim fb As CustomMapper | |
| Dim charsToReturn As String | |
| Public Sub New(ByVal fallback As CustomMapper) | |
| MyBase.New() | |
| Me.fb = fallback | |
| End Sub | |
| Public Overloads Overrides Function Fallback(ByVal charUnknownHigh As Char, ByVal charUnknownLow As Char, ByVal index As Integer) As Boolean | |
| ' Do not try to map surrogates to ASCII. | |
| Return False | |
| End Function | |
| Public Overloads Overrides Function Fallback(ByVal charUnknown As Char, ByVal index As Integer) As Boolean | |
| ' Return false if there are already characters to map. | |
| If count >= 1 Then Return False | |
| ' Determine number of characters to return. | |
| charsToReturn = String.Empty | |
| Dim key As UShort = Convert.ToUInt16(charUnknown) | |
| If fb.mapping.ContainsKey(key) Then | |
| Dim bytes() As Byte = BitConverter.GetBytes(fb.mapping.Item(key)) | |
| Dim ctr As Integer | |
| For Each byt In bytes | |
| If byt > 0 Then | |
| ctr += 1 | |
| charsToReturn += Chr(byt) | |
| End If | |
| Next | |
| count = ctr | |
| Else | |
| ' Return default. | |
| charsToReturn = fb.DefaultString | |
| count = 1 | |
| End If | |
| Me.index = charsToReturn.Length - 1 | |
| Return True | |
| End Function | |
| Public Overrides Function GetNextChar() As Char | |
| ' We'll return a character if possible, so subtract from the count of chars to return. | |
| count -= 1 | |
| ' If count is less than zero, we've returned all characters. | |
| If count < 0 Then Return ChrW(0) | |
| Me.index -= 1 | |
| Return charsToReturn(Me.index + 1) | |
| End Function | |
| Public Overrides Function MovePrevious() As Boolean | |
| ' Original: if count >= -1 and pos >= 0 | |
| If count >= -1 Then | |
| count += 1 | |
| Return True | |
| Else | |
| Return False | |
| End If | |
| End Function | |
| Public Overrides ReadOnly Property Remaining As Integer | |
| Get | |
| Return If(count < 0, 0, count) | |
| End Get | |
| End Property | |
| Public Overrides Sub Reset() | |
| count = -1 | |
| index = -1 | |
| End Sub | |
| End Class | |
| ' </Snippet6> |