RegexTrial.cs 1.8 KB

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