String.hx 3.9 KB

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