class XPath::XPathString

Public Class Methods

new(str) click to toggle source
# File lib/xml/xpath.rb, line 800
def initialize(str)
  raise ::TypeError, "must be a String" unless str.is_a? String
  @value = str
end

Public Instance Methods

concat(s) click to toggle source
# File lib/xml/xpath.rb, line 830
def concat(s)
  @value = @value + s
  self
end
contain?(s) click to toggle source
# File lib/xml/xpath.rb, line 839
def contain?(s)
  /#{Regexp.quote(s)}/ =~ @value
end
normalize_space() click to toggle source
# File lib/xml/xpath.rb, line 898
def normalize_space
  @value = @value.strip
  @value.gsub!(/\s+/, ' ')
  self
end
replace(str) click to toggle source
# File lib/xml/xpath.rb, line 914
def replace(str)
  @value = str
  self
end
size() click to toggle source
# File lib/xml/xpath.rb, line 894
def size
  @value.gsub(/[^\Wa-zA-Z_\d]/, ' ').size
end
start_with?(s) click to toggle source
# File lib/xml/xpath.rb, line 835
def start_with?(s)
  /\A#{Regexp.quote(s)}/ =~ @value
end
substring(start, len) click to toggle source
# File lib/xml/xpath.rb, line 861
def substring(start, len)
  start = start.round.to_f
  if start.infinite? or start.nan? then
    @value = ''
  elsif len then
    len = len.round.to_f
    maxlen = start + len
    len = maxlen - 1.0 if len >= maxlen
    if start <= 1.0 then
      start = 0
    else
      start = start.to_i - 1
    end
    if len.nan? or len < 1.0 then
      @value = ''
    elsif len.infinite? then
      # @value = @value[start..-1]
      /\A[\W\w]{0,#{start}}/ =~ @value
      @value = $'
    else
      # @value = @value[start, len.to_i]
      /\A[\W\w]{0,#{start}}([\W\w]{0,#{len.to_i}})/ =~ @value
      @value = $1
    end
  elsif start > 1.0 then
    # @value = @value[(start-1)..-1]
    /\A[\W\w]{0,#{start.to_i-1}}/ =~ @value
    @value = $'
  end
  raise "BUG" unless @value
  self
end
substring_after(s) click to toggle source
# File lib/xml/xpath.rb, line 852
def substring_after(s)
  if /#{Regexp.quote(s)}/ =~ @value then
    @value = $'
  else
    @value = ''
  end
  self
end
substring_before(s) click to toggle source
# File lib/xml/xpath.rb, line 843
def substring_before(s)
  if /#{Regexp.quote(s)}/ =~ @value then
    @value = $`
  else
    @value = ''
  end
  self
end
to_f() click to toggle source
# File lib/xml/xpath.rb, line 809
def to_f
  if /\A\s*(-?\d+\.?\d*)(?:\s|\z)/ =~ @value then
    $1.to_f
  else
    0.0 / 0.0  # NaN
  end
end
to_ruby() click to toggle source
# File lib/xml/xpath.rb, line 821
def to_ruby
  to_str
end
to_str() click to toggle source
# File lib/xml/xpath.rb, line 805
def to_str
  @value
end
to_string(context) click to toggle source
# File lib/xml/xpath.rb, line 825
def to_string(context)
  self
end
translate(from, to) click to toggle source
# File lib/xml/xpath.rb, line 904
def translate(from, to)
  to = to.split(//)
  h = {}
  from.split(//).each_with_index { |i,n|
    h[i] = to[n] unless h.key? i
  }
  @value = @value.gsub(/[#{Regexp.quote(h.keys.join)}]/) { |s| h[s] }
  self
end
true?() click to toggle source
# File lib/xml/xpath.rb, line 817
def true?
  not @value.empty?
end