Created
November 10, 2010 22:24
-
-
Save ammar/671652 to your computer and use it in GitHub Desktop.
Another take on Ctype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# defines character type constants (as arrays) and methods that test | |
# whether a given character belongs in one of them. | |
module CType | |
Digit = ('0'..'9').to_a.freeze | |
Lower = ('a'..'z').to_a.freeze | |
Upper = ('A'..'Z').to_a.freeze | |
Alpha = [Lower, Upper].flatten.freeze | |
Alnum = [Alpha, Digit].flatten.freeze | |
Word = [Alnum, '_'].flatten.freeze | |
Blank = [' ', "\t"].freeze | |
Space = [" ", "\t", "\r", "\n", "\v", "\f"].freeze | |
Cntrl = ( 0..31 ).map {|c| c.chr}.freeze | |
Graph = (33..126).map {|c| c.chr}.freeze | |
Print = (32..126).map {|c| c.chr}.freeze | |
ASCII = ( 0..127).map {|c| c.chr}.freeze | |
Punct = [ | |
('!'..'/').to_a, | |
(':'..'@').to_a, | |
('['..'`').to_a, | |
('{'..'~').to_a | |
].flatten.freeze | |
XDigit = [ | |
Digit, | |
('a'..'f').to_a, | |
('A'..'F').to_a | |
].flatten.freeze | |
def self.alnum?(c); Alnum.include?(c) end | |
def self.alpha?(c); Alpha.include?(c) end | |
def self.blank?(c); Blank.include?(c) end | |
def self.cntrl?(c); Cntrl.include?(c) end | |
def self.digit?(c); Digit.include?(c) end | |
def self.graph?(c); Graph.include?(c) end | |
def self.lower?(c); Lower.include?(c) end | |
def self.print?(c); Print.include?(c) end | |
def self.punct?(c); Punct.include?(c) end | |
def self.space?(c); Space.include?(c) end | |
def self.upper?(c); Upper.include?(c) end | |
def self.xdigit?(c); XDigit.include?(c) end | |
def self.word?(c); Word.include?(c) end | |
def self.ascii?(c); ASCII.include?(c) end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test/unit" | |
require File.expand_path('../ctype', __FILE__) | |
class TestCharTypes < Test::Unit::TestCase | |
def test_ctype_alnum | |
assert_equal( true, CType.alnum?('a') ) | |
assert_equal( true, CType.alnum?('7') ) | |
assert_equal( false, CType.alnum?(' ') ) | |
end | |
def test_ctype_alpha | |
assert_equal( true, CType.alpha?('a') ) | |
assert_equal( true, CType.alpha?('X') ) | |
assert_equal( false, CType.alpha?('7') ) | |
assert_equal( false, CType.alpha?(' ') ) | |
end | |
def test_ctype_blank | |
assert_equal( true, CType.blank?(' ') ) | |
assert_equal( true, CType.blank?("\t") ) | |
assert_equal( false, CType.blank?('a') ) | |
assert_equal( false, CType.blank?('X') ) | |
assert_equal( false, CType.blank?('7') ) | |
end | |
def test_ctype_cntrl | |
assert_equal( true, CType.cntrl?("\x00") ) | |
assert_equal( true, CType.cntrl?("\x1f") ) | |
assert_equal( false, CType.cntrl?("\x3f") ) | |
end | |
def test_ctype_digit | |
assert_equal( true, CType.digit?('3') ) | |
assert_equal( false, CType.digit?(" ") ) | |
assert_equal( false, CType.digit?("\x00") ) | |
end | |
def test_ctype_graph | |
assert_equal( true, CType.graph?('3') ) | |
assert_equal( false, CType.graph?(' ') ) | |
end | |
def test_ctype_lower | |
assert_equal( true, CType.lower?('a') ) | |
assert_equal( true, CType.lower?('z') ) | |
assert_equal( false, CType.lower?('A') ) | |
assert_equal( false, CType.lower?('G') ) | |
assert_equal( false, CType.lower?(' ') ) | |
end | |
def test_ctype_print | |
assert_equal( true, CType.print?(' ') ) | |
assert_equal( true, CType.print?("\\") ) | |
assert_equal( true, CType.print?("#") ) | |
assert_equal( false, CType.print?("\x10") ) | |
end | |
def test_ctype_punct | |
assert_equal( true, CType.punct?('!') ) | |
assert_equal( true, CType.punct?('#') ) | |
assert_equal( false, CType.punct?('a') ) | |
assert_equal( false, CType.punct?(' ') ) | |
assert_equal( false, CType.punct?("\x80") ) | |
end | |
def test_ctype_space | |
assert_equal( true, CType.space?(' ') ) | |
assert_equal( true, CType.space?("\t") ) | |
assert_equal( false, CType.space?('a') ) | |
assert_equal( false, CType.space?('z') ) | |
end | |
def test_ctype_upper | |
assert_equal( true, CType.upper?('A') ) | |
assert_equal( true, CType.upper?('G') ) | |
assert_equal( false, CType.upper?('a') ) | |
assert_equal( false, CType.upper?('z') ) | |
end | |
def test_ctype_xdigit | |
assert_equal( true, CType.xdigit?('5') ) | |
assert_equal( true, CType.xdigit?('A') ) | |
assert_equal( true, CType.xdigit?('F') ) | |
assert_equal( false, CType.xdigit?('h') ) | |
assert_equal( false, CType.xdigit?('z') ) | |
end | |
def test_ctype_word | |
assert_equal( true, CType.word?('5') ) | |
assert_equal( true, CType.word?('A') ) | |
assert_equal( true, CType.word?('F') ) | |
assert_equal( true, CType.word?('_') ) | |
assert_equal( false, CType.word?(' ') ) | |
assert_equal( false, CType.word?("\t") ) | |
assert_equal( false, CType.word?("-") ) | |
end | |
def test_ctype_ascii | |
assert_equal( true, CType.ascii?('1') ) | |
assert_equal( true, CType.ascii?('d') ) | |
assert_equal( true, CType.ascii?('X') ) | |
assert_equal( true, CType.ascii?('_') ) | |
assert_equal( true, CType.ascii?('&') ) | |
assert_equal( false, CType.ascii?("\x8F") ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment