Module: IRCSupport::Case

Defined in:
lib/ircsupport/case.rb

Constant Summary collapse

@@ascii_map =
['a-z', 'A-Z']
@@rfc1459_map =
['a-z{}^|', 'A-Z[]~\\']
@@strict_rfc1459_map =
['a-z{}|', 'A-Z[]\\']

Class Method Summary collapse

Class Method Details

.irc_downcase(irc_string, casemapping = :rfc1459) ⇒ String

Turn a string into IRC lower case.

Parameters:

  • irc_string (String)

    An IRC string (nickname, channel, etc).

  • casemapping (Symbol) (defaults to: :rfc1459)

    An IRC casemapping.

Returns:

  • (String)

    A lower case version of the IRC string according to the casemapping



68
69
70
71
72
# File 'lib/ircsupport/case.rb', line 68

def irc_downcase(irc_string, casemapping = :rfc1459)
  result = irc_string.dup
  irc_downcase!(result, casemapping)
  return result
end

.irc_downcase!(irc_string, casemapping = :rfc1459) ⇒ String

Turn a string into IRC lower case, modifying it in place.

Parameters:

  • irc_string (String)

    An IRC string (nickname, channel, etc)

  • casemapping (Symbol) (defaults to: :rfc1459)

    An IRC casemapping

Returns:

  • (String)

    A lower case version of the IRC string according to the casemapping



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ircsupport/case.rb', line 48

def irc_downcase!(irc_string, casemapping = :rfc1459)
  case casemapping
  when :ascii
    irc_string.tr!(*@@ascii_map.reverse)
  when :rfc1459
    irc_string.tr!(*@@rfc1459_map.reverse)
  when :'strict-rfc1459'
    irc_string.tr!(*@@strict_rfc1459_map.reverse)
  else
    raise ArgumentError, "Unsupported casemapping #{casemapping}"
  end

  return irc_string
end

.irc_eql?(first, second, casemapping = :rfc1459) ⇒ Boolean

Check an IRC identifier (nick, channel) for equality.

Parameters:

  • first (String)

    The first IRC string to compare.

  • second (String)

    The second IRC string to compare.

  • casemapping (Symbol) (defaults to: :rfc1459)

    The IRC casemappig to use for the comparison.

Returns:

  • (Boolean)

    Will be ‘true` if the strings only differ in case or not all, but `false` otherwise.



80
81
82
# File 'lib/ircsupport/case.rb', line 80

def irc_eql?(first, second, casemapping = :rfc1459)
  return irc_upcase(first, casemapping) == irc_upcase(second, casemapping)
end

.irc_upcase(irc_string, casemapping = :rfc1459) ⇒ String

Turn a string into IRC upper case.

Parameters:

  • irc_string (String)

    An IRC string (nickname, channel, etc)

  • casemapping (Symbol) (defaults to: :rfc1459)

    An IRC casemapping

Returns:

  • (String)

    An upper case version of the IRC string according to the casemapping.



37
38
39
40
41
# File 'lib/ircsupport/case.rb', line 37

def irc_upcase(irc_string, casemapping = :rfc1459)
  result = irc_string.dup
  irc_upcase!(result, casemapping)
  return result
end

.irc_upcase!(irc_string, casemapping = :rfc1459) ⇒ String

Turn a string into IRC upper case, modifying it in place.

Parameters:

  • irc_string (String)

    An IRC string (nickname, channel, etc).

  • casemapping (Symbol) (defaults to: :rfc1459)

    An IRC casemapping.

Returns:

  • (String)

    An upper case version of the IRC string according to the casemapping.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ircsupport/case.rb', line 17

def irc_upcase!(irc_string, casemapping = :rfc1459)
  case casemapping
  when :ascii
    irc_string.tr!(*@@ascii_map)
  when :rfc1459
    irc_string.tr!(*@@rfc1459_map)
  when :'strict-rfc1459'
    irc_string.tr!(*@@strict_rfc1459_map)
  else
    raise ArgumentError, "Unsupported casemapping #{casemapping}"
  end

  return irc_string
end