StringExtensions.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  7. namespace XUnity.AutoTranslator.Plugin.Core.Extensions
  8. {
  9. public static class StringExtensions
  10. {
  11. private static readonly HashSet<char> Numbers = new HashSet<char>
  12. {
  13. '0',
  14. '1',
  15. '2',
  16. '3',
  17. '4',
  18. '5',
  19. '6',
  20. '7',
  21. '8',
  22. '9',
  23. '0',
  24. '1',
  25. '2',
  26. '3',
  27. '4',
  28. '5',
  29. '6',
  30. '7',
  31. '8',
  32. '9'
  33. };
  34. private static readonly HashSet<char> NumbersWithDot = new HashSet<char>
  35. {
  36. '0',
  37. '1',
  38. '2',
  39. '3',
  40. '4',
  41. '5',
  42. '6',
  43. '7',
  44. '8',
  45. '9',
  46. '0',
  47. '1',
  48. '2',
  49. '3',
  50. '4',
  51. '5',
  52. '6',
  53. '7',
  54. '8',
  55. '9',
  56. '.'
  57. };
  58. public static TemplatedString TemplatizeByNumbers( this string str )
  59. {
  60. var dict = new Dictionary<string, string>();
  61. bool isNumber = false;
  62. StringBuilder carg = null;
  63. char arg = 'A';
  64. for( int i = 0 ; i < str.Length ; i++ )
  65. {
  66. var c = str[ i ];
  67. if( isNumber )
  68. {
  69. if( NumbersWithDot.Contains( c ) )
  70. {
  71. carg.Append( c );
  72. }
  73. else
  74. {
  75. // end current number
  76. var variable = carg.ToString();
  77. var ok = true;
  78. var c1 = variable[ 0 ];
  79. if( c1 == '.' )
  80. {
  81. if( variable.Length == 1 )
  82. {
  83. ok = false;
  84. }
  85. else
  86. {
  87. var c2 = variable[ 1 ];
  88. ok = Numbers.Contains( c2 );
  89. }
  90. }
  91. if( ok && !dict.ContainsKey( variable ) )
  92. {
  93. dict.Add( variable, "{{" + arg + "}}" );
  94. arg++;
  95. }
  96. carg = null;
  97. isNumber = false;
  98. }
  99. }
  100. else
  101. {
  102. if( NumbersWithDot.Contains( c ) )
  103. {
  104. isNumber = true;
  105. carg = new StringBuilder();
  106. carg.Append( c );
  107. }
  108. }
  109. }
  110. if( carg != null )
  111. {
  112. // end current number
  113. var variable = carg.ToString();
  114. var ok = true;
  115. var c1 = variable[ 0 ];
  116. if( c1 == '.' )
  117. {
  118. if( variable.Length == 1 )
  119. {
  120. ok = false;
  121. }
  122. else
  123. {
  124. var c2 = variable[ 1 ];
  125. ok = Numbers.Contains( c2 );
  126. }
  127. }
  128. if( ok && !dict.ContainsKey( variable ) )
  129. {
  130. dict.Add( variable, "{{" + arg + "}}" );
  131. arg++;
  132. }
  133. }
  134. if( dict.Count > 0 )
  135. {
  136. foreach( var kvp in dict )
  137. {
  138. str = str.Replace( kvp.Key, kvp.Value );
  139. }
  140. return new TemplatedString( str, dict.ToDictionary( x => x.Value, x => x.Key ) );
  141. }
  142. else
  143. {
  144. return null;
  145. }
  146. }
  147. public static string SplitToLines( this string text, int maxStringLength, params char[] splitOnCharacters )
  148. {
  149. var sb = new StringBuilder();
  150. var index = 0;
  151. while( text.Length > index )
  152. {
  153. // start a new line, unless we've just started
  154. if( index != 0 )
  155. sb.Append( '\n' );
  156. // get the next substring, else the rest of the string if remainder is shorter than `maxStringLength`
  157. var splitAt = index + maxStringLength <= text.Length
  158. ? text.Substring( index, maxStringLength ).LastIndexOfAny( splitOnCharacters )
  159. : text.Length - index;
  160. // if can't find split location, take `maxStringLength` characters
  161. splitAt = ( splitAt == -1 ) ? maxStringLength : splitAt;
  162. // add result to collection & increment index
  163. sb.Append( text.Substring( index, splitAt ).Trim() );
  164. index += splitAt;
  165. }
  166. return sb.ToString();
  167. }
  168. public static string RemoveWhitespace( this string text )
  169. {
  170. // Japanese whitespace, wtf
  171. return text.Replace( "\n", "" ).Replace( "\r", "" ).Replace( " ", "" ).Replace( " ", "" );
  172. }
  173. public static bool ContainsNumbers( this string text )
  174. {
  175. foreach( var c in text )
  176. {
  177. if( Numbers.Contains( c ) )
  178. {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. public static string UnescapeJson( this string str )
  185. {
  186. if( str == null ) return null;
  187. var builder = new StringBuilder( str );
  188. bool escapeNext = false;
  189. for( int i = 0 ; i < builder.Length ; i++ )
  190. {
  191. var c = builder[ i ];
  192. if( escapeNext )
  193. {
  194. bool found = true;
  195. char escapeWith = default( char );
  196. switch( c )
  197. {
  198. case 'b':
  199. escapeWith = '\b';
  200. break;
  201. case 'f':
  202. escapeWith = '\f';
  203. break;
  204. case 'n':
  205. escapeWith = '\n';
  206. break;
  207. case 'r':
  208. escapeWith = '\r';
  209. break;
  210. case 't':
  211. escapeWith = '\t';
  212. break;
  213. case '"':
  214. escapeWith = '\"';
  215. break;
  216. case '\\':
  217. escapeWith = '\\';
  218. break;
  219. case 'u':
  220. escapeWith = 'u';
  221. break;
  222. default:
  223. found = false;
  224. break;
  225. }
  226. // remove previous char and go one back
  227. if( found )
  228. {
  229. if( escapeWith == 'u' )
  230. {
  231. // unicode crap, lets handle the next 4 characters manually
  232. int code = int.Parse( new string( new char[] { builder[ i + 1 ], builder[ i + 2 ], builder[ i + 3 ], builder[ i + 4 ] } ), NumberStyles.HexNumber );
  233. var replacingChar = (char)code;
  234. builder.Remove( --i, 6 );
  235. builder.Insert( i, replacingChar );
  236. }
  237. else
  238. {
  239. // found proper escaping
  240. builder.Remove( --i, 2 );
  241. builder.Insert( i, escapeWith );
  242. }
  243. }
  244. else
  245. {
  246. // dont do anything
  247. }
  248. escapeNext = false;
  249. }
  250. else if( c == '\\' )
  251. {
  252. escapeNext = true;
  253. }
  254. }
  255. return builder.ToString();
  256. }
  257. }
  258. }