Skip to content

Instantly share code, notes, and snippets.

@enukane
Created June 20, 2012 07:51
Show Gist options
  • Save enukane/2958688 to your computer and use it in GitHub Desktop.
Save enukane/2958688 to your computer and use it in GitHub Desktop.
regexdiff for mruby : not knowing if it runs...
diff --git include/mrbconf.h include/mrbconf.h
index 21b8fea..2f4c4d5 100644
--- include/mrbconf.h
+++ include/mrbconf.h
@@ -14,7 +14,8 @@
#undef MRB_USE_FLOAT
/* -DDISABLE_XXXX to change to drop the feature */
-#define DISABLE_REGEXP /* regular expression classes */
+//#define DISABLE_REGEXP /* regular expression classes */
+#undef DISABLE_REGEXP /* regular expression classes */
#undef DISABLE_KERNEL_SPRINTF /* Kernel.sprintf method */
#undef DISABLE_MATH /* Math functions */
#undef DISABLE_TIME /* Time class */
diff --git include/mruby.h include/mruby.h
index ef742ce..971e49e 100644
--- include/mruby.h
+++ include/mruby.h
@@ -35,6 +35,8 @@ extern "C" {
#include <stdlib.h>
#include "mrbconf.h"
+typedef int ID;
+
enum mrb_vtype {
MRB_TT_FALSE = 0, /* 0 */
MRB_TT_FREE, /* 1 */
@@ -273,7 +275,7 @@ typedef struct mrb_state {
mrb_sym symidx;
struct kh_n2s *name2sym; /* symbol table */
struct kh_s2n *sym2name; /* reverse symbol table */
-#ifdef INCLUDE_REGEXP
+#ifdef ENABLE_REGEXP
struct RNode *local_svar;/* regexp */
#endif
diff --git include/mruby/string.h include/mruby/string.h
index 1449a2e..78253a1 100644
--- include/mruby/string.h
+++ include/mruby/string.h
@@ -86,6 +86,11 @@ mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
+#ifdef ENABLE_REGEXP
+mrb_value mrb_str_subseq(mrb_state *mrb, mrb_value str, int beg, int len);
+mrb_value mrb_str_size(mrb_state *mrb, mrb_value self);
+#endif /* ENABLE_REGEXP */
+
#if defined(__cplusplus)
} /* extern "C" { */
#endif
diff --git src/load.c src/load.c
index d3766da..cc5b345 100644
--- src/load.c
+++ src/load.c
@@ -329,6 +329,9 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32
mrb_int fix_num;
mrb_float f;
int ai = mrb_gc_arena_save(mrb);
+#ifdef ENABLE_REGEXP
+ mrb_value str;
+#endif
recordStart = src;
buf = mrb_malloc(mrb, bufsize);
diff --git src/node.h src/node.h
index 29edd6c..f4f75f0 100644
--- src/node.h
+++ src/node.h
@@ -103,3 +103,28 @@ enum node_type {
NODE_ATTRASGN,
NODE_LAST
};
+typedef struct RNode {
+ mrb_value flags;
+ mrb_value nd_reserved; /* ex nd_file */
+ union {
+ struct RNode *node;
+ ID id;
+ mrb_value value;
+ mrb_value (*cfunc)(ANYARGS);
+ ID *tbl;
+ } u1;
+ union {
+ struct RNode *node;
+ ID id;
+ long argc;
+ mrb_value value;
+ } u2;
+ union {
+ struct RNode *node;
+ ID id;
+ long state;
+ struct rb_global_entry *entry;
+ long cnt;
+ mrb_value value;
+ } u3;
+} NODE;
diff --git src/re.c src/re.c
index 70e7c59..dc1a60a 100644
--- src/re.c
+++ src/re.c
@@ -386,7 +386,7 @@ mrb_reg_prepare_re(mrb_state *mrb, mrb_value re, mrb_value str)
OnigErrorInfo einfo;
const char *pattern;
mrb_value unescaped;
- mrb_encoding *enc = mrb_ascii8bit_encoding(mrb);
+ mrb_encoding *enc = ONIG_ENCODING_ASCII; //mrb_ascii8bit_encoding(mrb);
mrb_reg_check(mrb, re);
reg = RREGEXP(re)->ptr;
@@ -515,7 +515,7 @@ mrb_reg_adjust_startpos(mrb_state *mrb, mrb_value re, mrb_value str, mrb_int pos
range = s->len - pos;
}
return re_adjust_startpos(r->ptr,
- s->buf, s->len,
+ s->ptr, s->len,
pos, range);
}
@@ -656,7 +656,7 @@ mrb_reg_initialize(mrb_state *mrb, mrb_value obj, const char *s, long len,
{
struct RRegexp *re = RREGEXP(obj);
mrb_value unescaped;
- mrb_encoding *enc = mrb_ascii8bit_encoding(mrb);
+ mrb_encoding *enc = ONIG_ENCODING_ASCII; //mrb_ascii8bit_encoding(mrb);
if (re->ptr)
mrb_raise(mrb, E_TYPE_ERROR, "already initialized regexp");
re->ptr = 0;
@@ -2084,7 +2084,7 @@ mrb_reg_regsub(mrb_state *mrb, mrb_value str, mrb_value src, struct re_registers
int no;
val.tt = 0;
- p = s = ps->buf;
+ p = s = ps->ptr;
e = s + ps->len;
while (s < e) {
diff --git src/re.h src/re.h
index 238b4e4..de5f33f 100644
--- src/re.h
+++ src/re.h
@@ -52,7 +52,7 @@ struct RRegexp {
#define mrb_regex_ptr(r) ((struct RRegexp*)((r).value.p))
#define RREGEXP(r) ((struct RRegexp*)((r).value.p))
#define RREGEXP_SRC(r) (RREGEXP(r)->src)
-#define RREGEXP_SRC_PTR(r) (RREGEXP_SRC(r)->buf)
+#define RREGEXP_SRC_PTR(r) (RREGEXP_SRC(r)->ptr)
#define RREGEXP_SRC_LEN(r) (RREGEXP_SRC(r)->len)
int re_adjust_startpos(struct re_pattern_buffer *bufp, const char *string, int size, int startpos, int range);
diff --git src/string.c src/string.c
index 6392b50..3d6725f 100644
--- src/string.c
+++ src/string.c
@@ -28,7 +28,9 @@ const char mrb_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static mrb_value get_pat(mrb_state *mrb, mrb_value pat, mrb_int quote);
#endif //ENABLE_REGEXP
static mrb_value str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2);
+#ifndef ENABLE_REGEXP
static mrb_value mrb_str_subseq(mrb_state *mrb, mrb_value str, int beg, int len);
+#endif
#define RESIZE_CAPA(s,capacity) do {\
s->ptr = mrb_realloc(mrb, s->ptr, (capacity)+1);\
@@ -721,6 +723,17 @@ mrb_str_dup(mrb_state *mrb, mrb_value str)
}
static mrb_value
+mrb_str_subpat(mrb_state *mrb, mrb_value str, mrb_value re, mrb_value backref)
+{
+ if (mrb_reg_search(mrb, re, str, 0, 0) >= 0) {
+ mrb_value match = mrb_backref_get(mrb);
+ int nth = mrb_reg_backref_number(mrb, match, backref);
+ return mrb_reg_nth_match(mrb, nth, match);
+ }
+ return mrb_nil_value();
+}
+
+static mrb_value
mrb_str_aref(mrb_state *mrb, mrb_value str, mrb_value indx)
{
long idx;
@@ -736,7 +749,7 @@ num_index:
case MRB_TT_REGEX:
#ifdef ENABLE_REGEXP
- return mrb_str_subpat(mrb, str, indx, 0); //mrb_str_subpat(str, indx, INT2FIX(0));
+ return mrb_str_subpat(mrb, str, indx, mrb_fixnum_value(0)); //mrb_str_subpat(str, indx, INT2FIX(0));
#else
mrb_raise(mrb, E_TYPE_ERROR, "Regexp Class not supported");
return mrb_nil_value();
@@ -828,7 +841,7 @@ mrb_str_aref_m(mrb_state *mrb, mrb_value str)
if (argc == 2) {
if (mrb_type(a1) == MRB_TT_REGEX) {
#ifdef ENABLE_REGEXP
- return mrb_str_subpat(mrb, str, argv[0], mrb_fixnum(argv[1]));
+ return mrb_str_subpat(mrb, str, a1, a2);
#else
mrb_raise(mrb, E_TYPE_ERROR, "Regexp Class not supported");
return mrb_nil_value();
@@ -1188,7 +1201,11 @@ mrb_str_eql(mrb_state *mrb, mrb_value self)
return mrb_false_value();
}
+#ifdef ENABLE_REGEXP
+mrb_value
+#else
static mrb_value
+#endif
mrb_str_subseq(mrb_state *mrb, mrb_value str, int beg, int len)
{
struct RString *orig, *s;
@@ -1251,7 +1268,7 @@ str_gsub(mrb_state *mrb, mrb_value str, mrb_int bang)
mrb_int offset, blen, len, last;
char *sp, *cp;
- if (bang) str_modify(mrb, mrb_str_ptr(self));
+ if (bang) str_modify(mrb, mrb_str_ptr(str));
mrb_get_args(mrb, "*", &argv, &argc);
switch (argc) {
case 1:
@@ -1259,6 +1276,7 @@ str_gsub(mrb_state *mrb, mrb_value str, mrb_int bang)
break;
case 2:
repl = argv[1];
+ printf("%u %s : %s %s\n", __LINE__, __func__, RSTRING(argv[0])->ptr, RSTRING(argv[1])->ptr);
mrb_string_value(mrb, &repl);
break;
default:
@@ -1355,6 +1373,7 @@ static mrb_value
mrb_str_gsub(mrb_state *mrb, mrb_value self)
{
//return str_gsub(argc, argv, self, 0);
+ printf("%u %s : in\n", __LINE__, __func__);
return str_gsub(mrb, self, 0);
}
@@ -1370,10 +1389,10 @@ mrb_str_gsub(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_str_gsub_bang(mrb_state *mrb, mrb_value self)
{
- striuct RString *s = mrb_str_ptr(self);
+ struct RString *s = mrb_str_ptr(self);
str_modify(mrb, s);
- return str_gsub(mrb, s, 1);
+ return str_gsub(mrb, self, 1);
}
#endif //ENABLE_REGEXP
@@ -2310,7 +2329,7 @@ mrb_block_given_p()
static mrb_value
mrb_str_sub_bang(mrb_state *mrb, mrb_value str)
{
- str_modify(mrb, str);
+ str_modify(mrb, RSTRING(str));
return mrb_nil_value();
}
#endif //ENABLE_REGEXP
@@ -2997,6 +3016,7 @@ void
mrb_init_string(mrb_state *mrb)
{
struct RClass *s;
+ printf("%u %s : init\n", __LINE__, __func__);
s = mrb->string_class = mrb_define_class(mrb, "String", mrb->object_class);
MRB_SET_INSTANCE_TT(s, MRB_TT_STRING);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment