LangManager.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Text;
  2. namespace MinorShift._Library
  3. {
  4. //マルチ言語に対応可能な形式に変更
  5. internal static class LangManager
  6. {
  7. static Encoding lang;
  8. public static void setEncode(int code)
  9. {
  10. lang = Encoding.GetEncoding(code);
  11. }
  12. public static int GetStrlenLang(string str)
  13. {
  14. return lang.GetByteCount(str);
  15. }
  16. public static int GetUFTIndex(string str, int LangIndex)
  17. {
  18. if (LangIndex <= 0)
  19. return 0;
  20. int totalByte = GetStrlenLang(str);
  21. if (LangIndex >= totalByte)
  22. return str.Length;
  23. int UTFcnt = 0;
  24. int JIScnt = 0;
  25. for (int i = 0; i < str.Length; i++)
  26. {
  27. JIScnt += lang.GetByteCount(str[UTFcnt].ToString());
  28. UTFcnt++;
  29. if (JIScnt >= LangIndex)
  30. break;
  31. }
  32. return UTFcnt;
  33. }
  34. public static string GetSubStringLang(string str, int startindex, int length)
  35. {
  36. int totalByte = GetStrlenLang(str);
  37. if ((startindex >= totalByte) || (length == 0))
  38. return "";
  39. if ((length < 0) || (length > totalByte))
  40. length = totalByte;
  41. StringBuilder ret = new StringBuilder();
  42. int UTFcnt = 0;
  43. int JIScnt = 0;
  44. if (startindex <= 0)
  45. {
  46. if (length == totalByte)
  47. return str;
  48. }
  49. else
  50. {
  51. for (int i = 0; i < str.Length; i++)
  52. {
  53. JIScnt += lang.GetByteCount(str[UTFcnt].ToString());
  54. UTFcnt++;
  55. if (JIScnt >= startindex)
  56. break;
  57. }
  58. if (UTFcnt >= str.Length)
  59. return "";
  60. }
  61. JIScnt = 0;
  62. while (true)
  63. {
  64. ret.Append(str[UTFcnt]);
  65. JIScnt += lang.GetByteCount(str[UTFcnt].ToString());
  66. UTFcnt++;
  67. if (JIScnt >= length)
  68. break;
  69. if (UTFcnt >= str.Length)
  70. break;
  71. }
  72. return ret.ToString();
  73. }
  74. }
  75. }