PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/System/System.Text.RegularExpressions/RegexReplacement.cs

https://bitbucket.org/cosi2/dotnetanywhere-wb
C# | 357 lines | 345 code | 12 blank | 0 comment | 73 complexity | c8e0320911597fcb803aa87299ea2bd3 MD5 | raw file
  1. namespace System.Text.RegularExpressions
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Text;
  6. internal sealed class RegexReplacement
  7. {
  8. internal string _rep;
  9. internal int[] _rules;
  10. internal string[] _strings;
  11. internal const int LastGroup = -3;
  12. internal const int LeftPortion = -1;
  13. internal const int RightPortion = -2;
  14. internal const int Specials = 4;
  15. internal const int WholeString = -4;
  16. internal RegexReplacement(string rep, RegexNode concat, System.Collections.Generic.Dictionary<object,object> _caps)
  17. {
  18. this._rep = rep;
  19. if (concat.Type() != 0x19)
  20. {
  21. throw new ArgumentException(RegExRes.GetString(0x25));
  22. }
  23. StringBuilder builder = new StringBuilder();
  24. ArrayList list = new ArrayList();
  25. ArrayList list2 = new ArrayList();
  26. for (int i = 0; i < concat.ChildCount(); i++)
  27. {
  28. RegexNode node = concat.Child(i);
  29. switch (node.Type())
  30. {
  31. case 9:
  32. {
  33. builder.Append(node._ch);
  34. continue;
  35. }
  36. case 12:
  37. {
  38. builder.Append(node._str);
  39. continue;
  40. }
  41. case 13:
  42. {
  43. if (builder.Length > 0)
  44. {
  45. list2.Add(list.Count);
  46. list.Add(builder.ToString());
  47. builder.Length = 0;
  48. }
  49. int num = node._m;
  50. if ((_caps != null) && (num >= 0))
  51. {
  52. num = (int) _caps[num];
  53. }
  54. list2.Add(-5 - num);
  55. continue;
  56. }
  57. }
  58. throw new ArgumentException(RegExRes.GetString(0x25));
  59. }
  60. if (builder.Length > 0)
  61. {
  62. list2.Add(list.Count);
  63. list.Add(builder.ToString());
  64. }
  65. this._strings = new string[list.Count];
  66. list.CopyTo(0, this._strings, 0, list.Count);
  67. this._rules = new int[list2.Count];
  68. for (int j = 0; j < list2.Count; j++)
  69. {
  70. this._rules[j] = (int) list2[j];
  71. }
  72. }
  73. internal string Replace(Regex regex, string input, int count, int startat)
  74. {
  75. StringBuilder builder;
  76. if (count < -1)
  77. {
  78. throw new ArgumentOutOfRangeException();
  79. }
  80. if ((startat < 0) || (startat > input.Length))
  81. {
  82. throw new ArgumentOutOfRangeException();
  83. }
  84. if (count == 0)
  85. {
  86. return input;
  87. }
  88. Match match = regex.Match(input, startat);
  89. if (!match.Success)
  90. {
  91. return input;
  92. }
  93. if (regex.RightToLeft)
  94. {
  95. ArrayList list = new ArrayList();
  96. int length = input.Length;
  97. Label_00BF:
  98. if ((match.Index + match.Length) != length)
  99. {
  100. list.Add(input.Substring(match.Index + match.Length, (length - match.Index) - match.Length));
  101. }
  102. length = match.Index;
  103. for (int i = this._rules.Length - 1; i >= 0; i--)
  104. {
  105. int index = this._rules[i];
  106. if (index >= 0)
  107. {
  108. list.Add(this._strings[index]);
  109. }
  110. else
  111. {
  112. list.Add(match.GroupToStringImpl(-5 - index));
  113. }
  114. }
  115. if (--count != 0)
  116. {
  117. match = match.NextMatch();
  118. if (match.Success)
  119. {
  120. goto Label_00BF;
  121. }
  122. }
  123. builder = new StringBuilder();
  124. if (length > 0)
  125. {
  126. builder.Append(input, 0, length);
  127. }
  128. for (int j = list.Count - 1; j >= 0; j--)
  129. {
  130. builder.Append((string) list[j]);
  131. }
  132. goto Label_01AA;
  133. }
  134. builder = new StringBuilder();
  135. int startIndex = 0;
  136. Label_0048:
  137. if (match.Index != startIndex)
  138. {
  139. builder.Append(input, startIndex, match.Index - startIndex);
  140. }
  141. startIndex = match.Index + match.Length;
  142. this.ReplacementImpl(builder, match);
  143. if (--count != 0)
  144. {
  145. match = match.NextMatch();
  146. if (match.Success)
  147. {
  148. goto Label_0048;
  149. }
  150. }
  151. if (startIndex < input.Length)
  152. {
  153. builder.Append(input, startIndex, input.Length - startIndex);
  154. }
  155. Label_01AA:
  156. return builder.ToString();
  157. }
  158. internal static string Replace(MatchEvaluator evaluator, Regex regex, string input, int count, int startat)
  159. {
  160. StringBuilder builder;
  161. if (evaluator == null)
  162. {
  163. throw new ArgumentNullException();
  164. }
  165. if (count < -1)
  166. {
  167. throw new ArgumentOutOfRangeException();
  168. }
  169. if ((startat < 0) || (startat > input.Length))
  170. {
  171. throw new ArgumentOutOfRangeException();
  172. }
  173. if (count == 0)
  174. {
  175. return input;
  176. }
  177. Match match = regex.Match(input, startat);
  178. if (!match.Success)
  179. {
  180. return input;
  181. }
  182. if (regex.RightToLeft)
  183. {
  184. ArrayList list = new ArrayList();
  185. int length = input.Length;
  186. Label_00CE:
  187. if ((match.Index + match.Length) != length)
  188. {
  189. list.Add(input.Substring(match.Index + match.Length, (length - match.Index) - match.Length));
  190. }
  191. length = match.Index;
  192. list.Add(evaluator(match));
  193. if (--count != 0)
  194. {
  195. match = match.NextMatch();
  196. if (match.Success)
  197. {
  198. goto Label_00CE;
  199. }
  200. }
  201. builder = new StringBuilder();
  202. if (length > 0)
  203. {
  204. builder.Append(input, 0, length);
  205. }
  206. for (int i = list.Count - 1; i >= 0; i--)
  207. {
  208. builder.Append((string) list[i]);
  209. }
  210. goto Label_0177;
  211. }
  212. builder = new StringBuilder();
  213. int startIndex = 0;
  214. Label_0051:
  215. if (match.Index != startIndex)
  216. {
  217. builder.Append(input, startIndex, match.Index - startIndex);
  218. }
  219. startIndex = match.Index + match.Length;
  220. builder.Append(evaluator(match));
  221. if (--count != 0)
  222. {
  223. match = match.NextMatch();
  224. if (match.Success)
  225. {
  226. goto Label_0051;
  227. }
  228. }
  229. if (startIndex < input.Length)
  230. {
  231. builder.Append(input, startIndex, input.Length - startIndex);
  232. }
  233. Label_0177:
  234. return builder.ToString();
  235. }
  236. internal string Replacement(Match match)
  237. {
  238. StringBuilder sb = new StringBuilder();
  239. this.ReplacementImpl(sb, match);
  240. return sb.ToString();
  241. }
  242. private void ReplacementImpl(StringBuilder sb, Match match)
  243. {
  244. for (int i = 0; i < this._rules.Length; i++)
  245. {
  246. int index = this._rules[i];
  247. if (index >= 0)
  248. {
  249. sb.Append(this._strings[index]);
  250. }
  251. else if (index < -4)
  252. {
  253. sb.Append(match.GroupToStringImpl(-5 - index));
  254. }
  255. else
  256. {
  257. switch ((-5 - index))
  258. {
  259. case -4:
  260. sb.Append(match.GetOriginalString());
  261. break;
  262. case -3:
  263. sb.Append(match.LastGroupToStringImpl());
  264. break;
  265. case -2:
  266. sb.Append(match.GetRightSubstring());
  267. break;
  268. case -1:
  269. sb.Append(match.GetLeftSubstring());
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. internal static string[] Split(Regex regex, string input, int count, int startat)
  276. {
  277. string[] strArray2;
  278. if (count < 0)
  279. {
  280. throw new ArgumentOutOfRangeException();
  281. }
  282. if ((startat < 0) || (startat > input.Length))
  283. {
  284. throw new ArgumentOutOfRangeException();
  285. }
  286. if (count == 1)
  287. {
  288. return new string[] { input };
  289. }
  290. count--;
  291. Match match = regex.Match(input, startat);
  292. if (!match.Success)
  293. {
  294. return new string[] { input };
  295. }
  296. ArrayList list = new ArrayList();
  297. if (regex.RightToLeft)
  298. {
  299. int length = input.Length;
  300. do
  301. {
  302. list.Add(input.Substring(match.Index + match.Length, (length - match.Index) - match.Length));
  303. length = match.Index;
  304. if (--count == 0)
  305. {
  306. break;
  307. }
  308. match = match.NextMatch();
  309. }
  310. while (match.Success);
  311. list.Add(input.Substring(0, length));
  312. list.Reverse(0, list.Count);
  313. goto Label_0153;
  314. }
  315. int startIndex = 0;
  316. Label_0064:
  317. list.Add(input.Substring(startIndex, match.Index - startIndex));
  318. startIndex = match.Index + match.Length;
  319. for (int i = 1; match.IsMatched(i); i++)
  320. {
  321. list.Add(match.Groups[i].ToString());
  322. }
  323. if (--count != 0)
  324. {
  325. match = match.NextMatch();
  326. if (match.Success)
  327. {
  328. goto Label_0064;
  329. }
  330. }
  331. list.Add(input.Substring(startIndex, input.Length - startIndex));
  332. Label_0153:
  333. strArray2 = new string[list.Count];
  334. list.CopyTo(0, strArray2, 0, list.Count);
  335. return strArray2;
  336. }
  337. internal string Pattern
  338. {
  339. get
  340. {
  341. return this._rep;
  342. }
  343. }
  344. }
  345. }