Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset default chapter and verse at semicolons when parsing references #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/pericope/parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def match_all(text)
end

def parse_reference(book, reference)
parse_ranges(book, normalize_reference(reference).split(/[,;]/))
# Use positive lookahead to keep delimiter with the fragment that follows
parse_ranges(book, normalize_reference(reference).split(/(?=[,;])/))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that the positive lookahead does impact performance — but not by a lot

Before
PERFORMANCE
  0.021496   0.000342   0.021838 (  0.021919)
  0.191024   0.001824   0.192848 (  0.193286)
  1.908849   0.009966   1.918815 (  1.924357)
After
PERFORMANCE
  0.022117   0.000469   0.022586 (  0.022739)
  0.252090   0.004195   0.256285 (  0.258896)
  2.136289   0.012225   2.148514 (  2.161139)

end

def normalize_reference(reference)
Expand All @@ -93,8 +94,12 @@ def parse_ranges(book, ranges)
default_verse = nil

ranges.map do |range|
range_delimiter, range = range[0], range[1..-1] if range =~ /^[,;]/
range_begin_string, range_end_string = range.split("-")

# semicolon is used between chapters, even if it doesn't otherwise look it, e.g., Psalm 1; 3
default_chapter = default_verse = nil if range_delimiter == ";" && book_has_chapters?(book)

# treat 12:4 as 12:4-12:4
range_end_string ||= range_begin_string

Expand Down
6 changes: 5 additions & 1 deletion test/pericope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def setup
assert_equal [r(1001001, 1001031)], Pericope.parse_reference(1, "1") # Genesis 1
end

should "parse multiple chapters into ranges of verses in that chapter" do
assert_equal [r(1001001, 1001031), r(1003001, 1003024)], Pericope.parse_reference(1, "1; 3") # Genesis 1; 3
end

should "parse multiple ranges into an array of ranges" do
expected_ranges = [
r(40003001),
Expand All @@ -155,7 +159,7 @@ def setup

tests = [
"3:1,3,4-5,7; 4:19",
"3:1, 3 ,4-5; 7,4:19"
"3:1, 3 ,4-5, 7;4:19"
]

tests.each do |input|
Expand Down