class BaseConvert::Chars

Chars provides ways of populating an ordered(as constructed) set with characters in UTF-8.

Attributes

start[RW]
stop[RW]

Public Class Methods

new() click to toggle source
# File lib/base_convert/chars.rb, line 5
def initialize
  @start,@stop,@a = 32,126,[]
end

Public Instance Methods

add(x) click to toggle source
# File lib/base_convert/chars.rb, line 47
  def add(x)    = chars_in(x){@a.push _1 unless @a.include?_1}
  def top(x)    = chars_in(x){@a.delete _1;@a.push _1}
  def remove(x) = chars_in(x){@a.delete _1}
end
chars_in(x) { |c| ... } click to toggle source
# File lib/base_convert/chars.rb, line 32
def chars_in(x)
  case x
  when Regexp
    (@start..@stop).each{x.match?(c=_1.chr(Encoding::UTF_8)) && yield(c)}
  when Symbol
    yield x[1..-1].to_i((x[0]=='u')? 16: 10).chr(Encoding::UTF_8)
  when String
    x.chars.each{|c| yield c}
  when Integer
    yield x.chr(Encoding::UTF_8)
  else
    raise "expected Regexp|Symbol|String|Integer, got #{x.class}"
  end
end
remove(x) click to toggle source
# File lib/base_convert/chars.rb, line 49
  def remove(x) = chars_in(x){@a.delete _1}
end
set(s) click to toggle source

Chars can search a range in UTF-8 for character classes. Use Chars#set to set the start and stop of the range. i<n>: @start=n.to_i v<n>: @start=n.to_i(16) j<n>: @stop=n.to_i w<n>: @stop=n.to_i(16)

# File lib/base_convert/chars.rb, line 19
def set(s)
  unless /^([ij]\d+)|([vw]\h+)$/.match?(s)
    raise 'expected /^([ij]\d+)|([vw]\h+)$/'
  end
  t,n = s[0],s[1..-1]
  case t
  when 'i','v'
    @start = n.to_i((t=='v')? 16 : 10)
  when 'j','w'
    @stop = n.to_i((t=='w')? 16 : 10)
  end
end
to_a(= @a) click to toggle source
# File lib/base_convert/chars.rb, line 10
def to_a = @a
def to_s = @a.join

# Chars can search a range in UTF-8 for character classes.
# Use Chars#set to set the start and stop of the range.
# i<n>: @start=n.to_i
# v<n>: @start=n.to_i(16)
# j<n>: @stop=n.to_i
# w<n>: @stop=n.to_i(16)
def set(s)
  unless /^([ij]\d+)|([vw]\h+)$/.match?(s)
    raise 'expected /^([ij]\d+)|([vw]\h+)$/'
  end
  t,n = s[0],s[1..-1]
  case t
  when 'i','v'
    @start = n.to_i((t=='v')? 16 : 10)
  when 'j','w'
    @stop = n.to_i((t=='w')? 16 : 10)
  end
end

def chars_in(x)
  case x
  when Regexp
    (@start..@stop).each{x.match?(c=_1.chr(Encoding::UTF_8)) && yield(c)}
  when Symbol
    yield x[1..-1].to_i((x[0]=='u')? 16: 10).chr(Encoding::UTF_8)
  when String
    x.chars.each{|c| yield c}
  when Integer
    yield x.chr(Encoding::UTF_8)
  else
    raise "expected Regexp|Symbol|String|Integer, got #{x.class}"
  end
end

def add(x)    = chars_in(x){@a.push _1 unless @a.include?_1}
def top(x)    = chars_in(x){@a.delete _1;@a.push _1}
def remove(x) = chars_in(x){@a.delete _1
to_s(= @a.join) click to toggle source
# File lib/base_convert/chars.rb, line 11
def to_s = @a.join

# Chars can search a range in UTF-8 for character classes.
# Use Chars#set to set the start and stop of the range.
# i<n>: @start=n.to_i
# v<n>: @start=n.to_i(16)
# j<n>: @stop=n.to_i
# w<n>: @stop=n.to_i(16)
def set(s)
  unless /^([ij]\d+)|([vw]\h+)$/.match?(s)
    raise 'expected /^([ij]\d+)|([vw]\h+)$/'
  end
  t,n = s[0],s[1..-1]
  case t
  when 'i','v'
    @start = n.to_i((t=='v')? 16 : 10)
  when 'j','w'
    @stop = n.to_i((t=='w')? 16 : 10)
  end
end

def chars_in(x)
  case x
  when Regexp
    (@start..@stop).each{x.match?(c=_1.chr(Encoding::UTF_8)) && yield(c)}
  when Symbol
    yield x[1..-1].to_i((x[0]=='u')? 16: 10).chr(Encoding::UTF_8)
  when String
    x.chars.each{|c| yield c}
  when Integer
    yield x.chr(Encoding::UTF_8)
  else
    raise "expected Regexp|Symbol|String|Integer, got #{x.class}"
  end
end

def add(x)    = chars_in(x){@a.push _1 unless @a.include?_1}
def top(x)    = chars_in(x){@a.delete _1;@a.push _1}
def remove(x) = chars_in(x){@a.delete _1}
top(x) click to toggle source
# File lib/base_convert/chars.rb, line 48
    def top(x)    = chars_in(x){@a.delete _1;@a.push _1}
    def remove(x) = chars_in(x){@a.delete _1}
  end
end