Sha256: 1c601b405abf1d9d3266a3630be4511d36b2e70bd9342a19a56a1733017e6f50

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

module JavaProperties
  module Encoding
    # Module to escape separators as : or =
    # @see JavaProperties::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

6 entries across 6 versions & 2 rubygems

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