Sha256: 84fdf41ed7f1c699f9d251f5f551a39237820383bb1e23d6309c0441ac9f0aff

Contents?: true

Size: 1.92 KB

Versions: 71

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module RuboCop
  # Take a string with embedded escapes, and convert the escapes as the Ruby
  # interpreter would when reading a double-quoted string literal.
  # For example, "\\n" will be converted to "\n".
  class StringInterpreter
    STRING_ESCAPES = {
      '\a' => "\a", '\b' => "\b", '\e' => "\e", '\f' => "\f", '\n' => "\n",
      '\r' => "\r", '\s' => ' ',  '\t' => "\t", '\v' => "\v", "\\\n" => ''
    }.freeze
    STRING_ESCAPE_REGEX = /\\(?:
                            [abefnrstv\n]     |   # simple escapes (above)
                            \d{1,3}           |   # octal byte escape
                            x[0-9a-fA-F]{1,2} |   # hex byte escape
                            u[0-9a-fA-F]{4}   |   # unicode char escape
                            u\{[^}]*\}        |   # extended unicode escape
                            .                     # any other escaped char
                          )/x.freeze

    private_constant :STRING_ESCAPES, :STRING_ESCAPE_REGEX

    class << self
      def interpret(string)
        # We currently don't handle \cx, \C-x, and \M-x
        string.gsub(STRING_ESCAPE_REGEX) do |escape|
          STRING_ESCAPES[escape] || interpret_string_escape(escape)
        end
      end

      private

      def interpret_string_escape(escape)
        case escape[1]
        when 'u' then interpret_unicode(escape)
        when 'x' then interpret_hex(escape)
        when /\d/       then interpret_octal(escape)
        else
          escape[1] # literal escaped char, like \\
        end
      end

      def interpret_unicode(escape)
        if escape[2] == '{'
          escape[3..-1].split(/\s+/).map(&:hex).pack('U*')
        else
          [escape[2..-1].hex].pack('U')
        end
      end

      def interpret_hex(escape)
        [escape[2..-1].hex].pack('C')
      end

      def interpret_octal(escape)
        [escape[1..-1].to_i(8)].pack('C')
      end
    end
  end
end

Version data entries

71 entries across 71 versions & 8 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/string_interpreter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/string_interpreter.rb
rubocop-1.28.2 lib/rubocop/string_interpreter.rb
rubocop-1.28.1 lib/rubocop/string_interpreter.rb
rubocop-1.28.0 lib/rubocop/string_interpreter.rb
rubocop-1.27.0 lib/rubocop/string_interpreter.rb
rubocop-1.26.1 lib/rubocop/string_interpreter.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/string_interpreter.rb
rubocop-1.26.0 lib/rubocop/string_interpreter.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/string_interpreter.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/string_interpreter.rb
rubocop-1.25.1 lib/rubocop/string_interpreter.rb
rubocop-1.25.0 lib/rubocop/string_interpreter.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/string_interpreter.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/string_interpreter.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/string_interpreter.rb
rubocop-1.24.1 lib/rubocop/string_interpreter.rb
rubocop-1.24.0 lib/rubocop/string_interpreter.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/string_interpreter.rb
rubocop-1.23.0 lib/rubocop/string_interpreter.rb