String.hx 4.3 KB

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