Permalink
Browse files

more string/regexp work

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/branches/icu@3583 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
lrz committed Feb 20, 2010
1 parent cda2073 commit 1775a38576cfa9255b9c9e91163f5e932bf7bb69
Showing with 43 additions and 17 deletions.
  1. +1 −0 dispatcher.cpp
  2. +4 −7 re.cpp
  3. +7 −0 re.h
  4. +31 −10 string.c
View
@@ -16,6 +16,7 @@
#include "dtrace.h"
#include "array.h"
#include "hash.h"
+#include "encoding.h"
#include "re.h"
#include <execinfo.h>
View
11 re.cpp
@@ -12,6 +12,7 @@
#include "ruby/ruby.h"
#include "encoding.h"
#include "objc.h"
+#include "re.h"
extern "C" {
@@ -368,15 +369,11 @@ reg_operand(VALUE s, bool check)
}
}
-static int
-rb_reg_adjust_startpos(VALUE re, VALUE str, int pos, bool reverse)
-{
- return reverse ? -pos : rb_str_chars_len(str) - pos;
-}
-
-static int
+int
rb_reg_search(VALUE re, VALUE str, int pos, bool reverse)
{
+ assert(!reverse); // TODO
+
const long len = rb_str_chars_len(str);
if (pos > len || pos < 0) {
rb_backref_set(Qnil);
View
7 re.h
@@ -15,6 +15,13 @@ extern "C" {
bool rb_char_to_icu_option(int c, int *option);
VALUE regexp_eqq(VALUE rcv, SEL sel, VALUE str);
+int rb_reg_search(VALUE re, VALUE str, int pos, bool reverse);
+
+static inline int
+rb_reg_adjust_startpos(VALUE re, VALUE str, int pos, bool reverse)
+{
+ return reverse ? -pos : rb_str_chars_len(str) - pos;
+}
#if defined(__cplusplus)
} // extern "C"
View
@@ -16,6 +16,7 @@
#include "ruby.h"
#include "ruby/encoding.h"
#include "encoding.h"
+#include "re.h"
#include "objc.h"
#include "id.h"
@@ -1311,14 +1312,21 @@ rstr_is_ascii_only(VALUE self, SEL sel)
* a["bye"] #=> nil
*/
+static VALUE
+rb_str_subpat(VALUE str, VALUE re, int nth)
+{
+ if (rb_reg_search(re, str, 0, 0) >= 0) {
+ return rb_reg_nth_match(nth, rb_backref_get());
+ }
+ return Qnil;
+}
+
static VALUE
rstr_aref(VALUE str, SEL sel, int argc, VALUE *argv)
{
if (argc == 2) {
if (TYPE(argv[0]) == T_REGEXP) {
- // TODO
- //return rb_str_subpat(str, argv[0], NUM2INT(argv[1]));
- return Qnil;
+ return rb_str_subpat(str, argv[0], NUM2INT(argv[1]));
}
return str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
}
@@ -1337,7 +1345,7 @@ rstr_aref(VALUE str, SEL sel, int argc, VALUE *argv)
return str;
case T_REGEXP:
- abort(); // TODO
+ return rb_str_subpat(str, indx, 0);
case T_STRING:
{
@@ -1427,10 +1435,8 @@ rstr_index(VALUE self, SEL sel, int argc, VALUE *argv)
switch (TYPE(sub)) {
case T_REGEXP:
- // TODO
- //pos = rb_reg_adjust_startpos(sub, str, pos, 0);
- //pos = rb_reg_search(sub, str, pos, 0);
- //pos = rb_str_sublen(str, pos);
+ pos = rb_reg_adjust_startpos(sub, self, pos, false);
+ pos = rb_reg_search(sub, self, pos, false);
break;
default:
@@ -1502,13 +1508,24 @@ static VALUE
rstr_concat(VALUE self, SEL sel, VALUE other)
{
rstr_modify(self);
+
+ long codepoint = 0;
switch (TYPE(other)) {
case T_FIXNUM:
- abort(); // TODO
+ codepoint = FIX2LONG(other);
+ break;
+ case T_BIGNUM:
+ codepoint = rb_big2ulong(other);
+ break;
+
default:
str_concat_string(RSTR(self), str_need_string(other));
+ return self;
}
+
+ // TODO: handle codepoint
+
return self;
}
@@ -2312,7 +2329,11 @@ rb_str_inspect(VALUE rcv)
VALUE
rb_str_subseq(VALUE str, long beg, long len)
{
- abort(); // TODO
+ if (IS_RSTR(str)) {
+ return str_substr(str, beg, len);
+ }
+ // TODO
+ return Qnil;
}
VALUE

0 comments on commit 1775a38

Please sign in to comment.