PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/dotnet/fan/sys/Regex.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 107 lines | 62 code | 21 blank | 24 comment | 16 complexity | 0cc20ffe3ee24defe9f1dbf42bd78c31 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2007, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 27 Dec 07 Andy Frank Creation
  7. //
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using NRegex = System.Text.RegularExpressions.Regex;
  11. namespace Fan.Sys
  12. {
  13. /// <summary>
  14. /// Regex
  15. /// </summary>
  16. public sealed class Regex : FanObj
  17. {
  18. //////////////////////////////////////////////////////////////////////////
  19. // Constructors
  20. //////////////////////////////////////////////////////////////////////////
  21. public static Regex fromStr(string pattern)
  22. {
  23. return new Regex(pattern);
  24. }
  25. public static Regex glob(string pattern)
  26. {
  27. StringBuilder s = new StringBuilder();
  28. for (int i=0; i<pattern.Length; ++i)
  29. {
  30. int c = pattern[i];
  31. if (FanInt.isAlphaNum(c)) s.Append((char)c);
  32. else if (c == '?') s.Append('.');
  33. else if (c == '*') s.Append('.').Append('*');
  34. else s.Append('\\').Append((char)c);
  35. }
  36. return new Regex(s.ToString());
  37. }
  38. Regex(string source)
  39. {
  40. this.m_source = source;
  41. this.m_pattern = new NRegex(source);
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. // Identity
  45. //////////////////////////////////////////////////////////////////////////
  46. public sealed override bool Equals(object obj)
  47. {
  48. if (obj is Regex)
  49. return ((Regex)obj).m_source == this.m_source;
  50. else
  51. return false;
  52. }
  53. public sealed override int GetHashCode() { return m_source.GetHashCode(); }
  54. public sealed override long hash() { return FanStr.hash(m_source); }
  55. public override string toStr() { return m_source; }
  56. public override Type @typeof() { return Sys.RegexType; }
  57. //////////////////////////////////////////////////////////////////////////
  58. // Regular expression
  59. //////////////////////////////////////////////////////////////////////////
  60. public bool matches(string s)
  61. {
  62. return new RegexMatcher(m_pattern.Match(s), s).matches();
  63. }
  64. public RegexMatcher matcher(string s)
  65. {
  66. return new RegexMatcher(m_pattern.Match(s), s);
  67. }
  68. public List split(string s) { return split(s, 0); }
  69. public List split(string s, long limit)
  70. {
  71. int l = (limit < 0) ? 0 : (int)limit;
  72. List result = new List(m_pattern.Split(s, l));
  73. // to match java we need to discard any trailing
  74. // emptys strings (use limit, not l)
  75. if (limit == 0)
  76. while (result.sz() > 0 && (result.last() as string).Length == 0)
  77. result.pop();
  78. return result;
  79. }
  80. //////////////////////////////////////////////////////////////////////////
  81. // Fields
  82. //////////////////////////////////////////////////////////////////////////
  83. private string m_source;
  84. private NRegex m_pattern;
  85. }
  86. }