String.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package;
  2. /*
  3. * Copyright (C)2005-2012 Haxe Foundation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  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. **/
  31. #if !macro
  32. import python.internal.StringImpl;
  33. import python.lib.Builtin;
  34. #end
  35. @:coreApi
  36. @:native("_hx_c_String")
  37. extern class String extends StringImpl {
  38. /**
  39. The number of characters in [this] String.
  40. **/
  41. var length(default,null) : Int;
  42. private inline function get_length ():Int {
  43. return python.lib.Builtin.len(this);
  44. }
  45. /**
  46. Creates a copy from a given String.
  47. **/
  48. function new(string:String) : Void;
  49. /**
  50. Returns a String where all characters of [this] String are upper case.
  51. Affects the characters [a-z]. Other characters remain unchanged.
  52. **/
  53. @:runtime public inline function toUpperCase() : String {
  54. return untyped this.upper();
  55. }
  56. /**
  57. Returns a String where all characters of [this] String are lower case.
  58. Affects the characters [A-Z]. Other characters remain unchanged.
  59. **/
  60. inline function toLowerCase() : String {
  61. return untyped this.lower();
  62. }
  63. /**
  64. Returns the character at position [index] of [this] String.
  65. If [index] is negative or exceeds [this].length, the empty String ""
  66. is returned.
  67. **/
  68. inline public function charAt(index : Int) : String
  69. {
  70. return StringImpl.charAt(this, index);
  71. }
  72. /**
  73. Returns the character code at position [index] of [this] String.
  74. If [index] is negative or exceeds [this].length, null is returned.
  75. To obtain the character code of a single character, "x".code can be used
  76. instead to inline the character code at compile time. Note that this
  77. only works on String literals of length 1.
  78. **/
  79. inline public function charCodeAt( index : Int) : Null<Int>
  80. {
  81. return StringImpl.charCodeAt(this, index);
  82. }
  83. /**
  84. Returns the position of the leftmost occurence of [str] within [this]
  85. String.
  86. If [startIndex] is given, the search is performed within the substring
  87. of [this] String starting from [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. inline function indexOf( str : String, ?startIndex : Int ) : Int {
  93. if (startIndex == null)
  94. return untyped this.find(str)
  95. else
  96. return untyped this.find(str, startIndex);
  97. }
  98. /**
  99. Returns the position of the rightmost occurence of [str] within [this]
  100. String.
  101. If [startIndex] is given, the search is performed within the substring
  102. of [this] String from 0 to [startIndex]. Otherwise the search is
  103. performed within [this] String. In either case, the returned position
  104. is relative to the beginning of [this] String.
  105. If [str] cannot be found, -1 is returned.
  106. **/
  107. inline function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  108. return StringImpl.lastIndexOf(this, str, startIndex);
  109. }
  110. /**
  111. Splits [this] String at each occurence of [delimiter].
  112. If [delimiter] is the empty String "", [this] String is split into an
  113. Array of [this].length elements, where the elements correspond to the
  114. characters of [this] String.
  115. If [delimiter] is not found within [this] String, the result is an Array
  116. with one element, which equals [this] String.
  117. If [delimiter] is null, the result is unspecified.
  118. Otherwise, [this] String is split into parts at each occurence of
  119. [delimiter]. If [this] String starts (or ends) with [delimiter}, the
  120. result Array contains a leading (or trailing) empty String "" element.
  121. Two subsequent delimiters also result in an empty String "" element.
  122. **/
  123. inline function split( delimiter : String ) : Array<String> return StringImpl.split(this, delimiter);
  124. /**
  125. Returns [len] characters of [this] String, starting at position [pos].
  126. If [len] is omitted, all characters from position [pos] to the end of
  127. [this] String are included.
  128. If [pos] is negative, its value is calculated from the end of [this]
  129. String by [this].length + [pos]. If this yields a negative value, 0 is
  130. used instead.
  131. If the calculated position + [len] exceeds [this].length, the characters
  132. from that position to the end of [this] String are returned.
  133. If [len] is negative, the result is unspecified.
  134. **/
  135. inline public function substr( pos : Int, ?len : Int ) : String
  136. {
  137. return python.Tools.substr(this, pos, len);
  138. }
  139. /**
  140. Returns the part of [this] String from [startIndex] to [endIndex].
  141. If [startIndex] or [endIndex] are negative, 0 is used instead.
  142. If [startIndex] exceeds [endIndex], they are swapped.
  143. If the (possibly swapped) [endIndex] is omitted or exceeds
  144. [this].length, [this].length is used instead.
  145. If the (possibly swapped) [startIndex] exceeds [this].length, the empty
  146. String "" is returned.
  147. **/
  148. inline function substring( startIndex : Int, ?endIndex : Int ) : String {
  149. return python.Tools.substring(this, startIndex, endIndex);
  150. }
  151. /**
  152. Returns the String itself.
  153. **/
  154. inline function toString() : String return this;
  155. /**
  156. Returns the String corresponding to the character code [code].
  157. If [code] is negative or has another invalid value, the result is
  158. unspecified.
  159. **/
  160. public static function fromCharCode( code : Int ) : String {
  161. return StringImpl.fromCharCode(code);
  162. }
  163. static function __init__ ():Void {
  164. python.Macros.importFromAs("builtins", "str", "String");
  165. //untyped __python__("String = __builtin__.str");
  166. }
  167. }