| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#ifndef LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H |
| 10 |
#define LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H |
| 11 |
|
| 12 |
#include <string> |
| 13 |
|
| 14 |
namespace detail { |
| 15 |
template <class T> |
| 16 |
struct parse_integer_impl; |
| 17 |
|
| 18 |
template <> |
| 19 |
struct parse_integer_impl<int> { |
| 20 |
template <class CharT> |
| 21 |
int operator()(std::basic_string<CharT> const& str) const { |
| 22 |
return std::stoi(str); |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
template <> |
| 27 |
struct parse_integer_impl<long> { |
| 28 |
template <class CharT> |
| 29 |
long operator()(std::basic_string<CharT> const& str) const { |
| 30 |
return std::stol(str); |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
template <> |
| 35 |
struct parse_integer_impl<long long> { |
| 36 |
template <class CharT> |
| 37 |
long long operator()(std::basic_string<CharT> const& str) const { |
| 38 |
return std::stoll(str); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
template <> |
| 43 |
struct parse_integer_impl<unsigned int> { |
| 44 |
template <class CharT> |
| 45 |
unsigned int operator()(std::basic_string<CharT> const& str) const { |
| 46 |
return std::stoul(str); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
template <> |
| 51 |
struct parse_integer_impl<unsigned long> { |
| 52 |
template <class CharT> |
| 53 |
unsigned long operator()(std::basic_string<CharT> const& str) const { |
| 54 |
return std::stoul(str); |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
template <> |
| 59 |
struct parse_integer_impl<unsigned long long> { |
| 60 |
template <class CharT> |
| 61 |
unsigned long long operator()(std::basic_string<CharT> const& str) const { |
| 62 |
return std::stoull(str); |
| 63 |
} |
| 64 |
}; |
| 65 |
} // end namespace detail |
| 66 |
|
| 67 |
template <class T, class CharT> |
| 68 |
T parse_integer(std::basic_string<CharT> const& str) { |
| 69 |
return detail::parse_integer_impl<T>()(str); |
| 70 |
} |
| 71 |
|
| 72 |
#endif // LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H |