match_at 2 → 3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b20934cbff3ba306998d01baf791fd6dd2ea9b4b
4
- data.tar.gz: fe95877702b2dadaa6df96ce90ce9d12286efc6d
3
+ metadata.gz: d5eee137912b36f2e3d5cd1338d6519bbf530bd3
4
+ data.tar.gz: 9d9cffc18535c9cf277455dfc17b6964bce74b7b
5
5
  SHA512:
6
- metadata.gz: ad86d7bd9683a9fe869a7c29e22162a15c45d3344a0b7a9456d373366efa3f3362c4b5576f659053dbd991f952d581389d813cfb73b99c1f36f261ee48a48778
7
- data.tar.gz: bc97cfe8229b2a181d6ed9cb7ac6a18013673f8bf52a8815fc13ac05a2fa9ee850422df580afa5ade387ac6c595e87a2ae465ec0fdd6be6c1e603346d94c1d02
6
+ metadata.gz: c144b85c5c41c5aea39e86d0bda594ba5b812ee45d3cf27960d28ad1efc5e19049b568b542a81556f5dbe8a4c921fb7bfa1aaab01e57638014084be8bf66397e
7
+ data.tar.gz: bd71a51305170776436d503ebc1cff1e0f8fe7ee236f1a1f993a568b43c83669215a281e38dbed95861e961e4193d668d8c17a493056b8b252263208561bccf9
@@ -27,7 +27,7 @@
27
27
 
28
28
  static VALUE match_at(VALUE RB_UNUSED_VAR(mod), VALUE str, VALUE rexp, VALUE pos);
29
29
  static VALUE match_at_p(VALUE RB_UNUSED_VAR(mod), VALUE str, VALUE rexp, VALUE pos);
30
- static bool do_match(VALUE str, VALUE rexp, VALUE pos, OnigRegion *region);
30
+ static OnigPosition do_match(VALUE str, VALUE rexp, VALUE pos, OnigRegion *region);
31
31
 
32
32
  /**
33
33
  * Match the rexp against str's position pos. This is lightweight because no
@@ -42,7 +42,22 @@ static bool do_match(VALUE str, VALUE rexp, VALUE pos, OnigRegion *region);
42
42
  VALUE
43
43
  match_at_p(VALUE mod, VALUE str, VALUE rexp, VALUE pos)
44
44
  {
45
- return do_match(str, rexp, pos, NULL) ? Qtrue : Qfalse;
45
+ OnigPosition result = do_match(str, rexp, pos, NULL);
46
+
47
+ if (result >= 0) {
48
+ long n = NUM2LONG(pos);
49
+ rb_encoding *enc = rb_enc_get(str);
50
+ const char *beg = rb_string_value_ptr(&str);
51
+ const char *end = RSTRING_END(str);
52
+ const char *ptr = rb_enc_nth(beg, end, n, enc);
53
+ const char *term = &ptr[result];
54
+ long len = rb_enc_strlen(ptr, term, enc);
55
+
56
+ return LONG2NUM(len);
57
+ }
58
+ else {
59
+ return Qnil;
60
+ }
46
61
  }
47
62
 
48
63
  /**
@@ -59,10 +74,11 @@ match_at_p(VALUE mod, VALUE str, VALUE rexp, VALUE pos)
59
74
  VALUE
60
75
  match_at(VALUE mod, VALUE str, VALUE rexp, VALUE pos)
61
76
  {
62
- OnigRegion region = { 0 };
63
- VALUE ret = Qnil;
77
+ OnigRegion region = { 0 };
78
+ VALUE ret = Qnil;
79
+ OnigPosition result = do_match(str, rexp, pos, &region);
64
80
 
65
- if (do_match(str, rexp, pos, &region)) {
81
+ if (result >= 0) {
66
82
  int err;
67
83
  ret = rb_funcall(rb_cMatch, rb_intern("allocate"), 0);
68
84
  err = rb_reg_region_copy(RMATCH_REGS(ret), &region);
@@ -81,7 +97,7 @@ match_at(VALUE mod, VALUE str, VALUE rexp, VALUE pos)
81
97
  return ret;
82
98
  }
83
99
 
84
- bool
100
+ OnigPosition
85
101
  do_match(VALUE vstr, VALUE vreg, VALUE vpos, OnigRegion *region)
86
102
  {
87
103
  const char *str;
@@ -119,11 +135,8 @@ do_match(VALUE vstr, VALUE vreg, VALUE vpos, OnigRegion *region)
119
135
  }
120
136
  }
121
137
 
122
- if (result >= 0) {
123
- return true;
124
- }
125
- else if (result == ONIG_MISMATCH) {
126
- return false;
138
+ if ((result >= 0) || (result == ONIG_MISMATCH)) {
139
+ return result;
127
140
  }
128
141
  else {
129
142
  OnigUChar err[ONIG_MAX_ERROR_MESSAGE_LEN] = { 0 };
@@ -24,4 +24,4 @@
24
24
  # SOFTWARE.
25
25
  ;
26
26
 
27
- MatchAt::VERSION = 2
27
+ MatchAt::VERSION = 3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: match_at
3
3
  version: !ruby/object:Gem::Version
4
- version: '2'
4
+ version: '3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Urabe, Shyouhei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-15 00:00:00.000000000 Z
11
+ date: 2017-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler