re2 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -177,12 +177,17 @@ extern "C" {
177
177
  re2::StringPiece match;
178
178
 
179
179
  Data_Get_Struct(self, re2_matchdata, m);
180
- match = m->matches[nth];
181
180
 
182
- if (nth >= m->number_of_matches || match.empty()) {
181
+ if (nth < 0 || nth >= m->number_of_matches) {
183
182
  return Qnil;
184
183
  } else {
185
- return rb_str_new(match.data(), match.size());
184
+ match = m->matches[nth];
185
+
186
+ if (match.empty()) {
187
+ return Qnil;
188
+ } else {
189
+ return rb_str_new(match.data(), match.size());
190
+ }
186
191
  }
187
192
  }
188
193
 
@@ -14,14 +14,30 @@ module RE2
14
14
  # @param [String, RE2::Regexp] pattern a regexp matching text to be replaced
15
15
  # @param [String] rewrite the string to replace with
16
16
  # @example
17
+ # "hello there".re2_sub!("hello", "howdy") #=> "howdy there"
18
+ # re2 = RE2.new("hel+o")
19
+ # "hello there".re2_sub!(re2, "yo") #=> "yo there"
20
+ # text = "Good morning"
21
+ # text.re2_sub!("morn", "even") #=> "Good evening"
22
+ # text #=> "Good evening"
23
+ def re2_sub!(*args)
24
+ RE2.Replace(self, *args)
25
+ end
26
+
27
+ # Replaces the first occurrence +pattern+ with +rewrite+ and returns a new
28
+ # string.
29
+ #
30
+ # @param [String, RE2::Regexp] pattern a regexp matching text to be replaced
31
+ # @param [String] rewrite the string to replace with
32
+ # @example
17
33
  # "hello there".re2_sub("hello", "howdy") #=> "howdy there"
18
34
  # re2 = RE2.new("hel+o")
19
35
  # "hello there".re2_sub(re2, "yo") #=> "yo there"
20
36
  # text = "Good morning"
21
37
  # text.re2_sub("morn", "even") #=> "Good evening"
22
- # text #=> "Good evening"
38
+ # text #=> "Good morning"
23
39
  def re2_sub(*args)
24
- RE2.Replace(self, *args)
40
+ dup.re2_sub!(*args)
25
41
  end
26
42
 
27
43
  # Replaces every occurrence of +pattern+ with +rewrite+ <i>in place</i>.
@@ -29,14 +45,29 @@ module RE2
29
45
  # @param [String, RE2::Regexp] pattern a regexp matching text to be replaced
30
46
  # @param [String] rewrite the string to replace with
31
47
  # @example
48
+ # "hello there".re2_gsub!("e", "i") #=> "hillo thiri"
49
+ # re2 = RE2.new("oo?")
50
+ # "whoops-doops".re2_gsub!(re2, "e") #=> "wheps-deps"
51
+ # text = "Good morning"
52
+ # text.re2_gsub!("o", "ee") #=> "Geeeed meerning"
53
+ # text #=> "Geeeed meerning"
54
+ def re2_gsub!(*args)
55
+ RE2.GlobalReplace(self, *args)
56
+ end
57
+
58
+ # Replaces every occurrence of +pattern+ with +rewrite+ and return a new string.
59
+ #
60
+ # @param [String, RE2::Regexp] pattern a regexp matching text to be replaced
61
+ # @param [String] rewrite the string to replace with
62
+ # @example
32
63
  # "hello there".re2_gsub("e", "i") #=> "hillo thiri"
33
64
  # re2 = RE2.new("oo?")
34
65
  # "whoops-doops".re2_gsub(re2, "e") #=> "wheps-deps"
35
66
  # text = "Good morning"
36
67
  # text.re2_gsub("o", "ee") #=> "Geeeed meerning"
37
- # text #=> "Geeeed meerning"
68
+ # text #=> "Good morning"
38
69
  def re2_gsub(*args)
39
- RE2.GlobalReplace(self, *args)
70
+ dup.re2_gsub!(*args)
40
71
  end
41
72
 
42
73
  # Match the pattern and return either a boolean (if no submatches are required)
@@ -6,16 +6,48 @@ class String
6
6
  end
7
7
 
8
8
  describe RE2::String do
9
+ describe "#re2_sub!" do
10
+ it "delegates to RE2.Replace to perform replacement" do
11
+ "My name is Robert Paulson".re2_sub!('Robert', 'Crobert').must_equal("My name is Crobert Paulson")
12
+ end
13
+
14
+ it "doe perform an in-place replacement" do
15
+ string = "My name is Robert Paulson"
16
+ string.re2_sub!('Robert', 'Crobert').must_be_same_as(string)
17
+ end
18
+ end
19
+
20
+ describe "#re2_gsub!" do
21
+ it "delegates to RE2.GlobalReplace to perform replacement" do
22
+ "My name is Robert Paulson".re2_gsub!('a', 'e').must_equal("My neme is Robert Peulson")
23
+ end
24
+
25
+ it "doe perform an in-place replacement" do
26
+ string = "My name is Robert Paulson"
27
+ string.re2_gsub!('a', 'e').must_be_same_as(string)
28
+ end
29
+ end
30
+
9
31
  describe "#re2_sub" do
10
32
  it "delegates to RE2.Replace to perform replacement" do
11
33
  "My name is Robert Paulson".re2_sub('Robert', 'Crobert').must_equal("My name is Crobert Paulson")
12
34
  end
35
+
36
+ it "doesn't perform an in-place replacement" do
37
+ string = "My name is Robert Paulson"
38
+ string.re2_sub('Robert', 'Crobert').wont_be_same_as(string)
39
+ end
13
40
  end
14
41
 
15
42
  describe "#re2_gsub" do
16
43
  it "delegates to RE2.GlobalReplace to perform replacement" do
17
44
  "My name is Robert Paulson".re2_gsub('a', 'e').must_equal("My neme is Robert Peulson")
18
45
  end
46
+
47
+ it "doesn't perform an in-place replacement" do
48
+ string = "My name is Robert Paulson"
49
+ string.re2_gsub('a', 'e').wont_be_same_as(string)
50
+ end
19
51
  end
20
52
 
21
53
  describe "#re2_match" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: re2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-06 00:00:00.000000000 Z
12
+ date: 2012-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler
16
- requirement: &70129597606260 !ruby/object:Gem::Requirement
16
+ requirement: &70127849363640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70129597606260
24
+ version_requirements: *70127849363640
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &70129597605760 !ruby/object:Gem::Requirement
27
+ requirement: &70127849363160 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70129597605760
35
+ version_requirements: *70127849363160
36
36
  description: Ruby bindings to re2, "an efficient, principled regular expression library".
37
37
  email: ruby.re2@librelist.com
38
38
  executables: []