Sha256: 04977ba2dbe3dfb46b96f291cb7853958dcd8c1be490abc32c90c1710c18fb31

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

module JavaProperties
  module Encoding
    # Module to escape and unescape special chars
    # @see JavaProperties::Encoding
    module SpecialChars

      # Lookup table for escaping special chars
      # @return [Hash]
      ESCAPING = {
        "\t" => '\\t',
        "\r" => '\\r',
        "\n" => '\\n',
        "\f" => '\\f'
      }.freeze

      # Lookup table to remove escaping from special chars
      # @return [Hash]
      DESCAPING = ESCAPING.invert.freeze

      # Marks a segment which has is an encoding special char
      # @return [Regexp]
      DESCAPING_MARKER = /\\./

      # Encodes the content a text by escaping all special chars
      # @param text [String]
      # @return [String] The escaped text for chaining
      def self.encode!(text)
        buffer = StringIO.new
        text.each_char do |char|
          buffer << ESCAPING.fetch(char, char)
        end
        text.replace buffer.string
        text
      end

      # Decodes the content a text by removing all escaping from special chars
      # @param text [String]
      # @return [String] The unescaped text for chaining
      def self.decode!(text)
        text.gsub!(DESCAPING_MARKER) do |match|
          DESCAPING.fetch(match, match)
        end
        text
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
java-properties-0.2.0 lib/java-properties/encoding/special_chars.rb
java-properties-0.1.1 lib/java-properties/encoding/special_chars.rb
java-properties-0.1.0 lib/java-properties/encoding/special_chars.rb
ruby-properties-file-0.0.2 lib/java-properties/encoding/special_chars.rb