TextTuple.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. namespace BeatLyrics.Tool.Models
  2. {
  3. public class TextTuple
  4. {
  5. public string Text { get; set; }
  6. public string Ruby { get; set; }
  7. public override string ToString()
  8. {
  9. var left = 0;
  10. var regexKata = new System.Text.RegularExpressions.Regex("^[\\p{IsKatakana}]+$");
  11. if (regexKata.IsMatch(Text)) return Text;
  12. if (Ruby == Text) return Text;
  13. if (false == string.IsNullOrWhiteSpace(Ruby))
  14. {
  15. for (int i = 0; i < Ruby.Length; i++)
  16. {
  17. if (Text[Text.Length - 1 - i] == Ruby[Ruby.Length - 1 - i]) continue;
  18. left = i;
  19. break;
  20. }
  21. var textLeft = Text.Substring(0, Text.Length - left);
  22. var textRight = Text.Substring(Text.Length - left);
  23. var midRuby = Ruby.Substring(0, Ruby.Length - left);
  24. return $"{textLeft}({midRuby}){textRight}";
  25. }
  26. return Text + (Text == Ruby || string.IsNullOrWhiteSpace(Ruby) ? "" : $"({Ruby})");
  27. }
  28. }
  29. }