EReg.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C)2005-2017 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. import cs.system.text.regularexpressions.Match;
  24. import cs.system.text.regularexpressions.RegexOptions;
  25. import cs.system.text.regularexpressions.*;
  26. @:coreApi @:final class EReg {
  27. private var regex : Regex;
  28. private var m : Match;
  29. private var isGlobal : Bool;
  30. private var cur : String;
  31. public function new( r : String, opt : String ) : Void {
  32. var opts:Int = cast CultureInvariant;
  33. for (i in 0...opt.length) untyped {
  34. switch(cast(opt[i], Int))
  35. {
  36. case 'i'.code:
  37. opts |= cast(IgnoreCase, Int);
  38. case 'g'.code:
  39. isGlobal = true;
  40. case 'm'.code:
  41. opts |= cast(Multiline, Int);
  42. #if (!unity && !unity_std_target)
  43. case 'c'.code:
  44. opts |= cast(Compiled, Int);
  45. #end
  46. }
  47. }
  48. this.regex = new Regex(r, cast(opts, RegexOptions));
  49. }
  50. public function match( s : String ) : Bool {
  51. m = regex.Match(s);
  52. cur = s;
  53. return m.Success;
  54. }
  55. public function matched( n : Int ) : String {
  56. if (m == null || cast(n, UInt) > m.Groups.Count)
  57. throw "EReg::matched";
  58. if (!m.Groups[n].Success) return null;
  59. return m.Groups[n].Value;
  60. }
  61. public function matchedLeft() : String {
  62. return untyped cur.Substring(0, m.Index);
  63. }
  64. public function matchedRight() : String {
  65. return untyped cur.Substring(m.Index + m.Length);
  66. }
  67. public function matchedPos() : { pos : Int, len : Int } {
  68. return { pos : m.Index, len : m.Length };
  69. }
  70. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  71. m= if (len<0) regex.Match(s,pos) else regex.Match(s, pos, len);
  72. cur = s;
  73. return m.Success;
  74. }
  75. public function split( s : String ) : Array<String> {
  76. if (isGlobal)
  77. return cs.Lib.array(regex.Split(s));
  78. var m = regex.Match(s);
  79. if (!m.Success) return [s];
  80. return untyped [s.Substring(0, m.Index), s.Substring(m.Index + m.Length)];
  81. }
  82. inline function start(group:Int) : Int
  83. {
  84. return m.Groups[group].Index;
  85. }
  86. inline function len(group:Int) : Int
  87. {
  88. return m.Groups[group].Length;
  89. }
  90. public function replace( s : String, by : String ) : String
  91. {
  92. return (isGlobal) ? regex.Replace(s, by): regex.Replace(s,by,1);
  93. }
  94. public function map( s : String, f : EReg -> String ) : String {
  95. var offset = 0;
  96. var buf = new StringBuf();
  97. do {
  98. if (offset >= s.length)
  99. break;
  100. else if (!matchSub(s, offset)) {
  101. buf.add(s.substr(offset));
  102. break;
  103. }
  104. var p = matchedPos();
  105. buf.add(s.substr(offset, p.pos - offset));
  106. buf.add(f(this));
  107. if (p.len == 0) {
  108. buf.add(s.substr(p.pos, 1));
  109. offset = p.pos + 1;
  110. }
  111. else
  112. offset = p.pos + p.len;
  113. } while (isGlobal);
  114. if (!isGlobal && offset > 0 && offset < s.length)
  115. buf.add(s.substr(offset));
  116. return buf.toString();
  117. }
  118. public static inline function escape( s : String ) : String {
  119. return Regex.Escape(s);
  120. }
  121. }