Skip to content

Commit 6431c3a

Browse files
author
Tom Lord
committed
Improved character sets
1 parent b822144 commit 6431c3a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

lib/regexp-examples/constants.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ module CharSets
3535
Lower = Array('a'..'z')
3636
Upper = Array('A'..'Z')
3737
Digit = Array('0'..'9')
38-
Punct = [33..47, 58..64, 91..96, 123..126].map { |r| r.map { |val| val.chr } }.flatten
38+
Punct = [33..47, 58..64, 91..96, 123..126].map { |r| r.map(&:chr) }.flatten
3939
Hex = Array('a'..'f') | Array('A'..'F') | Digit
40+
Word = Lower | Upper | Digit | ['_']
4041
Whitespace = [' ', "\t", "\n", "\r", "\v", "\f"]
41-
Any = Lower | Upper | Digit | Punct
42-
end
42+
# Ensure that the "common" characters appear first in the array. Do not include "\n"!
43+
Any = Lower | Upper | Digit | Punct | (0..255).map(&:chr) - ["\n"]
44+
end.freeze
4345

4446
# Map of special regex characters, to their associated character sets
4547
BackslashCharMap = {
4648
'd' => CharSets::Digit,
47-
'D' => CharSets::Lower | CharSets::Upper | CharSets::Punct,
48-
'w' => CharSets::Lower | CharSets::Upper | CharSets::Digit | ['_'],
49-
'W' => CharSets::Punct.reject { |val| val == '_' },
49+
'D' => CharSets::Any - CharSets::Digit,
50+
'w' => CharSets::Word,
51+
'W' => CharSets::Any - CharSets::Word,
5052
's' => CharSets::Whitespace,
5153
'S' => CharSets::Any - CharSets::Whitespace,
5254
'h' => CharSets::Hex,
@@ -59,6 +61,6 @@ module CharSets
5961
'a' => ["\a"], # alarm
6062
'v' => ["\v"], # vertical tab
6163
'e' => ["\e"], # escape
62-
}
64+
}.freeze
6365
end
6466

0 commit comments

Comments
 (0)