parse: add throwing native type parsers

This commit is contained in:
Danny Robson 2017-07-26 15:25:29 +10:00
parent d29276a427
commit f39f7d7362
4 changed files with 160 additions and 0 deletions

View File

@ -334,6 +334,8 @@ list (
memory/deleter.cpp memory/deleter.cpp
memory/deleter.hpp memory/deleter.hpp
nocopy.hpp nocopy.hpp
parse.cpp
parse.hpp
pascal.cpp pascal.cpp
pascal.hpp pascal.hpp
platform.hpp platform.hpp
@ -491,6 +493,7 @@ if (TESTS)
maths maths
matrix matrix
memory/deleter memory/deleter
parse
point point
polynomial polynomial
pool pool

86
parse.cpp Normal file
View File

@ -0,0 +1,86 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
*/
#include "./parse.hpp"
#include <cstdlib>
#include <stdexcept>
using util::parse;
///////////////////////////////////////////////////////////////////////////////
template <typename T> struct c_traits;
template <> struct c_traits<long> { static constexpr auto func = strtol; };
template <> struct c_traits<long long> { static constexpr auto func = strtoll; };
template <> struct c_traits<unsigned long> { static constexpr auto func = strtoul; };
template <> struct c_traits<unsigned long long> { static constexpr auto func = strtoull; };
template <> struct c_traits<float> { static constexpr auto func = strtof; };
template <> struct c_traits<double> { static constexpr auto func = strtod; };
template <> struct c_traits<long double> { static constexpr auto func = strtold; };
//-----------------------------------------------------------------------------
template <typename T>
T
c_iparse (const char *first, const char *last)
{
auto tail = const_cast<char *> (last);
auto val = c_traits<T>::func (first, &tail, 0);
if (tail != last)
throw std::invalid_argument ("unable to parse");
return val;
}
//-----------------------------------------------------------------------------
template <typename T>
T
c_fparse (const char *first, const char *last)
{
auto tail = const_cast<char *> (last);
auto val = c_traits<T>::func (first, &tail);
if (tail != last)
throw std::invalid_argument ("unable to parse");
return val;
}
///////////////////////////////////////////////////////////////////////////////
#define C_PARSE(T, KLASS) \
template <> \
T \
util::parse<T> (const char *first, const char *last) \
{ \
return c_ ## KLASS ## parse<T> (first, last); \
}
//-----------------------------------------------------------------------------
C_PARSE(long, i)
C_PARSE(long long, i)
C_PARSE(unsigned long, i)
C_PARSE(unsigned long long, i)
C_PARSE(float, f)
C_PARSE(double, f)
C_PARSE(long double, f)

53
parse.hpp Normal file
View File

@ -0,0 +1,53 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_PARSE_HPP
#define CRUFT_UTIL_PARSE_HPP
#include <cstring>
#include <string>
#include <iterator>
namespace util {
///////////////////////////////////////////////////////////////////////////
/// extracts an instance of a native type T from the string [first, last).
///
/// throws std::invalid_argument when the type cannot be parsed.
template <typename T>
T
parse (const char *first, const char *last);
//-------------------------------------------------------------------------
template <typename T>
T
parse (const char *str)
{
return parse<T> (str, str + strlen (str));
}
//-------------------------------------------------------------------------
template <typename T>
T
parse (const std::string &str)
{
return parse<T> (std::cbegin (str), std::cend (str));
}
}
#endif

18
test/parse.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "parse.hpp"
#include "tap.hpp"
///////////////////////////////////////////////////////////////////////////////
int
main (void)
{
util::TAP::logger tap;
tap.expect_eq (util::parse<long> ("1"), 1L, "parsing long '1'");
tap.expect_throw<std::invalid_argument> ([] () { util::parse<long> ("a"); }, "parsing long 'a'");
tap.expect_eq (util::parse<float> ("1"), 1.f, "parsing float '1'");
tap.expect_throw<std::invalid_argument> ([] () { util::parse<float> ("a"); }, "parsing float 'a'");
return tap.status ();
}