regexp-examples 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,11 +54,16 @@ module RegexpExamples
54
54
  when BackslashCharMap.keys.include?(regexp_string[@current_position])
55
55
  group = CharGroup.new(
56
56
  BackslashCharMap[regexp_string[@current_position]])
57
- when rest_of_string =~ /\Ac(.)/ # Control character
58
- @current_position += 1
59
- group = parse_single_char_group( parse_control_character($1) )
60
- # TODO: There are also a bunch of multi-char matches to watch out for:
57
+ when rest_of_string =~ /\A(c|C-)(.)/ # Control character
61
58
  # http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals
59
+ @current_position += $1.length
60
+ group = parse_single_char_group( parse_control_character($2) )
61
+ when rest_of_string =~ /\Ax(\h{1,2})/ # Escape sequence
62
+ @current_position += $1.length
63
+ group = parse_single_char_group( parse_escape_sequence($1) )
64
+ when rest_of_string =~ /\Au(\h{4})/ # Unicode sequence
65
+ @current_position += 4
66
+ group = parse_single_char_group( parse_unicode_sequence($1) )
62
67
  else
63
68
  group = parse_single_char_group( regexp_string[@current_position] )
64
69
  # TODO: What about cases like \A, \z, \Z ?
@@ -152,8 +157,16 @@ module RegexpExamples
152
157
  end
153
158
 
154
159
  def parse_control_character(char)
155
- # TODO: Is there a better way of doing this? I.e. not just brute-force
156
- (0..255).detect { |x| x.chr =~ Regexp.new("\\c#{char}") }.chr
160
+ (char.ord % 32).chr # Black magic!
161
+ # eval "?\\C-#{char.chr}" # Doesn't work for e.g. char = "?"
162
+ end
163
+
164
+ def parse_escape_sequence(match)
165
+ eval "?\\x#{match}"
166
+ end
167
+
168
+ def parse_unicode_sequence(match)
169
+ eval "?\\u#{match}"
157
170
  end
158
171
 
159
172
  def parse_star_repeater(group)
@@ -1,3 +1,3 @@
1
1
  module RegexpExamples
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -129,8 +129,27 @@ RSpec.describe Regexp, "#examples" do
129
129
  /\c9/,
130
130
  /\c[/,
131
131
  /\c#/,
132
- /\c?/
132
+ /\c?/,
133
+ /\C-a/,
134
+ /\C-&/
133
135
  )
134
136
  end
137
+
138
+ context "for escape sequences" do
139
+ examples_exist_and_match(
140
+ /\x42/,
141
+ /\x1D/,
142
+ /\x3word/,
143
+ /#{"\x80".force_encoding("ASCII-8BIT")}/
144
+ )
145
+ end
146
+
147
+ context "for unicode sequences" do
148
+ examples_exist_and_match(
149
+ /\u6829/,
150
+ /\uabcd/
151
+ )
152
+ end
153
+
135
154
  end
136
155
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regexp-examples
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lord
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  requirements: []
114
114
  rubyforge_project:
115
- rubygems_version: 2.2.2
115
+ rubygems_version: 2.4.5
116
116
  signing_key:
117
117
  specification_version: 4
118
118
  summary: Extends the Regexp class with '#examples'