String.hx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C)2005-2017 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. import php.*;
  23. /**
  24. The basic String class.
  25. A Haxe String is immutable, it is not possible to modify individual
  26. characters. No method of this class changes the state of `this` String.
  27. Strings can be constructed using the String literal syntax `"string value"`.
  28. String can be concatenated by using the `+` operator. If an operand is not a
  29. String, it is passed through `Std.string()` first.
  30. @see https://haxe.org/manual/std-String.html
  31. **/
  32. @:coreApi extern class String {
  33. /**
  34. The number of characters in `this` String.
  35. **/
  36. var length(default,null) : Int;
  37. /**
  38. Creates a copy from a given String.
  39. **/
  40. @:pure function new(string:String) : Void;
  41. /**
  42. Returns a String where all characters of `this` String are upper case.
  43. Affects the characters `a-z`. Other characters remain unchanged.
  44. **/
  45. @:pure @:runtime inline function toUpperCase() : String {
  46. return Global.strtoupper(this);
  47. }
  48. /**
  49. Returns a String where all characters of `this` String are lower case.
  50. Affects the characters `A-Z`. Other characters remain unchanged.
  51. **/
  52. @:pure @:runtime inline function toLowerCase() : String {
  53. return Global.strtolower(this);
  54. }
  55. /**
  56. Returns the character at position `index` of `this` String.
  57. If `index` is negative or exceeds `this.length`, the empty String `""`
  58. is returned.
  59. **/
  60. @:pure @:runtime inline function charAt(index : Int) : String {
  61. return (index < 0 || index >= this.length ? '' : (this:NativeString)[index]);
  62. }
  63. /**
  64. Returns the character code at position `index` of `this` String.
  65. If `index` is negative or exceeds `this.length`, `null` is returned.
  66. To obtain the character code of a single character, `"x".code` can be
  67. used instead to @:runtime inline the character code at compile time. Note that
  68. this only works on String literals of length 1.
  69. **/
  70. @:pure @:runtime inline function charCodeAt( index : Int) : Null<Int> {
  71. return (index < 0 || index >= this.length ? null : Global.ord((this:NativeString)[index]));
  72. }
  73. /**
  74. Returns the position of the leftmost occurrence of `str` within `this`
  75. String.
  76. If `startIndex` is given, the search is performed within the substring
  77. of `this` String starting from `startIndex`. Otherwise the search is
  78. performed within `this` String. In either case, the returned position
  79. is relative to the beginning of `this` String.
  80. If `str` cannot be found, -1 is returned.
  81. **/
  82. @:pure function indexOf( str : String, ?startIndex : Int ) : Int;
  83. /**
  84. Returns the position of the rightmost occurrence of `str` within `this`
  85. String.
  86. If `startIndex` is given, the search is performed within the substring
  87. of `this` String from 0 to `startIndex`. Otherwise the search is
  88. performed within `this` String. In either case, the returned position
  89. is relative to the beginning of `this` String.
  90. If `str` cannot be found, -1 is returned.
  91. **/
  92. @:pure function lastIndexOf( str : String, ?startIndex : Int ) : Int;
  93. /**
  94. Splits `this` String at each occurrence of `delimiter`.
  95. If `this` String is the empty String `""`, the result is not consistent
  96. across targets and may either be `[]` (on Js, Cpp) or `[""]`.
  97. If `delimiter` is the empty String `""`, `this` String is split into an
  98. Array of `this.length` elements, where the elements correspond to the
  99. characters of `this` String.
  100. If `delimiter` is not found within `this` String, the result is an Array
  101. with one element, which equals `this` String.
  102. If `delimiter` is null, the result is unspecified.
  103. Otherwise, `this` String is split into parts at each occurrence of
  104. `delimiter`. If `this` String starts (or ends) with `delimiter`, the
  105. result `Array` contains a leading (or trailing) empty String `""` element.
  106. Two subsequent delimiters also result in an empty String `""` element.
  107. **/
  108. @:pure @:runtime inline function split( delimiter : String ) : Array<String> {
  109. return @:privateAccess Array.wrap(delimiter == '' ? Global.str_split(this) : Global.explode(delimiter, this));
  110. }
  111. /**
  112. Returns `len` characters of `this` String, starting at position `pos`.
  113. If `len` is omitted, all characters from position `pos` to the end of
  114. `this` String are included.
  115. If `pos` is negative, its value is calculated from the end of `this`
  116. String by `this.length + pos`. If this yields a negative value, 0 is
  117. used instead.
  118. If the calculated position + `len` exceeds `this.length`, the characters
  119. from that position to the end of `this` String are returned.
  120. If `len` is negative, the result is unspecified.
  121. **/
  122. @:pure function substr( pos : Int, ?len : Int ) : String;
  123. /**
  124. Returns the part of `this` String from `startIndex` to but not including `endIndex`.
  125. If `startIndex` or `endIndex` are negative, 0 is used instead.
  126. If `startIndex` exceeds `endIndex`, they are swapped.
  127. If the (possibly swapped) `endIndex` is omitted or exceeds
  128. `this.length`, `this.length` is used instead.
  129. If the (possibly swapped) `startIndex` exceeds `this.length`, the empty
  130. String `""` is returned.
  131. **/
  132. @:pure function substring( startIndex : Int, ?endIndex : Int ) : String;
  133. /**
  134. Returns the String itself.
  135. **/
  136. @:pure @:runtime inline function toString() : String {
  137. return this;
  138. }
  139. /**
  140. Returns the String corresponding to the character code `code`.
  141. If `code` is negative or has another invalid value, the result is
  142. unspecified.
  143. **/
  144. @:pure @:runtime static inline function fromCharCode( code : Int ) : String {
  145. return Global.chr(code);
  146. }
  147. }