2
0

String.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. import lua.Lua;
  23. import lua.Table;
  24. import lua.Boot;
  25. #if lua_vanilla
  26. private typedef BaseString = lua.NativeStringTools;
  27. #else
  28. private typedef BaseString = lua.lib.luautf8.Utf8;
  29. #end
  30. @:coreApi
  31. class String {
  32. static var __oldindex:String->String->Dynamic;
  33. public var length(default, null):Int;
  34. public inline function new(string:String) untyped {}
  35. @:keep
  36. static function __index(s:Dynamic, k:Dynamic):Dynamic {
  37. if (k == "length")
  38. return BaseString.len(s);
  39. else if (Reflect.hasField(untyped String.prototype, k))
  40. return untyped String.prototype[k];
  41. else if (__oldindex != null) {
  42. if (Lua.type(__oldindex) == "function") {
  43. return __oldindex(s, k);
  44. } else if (Lua.type(__oldindex) == "table") {
  45. return untyped __oldindex[k];
  46. }
  47. return null;
  48. } else
  49. return null;
  50. }
  51. public inline function toUpperCase():String
  52. return BaseString.upper(this);
  53. public inline function toLowerCase():String
  54. return BaseString.lower(this);
  55. public function indexOf(str:String, ?startIndex:Int):Int {
  56. if (startIndex == null || startIndex < 0)
  57. startIndex = 1;
  58. else
  59. startIndex += 1;
  60. if (str == "") {
  61. return indexOfEmpty(this, startIndex - 1);
  62. }
  63. var r = BaseString.find(this, str, startIndex, true).begin;
  64. if (r != null && r > 0)
  65. return r - 1;
  66. else
  67. return -1;
  68. }
  69. static function indexOfEmpty(s:String, startIndex:Int):Int {
  70. var length = BaseString.len(s);
  71. if (startIndex < 0) {
  72. startIndex = 0;
  73. }
  74. return startIndex > length ? length : startIndex;
  75. }
  76. public function lastIndexOf(str:String, ?startIndex:Int):Int {
  77. var ret = -1;
  78. if (startIndex == null)
  79. startIndex = length;
  80. if (str == "") {
  81. if (this == "") {
  82. return 0;
  83. } else {
  84. var max = cast Math.max(startIndex, 0);
  85. return cast Math.min(length, max);
  86. }
  87. } else {
  88. while (true) {
  89. var p = indexOf(str, ret + 1);
  90. if (p == -1 || p > startIndex || p == ret)
  91. break;
  92. ret = p;
  93. }
  94. return ret;
  95. }
  96. }
  97. public function split(delimiter:String):Array<String> {
  98. var idx:Null<Int> = 1;
  99. var ret = [];
  100. while (idx != null) {
  101. var newidx:Null<Int> = 0;
  102. if (delimiter.length > 0) {
  103. newidx = BaseString.find(this, delimiter, idx, true).begin;
  104. } else if (idx >= this.length) {
  105. newidx = null;
  106. } else {
  107. newidx = idx + 1;
  108. }
  109. if (newidx != null) {
  110. var match = BaseString.sub(this, idx, newidx - 1).match;
  111. ret.push(match);
  112. idx = newidx + delimiter.length;
  113. } else {
  114. ret.push(BaseString.sub(this, idx, this.length).match);
  115. idx = null;
  116. }
  117. }
  118. return ret;
  119. }
  120. public inline function toString():String {
  121. return this;
  122. }
  123. public function substring(startIndex:Int, ?endIndex:Int):String {
  124. if (endIndex == null)
  125. endIndex = this.length;
  126. if (endIndex < 0)
  127. endIndex = 0;
  128. if (startIndex < 0)
  129. startIndex = 0;
  130. if (endIndex < startIndex) {
  131. // swap the index positions
  132. return BaseString.sub(this, endIndex + 1, startIndex).match;
  133. } else {
  134. return BaseString.sub(this, startIndex + 1, endIndex).match;
  135. }
  136. }
  137. public inline function charAt(index:Int):String {
  138. return BaseString.sub(this, index + 1, index + 1).match;
  139. }
  140. public inline function charCodeAt(index:Int):Null<Int> {
  141. return BaseString.byte(this, index + 1);
  142. }
  143. public function substr(pos:Int, ?len:Int):String {
  144. if (len == null || len > pos + this.length)
  145. len = this.length;
  146. else if (len < 0)
  147. len = length + len;
  148. if (pos < 0)
  149. pos = length + pos;
  150. if (pos < 0)
  151. pos = 0;
  152. return BaseString.sub(this, pos + 1, pos + len).match;
  153. }
  154. public inline static function fromCharCode(code:Int):String {
  155. return BaseString.char(code);
  156. }
  157. }