Sha256: 3cdd11e2d78908e4e7384c4f4a80559c2994275774df683df583f10a97d6e219
Contents?: true
Size: 1.26 KB
Versions: 2
Compression:
Stored size: 1.26 KB
Contents
module ProperProperties module Encoding # Module to escape and unescape special chars # @see ProperProperties::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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
proper_properties-0.0.2 | lib/proper_properties/encoding/special_chars.rb |
proper_properties-0.0.1 | lib/proper_properties/encoding/special_chars.rb |