Sha256: 5d058bdd03850573d555925de534028d5f42f8c74b994060fdd1b459e2d305a6

Contents?: true

Size: 1.31 KB

Versions: 34

Compression:

Stored size: 1.31 KB

Contents

module Fig; end
class  Fig::TokenizedString; end

class Fig::TokenizedString::PlainSegment
  attr_reader :raw_value

  def initialize(raw_value)
    @raw_value     = raw_value

    return
  end

  def type
    return nil
  end

  def to_expanded_string(&block)
    return @raw_value.gsub(%r< \\ (.) >xm, '\1')
  end

  def to_escaped_string()
    return @raw_value
  end

  # Should not be invoked if original string was single quoted.
  def to_single_quoted_string()
    # Raw value will have come from a non-single quoted string, so we unescape
    # everything (including backslashes) and then escape backslashes and single
    # quotes (which cannot be escaped outside of single quotes).
    return \
      @raw_value.gsub(%r< \\ (.) >xm, '\1').gsub(%r< ([\\']) >xm, '\\\\\1')
  end

  # Should not be invoked if original string was not single quoted.
  def to_double_quotable_string(metacharacters)
    quoted_value = @raw_value.gsub %r< ( ["#{metacharacters}] ) >xm, '\\\\\1'

    quoted_value.gsub!(
      %r<
        (
          (?: ^ | [^\\] ) # New line or non-backslash
          (\\{2})*        # Even number of backslashes
        )

        # All single quotes must have been escaped.  The important bit is to
        # not lose escaped backslashes.
        \\'
      >xm,
      %q<\1'>
    )

    return quoted_value
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
fig-1.23.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.22.1.beta.1 lib/fig/tokenized_string/plain_segment.rb
fig-1.22.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.21.1.beta.2 lib/fig/tokenized_string/plain_segment.rb
fig-1.21.1.beta.1 lib/fig/tokenized_string/plain_segment.rb
fig-1.21.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.20.1.beta.1 lib/fig/tokenized_string/plain_segment.rb
fig-1.20.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.19.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.18.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.17.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.16.1.beta.1 lib/fig/tokenized_string/plain_segment.rb
fig-1.16.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.15.1.beta.2 lib/fig/tokenized_string/plain_segment.rb
fig-1.15.1.beta.1 lib/fig/tokenized_string/plain_segment.rb
fig-1.15.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.14.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.13.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.12.0 lib/fig/tokenized_string/plain_segment.rb
fig-1.11.0 lib/fig/tokenized_string/plain_segment.rb