NativeStringTools.hx 5.5 KB

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