NativeStringTools.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. Returns the character code at position `index` of `str`.
  33. **/
  34. public static function charCodeAt(str : String, index : Int): Int;
  35. /**
  36. Looks for the first match of pattern in the string `str`.
  37. If it finds a match, then `find` returns the indices of `str` where this
  38. occurrence starts and ends.
  39. @param target If the target has captures, then in a successful match the
  40. captured values are also returned, after the two indices.
  41. @param start specifies where to start the search; its default value is `1`
  42. and can be negative.
  43. @param plain turns off the pattern matching facilities, so the function does
  44. a plain "find substring" operation, with no characters in pattern
  45. being considered "magic". Note that if plain is given, then `start` must be given as well.
  46. **/
  47. public static function find(str : String, target : String, ?start : Int, ?plain : Bool): StringFind;
  48. /**
  49. Returns the internal numerical codes of the characters `str[index]`.
  50. Note that numerical codes are not necessarily portable across platforms.
  51. **/
  52. public static function byte(str : String, ?index : Int) : Int;
  53. /**
  54. Returns a formatted version of its variable number of arguments following
  55. the description given in its first argument (which must be a string).
  56. The format string follows the same rules as the printf family of standard C
  57. functions. The only differences are that the options/modifiers
  58. `*`, `l`, `L`, `n`, `p`, and `h` are not supported and that there is an
  59. extra option, `q`. The `q` option formats a string in a form suitable to be
  60. safely read back by the Lua interpreter: the string is written between
  61. double quotes, and all double quotes, newlines, embedded zeros,
  62. and backslashes in the string are correctly escaped when written.
  63. For instance, the call
  64. `string.format('%q', 'a string with "quotes" and \n new line')`
  65. will produce the string:
  66. `"a string with \"quotes\" and \
  67. new line"`
  68. The options `c`, `d` `E`, `e`, `f`, `g`, `G`, `i`, `o`, `u, `X-, and `x` all
  69. expect a number as argument, whereas `q` and `s` expect a string.
  70. This function does not accept string values containing embedded zeros,
  71. except as arguments to the `q` option.
  72. **/
  73. public static function format(str : String, ?e1 : Dynamic, ?e2 : Dynamic, ?e3 : Dynamic, ?e4 : Dynamic): String;
  74. /**
  75. **/
  76. @:overload( function (str : String, pattern : String, replace : String->Void, ?n : Int): String {})
  77. @:overload( function (str : String, pattern : String, replace : String->String, ?n : Int): String {})
  78. public static function gsub(str : String, pattern : String, replace : String, ?n : Int): String;
  79. /**
  80. Returns an iterator function that, each time it is called, returns the next
  81. captures from pattern over string `str`. If `pattern` specifies no captures,
  82. then the whole match is produced in each call.
  83. **/
  84. @:overload( function (str : String, pattern : String, match : Void->String, ?n : Int): String->Void {})
  85. public static function gmatch(str : String, pattern : String): Void->String;
  86. /**
  87. Looks for the first match of pattern in the string s. If it finds one,
  88. then match returns the captures from the pattern; otherwise it returns `null`.
  89. If pattern specifies no captures, then the whole match is returned.
  90. The optional argument `n` specifies where to start the search;
  91. its default value is `1` and can be negative.
  92. **/
  93. public static function match(str : String, pattern : String, ?n : Int): String;
  94. /**
  95. Receives a string and returns a copy of this string with all lowercase
  96. letters changed to uppercase. All other characters are left unchanged.
  97. The definition of what a lowercase letter is depends on the current locale.
  98. **/
  99. public static function upper(str:String) : String;
  100. /**
  101. Receives a string and returns a copy of this string with all uppercase
  102. letters changed to lowercase. All other characters are left unchanged.
  103. The definition of what an uppercase letter is depends on the current locale.
  104. **/
  105. public static function lower(str:String) : String;
  106. /**
  107. Returns a string containing a binary representation of the given function,
  108. so that a later loadstring on this string returns a copy of the function.
  109. function must be a Lua function without upvalues.
  110. **/
  111. public static function dump(d:Dynamic) : Dynamic;
  112. }
  113. @:multiReturn extern class StringFind {
  114. var begin : Int;
  115. var end : Int;
  116. }
  117. @:multiReturn extern class StringSub {
  118. var match : String;
  119. var count : Int;
  120. }