def format(input, pattern, options={})
      fill         = options[:fill]
      intl_prefix  = options[:intl_prefix]||''
      trunk_prefix = options[:trunk_prefix]||''
      slots        = pattern.count(PLACEHOLDER_CHAR)
      
      # Return original input if it is too long
      return input if (fill.nil? && input.length > slots)
      
      # Pad and clone the string if necessary.
      source = (fill.nil? || fill.empty?) ? input : input.ljust(slots, fill)
      
      result   = ''
      slot     = 0
      has_open = had_c = had_n = false
      
      pattern.split('').each_with_index do |chr, index|
        case chr
        when 'c'
          had_c = true
          result << intl_prefix
        when 'n'
          had_n = true
          result << trunk_prefix
        when '#'
          if slot < source.length
            result << source[slot]
            slot += 1
          else
            result << ' ' if has_open
          end
        when '('
          if slot < source.length
            has_open = true
            result << chr
          end
        when ')'
          if (slot < source.length || has_open)
            has_open = false
            result << chr
          end
        else
          # Don't show space after n if no trunk prefix or after c if no intl prefix
          next if (chr == ' ' && pattern[index-1] == 'n' && trunk_prefix.empty?)
          next if (chr == ' ' && pattern[index-1] == 'c' && intl_prefix.empty?)
          
          result << chr if (slot < source.length)
        end
      end

      # Not all format strings have a 'c' or 'n' in them.
      # If we have an international prefix or a trunk prefix but the format string
      # doesn't explictly say where to put it then simply add it to the beginning.
      result.prepend trunk_prefix if (!had_n && !trunk_prefix.empty?)
      result.prepend "#{intl_prefix} " if (!had_c && !intl_prefix.empty?)
      
      result.strip
    end
def format(input, pattern, options={})
      fill         = options[:fill]
      intl_prefix  = options[:intl_prefix]||''
      trunk_prefix = options[:trunk_prefix]||''
      slots        = pattern.count(PLACEHOLDER_CHAR)
      
      # Return original input if it is too long
      return input if (fill.nil? && input.length > slots)
      
      # Pad and clone the string if necessary.
      source = (fill.nil? || fill.empty?) ? input : input.ljust(slots, fill)
      
      result   = ''
      slot     = 0
      has_open = had_c = had_n = false
      
      pattern.split('').each_with_index do |chr, index|
        case chr
        when 'c'
          had_c = true
          result << intl_prefix
        when 'n'
          had_n = true
          result << trunk_prefix
        when '#'
          if slot < source.length
            result << source[slot]
            slot += 1
          else
            result << ' ' if has_open
          end
        when '('
          if slot < source.length
            has_open = true
            result << chr
          end
        when ')'
          if (slot < source.length || has_open)
            has_open = false
            result << chr
          end
        else
          # Don't show space after n if no trunk prefix or after c if no intl prefix
          next if (chr == ' ' && pattern[index-1] == 'n' && trunk_prefix.empty?)
          next if (chr == ' ' && pattern[index-1] == 'c' && intl_prefix.empty?)
          
          result << chr if (slot < source.length)
        end
      end

      # Not all format strings have a 'c' or 'n' in them.
      # If we have an international prefix or a trunk prefix but the format string
      # doesn't explictly say where to put it then simply add it to the beginning.
      result.prepend trunk_prefix if (!had_n && !trunk_prefix.empty?)
      result.prepend "#{intl_prefix} " if (!had_c && !intl_prefix.empty?)
      
      result.strip
    end
Returns the string formatted according to a pattern.

 Examples:
 format('123456789', 'XXX-XX-XXXX')
    => "123-45-6789"
 format('12345', 'XXX-XX-XXXX')
    => "123-45"

 Parameters:
 string  -- The string to be formatted.
 pattern -- The format string, see above examples.
 fill    -- A string for padding. If the empty string, then the pattern is
            filled as much as possible, and the rest of the pattern is
            truncated. If nil, and the string is too long for the pattern,
            the string is returned unchanged.  Otherwise, the string is
            padded to fill the pattern, which is not truncated.