String.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. @:coreApi
  23. class String {
  24. public var length(default,null) : Int;
  25. public function new(string:String) untyped {
  26. if (string != null) __lua__("self = string");
  27. else __lua__("self = ''");
  28. }
  29. static function __init__() : Void untyped{
  30. __lua__("getmetatable('').__index = String.__index");
  31. }
  32. @:keep
  33. static function __index(s:Dynamic, k:Dynamic) : Dynamic {
  34. if (k == "length") return untyped __lua__("#s");
  35. else return untyped String.prototype[k];
  36. }
  37. public function toUpperCase() : String return untyped this.upper();
  38. public function toLowerCase() : String return untyped this.lower();
  39. public function indexOf( str : String, ?startIndex : Int ) : Int {
  40. if (startIndex == null) startIndex = 1;
  41. else startIndex += 1;
  42. return lua.StringTools.find(this, str, startIndex, str.length, true);
  43. }
  44. public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  45. var i = 0;
  46. var ret = 0;
  47. while(i != null){
  48. i = this.indexOf(str, i);
  49. if (i != null) ret = i;
  50. }
  51. return ret-1;
  52. }
  53. public function split( delimiter : String ) : Array<String> {
  54. var ret : Array<String> = [];
  55. var qd = lua.Boot.patternQuote(delimiter);
  56. lua.StringTools.gsub(this, qd, function(c) ret.push(c));
  57. return ret;
  58. }
  59. public function toString() : String {
  60. return this;
  61. }
  62. public function substring( startIndex : Int, ?endIndex : Int ) : String {
  63. if (endIndex == null) endIndex = this.length;
  64. return untyped lua.StringTools.sub(this, startIndex + 1,endIndex + 1);
  65. }
  66. function get_length() : Int {
  67. return lua.StringTools.len(this);
  68. }
  69. public function charAt( index : Int) : String {
  70. return lua.StringTools.sub(this,index+1, index+1);
  71. }
  72. public function charCodeAt( index : Int) : Null<Int> {
  73. return lua.StringTools.byte(this,index+1);
  74. }
  75. public function substr( pos : Int, ?len : Int ) : String {
  76. if (len == null || len > pos + this.length) len = this.length;
  77. return lua.StringTools.sub(this, pos + 1, pos+len);
  78. }
  79. public static function fromCharCode( code : Int ) : String {
  80. return untyped String.char(code);
  81. }
  82. }