Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct TranslateText : IEquatable<TranslateText>
- {
- public string DictRef { get; }
- public string AtRef { get; }
- public TranslateText(string value)
- {
- if (value.StartsWith("@"))
- {
- DictRef = AtRefToDictRef(value);
- AtRef = value;
- }
- else
- {
- DictRef = value;
- AtRef = DictRefToAtRef(value);
- }
- }
- public TranslateText(string dictRef, string atRef)
- {
- DictRef = dictRef;
- AtRef = atRef;
- }
- public static implicit operator TranslateText(string value) => new TranslateText(value);
- /* Format comparison:
- There's 3 situations, and two major formats (atRefs and dictRefs).
- dictRefs use numeral literals, atRefs use a fixed-width (3 or 4 digits) base-16 literal that starts with 0 = A, 1 = B, etc.
- Situations:
- System dictionary.
- AtRef: @9xxxx
- DictRef: <10:x>
- Database dictionary.
- AtRef: @8xxxx
- DictRef: <20:x>
- Other dictionary.
- AtRef: @yyy0xxxx
- DictRef: <0:x-y>
- */
- private static string AtRefToDictRef(string atRef)
- {
- string Convert(string value) => StringToDecimal(value.TrimStart('@', '0', '8', '9')).ToString();
- try
- {
- if (atRef[1] == '9') // System dictionary
- return $"<10:{Convert(atRef)}>";
- else if (atRef[1] == '8') // Database dictionary.
- return $"<20:{Convert(atRef)}>";
- else // Other dictionary.
- {
- var items = atRef.Split('0');
- return $"<0:{Convert(items[2])}-{Convert(items[1])}>";
- }
- }
- catch
- {
- return null;
- }
- }
- private static string DictRefToAtRef(string dictRef)
- {
- string Convert(string value, int pad = 4) => DecimalToString(int.Parse(value)).PadLeft(pad, 'A');
- try
- {
- var elems = dictRef.Trim('<', '>').Split(':', '-');
- if (elems[0] == "10") // System dictionary.
- return $"@9{Convert(elems[1])}";
- else if (elems[0] == "20") // Database dictionary.
- return $"@8{Convert(elems[1])}";
- else // Other dictionary.
- return $"@{Convert(elems[2], 3)}0{Convert(elems[1])}";
- }
- catch
- {
- return null;
- }
- }
- private static string DecimalToString(int input)
- {
- string result = "";
- while (input > 0)
- {
- result = $"{(char)('A' + (input % 16))}{result}";
- input /= 16;
- }
- return result;
- }
- private static int StringToDecimal(string input)
- {
- int result = 0;
- foreach (var letter in input)
- {
- result *= 16;
- result += letter - 'A';
- }
- return result;
- }
- public override int GetHashCode()
- {
- var hashCode = -564339277;
- hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(DictRef);
- hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(AtRef);
- return hashCode;
- }
- public override bool Equals(object other) => other is TranslateText text && Equals(text);
- public bool Equals(TranslateText other) => AtRef == other.AtRef && DictRef == other.DictRef;
- public static bool operator ==(TranslateText lhs, TranslateText rhs) => lhs.Equals(rhs);
- public static bool operator !=(TranslateText lhs, TranslateText rhs) => !lhs.Equals(rhs);
- }
- public class TranslateTextConverter : JsonConverter<TranslateText>
- {
- public static TranslateTextConverter Instance { get; } = new TranslateTextConverter();
- public override TranslateText ReadJson(JsonReader reader, Type objectType, TranslateText existingValue, bool hasExistingValue, JsonSerializer serializer)
- => new TranslateText(reader.Value as string);
- public override void WriteJson(JsonWriter writer, TranslateText value, JsonSerializer serializer)
- => writer.WriteValue(value.DictRef);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement