Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1.     public struct TranslateText : IEquatable<TranslateText>
  2.     {
  3.         public string DictRef { get; }
  4.         public string AtRef { get; }
  5.  
  6.         public TranslateText(string value)
  7.         {
  8.             if (value.StartsWith("@"))
  9.             {
  10.                 DictRef = AtRefToDictRef(value);
  11.                 AtRef = value;
  12.             }
  13.             else
  14.             {
  15.                 DictRef = value;
  16.                 AtRef = DictRefToAtRef(value);
  17.             }
  18.         }
  19.  
  20.         public TranslateText(string dictRef, string atRef)
  21.         {
  22.             DictRef = dictRef;
  23.             AtRef = atRef;
  24.         }
  25.  
  26.         public static implicit operator TranslateText(string value) => new TranslateText(value);
  27.  
  28.         /* Format comparison:
  29.        
  30.             There's 3 situations, and two major formats (atRefs and dictRefs).
  31.             dictRefs use numeral literals, atRefs use a fixed-width (3 or 4 digits) base-16 literal that starts with 0 = A, 1 = B, etc.
  32.  
  33.             Situations:
  34.                 System dictionary.
  35.                     AtRef:      @9xxxx
  36.                     DictRef:    <10:x>
  37.                 Database dictionary.
  38.                     AtRef:      @8xxxx
  39.                     DictRef:    <20:x>
  40.                 Other dictionary.
  41.                     AtRef:      @yyy0xxxx
  42.                     DictRef:    <0:x-y>
  43.         */
  44.  
  45.         private static string AtRefToDictRef(string atRef)
  46.         {
  47.             string Convert(string value) => StringToDecimal(value.TrimStart('@', '0', '8', '9')).ToString();
  48.  
  49.             try
  50.             {
  51.                 if (atRef[1] == '9') // System dictionary
  52.                     return $"<10:{Convert(atRef)}>";
  53.                 else if (atRef[1] == '8') // Database dictionary.
  54.                     return $"<20:{Convert(atRef)}>";
  55.                 else // Other dictionary.
  56.                 {
  57.                     var items = atRef.Split('0');
  58.                     return $"<0:{Convert(items[2])}-{Convert(items[1])}>";
  59.                 }
  60.             }
  61.             catch
  62.             {
  63.                 return null;
  64.             }
  65.         }
  66.  
  67.        
  68.         private static string DictRefToAtRef(string dictRef)
  69.         {
  70.             string Convert(string value, int pad = 4) => DecimalToString(int.Parse(value)).PadLeft(pad, 'A');
  71.  
  72.             try
  73.             {
  74.                 var elems = dictRef.Trim('<', '>').Split(':', '-');
  75.  
  76.                 if (elems[0] == "10") // System dictionary.
  77.                     return $"@9{Convert(elems[1])}";
  78.                 else if (elems[0] == "20") // Database dictionary.
  79.                     return $"@8{Convert(elems[1])}";
  80.                 else // Other dictionary.
  81.                     return $"@{Convert(elems[2], 3)}0{Convert(elems[1])}";
  82.             }
  83.             catch
  84.             {
  85.                 return null;
  86.             }
  87.         }
  88.  
  89.         private static string DecimalToString(int input)
  90.         {
  91.             string result = "";
  92.             while (input > 0)
  93.             {
  94.                 result = $"{(char)('A' + (input % 16))}{result}";
  95.                 input /= 16;
  96.             }
  97.             return result;
  98.         }
  99.  
  100.         private static int StringToDecimal(string input)
  101.         {
  102.             int result = 0;
  103.             foreach (var letter in input)
  104.             {
  105.                 result *= 16;
  106.                 result += letter - 'A';
  107.             }
  108.             return result;
  109.         }
  110.  
  111.         public override int GetHashCode()
  112.         {
  113.             var hashCode = -564339277;
  114.             hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(DictRef);
  115.             hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(AtRef);
  116.             return hashCode;
  117.         }
  118.  
  119.         public override bool Equals(object other) => other is TranslateText text && Equals(text);
  120.         public bool Equals(TranslateText other) => AtRef == other.AtRef && DictRef == other.DictRef;
  121.  
  122.         public static bool operator ==(TranslateText lhs, TranslateText rhs) => lhs.Equals(rhs);
  123.         public static bool operator !=(TranslateText lhs, TranslateText rhs) => !lhs.Equals(rhs);
  124.     }
  125.  
  126.     public class TranslateTextConverter : JsonConverter<TranslateText>
  127.     {
  128.         public static TranslateTextConverter Instance { get; } = new TranslateTextConverter();
  129.  
  130.         public override TranslateText ReadJson(JsonReader reader, Type objectType, TranslateText existingValue, bool hasExistingValue, JsonSerializer serializer)
  131.             => new TranslateText(reader.Value as string);
  132.  
  133.         public override void WriteJson(JsonWriter writer, TranslateText value, JsonSerializer serializer)
  134.             => writer.WriteValue(value.DictRef);
  135.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement