Skip to content

Commit

Permalink
adapt code to not modify parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Jul 8, 2019
1 parent 1ad42d9 commit aa28312
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/cfa/augeas_parser/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,21 @@ def find_preceding_index(paths, preceding)
new_path = +"/"
path.split("/").each do |element|
new_path << "/" unless new_path.end_with?("/")
pick_candidate(paths, new_path, element)
new_path << pick_candidate(paths, new_path, element)
end

paths.index(new_path) ||
raise("Cannot find path #{preceding.path} in #{paths.inspect}")
end

# it returns variant of element that exists in path
def pick_candidate(paths, new_path, element)
# NOTE: order here is important due to early matching
candidates = [element + "/", element + "[1]",
element.sub(/\[\d+\]/, ""), element]
paths.any? do |p|
candidates.any? do |c|
if p.start_with?(new_path + c)
new_path << c
true
end
paths.each do |p|
candidates.each do |c|
return c if p.start_with?(new_path + c)
end
end
end
Expand Down

0 comments on commit aa28312

Please sign in to comment.