summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authorYukihiro Matsumoto <matz@ruby-lang.org>1995-01-10 00:58:20 +0900
committerTakashi Kokubun <takashikkbn@gmail.com>2019-08-17 22:09:31 +0900
commit5d828b25d4ae30a000c054a724ac248dadbb97b3 (patch)
tree080fc6e3b41b9c59cf5def77ae619a82e7e252db /re.c
parentbd0c733b77b561f4d12daccebf06a7c71917000b (diff)
version 0.64v0_64
https://cache.ruby-lang.org/pub/ruby/1.0/ruby-0.64.tar.gz Tue Jan 10 00:58:20 1995 Yukihiro Matsumoto (matz@dyna) * eval.c: レシーバと引数は常にiterではない. * cons.c(aref,aset): negative offset対応. Mon Jan 9 14:40:39 1995 Yukihiro Matsumoto (matz@ix-02) * parse.y: foo{..}の形式において,fooをローカル変数やクラス名では なく,引数なしの関数型メソッド呼び出しとみなすようにした. * list.c -> cons.c: 名称変更(クラス名も). * list.c: a::b::c::nilをリスト(a b c)とみなすlisp形式から,a::b::c をリスト(a b c)とみなすruby形式に変更.[], []=, eachもそれに会わ せた仕様とする. * list.c: consペアとしての機能を強調.仕様変更. Sat Jan 7 01:26:26 1995 Yukihiro Matsumoto (matz@dyna) * eval.c: 自己代入の不具合修正. * eval.c(masign): 多重代入が配列もリストもとれるようにした. * list.c: assocを2要素の配列からList(CONSペア)に変更した. Fri Jan 6 13:42:12 1995 Yukihiro Matsumoto (matz@ix-02) * parse.y: a[b]+=cやa.b+=cなどの自己代入形式で,aやbを2度評価しな くなった. * eval.c: iterator設定のバグフィックス. * list.c: Listクラスを新設. Thu Jan 5 13:55:00 1995 Yukihiro Matsumoto (matz@ix-02) * parse.y: SCOPEのメモリリークをなくした. * eval.c: built-inメソッドへの引数の引き渡し方を変更して,配列の生 成数を減らした. * re.c: match-dataを毎回生成することをやめた.`$~'をアクセスした時 にon-demandで生成する. * string.c etc: 不必要なmemmoveをmemcpyに置換. * parse.y: =~, !~は副作用があるのでコンパイル時に展開できない. Tue Jan 3 02:04:36 1995 Yukihiro Matsumoto (matz@dyna) * eval.c: rest引数のbug fix. * eval.c,gc.c: scopeをオブジェクトにした. * eval.c: envとscopeの扱いを変更した. Wed Dec 28 09:46:57 1994 Yukihiro Matsumoto (matz@ix-02) * parse.y: evalでローカル変数が追加された場合に対応した. * parse.y: 演算子を含むaliasのbug fix. Tue Dec 27 16:45:20 1994 Yukihiro Matsumoto (matz@ix-02) * parse.y: def A Bをalias A Bに変更. * eval.c: alias関係のbug修正.nodeをオブジェクト化した時にenbugし たようだ. * signal.c: システムコールの再定義を止めた. * io.c(select): write/exceptのフラグ設定にバグ. * Makefile.in: static link用オプションをMake変数として独立させた.
Diffstat (limited to 're.c')
-rw-r--r--re.c151
1 files changed, 90 insertions, 61 deletions
diff --git a/re.c b/re.c
index 06770ab..cdff895 100644
--- a/re.c
+++ b/re.c
@@ -3,7 +3,7 @@
re.c -
$Author: matz $
- $Date: 1994/12/19 08:30:12 $
+ $Date: 1995/01/10 10:42:49 $
created at: Mon Aug 9 18:24:49 JST 1993
Copyright (C) 1994 Yukihiro Matsumoto
@@ -101,7 +101,7 @@ int len;
*/
rp = ALLOC(Regexp);
- memset((char *)rp, 0, sizeof(Regexp));
+ MEMZERO((char *)rp, Regexp, 1);
rp->pat.buffer = ALLOC_N(char, 16);
rp->pat.allocated = 16;
rp->pat.fastmap = ALLOC_N(char, 256);
@@ -117,14 +117,7 @@ struct match {
struct re_registers regs;
};
-static void
-free_match(data)
- struct match *data;
-{
- free(data->ptr);
-}
-
-VALUE last_match_data;
+struct match last_match;
int
research(reg, str, start, ignorecase)
@@ -148,20 +141,16 @@ research(reg, str, start, ignorecase)
start, str->len - start, &(reg->ptr->regs));
if (result >= 0) {
- struct RData *obj;
- struct match *data;
- int beg, i;
-
- data = ALLOC(struct match);
- obj = (struct RData*)data_new(data, free_match, Qnil);
-
- data->len = str->len;
- data->ptr = ALLOC_N(char, str->len+1);
- memcpy(data->ptr, str->ptr, data->len);
- data->ptr[data->len] = '\0';
- data->regs = reg->ptr->regs;
-
- last_match_data = (VALUE)obj;
+ last_match.len = str->len;
+ if (last_match.ptr == Qnil) {
+ last_match.ptr = ALLOC_N(char, str->len+1);
+ }
+ else {
+ REALLOC_N(last_match.ptr, char, str->len+1);
+ }
+ memcpy(last_match.ptr, str->ptr, last_match.len);
+ last_match.ptr[last_match.len] = '\0';
+ last_match.regs = reg->ptr->regs;
}
return result;
@@ -171,21 +160,16 @@ static VALUE
nth_match(nth)
int nth;
{
+ int start, end, len;
+
if (nth >= RE_NREGS) {
- Fail("argument out of range %d, %d", nth, RE_NREGS);
- }
- if (last_match_data) {
- int start, end, len;
- struct match *match;
-
- match = (struct match*)DATA_PTR(last_match_data);
- start = match->regs.start[nth];
- if (start == -1) return Qnil;
- end = match->regs.end[nth];
- len = end - start;
- return str_new(match->ptr + start, len);
+ Fail("match out of range %d, %d", nth, RE_NREGS);
}
- return Qnil;
+ start = last_match.regs.start[nth];
+ if (start == -1) return Qnil;
+ end = last_match.regs.end[nth];
+ len = end - start;
+ return str_new(last_match.ptr + start, len);
}
VALUE
@@ -200,10 +184,8 @@ re_match_pre()
{
struct match *match;
- if (!last_match_data) return Qnil;
-
- match = (struct match*)DATA_PTR(last_match_data);
- return str_new(match->ptr, match->regs.start[0]);
+ if (last_match.regs.start[0] == -1) return Qnil;
+ return str_new(last_match.ptr, last_match.regs.start[0]);
}
static VALUE
@@ -211,10 +193,9 @@ re_match_post()
{
struct match *match;
- if (!last_match_data) return Qnil;
-
- match = (struct match*)DATA_PTR(last_match_data);
- return str_new(match->ptr+match->regs.end[0], match->ptr+match->len);
+ if (last_match.regs.start[0] == -1) return Qnil;
+ return str_new(last_match.ptr+last_match.regs.end[0],
+ last_match.len-last_match.regs.end[0]);
}
static VALUE
@@ -223,14 +204,13 @@ re_match_last()
struct match *match;
int i;
- if (!last_match_data) return Qnil;
+ if (last_match.regs.start[0] == -1) return Qnil;
- match = (struct match*)DATA_PTR(last_match_data);
for (i=0; i<RE_NREGS; i++) {
- if (match->regs.start[i] == -1) break;
+ if (last_match.regs.start[i] == -1) break;
}
- i--;
- return nth_match(i);
+ if (i == RE_NREGS) return Qnil;
+ return nth_match(i-1);
}
static VALUE
@@ -241,11 +221,55 @@ get_match_data(id, nth)
return nth_match(nth);
}
+static void
+free_match(data)
+ struct match *data;
+{
+ free(data->ptr);
+}
+
+static VALUE
+get_match()
+{
+ struct match *data;
+ int beg, i;
+
+ data = ALLOC(struct match);
+
+ data->len = last_match.len;
+ data->ptr = ALLOC_N(char, last_match.len+1);
+ memcpy(data->ptr, last_match.ptr, data->len+1);
+ data->regs = last_match.regs;
+
+ return data_new(data, free_match, Qnil);
+}
+
static VALUE
-store_match_data(val)
+set_match(val)
struct RArray *val;
{
+ struct match *match;
+
Check_Type(val, T_DATA);
+ match = (struct match*)DATA_PTR(val);
+ last_match.len = match->len;
+ if (last_match.len == 0) {
+ if (last_match.ptr) {
+ free(last_match.ptr);
+ last_match.ptr = Qnil;
+ }
+ }
+ else {
+ if (last_match.ptr == Qnil) {
+ last_match.ptr = ALLOC_N(char, match->len+1);
+ }
+ else {
+ REALLOC_N(last_match.ptr, char, match->len+1);
+ }
+ }
+ memcpy(last_match.ptr, match->ptr, last_match.len+1);
+ last_match.regs = match->regs;
+
return (VALUE)val;
}
@@ -340,9 +364,10 @@ Freg_match2(re)
}
static VALUE
-Sreg_new(argc, argv)
+Sreg_new(argc, argv, self)
int argc;
VALUE *argv;
+ VALUE self;
{
VALUE src, reg;
@@ -353,10 +378,10 @@ Sreg_new(argc, argv)
src = argv[0];
switch (TYPE(src)) {
case T_STRING:
- reg = regexp_new_1(Qself, RREGEXP(src)->ptr, RREGEXP(src)->len);
+ reg = regexp_new_1(self, RREGEXP(src)->ptr, RREGEXP(src)->len);
case T_REGEXP:
- reg = regexp_new_1(Qself, RREGEXP(src)->str, RREGEXP(src)->len);
+ reg = regexp_new_1(self, RREGEXP(src)->str, RREGEXP(src)->len);
default:
Check_Type(src, T_STRING);
@@ -417,15 +442,13 @@ re_regsub(str)
if (no < 0) { /* Ordinary character. */
if (c == '\\' && (*s == '\\' || *s == '&'))
p = ++s;
- } else if (last_match_data) {
- struct match *match;
+ } else {
-#define BEG(no) match->regs.start[no]
-#define END(no) match->regs.end[no]
+#define BEG(no) last_match.regs.start[no]
+#define END(no) last_match.regs.end[no]
- match = (struct match*)DATA_PTR(last_match_data);
if (BEG(no) == -1) continue;
- str_cat(val, match->ptr+BEG(no), END(no)-BEG(no));
+ str_cat(val, last_match.ptr+BEG(no), END(no)-BEG(no));
}
}
@@ -491,12 +514,18 @@ kanji_var_set(val)
void
Init_Regexp()
{
+ int i;
+
obscure_syntax = RE_NO_BK_PARENS | RE_NO_BK_VBAR
| RE_CONTEXT_INDEP_OPS | RE_INTERVALS
| RE_NO_BK_CURLY_BRACES
| RE_MBCTYPE_EUC;
- rb_define_variable("$~", last_match_data, Qnil, store_match_data, 0);
+ for (i=0; i<RE_NREGS; i++) {
+ last_match.regs.start[i] = last_match.regs.end[i] = -1;
+ }
+
+ rb_define_variable("$~", Qnil, get_match, set_match, 0);
rb_define_variable("$&", Qnil, re_last_match, Qnil, 0);
rb_define_variable("$`", Qnil, re_match_pre, Qnil, 0);