Skip to content

Commit 124d581

Browse files
committed
Tighten up double regexp.
* Allows `.1` * Disallows `+INF`, and `InF`. * Disallows `NAN` All are parsed, but considered invalid.
1 parent a2bda54 commit 124d581

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

lib/rdf/model/literal/double.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module RDF; class Literal
1212
# @since 0.2.1
1313
class Double < Numeric
1414
DATATYPE = RDF::XSD.double
15-
GRAMMAR = /^(?:NaN|(?:[\+\-]?(?:INF|(?:\d+(\.\d*)?(e[\+\-]?\d+)?))))$/i.freeze
15+
GRAMMAR = /^(?:NaN|\-?INF|[+\-]?(?:\d+(:?\.\d*)?|\.\d+)(?:[eE][\+\-]?\d+)?)$/.freeze
1616

1717
##
1818
# @param [String, Float, #to_f] value
@@ -21,11 +21,11 @@ def initialize(value, datatype: nil, lexical: nil, **options)
2121
@datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
2222
@string = lexical || (value if value.is_a?(String))
2323
@object = case
24-
when value.is_a?(::String) then case value
24+
when value.is_a?(::String) then case value.upcase
2525
when '+INF' then 1/0.0
2626
when 'INF' then 1/0.0
2727
when '-INF' then -1/0.0
28-
when 'NaN' then 0/0.0
28+
when 'NAN' then 0/0.0
2929
else Float(value.sub(/\.[eE]/, '.0E')) rescue nil
3030
end
3131
when value.is_a?(::Float) then value

spec/model_literal_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,10 @@ def self.literals(*selector)
487487
%w(+INF INF),
488488
%w(INF INF),
489489
%w(-INF -INF),
490+
%w(+INF INF),
490491
#%w(NaN NaN),
492+
#%w(NAN NaN),
493+
%w(InF INF),
491494
%w(3E1 3.0E1)
492495
]
493496
it_behaves_like 'RDF::Literal validation', RDF::XSD.double,
@@ -500,12 +503,9 @@ def self.literals(*selector)
500503
1.0e+1
501504
1.0e-10
502505
123.456e4
503-
+INF
504506
INF
505507
-INF
506-
Inf
507508
NaN
508-
NAN
509509
3E1
510510
),
511511
%w(foo 12.xyz 1.0ez) + ['1.1e1 foo', 'foo 1.1e1']

0 commit comments

Comments
 (0)