Skip to content

Commit a9142be

Browse files
author
Tom Lord
committed
Defined POSIX char sets
1 parent 6431c3a commit a9142be

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/regexp-examples/constants.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ 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(&:chr) }.flatten
38+
# Chars in ranges: [33..47, 58..64, 91..96, 123..126]
39+
Punct = %w(] [ ! " # $ % & ' ( ) * + , . / : ; < = > ? @ \\ ^ _ ` { | } ~ -)
3940
Hex = Array('a'..'f') | Array('A'..'F') | Digit
4041
Word = Lower | Upper | Digit | ['_']
4142
Whitespace = [' ', "\t", "\n", "\r", "\v", "\f"]
43+
Control = (0..31).map(&:chr) | ["\x7f"]
4244
# Ensure that the "common" characters appear first in the array. Do not include "\n"!
4345
Any = Lower | Upper | Digit | Punct | (0..255).map(&:chr) - ["\n"]
4446
end.freeze
@@ -62,5 +64,22 @@ module CharSets
6264
'v' => ["\v"], # vertical tab
6365
'e' => ["\e"], # escape
6466
}.freeze
67+
68+
POSIXCharMap = {
69+
'alnum' => CharSets::Upper | CharSets::Lower | CharSets::Digit,
70+
'alpha' => CharSets::Upper | CharSets::Lower,
71+
'blank' => [" ", "\t"],
72+
'cntrl' => CharSets::Control,
73+
'digit' => CharSets::Digit,
74+
'graph' => (CharSets::Any - CharSets::Control) - [" "], # Visible chars
75+
'lower' => CharSets::Lower,
76+
'print' => CharSets::Any - CharSets::Control,
77+
'punct' => CharSets::Punct,
78+
'space' => CharSets::Whitespace,
79+
'upper' => CharSets::Upper,
80+
'xdigit' => CharSets::Hex,
81+
'word' => CharSets::Word,
82+
'ascii' => CharSets::Any | ["\n"],
83+
}.freeze
6584
end
6685

0 commit comments

Comments
 (0)