2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** Template function to convert any input type to std::string
26 template<typename T> inline std::string ConvNumeric(const T& in)
34 out += "0123456789"[std::abs((long)quotient % 10)];
39 std::reverse(out.begin(), out.end());
43 /** Template function to convert any input type to std::string
45 inline std::string ConvToStr(const int in)
47 return ConvNumeric(in);
50 /** Template function to convert any input type to std::string
52 inline std::string ConvToStr(const long in)
54 return ConvNumeric(in);
57 /** Template function to convert any input type to std::string
59 inline std::string ConvToStr(const char* in)
64 /** Template function to convert any input type to std::string
66 inline std::string ConvToStr(const bool in)
68 return (in ? "1" : "0");
71 /** Template function to convert any input type to std::string
73 inline std::string ConvToStr(char in)
75 return std::string(1, in);
78 inline const std::string& ConvToStr(const std::string& in)
83 /** Template function to convert any input type to std::string
85 template <class T> inline std::string ConvToStr(const T& in)
87 std::stringstream tmp;
93 /** Template function to convert a std::string to any numeric type.
95 template<typename TOut> inline TOut ConvToNum(const std::string& in)
98 std::istringstream tmp(in);
104 template<> inline char ConvToNum<char>(const std::string& in)
106 // We specialise ConvToNum for char to avoid istringstream treating
107 // the input as a character literal.
108 int16_t num = ConvToNum<int16_t>(in);
109 return num >= INT8_MIN && num <= INT8_MAX ? num : 0;
112 template<> inline unsigned char ConvToNum<unsigned char>(const std::string& in)
114 // We specialise ConvToNum for unsigned char to avoid istringstream
115 // treating the input as a character literal.
116 uint16_t num = ConvToNum<uint16_t>(in);
117 return num <= UINT8_MAX ? num : 0;