StringExtensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. private static readonly char[] NewlinesCharacters = new char[] { '\r', '\n' };
  59. private static readonly char[] WhitespacesAndNewlines = new char[] { '\r', '\n', ' ', ' ' };
  60. public static TemplatedString TemplatizeByNumbers( this string str )
  61. {
  62. var dict = new Dictionary<string, string>();
  63. bool isNumber = false;
  64. StringBuilder carg = null;
  65. char arg = 'A';
  66. for( int i = 0 ; i < str.Length ; i++ )
  67. {
  68. var c = str[ i ];
  69. if( isNumber )
  70. {
  71. if( NumbersWithDot.Contains( c ) )
  72. {
  73. carg.Append( c );
  74. }
  75. else
  76. {
  77. // end current number
  78. var variable = carg.ToString();
  79. var ok = true;
  80. var c1 = variable[ 0 ];
  81. if( c1 == '.' )
  82. {
  83. if( variable.Length == 1 )
  84. {
  85. ok = false;
  86. }
  87. else
  88. {
  89. var c2 = variable[ 1 ];
  90. ok = Numbers.Contains( c2 );
  91. }
  92. }
  93. if( ok && !dict.ContainsKey( variable ) )
  94. {
  95. dict.Add( variable, "{{" + arg + "}}" );
  96. arg++;
  97. }
  98. carg = null;
  99. isNumber = false;
  100. }
  101. }
  102. else
  103. {
  104. if( NumbersWithDot.Contains( c ) )
  105. {
  106. isNumber = true;
  107. carg = new StringBuilder();
  108. carg.Append( c );
  109. }
  110. }
  111. }
  112. if( carg != null )
  113. {
  114. // end current number
  115. var variable = carg.ToString();
  116. var ok = true;
  117. var c1 = variable[ 0 ];
  118. if( c1 == '.' )
  119. {
  120. if( variable.Length == 1 )
  121. {
  122. ok = false;
  123. }
  124. else
  125. {
  126. var c2 = variable[ 1 ];
  127. ok = Numbers.Contains( c2 );
  128. }
  129. }
  130. if( ok && !dict.ContainsKey( variable ) )
  131. {
  132. dict.Add( variable, "{{" + arg + "}}" );
  133. arg++;
  134. }
  135. }
  136. if( dict.Count > 0 )
  137. {
  138. foreach( var kvp in dict )
  139. {
  140. str = str.Replace( kvp.Key, kvp.Value );
  141. }
  142. return new TemplatedString( str, dict.ToDictionary( x => x.Value, x => x.Key ) );
  143. }
  144. else
  145. {
  146. return null;
  147. }
  148. }
  149. public static string SplitToLines( this string text, int maxStringLength, params char[] splitOnCharacters )
  150. {
  151. var sb = new StringBuilder();
  152. var index = 0;
  153. while( text.Length > index )
  154. {
  155. // start a new line, unless we've just started
  156. if( index != 0 )
  157. sb.Append( '\n' );
  158. // get the next substring, else the rest of the string if remainder is shorter than `maxStringLength`
  159. var splitAt = index + maxStringLength <= text.Length
  160. ? text.Substring( index, maxStringLength ).LastIndexOfAny( splitOnCharacters )
  161. : text.Length - index;
  162. // if can't find split location, take `maxStringLength` characters
  163. splitAt = ( splitAt == -1 ) ? maxStringLength : splitAt;
  164. // add result to collection & increment index
  165. sb.Append( text.Substring( index, splitAt ).Trim() );
  166. index += splitAt;
  167. }
  168. return sb.ToString();
  169. }
  170. public static string TrimIfConfigured( this string text )
  171. {
  172. if( text == null ) return text;
  173. if( Settings.TrimAllText )
  174. {
  175. return text.Trim();
  176. }
  177. return text;
  178. }
  179. public static string RemoveWhitespaceAndNewlines( this string text )
  180. {
  181. var builder = new StringBuilder( text.Length );
  182. if( Settings.WhitespaceRemovalStrategy == WhitespaceHandlingStrategy.AllOccurrences )
  183. {
  184. for( int i = 0 ; i < text.Length ; i++ )
  185. {
  186. var c = text[ i ];
  187. switch( c )
  188. {
  189. case '\n':
  190. case '\r':
  191. case ' ':
  192. case ' ':
  193. break;
  194. default:
  195. builder.Append( c );
  196. break;
  197. }
  198. }
  199. }
  200. else // if( Settings.WhitespaceHandlingStrategy == WhitespaceHandlingStrategy.TrimPerNewline )
  201. {
  202. var lines = text.Split( NewlinesCharacters, StringSplitOptions.RemoveEmptyEntries );
  203. for( int i = 0 ; i < lines.Length ; i++ )
  204. {
  205. var line = lines[ i ].Trim( WhitespacesAndNewlines );
  206. for( int j = 0 ; j < line.Length ; j++ )
  207. {
  208. var c = line[ j ];
  209. builder.Append( c );
  210. }
  211. }
  212. }
  213. return builder.ToString();
  214. }
  215. public static bool ContainsNumbers( this string text )
  216. {
  217. foreach( var c in text )
  218. {
  219. if( Numbers.Contains( c ) )
  220. {
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. public static string UnescapeJson( this string str )
  227. {
  228. if( str == null ) return null;
  229. var builder = new StringBuilder( str );
  230. bool escapeNext = false;
  231. for( int i = 0 ; i < builder.Length ; i++ )
  232. {
  233. var c = builder[ i ];
  234. if( escapeNext )
  235. {
  236. bool found = true;
  237. char escapeWith = default( char );
  238. switch( c )
  239. {
  240. case 'b':
  241. escapeWith = '\b';
  242. break;
  243. case 'f':
  244. escapeWith = '\f';
  245. break;
  246. case 'n':
  247. escapeWith = '\n';
  248. break;
  249. case 'r':
  250. escapeWith = '\r';
  251. break;
  252. case 't':
  253. escapeWith = '\t';
  254. break;
  255. case '"':
  256. escapeWith = '\"';
  257. break;
  258. case '\\':
  259. escapeWith = '\\';
  260. break;
  261. case 'u':
  262. escapeWith = 'u';
  263. break;
  264. default:
  265. found = false;
  266. break;
  267. }
  268. // remove previous char and go one back
  269. if( found )
  270. {
  271. if( escapeWith == 'u' )
  272. {
  273. // unicode crap, lets handle the next 4 characters manually
  274. int code = int.Parse( new string( new char[] { builder[ i + 1 ], builder[ i + 2 ], builder[ i + 3 ], builder[ i + 4 ] } ), NumberStyles.HexNumber );
  275. var replacingChar = (char)code;
  276. builder.Remove( --i, 6 );
  277. builder.Insert( i, replacingChar );
  278. }
  279. else
  280. {
  281. // found proper escaping
  282. builder.Remove( --i, 2 );
  283. builder.Insert( i, escapeWith );
  284. }
  285. }
  286. else
  287. {
  288. // dont do anything
  289. }
  290. escapeNext = false;
  291. }
  292. else if( c == '\\' )
  293. {
  294. escapeNext = true;
  295. }
  296. }
  297. return builder.ToString();
  298. }
  299. public static string EscapeJson( this string str )
  300. {
  301. if( str == null || str.Length == 0 )
  302. {
  303. return "";
  304. }
  305. char c;
  306. int len = str.Length;
  307. StringBuilder sb = new StringBuilder( len + 4 );
  308. for( int i = 0 ; i < len ; i += 1 )
  309. {
  310. c = str[ i ];
  311. switch( c )
  312. {
  313. case '\\':
  314. case '"':
  315. sb.Append( '\\' );
  316. sb.Append( c );
  317. break;
  318. case '/':
  319. sb.Append( '\\' );
  320. sb.Append( c );
  321. break;
  322. case '\b':
  323. sb.Append( "\\b" );
  324. break;
  325. case '\t':
  326. sb.Append( "\\t" );
  327. break;
  328. case '\n':
  329. sb.Append( "\\n" );
  330. break;
  331. case '\f':
  332. sb.Append( "\\f" );
  333. break;
  334. case '\r':
  335. sb.Append( "\\r" );
  336. break;
  337. default:
  338. sb.Append( c );
  339. break;
  340. }
  341. }
  342. return sb.ToString();
  343. }
  344. public static string GetBetween( this string strSource, string strStart, string strEnd )
  345. {
  346. const int kNotFound = -1;
  347. var startIdx = strSource.IndexOf( strStart );
  348. if( startIdx != kNotFound )
  349. {
  350. startIdx += strStart.Length;
  351. var endIdx = strSource.IndexOf( strEnd, startIdx );
  352. if( endIdx > startIdx )
  353. {
  354. return strSource.Substring( startIdx, endIdx - startIdx );
  355. }
  356. }
  357. return String.Empty;
  358. }
  359. }
  360. }