RegexTrial.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace MonoTests.System.Text.RegularExpressions {
  4. class RegexTrial {
  5. public string pattern;
  6. public RegexOptions options;
  7. public string input;
  8. public string expected;
  9. public string error = "";
  10. public RegexTrial (string pattern, RegexOptions options, string input, string expected) {
  11. this.pattern = pattern;
  12. this.options = options;
  13. this.input = input;
  14. this.expected = expected;
  15. }
  16. public string Expected {
  17. get { return expected; }
  18. }
  19. public string Error {
  20. get {
  21. return this.error;
  22. }
  23. }
  24. public string Execute () {
  25. string result;
  26. try {
  27. Regex re = new Regex (pattern, options);
  28. Match m = re.Match (input);
  29. if (m.Success) {
  30. result = "Pass.";
  31. for (int i = 0; i < m.Groups.Count; ++ i) {
  32. Group group = m.Groups[i];
  33. result += " Group[" + i + "]=";
  34. foreach (Capture cap in group.Captures) {
  35. result += "(" + cap.Index + "," + cap.Length + ")";
  36. }
  37. }
  38. }
  39. else
  40. result = "Fail.";
  41. }
  42. catch (Exception e) {
  43. error = e.Message + "\n" + e.StackTrace + "\n\n";
  44. result = "Error.";
  45. }
  46. return result;
  47. }
  48. public override string ToString () {
  49. return
  50. "Matching input '" + input +
  51. "' against pattern '" + pattern +
  52. "' with options '" + options + "'.";
  53. }
  54. }
  55. class Checksum {
  56. public Checksum () {
  57. this.sum = 0;
  58. }
  59. public uint Value {
  60. get { return sum; }
  61. }
  62. public void Add (string str) {
  63. for (int i = 0; i < str.Length; ++ i)
  64. Add (str[i], 16);
  65. }
  66. public void Add (uint n) {
  67. Add (n, 32);
  68. }
  69. public void Add (ulong n, int bits) {
  70. ulong mask = 1ul << (bits - 1);
  71. for (int i = 0; i < bits; ++ i) {
  72. Add ((n & mask) != 0);
  73. mask >>= 1;
  74. }
  75. }
  76. public void Add (bool bit) {
  77. bool top = (sum & 0x80000000) != 0;
  78. sum <<= 1;
  79. sum ^= bit ? (uint)1 : (uint)0;
  80. if (top)
  81. sum ^= key;
  82. }
  83. private uint sum;
  84. private readonly uint key = 0x04c11db7;
  85. }
  86. }