KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > builtin > meta > StringMetaClass


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2005 Thomas E Enebo <enebo@acm.org>
16  * Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
17  * Copyright (C) 2006 Ola Bini <ola@ologix.com>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

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 JavaDoc 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             // To override Comparable with faster String ones
68
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 JavaDoc 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