String.hx 4.6 KB

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