forked from MacRuby/MacRuby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSString.m
318 lines (287 loc) · 7.62 KB
/
NSString.m
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
/*
* MacRuby extensions to NSString.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2010, Apple Inc. All rights reserved.
*/
#import <Foundation/Foundation.h>
#include "ruby/ruby.h"
#include "ruby/node.h"
#include "objc.h"
#include "vm.h"
#include "encoding.h"
VALUE rb_cString;
VALUE rb_cNSString;
VALUE rb_cNSMutableString;
static inline VALUE
to_str(VALUE ary)
{
return rb_convert_type(ary, T_STRING, "String", "to_str");
}
static id
nsstr_dup(id rcv, SEL sel)
{
id dup = [rcv mutableCopy];
if (OBJ_TAINTED(rcv)) {
OBJ_TAINT(dup);
}
return dup;
}
static id
nsstr_clone(id rcv, SEL sel)
{
id clone = nsstr_dup(rcv, 0);
if (OBJ_FROZEN(rcv)) {
OBJ_FREEZE(clone);
}
return clone;
}
static id
nsstr_to_s(id rcv, SEL sel)
{
return rcv;
}
static id
nsstr_replace(id rcv, SEL sel, VALUE other)
{
[rcv setString:(id)to_str(other)];
return rcv;
}
static id
nsstr_clear(id rcv, SEL sel)
{
const long len = [rcv length];
if (len > 0) {
[rcv deleteCharactersInRange:NSMakeRange(0, len)];
}
return rcv;
}
static VALUE
nsstr_encoding(id rcv, SEL sel)
{
// All NSStrings are Unicode, so let's return UTF-8.
return (VALUE)rb_encodings[ENCODING_UTF8];
}
static VALUE
nsstr_length(id rcv, SEL sel)
{
return LONG2NUM([rcv length]);
}
static VALUE
nsstr_empty(id rcv, SEL sel)
{
return [rcv length] == 0 ? Qtrue : Qfalse;
}
static id
nsstr_concat(id rcv, SEL sel, VALUE other)
{
[rcv appendString:(id)to_str(other)];
return rcv;
}
static id
nsstr_plus(id rcv, SEL sel, VALUE other)
{
id newstr = [NSMutableString new];
[newstr appendString:rcv];
[newstr appendString:(id)to_str(other)];
return newstr;
}
static VALUE
nsstr_equal(id rcv, SEL sel, VALUE other)
{
return [rcv isEqualToString:(id)to_str(other)] ? Qtrue : Qfalse;
}
static VALUE
nsstr_cmp(id rcv, SEL sel, VALUE other)
{
int ret = [rcv compare:(id)to_str(other)];
if (ret > 0) {
ret = 1;
}
else if (ret < 0) {
ret = -1;
}
return INT2FIX(ret);
}
static VALUE
nsstr_casecmp(id rcv, SEL sel, VALUE other)
{
int ret = [rcv compare:(id)to_str(other) options:NSCaseInsensitiveSearch];
if (ret > 0) {
ret = 1;
}
else if (ret < 0) {
ret = -1;
}
return INT2FIX(ret);
}
static VALUE
nsstr_include(id rcv, SEL sel, VALUE other)
{
NSRange range = [rcv rangeOfString:(id)to_str(other)];
return range.location == NSNotFound ? Qfalse : Qtrue;
}
static id
rstr_only(id rcv, SEL sel)
{
rb_raise(rb_eArgError, "method `%s' does not work on NSStrings",
sel_getName(sel));
return rcv; // never reached
}
static VALUE
nsstr_to_rstr(id nsstr)
{
const long len = [nsstr length];
if (len == 0) {
return rb_str_new(NULL, 0);
}
unichar *buf = (unichar *)malloc(sizeof(unichar) * len);
[nsstr getCharacters:buf range:NSMakeRange(0, len)];
VALUE rstr = rb_unicode_str_new(buf, len);
free(buf);
return rstr;
}
static VALUE
nsstr_forward(id rcv, SEL sel, int argc, VALUE *argv)
{
return rb_vm_call(nsstr_to_rstr(rcv), sel, argc, argv, false);
}
static VALUE
nsstr_forward_bang(id rcv, SEL sel, int argc, VALUE *argv)
{
VALUE rcv_rstr = nsstr_to_rstr(rcv);
VALUE ret = rb_vm_call(rcv_rstr, sel, argc, argv, false);
[rcv setString:(id)rcv_rstr];
return ret;
}
void
rb_str_NSCoder_encode(void *coder, VALUE str, const char *key)
{
NSString *nskey = [NSString stringWithUTF8String:key];
[(NSCoder *)coder encodeObject:(NSString *)str forKey:nskey];
}
VALUE
rb_str_NSCoder_decode(void *coder, const char *key)
{
NSString *nskey = [NSString stringWithUTF8String:key];
return OC2RB([(NSCoder *)coder decodeObjectForKey:nskey]);
}
void
Init_NSString(void)
{
rb_cNSString = (VALUE)objc_getClass("NSString");
assert(rb_cNSString != 0);
rb_cString = rb_cNSString;
rb_include_module(rb_cString, rb_mComparable);
rb_cNSMutableString = (VALUE)objc_getClass("NSMutableString");
assert(rb_cNSMutableString != 0);
rb_objc_define_method(rb_cString, "dup", nsstr_dup, 0);
rb_objc_define_method(rb_cString, "clone", nsstr_clone, 0);
rb_objc_define_method(rb_cString, "to_s", nsstr_to_s, 0);
rb_objc_define_method(rb_cString, "to_str", nsstr_to_s, 0);
rb_objc_define_method(rb_cString, "replace", nsstr_replace, 1);
rb_objc_define_method(rb_cString, "clear", nsstr_clear, 0);
rb_objc_define_method(rb_cString, "encoding", nsstr_encoding, 0);
rb_objc_define_method(rb_cString, "size", nsstr_length, 0);
rb_objc_define_method(rb_cString, "empty?", nsstr_empty, 0);
rb_objc_define_method(rb_cString, "<<", nsstr_concat, 1);
rb_objc_define_method(rb_cString, "concat", nsstr_concat, 1);
rb_objc_define_method(rb_cString, "+", nsstr_plus, 1);
rb_objc_define_method(rb_cString, "==", nsstr_equal, 1);
rb_objc_define_method(rb_cString, "eql?", nsstr_equal, 1);
rb_objc_define_method(rb_cString, "<=>", nsstr_cmp, 1);
rb_objc_define_method(rb_cString, "casecmp", nsstr_casecmp, 1);
rb_objc_define_method(rb_cString, "include?", nsstr_include, 1);
#define forward(msg) \
rb_objc_define_method(rb_cString, msg, nsstr_forward, -1)
#define forward_bang(msg) \
rb_objc_define_method(rb_cString, msg, nsstr_forward_bang, -1)
// These methods are implemented as forwarders.
forward("[]");
forward_bang("[]=");
forward("slice");
forward_bang("slice!");
forward_bang("insert");
forward("index");
forward("rindex");
forward("+");
forward("*");
forward("%");
forward("start_with?");
forward("end_with?");
forward("to_sym");
forward("intern");
forward("inspect");
forward("dump");
forward("match");
forward("=~");
forward("scan");
forward("split");
forward("to_i");
forward("hex");
forward("oct");
forward("ord");
forward("chr");
forward("to_f");
forward("chomp");
forward_bang("chomp!");
forward("chop");
forward_bang("chop!");
forward("sub");
forward_bang("sub!");
forward("gsub");
forward_bang("gsub!");
forward("downcase");
forward_bang("downcase!");
forward("upcase");
forward_bang("upcase!");
forward("swapcase");
forward_bang("swapcase!");
forward("capitalize");
forward_bang("capitalize!");
forward("ljust");
forward("rjust");
forward("center");
forward("strip");
forward("lstrip");
forward("rstrip");
forward_bang("strip!");
forward_bang("lstrip!");
forward_bang("rstrip!");
forward("lines");
forward("each_line");
forward("chars");
forward("each_char");
forward("succ");
forward_bang("succ!");
forward("next");
forward_bang("next!");
forward("upto");
forward("reverse");
forward_bang("reverse!");
forward("count");
forward("delete");
forward_bang("delete!");
forward("squeeze");
forward_bang("squeeze!");
forward("tr");
forward_bang("tr!");
forward("tr_s");
forward_bang("tr_s!");
forward("sum");
forward("partition");
forward("rpartition");
forward("crypt");
#undef forward
#undef forward_bang
// These methods will not work on NSStrings.
rb_objc_define_method(rb_cString, "bytesize", rstr_only, 0);
rb_objc_define_method(rb_cString, "getbyte", rstr_only, 1);
rb_objc_define_method(rb_cString, "setbyte", rstr_only, 2);
rb_objc_define_method(rb_cString, "force_encoding", rstr_only, 1);
rb_objc_define_method(rb_cString, "valid_encoding?", rstr_only, 0);
rb_objc_define_method(rb_cString, "ascii_only?", rstr_only, 0);
rb_objc_define_method(rb_cString, "bytes", rstr_only, 0);
rb_objc_define_method(rb_cString, "each_byte", rstr_only, 0);
}