Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow node names to be suffixed with numbers

I had to remove the use of String#rindex and replace it with a custom
function that finds the earliest match that ends at a given index. For
example, take a string "a-b2" and a regex /\w[0-9]?/. String#rindex will
return 3, while the new String#index_of_earliest_match_ending_at will
return 2.
  • Loading branch information...
commit 6b8c6d1ce4b9eb73affc6c1ecec49df7ec667e7d 1 parent 8f84403
Adam Roben authored
Showing with 25 additions and 3 deletions.
  1. +3 −3 lib/asciidag.rb
  2. +9 −0 lib/coreext/string.rb
  3. +13 −0 test/parser_test.rb
6 lib/asciidag.rb
View
@@ -166,7 +166,7 @@ def self.find_parents(position, nodes_by_position, lines)
continue_search.call ord.chr, position, valid_direction
else
# This might be part of a multi-character node label.
- start_x = line.rindex(NODE_REGEXP, x)
+ start_x = line.index_of_earliest_match_ending_at NODE_REGEXP, x
return if start_x.nil? || start_x + line.substring_after(start_x)[NODE_REGEXP].length != x + 1
inner.call [start_x, y], '', valid_direction
end
@@ -174,8 +174,8 @@ def self.find_parents(position, nodes_by_position, lines)
continue_search.call(:initial, position, :either).flatten
end
- NODE_REGEXP = /\w['*]?/
- BRANCH_LABEL_REGEXP = /(?:(\w\w.*?)|"(.+?)")\s*$/
+ NODE_REGEXP = /\w[0-9'*]?/
+ BRANCH_LABEL_REGEXP = /(?:(\w{3,}.*?)|"(.+?)")\s*$/
ARROW_REGEXP = /\s+(<--)\s+$/
PIXELS_PER_CHARACTER_X = 25
PIXELS_PER_CHARACTER_Y = 40
9 lib/coreext/string.rb
View
@@ -1,4 +1,13 @@
class String
+ def index_of_earliest_match_ending_at(regexp, end_index)
+ (0..end_index).each do |index|
+ match = slice(index..end_index).match regexp
+ next if match.nil? || index + match[0].length != end_index + 1
+ return index
+ end
+ nil
+ end
+
def substring_after(index)
slice(index, length - index)
end
13 test/parser_test.rb
View
@@ -118,6 +118,12 @@
@graph16 = AsciiDag.parse <<'EOF'
---Z---o---X--...---o---A---o---o---Y*--...---o---B*--D*
EOF
+
+ @graph17 = AsciiDag.parse <<'EOF'
+ r1---r2---r3 remotes/git-svn
+ \
+ A---B master
+EOF
end
test 'parses nodes' do
@@ -293,4 +299,11 @@
d = find_node @graph16, 'D*'
assert_equal [b], d.parents
end
+
+ test 'should number-suffixed node names' do
+ r1 = find_node @graph17, 'r1'
+ r2 = find_node @graph17, 'r2'
+ assert_equal [], r1.parents
+ assert_equal [r1], r2.parents
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.