Skip to content

Commit

Permalink
Peg Fugit::Nat.parse(s) at 256 chars, gh-104
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Aug 14, 2024
1 parent d2f6cf3 commit 6a75274
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
15 changes: 14 additions & 1 deletion lib/fugit/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,20 @@ def parse(s)
def do_parse(s)

parse(s) ||
fail(ArgumentError.new("invalid cron string #{s.inspect}"))
fail(ArgumentError.new("invalid cron string #{trunc(s)}"))
end

protected

def trunc(s)

if s.is_a?(String)
r = s.length > 28 ? s[0, 28] + "... len #{s.length}" : s
r.inspect
else
r = s.inspect
r.length > 35 ? s[0, 35] + '...' : r
end
end
end

Expand Down
14 changes: 13 additions & 1 deletion lib/fugit/nat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Fugit
#
module Nat

MAX_INPUT_LENGTH = 256

class << self

def parse(s, opts={})
Expand All @@ -17,6 +19,16 @@ def parse(s, opts={})

s = s.strip

if s.length > MAX_INPUT_LENGTH

fail ArgumentError.new(
"input too long for a nat string, " +
"#{s.length} > #{MAX_INPUT_LENGTH}"
) if opts[:do_parse]

return nil
end

#p s; Raabro.pp(Parser.parse(s, debug: 3), colours: true)
#(p s; Raabro.pp(Parser.parse(s, debug: 1), colours: true)) rescue nil

Expand All @@ -29,7 +41,7 @@ def parse(s, opts={})

def do_parse(s, opts={})

parse(s, opts) ||
parse(s, opts.merge(do_parse: true)) ||
fail(ArgumentError.new("could not parse a nat #{s.inspect}"))
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/fugit/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def parse(s, opts={})

opts[:at] = opts[:in] if opts.has_key?(:in)

(opts[:cron] != false && parse_cron(s)) ||
(opts[:duration] != false && parse_duration(s)) ||
(opts[:nat] != false && parse_nat(s, opts)) ||
(opts[:at] != false && parse_at(s)) ||
(opts[:cron] != false && parse_cron(s)) || # 542ms 616ms
(opts[:duration] != false && parse_duration(s)) || # 645ms # 534ms
(opts[:nat] != false && parse_nat(s, opts)) || # 2s # 35s
(opts[:at] != false && parse_at(s)) || # 568ms 622ms
nil
end

Expand Down
10 changes: 8 additions & 2 deletions spec/nat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,18 @@

].each do |input|

it "fails with ArgumentError for #{input.inspect}" do
it "fails with an ArgumentError for #{input.inspect}" do

expect { Fugit::Nat.do_parse(input)
}.to raise_error(ArgumentError)
}.to raise_error(ArgumentError, /could not parse a nat/)
end
end

it "fails with an ArgumentError when the input length > 256" do

expect { Fugit::Nat.do_parse('a' * 5000)
}.to raise_error(ArgumentError, /too long .+ 5000 > 256/)
end
end
end

Expand Down

0 comments on commit 6a75274

Please sign in to comment.