NativeStringTools.hx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. package lua;
  23. /**
  24. These are all externs for the base Lua "string" class, which functions
  25. as an additional set of string tools.
  26. Note that all relevant indexes are "1" based.
  27. **/
  28. @:native("_G.string")
  29. extern class NativeStringTools {
  30. /**
  31. Receives a string and returns its length. The empty string `""` has
  32. length `0`. Embedded zeros are counted, so `"a\000bc\000"` has length `5`.
  33. **/
  34. static function len(str:String):Int;
  35. /**
  36. Receives zero or more integers. Returns a string with length equal to the
  37. number of arguments, in which each character has the internal numerical
  38. code equal to its corresponding argument.
  39. Note that numerical codes are not necessarily portable across platforms.
  40. **/
  41. static function char(codes:haxe.extern.Rest<Int>):String;
  42. // TODO: make a note about handling matched groups with multireturn
  43. /**
  44. Returns the substring of `str` that starts at `start` and continues until `end`;
  45. `start` and `end` can be negative. If `end` is absent, then it is assumed to be
  46. equal to `-1` (which is the same as the string length).
  47. In particular, the call `sub(str,1,end)` returns a prefix of `str`
  48. with length `end`, and `sub(str, -end)` returns a suffix of `str` with
  49. length `start`.
  50. **/
  51. static function sub(str:String, start:Int, ?end:Int):StringSub;
  52. /**
  53. Looks for the first match of pattern in the string `str`.
  54. If it finds a match, then `find` returns the indices of `str` where this
  55. occurrence starts and ends.
  56. @param target If the target has captures, then in a successful match the
  57. captured values are also returned, after the two indices.
  58. @param start specifies where to start the search; its default value is `1`
  59. and can be negative.
  60. @param plain turns off the pattern matching facilities, so the function does
  61. a plain "find substring" operation, with no characters in pattern
  62. being considered "magic". Note that if plain is given, then `start` must be given as well.
  63. **/
  64. static function find(str:String, target:String, ?start:Int, ?plain:Bool):StringFind;
  65. /**
  66. Returns the internal numerical codes of the characters `str[index]`.
  67. Note that numerical codes are not necessarily portable across platforms.
  68. **/
  69. static function byte(str:String, ?index:Int):Int;
  70. /**
  71. Returns a formatted version of its variable number of arguments following
  72. the description given in its first argument (which must be a string).
  73. The format string follows the same rules as the printf family of standard C
  74. functions. The only differences are that the options/modifiers
  75. `*`, `l`, `L`, `n`, `p`, and `h` are not supported and that there is an
  76. extra option, `q`. The `q` option formats a string in a form suitable to be
  77. safely read back by the Lua interpreter: the string is written between
  78. double quotes, and all double quotes, newlines, embedded zeros,
  79. and backslashes in the string are correctly escaped when written.
  80. For instance, the call
  81. `string.format('%q', 'a string with "quotes" and \n new line')`
  82. will produce the string:
  83. `"a string with \"quotes\" and \
  84. new line"`
  85. The options `c`, `d` `E`, `e`, `f`, `g`, `G`, `i`, `o`, `u, `X-, and `x` all
  86. expect a number as argument, whereas `q` and `s` expect a string.
  87. This function does not accept string values containing embedded zeros,
  88. except as arguments to the `q` option.
  89. **/
  90. static function format(str:String, ?e1:Dynamic, ?e2:Dynamic, ?e3:Dynamic, ?e4:Dynamic):String;
  91. /**
  92. **/
  93. @:overload(function(str:String, pattern:String, replace:String->Void, ?n:Int):String {})
  94. @:overload(function(str:String, pattern:String, replace:String->String, ?n:Int):String {})
  95. static function gsub(str:String, pattern:String, replace:String, ?n:Int):String;
  96. /**
  97. Returns an iterator function that, each time it is called, returns the next
  98. captures from pattern over string `str`. If `pattern` specifies no captures,
  99. then the whole match is produced in each call.
  100. **/
  101. @:overload(function(str:String, pattern:String, match:Void->String, ?n:Int):String->Void {})
  102. static function gmatch(str:String, pattern:String):Void->String;
  103. /**
  104. Looks for the first match of pattern in the string s. If it finds one,
  105. then match returns the captures from the pattern; otherwise it returns `null`.
  106. If pattern specifies no captures, then the whole match is returned.
  107. The optional argument `n` specifies where to start the search;
  108. its default value is `1` and can be negative.
  109. **/
  110. static function match(str:String, pattern:String, ?n:Int):String;
  111. /**
  112. Receives a string and returns a copy of this string with all lowercase
  113. letters changed to uppercase. All other characters are left unchanged.
  114. The definition of what a lowercase letter is depends on the current locale.
  115. **/
  116. static function upper(str:String):String;
  117. /**
  118. Receives a string and returns a copy of this string with all uppercase
  119. letters changed to lowercase. All other characters are left unchanged.
  120. The definition of what an uppercase letter is depends on the current locale.
  121. **/
  122. static function lower(str:String):String;
  123. /**
  124. Returns a string containing a binary representation of the given function,
  125. so that a later loadstring on this string returns a copy of the function.
  126. function must be a Lua function without upvalues.
  127. **/
  128. static function dump(d:Dynamic):Dynamic;
  129. /**
  130. Returns a string that is the concatenation of n copies of
  131. the string s separated by the string sep. The default value
  132. for sep is the empty string (that is, no separator).
  133. Returns the empty string if n is not positive. (Note that
  134. it is very easy to exhaust the memory of your machine with
  135. a single call to this function.)
  136. **/
  137. static function rep(s:String, n : Int, ?sep : String) : String;
  138. }
  139. @:multiReturn extern class StringFind {
  140. var begin:Int;
  141. var end:Int;
  142. }
  143. @:multiReturn extern class StringSub {
  144. var match:String;
  145. var count:Int;
  146. }