Class: String

Inherits:
Object show all
Includes:
Rubylog::CompoundTerm
Defined in:
lib/rubylog/mixins/string.rb

Constant Summary collapse

RUBYLOG_VAR_START =

“u00ab”

"\u0091"
RUBYLOG_VAR_END =

“u00bb”

"\u0092"
RUBYLOG_VAR_REGEXP =
/#{RUBYLOG_VAR_START}([^\[]+?)\[(\d*)\]#{RUBYLOG_VAR_END}/
RubylogStringVariableGuards =
[[]]

Instance Attribute Summary

Attributes included from Rubylog::CompoundTerm

#rubylog_variables

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rubylog::CompoundTerm

#rubylog_match_variables

Class Method Details

.rubylog_unify_strings(a, a_segments, a_vars, b) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubylog/mixins/string.rb', line 8

def self.rubylog_unify_strings a, a_segments, a_vars, b
  if a_segments.count == 1
    segment = a_segments[0]
    if b.end_with?(segment)
      a_vars[0].rubylog_unify b[0...b.length-segment.length] do
        yield
      end
    end
  else
    b.scan /#{Regexp.quote(a_segments[0])}/ do
      a_vars[0].rubylog_unify(b[0...Regexp.last_match.begin(0)]) do
        rubylog_unify_strings(a, a_segments[1..-1], a_vars[1..-1], b[Regexp.last_match.end(0)..-1]) do
          yield
        end
      end
    end
  end
end

Instance Method Details

#rubylog_clone(&block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rubylog/mixins/string.rb', line 51

def rubylog_clone &block
  scan RUBYLOG_VAR_REGEXP do
    guards = RubylogStringVariableGuards[$2.to_i] 
    Rubylog::Variable.new($1.to_sym)[*guards].rubylog_clone(&block)
  end
  block[self]
end

#rubylog_deep_dereferenceObject



59
60
61
62
63
# File 'lib/rubylog/mixins/string.rb', line 59

def rubylog_deep_dereference 
  gsub RUBYLOG_VAR_REGEXP do
    rubylog_get_string_variable($1,$2).rubylog_deep_dereference.to_s
  end
end

#rubylog_segmentsObject

returns a list of substrings which are before, between and after the rubylog string variables, and the list of variabes in between



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubylog/mixins/string.rb', line 68

def rubylog_segments
  segments = [[0]]
  vars = []

  scan RUBYLOG_VAR_REGEXP do
    match = Regexp.last_match
    segments.last << match.begin(0)
    segments << [match.end(0)]
    vars << rubylog_get_string_variable(match[1], match[2])
  end
  

  segments.last << length
  segments = segments.map{|s|self[s[0]...s[1]]}
  return segments, vars
end

#rubylog_unify(other) ⇒ Object

Term methods

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubylog/mixins/string.rb', line 28

def rubylog_unify other
  return super{yield} unless other.instance_of? self.class

  self_has_vars = self =~ RUBYLOG_VAR_REGEXP
  other_has_vars = other =~ RUBYLOG_VAR_REGEXP

  return super{yield} unless self_has_vars or other_has_vars
  raise ArgumentError, "Cannot unify two strings with variables inside" if self_has_vars and other_has_vars

  a, b = self_has_vars ? [self, other] : [other, self]
  a_segments, a_vars = a.rubylog_segments

  return unless b.start_with? a_segments[0]
  b = b[a_segments[0].length..-1]; a_segments.shift

  String.rubylog_unify_strings(a, a_segments, a_vars, b) do
    yield
  end

end