Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| // <Snippet7> | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Encoding enc = Encoding.GetEncoding("us-ascii", new CustomMapper(), new DecoderExceptionFallback()); | |
| string str1 = "\u24C8 \u2075 \u221E"; | |
| Console.WriteLine(str1); | |
| for (int ctr = 0; ctr <= str1.Length - 1; ctr++) { | |
| Console.Write("{0} ", Convert.ToUInt16(str1[ctr]).ToString("X4")); | |
| if (ctr == str1.Length - 1) | |
| Console.WriteLine(); | |
| } | |
| Console.WriteLine(); | |
| // Encode the original string using the ASCII encoder. | |
| byte[] bytes = enc.GetBytes(str1); | |
| Console.Write("Encoded bytes: "); | |
| foreach (var byt in bytes) | |
| Console.Write("{0:X2} ", byt); | |
| Console.WriteLine("\n"); | |
| // Decode the ASCII bytes. | |
| string str2 = enc.GetString(bytes); | |
| Console.WriteLine("Round-trip: {0}", str1.Equals(str2)); | |
| if (! str1.Equals(str2)) { | |
| Console.WriteLine(str2); | |
| foreach (var ch in str2) | |
| Console.Write("{0} ", Convert.ToUInt16(ch).ToString("X4")); | |
| Console.WriteLine(); | |
| } | |
| } | |
| } | |
| // </Snippet7> | |
| // <Snippet5> | |
| public class CustomMapper : EncoderFallback | |
| { | |
| public string DefaultString; | |
| internal Dictionary<ushort, ulong> mapping; | |
| public CustomMapper() : this("*") | |
| { | |
| } | |
| public CustomMapper(string defaultString) | |
| { | |
| this.DefaultString = defaultString; | |
| // Create table of mappings | |
| mapping = new Dictionary<ushort, ulong>(); | |
| mapping.Add(0x24C8, 0x53); | |
| mapping.Add(0x2075, 0x35); | |
| mapping.Add(0x221E, 0x49004E0046); | |
| } | |
| public override EncoderFallbackBuffer CreateFallbackBuffer() | |
| { | |
| return new CustomMapperFallbackBuffer(this); | |
| } | |
| public override int MaxCharCount | |
| { | |
| get { return 3; } | |
| } | |
| } | |
| // </Snippet5> | |
| // <Snippet6> | |
| public class CustomMapperFallbackBuffer : EncoderFallbackBuffer | |
| { | |
| int count = -1; // Number of characters to return | |
| int index = -1; // Index of character to return | |
| CustomMapper fb; | |
| string charsToReturn; | |
| public CustomMapperFallbackBuffer(CustomMapper fallback) | |
| { | |
| this.fb = fallback; | |
| } | |
| public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index) | |
| { | |
| // Do not try to map surrogates to ASCII. | |
| return false; | |
| } | |
| public override bool Fallback(char charUnknown, int index) | |
| { | |
| // Return false if there are already characters to map. | |
| if (count >= 1) return false; | |
| // Determine number of characters to return. | |
| charsToReturn = String.Empty; | |
| ushort key = Convert.ToUInt16(charUnknown); | |
| if (fb.mapping.ContainsKey(key)) { | |
| byte[] bytes = BitConverter.GetBytes(fb.mapping[key]); | |
| int ctr = 0; | |
| foreach (var byt in bytes) { | |
| if (byt > 0) { | |
| ctr++; | |
| charsToReturn += (char) byt; | |
| } | |
| } | |
| count = ctr; | |
| } | |
| else { | |
| // Return default. | |
| charsToReturn = fb.DefaultString; | |
| count = 1; | |
| } | |
| this.index = charsToReturn.Length - 1; | |
| return true; | |
| } | |
| public override char GetNextChar() | |
| { | |
| // We'll return a character if possible, so subtract from the count of chars to return. | |
| count--; | |
| // If count is less than zero, we've returned all characters. | |
| if (count < 0) | |
| return '\u0000'; | |
| this.index--; | |
| return charsToReturn[this.index + 1]; | |
| } | |
| public override bool MovePrevious() | |
| { | |
| // Original: if count >= -1 and pos >= 0 | |
| if (count >= -1) { | |
| count++; | |
| return true; | |
| } | |
| else { | |
| return false; | |
| } | |
| } | |
| public override int Remaining | |
| { | |
| get { return count < 0 ? 0 : count; } | |
| } | |
| public override void Reset() | |
| { | |
| count = -1; | |
| index = -1; | |
| } | |
| } | |
| // </Snippet6> |