EReg.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import cs.system.text.regularExpressions.Regex;
  23. class EReg {
  24. private var regex : Regex;
  25. private var m : Match;
  26. private var isGlobal : Bool;
  27. private var cur : String;
  28. public function new( r : String, opt : String ) : Void {
  29. var opts:Int = cast CultureInvariant;
  30. for (i in 0...opt.length) untyped {
  31. switch(cast(opt[i], Int))
  32. {
  33. case 'i'.code:
  34. opts |= cast(IgnoreCase, Int);
  35. case 'g'.code:
  36. isGlobal = true;
  37. case 'm'.code:
  38. opts |= cast(Multiline, Int);
  39. case 'c'.code:
  40. opts |= cast(Compiled, Int);
  41. }
  42. }
  43. this.regex = new Regex(r, cast(opts, RegexOptions));
  44. }
  45. public function match( s : String ) : Bool {
  46. m = regex.Match(s);
  47. cur = s;
  48. return m.Success;
  49. }
  50. public function matched( n : Int ) : String {
  51. if (m == null || cast(n, UInt) > m.Groups.Count)
  52. throw "EReg::matched";
  53. if (!m.Groups[n].Success) return null;
  54. return m.Groups[n].Value;
  55. }
  56. public function matchedLeft() : String {
  57. return untyped cur.Substring(0, m.Index);
  58. }
  59. public function matchedRight() : String {
  60. return untyped cur.Substring(m.Index + m.Length);
  61. }
  62. public function matchedPos() : { pos : Int, len : Int } {
  63. return { pos : m.Index, len : m.Length };
  64. }
  65. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  66. m= if (len<0) regex.Match(s,pos) else regex.Match(s, pos, len);
  67. cur = s;
  68. return m.Success;
  69. }
  70. public function split( s : String ) : Array<String> {
  71. if (isGlobal)
  72. return cs.Lib.array(regex.Split(s));
  73. var m = regex.Match(s);
  74. if (!m.Success) return [s];
  75. return untyped [s.Substring(0, m.Index), s.Substring(m.Index + m.Length)];
  76. }
  77. inline function start(group:Int)
  78. {
  79. return m.Groups[group].Index;
  80. }
  81. inline function len(group:Int)
  82. {
  83. return m.Groups[group].Length;
  84. }
  85. public function replace( s : String, by : String ) : String
  86. {
  87. return (isGlobal) ? regex.Replace(s, by): regex.Replace(s,by,1);
  88. }
  89. public function map( s : String, f : EReg -> String ) : String {
  90. var offset = 0;
  91. var buf = new StringBuf();
  92. do {
  93. if (offset >= s.length)
  94. break;
  95. else if (!matchSub(s, offset)) {
  96. buf.add(s.substr(offset));
  97. break;
  98. }
  99. var p = matchedPos();
  100. buf.add(s.substr(offset, p.pos - offset));
  101. buf.add(f(this));
  102. if (p.len == 0) {
  103. buf.add(s.substr(p.pos, 1));
  104. offset = p.pos + 1;
  105. }
  106. else
  107. offset = p.pos + p.len;
  108. } while (isGlobal);
  109. if (!isGlobal && offset > 0 && offset < s.length)
  110. buf.add(s.substr(offset));
  111. return buf.toString();
  112. }
  113. }