1 31 package org.jruby.runtime.builtin.meta; 32 33 34 import org.jruby.Ruby; 35 import org.jruby.RubyClass; 36 import org.jruby.RubyString; 37 import org.jruby.runtime.Arity; 38 import org.jruby.runtime.ClassIndex; 39 import org.jruby.runtime.ObjectAllocator; 40 import org.jruby.runtime.builtin.IRubyObject; 41 import org.jruby.util.collections.SinglyLinkedList; 42 43 public class StringMetaClass extends ObjectMetaClass { 44 public StringMetaClass(Ruby runtime) { 45 super("String", RubyString.class, runtime.getObject(), STRING_ALLOCATOR); 46 this.index = ClassIndex.STRING; 47 } 48 49 private StringMetaClass(String name, RubyClass superClass, ObjectAllocator allocator, SinglyLinkedList parentCRef) { 50 super(name, RubyString.class, superClass, allocator, parentCRef); 51 this.index = ClassIndex.STRING; 52 } 53 54 protected class StringMeta extends Meta { 55 protected void initializeClass() { 56 includeModule(getRuntime().getModule("Comparable")); 57 includeModule(getRuntime().getModule("Enumerable")); 58 59 defineFastMethod("<=>", Arity.singleArgument(), "op_cmp"); 60 defineFastMethod("==", Arity.singleArgument(), "eql"); 61 defineFastMethod("+", Arity.singleArgument(), "op_plus"); 62 defineFastMethod("*", Arity.singleArgument(), "op_mul"); 63 defineFastMethod("%", Arity.singleArgument(), "format"); 64 defineFastMethod("hash", Arity.noArguments(), "hash"); 65 defineFastMethod("to_s", Arity.noArguments(), "to_s"); 66 67 defineFastMethod(">=", Arity.singleArgument(), "op_ge"); 69 defineFastMethod(">", Arity.singleArgument(), "op_gt"); 70 defineFastMethod("<=", Arity.singleArgument(), "op_le"); 71 defineFastMethod("<", Arity.singleArgument(), "op_lt"); 72 73 defineFastMethod("eql?", Arity.singleArgument(), "op_eql"); 74 75 defineFastMethod("[]", Arity.optional(), "aref"); 76 defineFastMethod("[]=", Arity.optional(), "aset"); 77 defineFastMethod("=~", Arity.singleArgument(), "match"); 78 defineFastMethod("~", Arity.noArguments(), "match2"); 79 defineFastMethod("capitalize", Arity.noArguments()); 80 defineFastMethod("capitalize!", Arity.noArguments(), "capitalize_bang"); 81 defineFastMethod("casecmp", Arity.singleArgument()); 82 defineFastMethod("center", Arity.optional()); 83 defineFastMethod("chop", Arity.noArguments()); 84 defineFastMethod("chop!", Arity.noArguments(), "chop_bang"); 85 defineFastMethod("chomp", Arity.optional()); 86 defineFastMethod("chomp!", Arity.optional(), "chomp_bang"); 87 defineFastMethod("concat", Arity.singleArgument()); 88 defineFastMethod("count", Arity.optional()); 89 defineFastMethod("crypt", Arity.singleArgument()); 90 defineFastMethod("delete", Arity.optional()); 91 defineFastMethod("delete!", Arity.optional(), "delete_bang"); 92 defineFastMethod("downcase", Arity.noArguments()); 93 defineFastMethod("downcase!", Arity.noArguments(), "downcase_bang"); 94 defineFastMethod("dump", Arity.noArguments()); 95 defineMethod("each_line", Arity.optional()); 96 defineMethod("each_byte", Arity.noArguments()); 97 defineFastMethod("empty?", Arity.noArguments(), "empty"); 98 defineMethod("gsub", Arity.optional()); 99 defineMethod("gsub!", Arity.optional(), "gsub_bang"); 100 defineFastMethod("hex", Arity.noArguments()); 101 defineFastMethod("include?", Arity.singleArgument(), "include"); 102 defineFastMethod("index", Arity.optional()); 103 defineMethod("initialize", Arity.optional(), "initialize"); 104 defineFastMethod("initialize_copy", Arity.singleArgument(), "replace"); 105 defineFastMethod("insert", Arity.twoArguments()); 106 defineFastMethod("inspect", Arity.noArguments()); 107 defineFastMethod("length", Arity.noArguments()); 108 defineFastMethod("ljust", Arity.optional()); 109 defineFastMethod("lstrip", Arity.noArguments()); 110 defineFastMethod("lstrip!", Arity.noArguments(), "lstrip_bang"); 111 defineFastMethod("match", Arity.singleArgument(), "match3"); 112 defineFastMethod("oct", Arity.noArguments()); 113 defineFastMethod("replace", Arity.singleArgument()); 114 defineFastMethod("reverse", Arity.noArguments()); 115 defineFastMethod("reverse!", Arity.noArguments(), "reverse_bang"); 116 defineFastMethod("rindex", Arity.optional()); 117 defineFastMethod("rjust", Arity.optional()); 118 defineFastMethod("rstrip", Arity.noArguments()); 119 defineFastMethod("rstrip!", Arity.noArguments(), "rstrip_bang"); 120 defineMethod("scan", Arity.singleArgument()); 121 defineFastMethod("slice!", Arity.optional(), "slice_bang"); 122 defineFastMethod("split", Arity.optional()); 123 defineFastMethod("strip", Arity.noArguments()); 124 defineFastMethod("strip!", Arity.noArguments(), "strip_bang"); 125 defineFastMethod("succ", Arity.noArguments()); 126 defineFastMethod("succ!", Arity.noArguments(), "succ_bang"); 127 defineFastMethod("squeeze", Arity.optional()); 128 defineFastMethod("squeeze!", Arity.optional(), "squeeze_bang"); 129 defineMethod("sub", Arity.optional()); 130 defineMethod("sub!", Arity.optional(), "sub_bang"); 131 defineFastMethod("sum", Arity.optional()); 132 defineFastMethod("swapcase", Arity.noArguments()); 133 defineFastMethod("swapcase!", Arity.noArguments(), "swapcase_bang"); 134 defineFastMethod("to_f", Arity.noArguments()); 135 defineFastMethod("to_i", Arity.optional()); 136 defineFastMethod("to_str", Arity.noArguments()); 137 defineFastMethod("to_sym", Arity.noArguments()); 138 defineFastMethod("tr", Arity.twoArguments()); 139 defineFastMethod("tr!", Arity.twoArguments(), "tr_bang"); 140 defineFastMethod("tr_s", Arity.twoArguments()); 141 defineFastMethod("tr_s!", Arity.twoArguments(), "tr_s_bang"); 142 defineFastMethod("unpack", Arity.singleArgument()); 143 defineFastMethod("upcase", Arity.noArguments()); 144 defineFastMethod("upcase!", Arity.noArguments(), "upcase_bang"); 145 defineMethod("upto", Arity.singleArgument()); 146 147 defineAlias("<<", "concat"); 148 defineAlias("each", "each_line"); 149 defineAlias("intern", "to_sym"); 150 defineAlias("next", "succ"); 151 defineAlias("next!", "succ!"); 152 defineAlias("size", "length"); 153 defineAlias("slice", "[]"); 154 } 155 }; 156 157 protected Meta getMeta() { 158 return new StringMeta(); 159 } 160 161 public RubyClass newSubClass(String name, SinglyLinkedList parentCRef) { 162 return new StringMetaClass(name, this, STRING_ALLOCATOR, parentCRef); 163 } 164 165 private static ObjectAllocator STRING_ALLOCATOR = new ObjectAllocator() { 166 public IRubyObject allocate(Ruby runtime, RubyClass klass) { 167 RubyString newString = runtime.newString(""); 168 169 newString.setMetaClass(klass); 170 171 return newString; 172 } 173 }; 174 } 175 | Popular Tags |