EReg.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C)2005-2019 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)
  34. untyped {
  35. switch (cast(opt[i], Int)) {
  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)
  59. return null;
  60. return m.Groups[n].Value;
  61. }
  62. public function matchedLeft():String {
  63. return untyped cur.Substring(0, m.Index);
  64. }
  65. public function matchedRight():String {
  66. return untyped cur.Substring(m.Index + m.Length);
  67. }
  68. public function matchedPos():{pos:Int, len:Int} {
  69. return {pos: m.Index, len: m.Length};
  70. }
  71. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  72. m = if (len < 0) regex.Match(s, pos) else regex.Match(s, pos, len);
  73. cur = s;
  74. return m.Success;
  75. }
  76. public function split(s:String):Array<String> {
  77. if (isGlobal)
  78. return cs.Lib.array(regex.Split(s));
  79. var m = regex.Match(s);
  80. if (!m.Success)
  81. return [s];
  82. return untyped [s.Substring(0, m.Index), s.Substring(m.Index + m.Length)];
  83. }
  84. inline function start(group:Int):Int {
  85. return m.Groups[group].Index;
  86. }
  87. inline function len(group:Int):Int {
  88. return m.Groups[group].Length;
  89. }
  90. public function replace(s:String, by:String):String {
  91. return (isGlobal) ? regex.Replace(s, by) : regex.Replace(s, by, 1);
  92. }
  93. public function map(s:String, f:EReg->String):String {
  94. var offset = 0;
  95. var buf = new StringBuf();
  96. do {
  97. if (offset >= s.length)
  98. break;
  99. else if (!matchSub(s, offset)) {
  100. buf.add(s.substr(offset));
  101. break;
  102. }
  103. var p = matchedPos();
  104. buf.add(s.substr(offset, p.pos - offset));
  105. buf.add(f(this));
  106. if (p.len == 0) {
  107. buf.add(s.substr(p.pos, 1));
  108. offset = p.pos + 1;
  109. } else
  110. offset = p.pos + p.len;
  111. } while (isGlobal);
  112. if (!isGlobal && offset > 0 && offset < s.length)
  113. buf.add(s.substr(offset));
  114. return buf.toString();
  115. }
  116. public static inline function escape(s:String):String {
  117. return Regex.Escape(s);
  118. }
  119. }