Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
94dc63af
Commit
94dc63af
authored
Jul 10, 2011
by
Jesse Beder
Browse files
Added parsing .inf and .nan (and friend)
parent
50474b6b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
1 deletion
+89
-1
include/yaml-cpp/conversion.h
include/yaml-cpp/conversion.h
+26
-1
test/parsertests.cpp
test/parsertests.cpp
+63
-0
No files found.
include/yaml-cpp/conversion.h
View file @
94dc63af
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
#include "yaml-cpp/null.h"
#include "yaml-cpp/null.h"
#include "yaml-cpp/traits.h"
#include "yaml-cpp/traits.h"
#include <limits>
#include <string>
#include <string>
#include <sstream>
#include <sstream>
...
@@ -21,12 +22,36 @@ namespace YAML
...
@@ -21,12 +22,36 @@ namespace YAML
YAML_CPP_API
bool
Convert
(
const
std
::
string
&
input
,
bool
&
output
);
YAML_CPP_API
bool
Convert
(
const
std
::
string
&
input
,
bool
&
output
);
YAML_CPP_API
bool
Convert
(
const
std
::
string
&
input
,
_Null
&
output
);
YAML_CPP_API
bool
Convert
(
const
std
::
string
&
input
,
_Null
&
output
);
inline
bool
IsInfinity
(
const
std
::
string
&
input
)
{
return
input
==
".inf"
||
input
==
".Inf"
||
input
==
".INF"
||
input
==
"+.inf"
||
input
==
"+.Inf"
||
input
==
"+.INF"
||
input
==
"-.inf"
||
input
==
"-.Inf"
||
input
==
"-.INF"
;
}
inline
bool
IsNaN
(
const
std
::
string
&
input
)
{
return
input
==
".nan"
||
input
==
".NaN"
||
input
==
".NAN"
;
}
template
<
typename
T
>
template
<
typename
T
>
inline
bool
Convert
(
const
std
::
string
&
input
,
T
&
output
,
typename
enable_if
<
is_numeric
<
T
>
>::
type
*
=
0
)
{
inline
bool
Convert
(
const
std
::
string
&
input
,
T
&
output
,
typename
enable_if
<
is_numeric
<
T
>
>::
type
*
=
0
)
{
std
::
stringstream
stream
(
input
);
std
::
stringstream
stream
(
input
);
stream
.
unsetf
(
std
::
ios
::
dec
);
stream
.
unsetf
(
std
::
ios
::
dec
);
stream
>>
output
;
stream
>>
output
;
return
!!
stream
;
if
(
!!
stream
)
return
true
;
if
(
std
::
numeric_limits
<
T
>::
has_infinity
&&
IsInfinity
(
input
))
{
output
=
std
::
numeric_limits
<
T
>::
infinity
();
return
true
;
}
if
(
std
::
numeric_limits
<
T
>::
has_quiet_NaN
&&
IsNaN
(
input
))
{
output
=
std
::
numeric_limits
<
T
>::
quiet_NaN
();
return
true
;
}
return
false
;
}
}
}
}
...
...
test/parsertests.cpp
View file @
94dc63af
...
@@ -803,6 +803,67 @@ namespace Test
...
@@ -803,6 +803,67 @@ namespace Test
return
ExpectedTagValue
(
node
,
"!"
);
return
ExpectedTagValue
(
node
,
"!"
);
}
}
bool
Infinity
()
{
std
::
string
input
=
"- .inf
\n
"
"- .Inf
\n
"
"- .INF
\n
"
"- +.inf
\n
"
"- +.Inf
\n
"
"- +.INF
\n
"
"- -.inf
\n
"
"- -.Inf
\n
"
"- -.INF
\n
"
;
std
::
stringstream
stream
(
input
);
YAML
::
Parser
parser
(
stream
);
YAML
::
Node
doc
;
parser
.
GetNextDocument
(
doc
);
for
(
unsigned
i
=
0
;
i
<
doc
.
size
();
i
++
)
if
(
doc
[
i
].
to
<
double
>
()
!=
std
::
numeric_limits
<
double
>::
infinity
())
return
false
;
for
(
unsigned
i
=
0
;
i
<
doc
.
size
();
i
++
)
if
(
doc
[
i
].
to
<
long
double
>
()
!=
std
::
numeric_limits
<
long
double
>::
infinity
())
return
false
;
for
(
unsigned
i
=
0
;
i
<
doc
.
size
();
i
++
)
if
(
doc
[
i
].
to
<
float
>
()
!=
std
::
numeric_limits
<
float
>::
infinity
())
return
false
;
return
true
;
}
bool
NaN
()
{
std
::
string
input
=
"- .nan
\n
"
"- .NaN
\n
"
"- .NAN
\n
"
;
std
::
stringstream
stream
(
input
);
YAML
::
Parser
parser
(
stream
);
YAML
::
Node
doc
;
parser
.
GetNextDocument
(
doc
);
for
(
unsigned
i
=
0
;
i
<
doc
.
size
();
i
++
)
{
double
d
;
doc
[
i
]
>>
d
;
if
(
d
==
d
)
return
false
;
}
for
(
unsigned
i
=
0
;
i
<
doc
.
size
();
i
++
)
{
long
double
d
;
doc
[
i
]
>>
d
;
if
(
d
==
d
)
return
false
;
}
for
(
unsigned
i
=
0
;
i
<
doc
.
size
();
i
++
)
{
float
d
;
doc
[
i
]
>>
d
;
if
(
d
==
d
)
return
false
;
}
return
true
;
}
}
}
namespace
{
namespace
{
...
@@ -1079,6 +1140,8 @@ namespace Test
...
@@ -1079,6 +1140,8 @@ namespace Test
RunParserTest
(
&
Parser
::
DefaultPlainScalarTag
,
"default plain scalar tag"
,
passed
,
total
);
RunParserTest
(
&
Parser
::
DefaultPlainScalarTag
,
"default plain scalar tag"
,
passed
,
total
);
RunParserTest
(
&
Parser
::
DefaultSequenceTag
,
"default sequence tag"
,
passed
,
total
);
RunParserTest
(
&
Parser
::
DefaultSequenceTag
,
"default sequence tag"
,
passed
,
total
);
RunParserTest
(
&
Parser
::
ExplicitNonSpecificSequenceTag
,
"explicit, non-specific sequence tag"
,
passed
,
total
);
RunParserTest
(
&
Parser
::
ExplicitNonSpecificSequenceTag
,
"explicit, non-specific sequence tag"
,
passed
,
total
);
RunParserTest
(
&
Parser
::
Infinity
,
"infinity"
,
passed
,
total
);
RunParserTest
(
&
Parser
::
NaN
,
"NaN"
,
passed
,
total
);
RunEncodingTest
(
&
EncodeToUtf8
,
false
,
"UTF-8, no BOM"
,
passed
,
total
);
RunEncodingTest
(
&
EncodeToUtf8
,
false
,
"UTF-8, no BOM"
,
passed
,
total
);
RunEncodingTest
(
&
EncodeToUtf8
,
true
,
"UTF-8 with BOM"
,
passed
,
total
);
RunEncodingTest
(
&
EncodeToUtf8
,
true
,
"UTF-8 with BOM"
,
passed
,
total
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment