String.hx 4.1 KB

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