Created
October 28, 2010 09:08
-
-
Save ammar/650968 to your computer and use it in GitHub Desktop.
Character type identification module
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
module CType | |
def self.alnum?(c); c =~ /[[:alnum:]]/ ? true : false end | |
def self.alpha?(c); c =~ /[[:alpha:]]/ ? true : false end | |
def self.blank?(c); c =~ /[[:blank:]]/ ? true : false end | |
def self.cntrl?(c); c =~ /[[:cntrl:]]/ ? true : false end | |
def self.digit?(c); c =~ /[[:digit:]]/ ? true : false end | |
def self.graph?(c); c =~ /[[:graph:]]/ ? true : false end | |
def self.lower?(c); c =~ /[[:lower:]]/ ? true : false end | |
def self.print?(c); c =~ /[[:print:]]/ ? true : false end | |
def self.punct?(c); c =~ /[[:punct:]]/ ? true : false end | |
def self.space?(c); c =~ /[[:space:]]/ ? true : false end | |
def self.upper?(c); c =~ /[[:upper:]]/ ? true : false end | |
def self.xdigit?(c); c =~ /[[:xdigit:]]/ ? true : false end | |
def self.word?(c); c =~ /[[:alnum:]_]/ ? true : false end | |
def self.ascii?(c); c =~ /[\x00-\x7F]/ ? true : false 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