String.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import haxe.iterators.StringIterator;
  26. import haxe.iterators.StringKeyValueIterator;
  27. #if lua_vanilla
  28. typedef BaseString = lua.NativeStringTools;
  29. #else
  30. typedef BaseString = lua.lib.luautf8.Utf8;
  31. #end
  32. @:coreApi
  33. class String {
  34. static var __oldindex : String->String->Dynamic;
  35. public var length(default,null) : Int;
  36. public inline function new(string:String) untyped {}
  37. @:keep
  38. static function __index(s:Dynamic, k:Dynamic) : Dynamic {
  39. if (k == "length") return BaseString.len(s);
  40. else if (Reflect.hasField(untyped String.prototype, k)) 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. }
  49. else return null;
  50. }
  51. public inline function toUpperCase() : String return BaseString.upper(this);
  52. public inline function toLowerCase() : String return BaseString.lower(this);
  53. public inline function indexOf( str : String, ?startIndex : Int ) : Int {
  54. if (startIndex == null) startIndex = 1;
  55. else startIndex += 1;
  56. var r = BaseString.find(this, str, startIndex, true).begin;
  57. if (r != null && r > 0) return r-1;
  58. else return -1;
  59. }
  60. public inline function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  61. var i = 0;
  62. var ret = -1;
  63. if( startIndex == null ) startIndex = length;
  64. while( true ) {
  65. var p = indexOf(str, ret+1);
  66. if( p == -1 || p > startIndex ) break;
  67. ret = p;
  68. }
  69. return ret;
  70. }
  71. public inline function split( delimiter : String ) : Array<String> {
  72. var idx = 1;
  73. var ret = [];
  74. var delim_offset = delimiter.length > 0 ? delimiter.length : 1;
  75. while (idx != null){
  76. var newidx = 0;
  77. if (delimiter.length > 0){
  78. newidx = BaseString.find(this, delimiter, idx, true).begin;
  79. } else if (idx >= this.length){
  80. newidx = null;
  81. } else {
  82. newidx = idx + 1;
  83. }
  84. if (newidx != null){
  85. var match = BaseString.sub(this, idx, newidx-1).match;
  86. ret.push(match);
  87. idx = newidx + delimiter.length;
  88. } else {
  89. ret.push(BaseString.sub(this,idx,this.length).match);
  90. idx = null;
  91. }
  92. }
  93. return ret;
  94. }
  95. public inline function toString() : String {
  96. return this;
  97. }
  98. public inline function substring( startIndex : Int, ?endIndex : Int ) : String {
  99. if (endIndex == null) endIndex = this.length;
  100. if (endIndex < 0) endIndex = 0;
  101. if (startIndex < 0) startIndex = 0;
  102. if (endIndex < startIndex) {
  103. // swap the index positions
  104. return BaseString.sub(this, endIndex+1, startIndex).match;
  105. } else {
  106. return BaseString.sub(this, startIndex+1, endIndex).match;
  107. }
  108. }
  109. public inline function charAt( index : Int) : String {
  110. return BaseString.sub(this,index+1, index+1).match;
  111. }
  112. public inline function charCodeAt( index : Int) : Null<Int> {
  113. return BaseString.byte(this,index+1);
  114. }
  115. @:runtime public inline function iterator() : StringIterator {
  116. return new StringIterator(this);
  117. }
  118. @:runtime public inline function keyValueIterator() : StringKeyValueIterator {
  119. return new StringKeyValueIterator(this);
  120. }
  121. public inline function substr( pos : Int, ?len : Int ) : String {
  122. if (len == null || len > pos + this.length) len = this.length;
  123. else if (len < 0) len = length + len;
  124. if (pos < 0) pos = length + pos;
  125. if (pos < 0) pos = 0;
  126. return BaseString.sub(this, pos + 1, pos+len).match;
  127. }
  128. public inline static function fromCharCode( code : Int ) : String {
  129. return BaseString.char(code);
  130. }
  131. }