File: ClrStringOps.cs

package info (click to toggle)
dlr-languages 20090805%2Bgit.e6b28d27%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 51,484 kB
  • ctags: 59,257
  • sloc: cs: 298,829; ruby: 159,643; xml: 19,872; python: 2,820; yacc: 1,960; makefile: 96; sh: 65
file content (332 lines) | stat: -rw-r--r-- 10,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System.Text;
using IronRuby.Runtime;
using Microsoft.Scripting.Runtime;
using System.Collections;
using System;
using IronRuby.Compiler;
using System.Runtime.InteropServices;
using Microsoft.Scripting;

namespace IronRuby.Builtins {

    [RubyClass(Extends = typeof(string), Restrictions = ModuleRestrictions.None)]
    [Includes(typeof(ClrString), typeof(Enumerable), typeof(Comparable))]
    public static class ClrStringOps {
        [RubyConstructor]
        public static string/*!*/ Create(RubyClass/*!*/ self, [DefaultProtocol]MutableString/*!*/ str) {
            return str.ToString();
        }

        [RubyConstructor]
        public static string/*!*/ Create(RubyClass/*!*/ self, char c, [DefaultParameterValue(1)]int repeatCount) {
            return new String(c, repeatCount);
        }

        [RubyConstructor]
        public static string/*!*/ Create(RubyClass/*!*/ self, [NotNull]char[]/*!*/ chars) {
            return new String(chars);
        }

        [RubyConstructor]
        public static string/*!*/ Create(RubyClass/*!*/ self, [NotNull]char[]/*!*/ chars, int startIndex, int length) {
            return new String(chars, startIndex, length);
        }
    }

    /// <summary>
    /// Mixed into System::String and System::Char.
    /// </summary>
    [RubyModule("String", DefineIn = typeof(IronRubyOps.ClrOps))]
    public static class ClrString {
        #region %, *, +

        [RubyMethod("%")]
        public static string/*!*/ Format(StringFormatterSiteStorage/*!*/ storage, string/*!*/ self, object arg) {
            IList args = arg as IList ?? new object[] { arg };
            StringFormatter formatter = new StringFormatter(storage, self, args);
            return formatter.Format().ToString();
        }

        [RubyMethod("*")]
        public static string/*!*/ Repeat(string/*!*/ self, [DefaultProtocol]int times) {
            if (times < 0) {
                throw RubyExceptions.CreateArgumentError("negative argument");
            }

            var result = new StringBuilder();
            for (int i = 0; i < times; i++) {
                result.Append(self);
            }

            return result.ToString();
        }

        [RubyMethod("+")]
        public static string/*!*/ Concatenate(string/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ other) {
            return self + other.ToString();
        }

        #endregion

        #region <=>, ==, ===

        [RubyMethod("<=>")]
        public static int Compare(string/*!*/ self, [NotNull]string/*!*/ other) {
            return Math.Sign(self.CompareTo(other));
        }

        [RubyMethod("<=>")]
        public static int Compare(string/*!*/ self, [NotNull]MutableString/*!*/ other) {
            // TODO: do not create MS
            return -Math.Sign(other.CompareTo(MutableString.Create(self, RubyEncoding.UTF8)));
        }

        [RubyMethod("<=>")]
        public static object Compare(BinaryOpStorage/*!*/ comparisonStorage, RespondToStorage/*!*/ respondToStorage, string/*!*/ self, object other) {
            return MutableStringOps.Compare(comparisonStorage, respondToStorage, self, other);
        }

        [RubyMethod("==")]
        [RubyMethod("===")]
        public static bool StringEquals(string/*!*/ lhs, [NotNull]string/*!*/ rhs) {
            return lhs.Equals(rhs);
        }

        [RubyMethod("==")]
        [RubyMethod("===")]
        public static bool StringEquals(string/*!*/ lhs, [NotNull]MutableString/*!*/ rhs) {
            // TODO: do not create MS
            return rhs.Equals(MutableString.Create(lhs, RubyEncoding.UTF8));
        }

        [RubyMethod("==")]
        [RubyMethod("===")]
        public static bool Equals(RespondToStorage/*!*/ respondToStorage, BinaryOpStorage/*!*/ equalsStorage, string/*!*/ self, object other) {
            return MutableStringOps.Equals(respondToStorage, equalsStorage, self, other);
        }

        #endregion

        #region  TODO: [], slice

        #endregion

        #region TODO: casecmp, capitalize, downcase, swapcase, upcase

        #endregion

        #region TODO: center

        #endregion

        #region TODO: chomp, chop
        #endregion

        #region dump, inspect

        [RubyMethod("inspect", RubyMethodAttributes.PublicInstance)]
        public static MutableString/*!*/ Inspect(string/*!*/ self) {
            return MutableString.Create(MutableString.AppendUnicodeRepresentation(
                new StringBuilder().Append('\''), self, false, false, '\'', -1).Append('\'').ToString()
            );
        }

