EReg.hx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /**
  23. The EReg class represents regular expressions.
  24. While basic usage and patterns consistently work across platforms, some more
  25. complex operations may yield different results. This is a necessary trade-
  26. off to retain a certain level of performance.
  27. EReg instances can be created by calling the constructor, or with the
  28. special syntax ~/pattern/modifier
  29. EReg instances maintain an internal state, which is affected by several of
  30. its methods.
  31. A detailed explanation of the supported operations is available at
  32. http://haxe.org/doc/cross/regexp
  33. **/
  34. class EReg {
  35. /**
  36. Creates a new regular expression with pattern [r] and modifiers [opt].
  37. This is equivalent to the shorthand syntax ~/r/opt
  38. If [r] or [opt] are null, the result is unspecified.
  39. **/
  40. public function new( r : String, opt : String ) {
  41. throw "Regular expressions are not implemented for this platform";
  42. }
  43. /**
  44. Tells if [this] regular expression matches String [s].
  45. This method modifies the internal state.
  46. If [s] is null, the result is unspecified.
  47. **/
  48. public function match( s : String ) : Bool {
  49. return false;
  50. }
  51. /**
  52. Returns the matched sub-group [n] of [this] EReg.
  53. This method should only be called after [this].match() or
  54. [this].matchSub(), and then operates on the String of that operation.
  55. The index [n] corresponds to the n-th set of parentheses in the pattern
  56. of [this] EReg. If no such sub-group exists, an exception is thrown.
  57. If [n] equals 0, the whole matched substring is returned.
  58. **/
  59. public function matched( n : Int ) : String {
  60. return null;
  61. }
  62. /**
  63. Returns the part to the left of the last matched substring.
  64. If the most recent call to [this].match() or [this].matchSub() did not
  65. match anything, the result is unspecified.
  66. If the global g modifier was in place for the matching, only the
  67. substring to the left of the leftmost match is returned.
  68. The result does not include the matched part.
  69. **/
  70. public function matchedLeft() : String {
  71. return null;
  72. }
  73. /**
  74. Returns the part to the right of the last matched substring.
  75. If the most recent call to [this].match() or [this].matchSub() did not
  76. match anything, the result is unspecified.
  77. If the global g modifier was in place for the matching, only the
  78. substring to the right of the leftmost match is returned.
  79. The result does not include the matched part.
  80. **/
  81. public function matchedRight() : String {
  82. return null;
  83. }
  84. /**
  85. Returns the position and length of the last matched substring, within
  86. the String which was last used as argument to [this].match() or
  87. [this].matchSub().
  88. If the most recent call to [this].match() or [this].matchSub() did not
  89. match anything, the result is unspecified.
  90. If the global g modifier was in place for the matching, the position and
  91. length of the leftmost substring is returned.
  92. **/
  93. public function matchedPos() : { pos : Int, len : Int } {
  94. return null;
  95. }
  96. /**
  97. Tells if [this] regular expression matches a substring of String [s].
  98. This function expects [pos] and [len] to describe a valid substring of
  99. [s], or else the result is unspecified. To get more robust behavior,
  100. [this].matchSub(s.substr(pos,len)) can be used instead.
  101. This method modifies the internal state.
  102. If [s] is null, the result is unspecified.
  103. **/
  104. public function matchSub( s : String, pos : Int, len : Int = 0):Bool {
  105. return false;
  106. }
  107. /**
  108. Splits String [s] at all substrings [this] EReg matches.
  109. If a match is found at the start of [s], the result contains a leading
  110. empty String "" entry.
  111. If a match is found at the end of [s], the result contains a trailing
  112. empty String "" entry.
  113. If two matching substrings appear next to each other, the result
  114. contains the empty String "" between them.
  115. By default, this method splits [s] into two parts at the first matched
  116. substring. If the global g modifier is in place, [s] is split at each
  117. matched substring.
  118. If [s] is null, the result is unspecified.
  119. **/
  120. public function split( s : String ) : Array<String> {
  121. return null;
  122. }
  123. /**
  124. Replaces the first substring of [s] which [this] EReg matches with [by].
  125. If [this] EReg does not match any substring, the result is [s].
  126. By default, this method replaces only the first matched substring. If
  127. the global g modifier is in place, all matched substrings are replaced.
  128. If [by] contains [$1] to [$9], the digit corresponds to number of a
  129. matched sub-group and its value is used instead. If no such sub-group
  130. exists, the replacement is unspecified. The string [$$] becomes [$].
  131. If [s] or [by] are null, the result is unspecified.
  132. **/
  133. public function replace( s : String, by : String ) : String {
  134. return null;
  135. }
  136. /**
  137. For each occurence of the pattern in the string [s], the function [f] is called and
  138. can return the string that needs to be replaced. All occurences are matched anyway,
  139. and setting the [g] flag might cause some incorrect behavior on some platforms.
  140. **/
  141. public function map( s : String, f : EReg -> String ) : String {
  142. var buf = new StringBuf();
  143. while( true ) {
  144. if( !match(s) )
  145. break;
  146. buf.add(matchedLeft());
  147. buf.add(f(this));
  148. s = matchedRight();
  149. }
  150. buf.add(s);
  151. return buf.toString();
  152. }
  153. }