String.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C)2005-2019 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. @:coreApi final class String {
  23. static var __is_String = true;
  24. private static var __split:Dynamic = neko.Lib.load("std", "string_split", 2);
  25. static function __init__():Void {
  26. __is_String = true;
  27. }
  28. public var length(default, null):Int;
  29. public function new(string:String):Void {
  30. untyped {
  31. if (__dollar__typeof(string) != __dollar__tstring)
  32. string = __dollar__string(string);
  33. this.__s = string;
  34. this.length = __dollar__ssize(string);
  35. }
  36. }
  37. public function charAt(index:Int):String {
  38. untyped {
  39. try {
  40. var s = __dollar__smake(1);
  41. __dollar__sset(s, 0, __dollar__sget(this.__s, index));
  42. return new String(s);
  43. } catch (e:Dynamic) {
  44. return "";
  45. }
  46. }
  47. }
  48. public function charCodeAt(index:Int):Null<Int> {
  49. untyped {
  50. return __dollar__sget(this.__s, index);
  51. }
  52. }
  53. public function indexOf(str:String, ?startIndex:Int):Int {
  54. var startIndex = startIndex == null || startIndex < 0 ? 0 : startIndex;
  55. if (str.length == 0) {
  56. var min = startIndex > length ? length : startIndex;
  57. return min < 0 ? 0 : min;
  58. }
  59. untyped {
  60. var l = __dollar__ssize(this.__s);
  61. if (startIndex == null || startIndex < -l)
  62. startIndex = 0;
  63. if (str == '' && startIndex >= l) {
  64. return l;
  65. }
  66. if (startIndex > l)
  67. return -1;
  68. if (__dollar__ssize(str.__s) == 0)
  69. return startIndex < 0 ? l + startIndex : startIndex;
  70. var p = try __dollar__sfind(this.__s, startIndex, str.__s) catch (e:Dynamic) null;
  71. if (p == null)
  72. return -1;
  73. return p;
  74. }
  75. }
  76. public function lastIndexOf(str:String, ?startIndex:Int):Int {
  77. if (str.length == 0) {
  78. var startIndex = startIndex == null ? length : startIndex;
  79. var min = startIndex > length ? length : startIndex;
  80. return min < 0 ? 0 : min;
  81. }
  82. untyped {
  83. var last = -1;
  84. var l = __dollar__ssize(this.__s);
  85. if (startIndex == null)
  86. startIndex = l;
  87. if (__dollar__ssize(str.__s) == 0)
  88. return startIndex > l ? l : startIndex;
  89. while (true) {
  90. var p = try __dollar__sfind(this.__s, last + 1, str.__s) catch (e:Dynamic) null;
  91. if (p == null || p > startIndex)
  92. return last;
  93. last = p;
  94. }
  95. }
  96. }
  97. public function split(delimiter:String):Array<String> {
  98. untyped {
  99. var l = __split(this.__s, delimiter.__s);
  100. var a = new Array<String>();
  101. if (l == null) {
  102. a.push("");
  103. return a;
  104. }
  105. do {
  106. a.push(new String(l[0]));
  107. l = l[1];
  108. } while (l != null);
  109. return a;
  110. }
  111. }
  112. public function substr(pos:Int, ?len:Int):String {
  113. if (len == 0)
  114. return "";
  115. var sl = length;
  116. if (len == null)
  117. len = sl;
  118. if (pos == null)
  119. pos = 0;
  120. if (pos != 0 && len < 0) {
  121. return "";
  122. }
  123. if (pos < 0) {
  124. pos = sl + pos;
  125. if (pos < 0)
  126. pos = 0;
  127. } else if (len < 0) {
  128. len = sl + len - pos;
  129. }
  130. if (pos + len > sl) {
  131. len = sl - pos;
  132. }
  133. if (pos < 0 || len <= 0)
  134. return "";
  135. return new String(untyped __dollar__ssub(this.__s, pos, len));
  136. }
  137. public function substring(startIndex:Int, ?endIndex:Int):String {
  138. if (endIndex == null) {
  139. endIndex = length;
  140. } else if (endIndex < 0) {
  141. endIndex = 0;
  142. } else if (endIndex > length) {
  143. endIndex = length;
  144. }
  145. if (startIndex < 0) {
  146. startIndex = 0;
  147. } else if (startIndex > length) {
  148. startIndex = length;
  149. }
  150. if (startIndex > endIndex) {
  151. var tmp = startIndex;
  152. startIndex = endIndex;
  153. endIndex = tmp;
  154. }
  155. return substr(startIndex, endIndex - startIndex);
  156. }
  157. public function toLowerCase():String {
  158. untyped {
  159. var s = this.__s;
  160. var l = this.length;
  161. var s2 = __dollar__scopy(s);
  162. var i = 0;
  163. while (i < l) {
  164. var c = __dollar__sget(s, i);
  165. if (c >= 65 && c <= 90)
  166. __dollar__sset(s2, i, c - 65 + 97);
  167. i++;
  168. }
  169. return new String(s2);
  170. }
  171. }
  172. public function toUpperCase():String {
  173. untyped {
  174. var s = this.__s;
  175. var l = this.length;
  176. var s2 = __dollar__scopy(s);
  177. var i = 0;
  178. while (i < l) {
  179. var c = __dollar__sget(s, i);
  180. if (c >= 97 && c <= 122)
  181. __dollar__sset(s2, i, c - 97 + 65);
  182. i++;
  183. }
  184. return new String(s2);
  185. }
  186. }
  187. public function toString():String {
  188. return this;
  189. }
  190. /* NEKO INTERNALS */
  191. private function __compare(o:String):Int {
  192. return untyped __dollar__compare(this.__s, o.__s);
  193. }
  194. private function __add(s:Dynamic):String {
  195. return new String(untyped this.__s + __dollar__string(s));
  196. }
  197. private function __radd(s:Dynamic):String {
  198. return new String(untyped __dollar__string(s) + this.__s);
  199. }
  200. public static function fromCharCode(code:Int):String untyped {
  201. var s = __dollar__smake(1);
  202. __dollar__sset(s, 0, code);
  203. return new String(s);
  204. }
  205. }