        [RubyMethod("dump", RubyMethodAttributes.PublicInstance)]
        public static MutableString/*!*/ Dump(string/*!*/ self) {
            return MutableString.Create(MutableString.AppendUnicodeRepresentation(
                new StringBuilder().Append('\''), self, false, true, '\'', -1).Append('\'').ToString()
            );
        }

        #endregion

        #region TODO: each, each_byte, each_line

        #endregion

        #region empty?, size, length, encoding

        [RubyMethod("empty?")]
        public static bool IsEmpty(string/*!*/ self) {
            return self.Length == 0;
        }

        [RubyMethod("size")]
        public static int GetLength(string/*!*/ self) {
            return self.Length;
        }

        [RubyMethod("encoding")]
        public static RubyEncoding/*!*/ GetEncoding(string/*!*/ self) {
            return RubyEncoding.UTF8;
        }

        #endregion

        #region TODO: sub, gsub
        #endregion

        #region TODO: index, rindex
        #endregion

        #region TODO: delete
        #endregion

        #region TODO: count
        #endregion

        #region TODO: include?

        #endregion

        #region TODO: insert

        #endregion

        #region TODO: match
        #endregion

        #region TODO: scan
        #endregion

        #region TODO: succ
        #endregion

        #region TODO: split
        #endregion

        #region TODO: strip, lstrip, rstrip
        #endregion

        #region TODO: squeeze
        #endregion

        #region to_i, hex, oct

        [RubyMethod("to_i")]
        public static object/*!*/ ToInteger(string/*!*/ self, [DefaultProtocol, DefaultParameterValue(10)]int @base) {
            if (@base == 1 || @base < 0 || @base > 36) {
                throw RubyExceptions.CreateArgumentError(String.Format("illegal radix {0}", @base));
            }
            return Tokenizer.ParseInteger(self, @base).ToObject();
        }

        [RubyMethod("hex")]
        public static object/*!*/ ToIntegerHex(string/*!*/ self) {
            return Tokenizer.ParseInteger(self, 16).ToObject();
        }

        [RubyMethod("oct")]
        public static object/*!*/ ToIntegerOctal(string/*!*/ self) {
            return Tokenizer.ParseInteger(self, 8).ToObject();
        }

        #endregion

        #region to_f, to_s, to_str, to_clr_string, to_sym, intern

        [RubyMethod("to_f")]
        public static double ToDouble(string/*!*/ self) {
            double result;
            bool complete;
            return Tokenizer.TryParseDouble(self, out result, out complete) ? result : 0.0;
        }

        [RubyMethod("to_str", RubyMethodAttributes.PublicInstance)]
        [RubyMethod("to_s", RubyMethodAttributes.PublicInstance)]
        public static MutableString/*!*/ ToStr(string/*!*/ self) {
            return MutableString.Create(self, RubyEncoding.UTF8);
        }

        [RubyMethod("to_clr_string", RubyMethodAttributes.PublicInstance)]
        public static string/*!*/ ToClrString(string/*!*/ self) {
            return self;
        }

        [RubyMethod("to_sym")]
        [RubyMethod("intern")]
        public static SymbolId ToSymbol(string/*!*/ self) {
            if (self.Length == 0) {
                throw RubyExceptions.CreateArgumentError("interning empty string");
            }

            // Cannot convert a string that contains null to a symbol
            if (self.IndexOf('\0') >= 0) {
                throw RubyExceptions.CreateArgumentError("symbol string may not contain '\0'");
            }

            return SymbolTable.StringToId(self);
        }

        #endregion

        #region TODO: upto
        #endregion

        #region TODO: replace, reverse
        #endregion

        #region TODO: tr, tr_s
        #endregion

        #region TODO: ljust
        #endregion

        #region TODO: rjust
        #endregion

        #region TODO: unpack
        #endregion

        #region TODO: sum
        #endregion

        #region TODO: Encodings (1.9)

        //ascii_only?
        //bytes
        //bytesize
        //chars
        //codepoints
        //each_byte
        //each_char
        //each_codepoint
        //encode
        //encoding
        //valid_esncoding?

        #endregion

        #region method_missing

        [RubyMethod("method_missing", RubyMethodAttributes.PrivateInstance)]
        [RubyStackTraceHidden]
        public static object MethodMissing(RubyScope/*!*/ scope, BlockParam block, string/*!*/ self, SymbolId symbol, [NotNull]params object[]/*!*/ args) {
            string name = SymbolTable.IdToString(symbol);

            if (name.EndsWith("=") || name.EndsWith("!")) {
                throw new InvalidOperationException(String.Format("Mutating method `{0}' called for an immutable string (System::String)", name));
            }

            // TODO: forward to MutableString until we implement the methods here:
            return KernelOps.SendMessageOpt(scope, block, ToStr(self), name, args);
        }

        #endregion
    }
}