73 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H10#define LIBCPP_TEST_SUPPORT_PARSE_INTEGER_H11 12#include <string>13 14namespace detail {15template <class T>16struct parse_integer_impl;17 18template <>19struct 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 26template <>27struct 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 34template <>35struct 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 42template <>43struct 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 50template <>51struct 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 58template <>59struct 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} // namespace detail66 67template <class T, class CharT>68T 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_H73