NativeStringTools.hx 5.6 KB

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