EReg.hx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /**
  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. <https://haxe.org/manual/std-regex.html>
  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 new haxe.exceptions.NotImplementedException("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, the result is unspecified.
  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.match(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 = -1):Bool {
  105. return false;
  106. }
  107. /**
  108. Returns the total number of groups captures by the last matched substring.
  109. To stay consistent with `this.matched`, the matched substring is also
  110. counted as a group.
  111. Returns `0` if no substring has been matched.
  112. **/
  113. public function matchedNum():Int {
  114. return 0;
  115. }
  116. /**
  117. Splits String `s` at all substrings `this` EReg matches.
  118. If a match is found at the start of `s`, the result contains a leading
  119. empty String "" entry.
  120. If a match is found at the end of `s`, the result contains a trailing
  121. empty String "" entry.
  122. If two matching substrings appear next to each other, the result
  123. contains the empty String `""` between them.
  124. By default, this method splits `s` into two parts at the first matched
  125. substring. If the global g modifier is in place, `s` is split at each
  126. matched substring.
  127. If `s` is null, the result is unspecified.
  128. **/
  129. public function split(s:String):Array<String> {
  130. return null;
  131. }
  132. /**
  133. Replaces the first substring of `s` which `this` EReg matches with `by`.
  134. If `this` EReg does not match any substring, the result is `s`.
  135. By default, this method replaces only the first matched substring. If
  136. the global g modifier is in place, all matched substrings are replaced.
  137. If `by` contains `$1` to `$9`, the digit corresponds to number of a
  138. matched sub-group and its value is used instead. If no such sub-group
  139. exists, the replacement is unspecified. The string `$$` becomes `$`.
  140. If `s` or `by` are null, the result is unspecified.
  141. **/
  142. public function replace(s:String, by:String):String {
  143. return null;
  144. }
  145. /**
  146. Calls the function `f` for the substring of `s` which `this` EReg matches
  147. and replaces that substring with the result of `f` call.
  148. The `f` function takes `this` EReg object as its first argument and should
  149. return a replacement string for the substring matched.
  150. If `this` EReg does not match any substring, the result is `s`.
  151. By default, this method replaces only the first matched substring. If
  152. the global g modifier is in place, all matched substrings are replaced.
  153. If `s` or `f` are null, the result is unspecified.
  154. **/
  155. public function map(s:String, f:EReg->String):String {
  156. return null;
  157. }
  158. /**
  159. Escape the string `s` for use as a part of regular expression.
  160. If `s` is null, the result is unspecified.
  161. **/
  162. public static function escape(s:String):String {
  163. return null;
  164. }
  165. }