Sha256: 3173fadc6b989cf50ad52eb2d5cc0b281f66d20fad1abf6b513b4da3b46c6f03

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module ProperProperties
  module Encoding
    # Module to escape separators as : or =
    # @see ProperProperties::Encoding
    module Separators

      # Marker for all separators
      # @return [Regexp]
      ENCODE_SEPARATOR_MARKER = /[ :=]/

      # Marker for already escaped separators
      # @return [Regexp]
      ESCAPING_MARKER  = /\\/

      # Char to use for escaping
      # @return [String]
      ESCAPE           = "\\"

      # Marker for all escaped separators
      # @return [Regexp]
      DECODE_SEPARATOR_MARKER = /\\([ :=])/

      # Escapes all not already escaped separators
      # @param text [text]
      # @return [String] The escaped text for chaining
      def self.encode!(text)
        buffer = StringIO.new
        last_token = ''
        text.each_char do |char|
          if char =~ ENCODE_SEPARATOR_MARKER && last_token !~ ESCAPING_MARKER
            buffer << ESCAPE
          end
          buffer << char
          last_token = char
        end
        text.replace buffer.string
        text
      end

      # Removes escapes from escaped separators
      # @param text [text]
      # @return [String] The unescaped text for chaining
      def self.decode!(text)
        text.gsub!(DECODE_SEPARATOR_MARKER) do
          $1
        end
        text
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proper_properties-0.0.2 lib/proper_properties/encoding/separators.rb
proper_properties-0.0.1 lib/proper_properties/encoding/separators.rb