Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. string eng = "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>`~ёЁ";
  2. string ru = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮеЕеЕ";
  3. for (int i = 0; i < eng.Length; ++i)
  4. if (query.Contains(eng[i]))
  5. query = query.Replace(eng[i], ru[i]);
  6.  
  7. public sealed class Replacer
  8. {
  9. private readonly Dictionary<Char, Char> _dictionary;
  10.  
  11. public Replacer(String sourceSymbols, String targetSymbols)
  12. {
  13. if (sourceSymbols.Length != targetSymbols.Length)
  14. throw new NotSupportedException("sourceSymbols.Length != targetSymbols.Length");
  15.  
  16. Int32 count = sourceSymbols.Length;
  17.  
  18. Dictionary<Char, Char> dictionary = new Dictionary<Char, Char>(count);
  19. for (int i = 0; i < count; i++)
  20. dictionary.Add(sourceSymbols[i], targetSymbols[i]);
  21.  
  22. _dictionary = dictionary;
  23. }
  24.  
  25. public void FixCharacters(ref String query)
  26. {
  27. if (String.IsNullOrEmpty(query))
  28. return;
  29.  
  30. if (String.IsInterned(query) == null)
  31. {
  32. FixNotInternedString(query);
  33. }
  34. else
  35. {
  36. FixInternedString(ref query);
  37. }
  38. }
  39.  
  40. private unsafe void FixNotInternedString(String query)
  41. {
  42. Int32 index = query.Length - 1;
  43. fixed (Char* chPtr = query)
  44. {
  45. while (index >= 0)
  46. {
  47. Char oldChar = chPtr[index];
  48.  
  49. Char newChar;
  50. if (_dictionary.TryGetValue(oldChar, out newChar))
  51. chPtr[index] = newChar;
  52.  
  53. index--;
  54. }
  55. }
  56. }
  57.  
  58. private void FixInternedString(ref String query)
  59. {
  60. StringBuilder sb = new StringBuilder(query.Length);
  61. foreach (Char c in query)
  62. {
  63. Char fixedChar;
  64. if (_dictionary.TryGetValue(c, out fixedChar))
  65. sb.Append(fixedChar);
  66. else
  67. sb.Append(c);
  68. }
  69. query = sb.ToString();
  70. }
  71. }
  72.  
  73. static void Main(string[] args)
  74. {
  75. String eng = "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>`~ёЁ";
  76. String rus = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮеЕеЕ";
  77.  
  78. Replacer replacer = new Replacer(eng, rus);
  79.  
  80. for (int i = 0; i < 10; i++)
  81. {
  82. String query = $"Hello World {i}";
  83.  
  84. replacer.FixCharacters(ref query);
  85.  
  86. Console.WriteLine(query); // "Руддщ Цщкдв"
  87. }
  88. }
  89.  
  90. public static class LangConversion
  91. {
  92. private static readonly Dictionary<char, char> engToRu = new Dictionary<char, char>();
  93.  
  94. static LangConversion()
  95. {
  96. var eng = "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>`~ёЁ";
  97. var ru = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮеЕеЕ";
  98. for (var i = 0; i < eng.Length; i++)
  99. engToRu[eng[i]] = ru[i];
  100. }
  101.  
  102. public static string Fix(string str)
  103. {
  104. var sb = new StringBuilder(str.Length);
  105. foreach (char c in str)
  106. {
  107. char fixedChar;
  108. sb.Append(engToRu.TryGetValue(c, out fixedChar) ? fixedChar : c);
  109. }
  110. return sb.ToString();
  111. }
  112. }
  113.  
  114. public static class LangConversion2
  115. {
  116. public static string Fix(string str)
  117. {
  118. var sb = new StringBuilder(str.Length);
  119. foreach (char c in str)
  120. {
  121. sb.Append(Replace(c));
  122. }
  123. return sb.ToString();
  124. }
  125.  
  126. private static char Replace(char c)
  127. {
  128. switch (c)
  129. {
  130. case 'q': return 'й';
  131. ...
  132. case 'Ё': return 'Е';
  133. default: return c;
  134. }
  135. }
  136. }
  137.  
  138. public static class LangConversion3
  139. {
  140. private static readonly char[] engToRu;
  141.  
  142. static LangConversion3()
  143. {
  144. var eng = "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>`~ёЁ";
  145. var ru = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮеЕеЕ";
  146. int maxCharCode = 0;
  147. foreach (char c in eng)
  148. maxCharCode = c > maxCharCode ? c : maxCharCode;
  149. engToRu = new char[maxCharCode + 1];
  150. for (var i = 0; i < eng.Length; i++)
  151. engToRu[eng[i]] = ru[i];
  152. }
  153.  
  154. public static string Fix(string str)
  155. {
  156. var sb = new StringBuilder(str.Length);
  157. foreach (char c in str)
  158. {
  159. sb.Append(Replace(c));
  160. }
  161. return sb.ToString();
  162. }
  163.  
  164. private static char Replace(char c)
  165. {
  166. if (c >= engToRu.Length)
  167. return c;
  168. var fixedChar = engToRu[c];
  169. return fixedChar != 0 ? fixedChar : c;
  170. }
  171. }
  172.  
  173. Исходный вариант: 6480мс
  174. Dictionary: 2550мс
  175. Pointers (ответ @LunarWhisper): 1560мс
  176. Switch-case: 1520мс
  177. Array: 1310мс
  178. Pointers + array: 720мс
  179. Pointers + switch-case: 580мс
  180.  
  181. for (int i = 0; i < eng.Length; ++i)
  182. query = query.Replace(eng[i], ru[i]);
  183.  
  184. // считаем один раз, оптимизация!
  185. static (char, char)[] dictionary = eng.Zip(ru, (a, b) => (a, b)).ToArray();
  186.  
  187. //....
  188.  
  189. foreach (var (e, r) in dictionary)
  190. query = query.Replace(e, r);
  191.  
  192. foreach (var (e, r) in eng.Zip(ru, (a, b) => (a, b)))
  193. query = query.Replace(e, r);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement