osb/source/core/StarLexicalCast.hpp
Kae 09d26d43b5 Add string view variant of Star::String and use it
it's 1:30 AM AGAIN !! !!!!!
This might have broken the inventory icons of custom hats a little, need to look into that
2023-06-26 01:42:18 +10:00

56 lines
1.2 KiB
C++

#ifndef STAR_LEXICAL_CAST_HPP
#define STAR_LEXICAL_CAST_HPP
#include "StarString.hpp"
#include "StarStringView.hpp"
#include "StarMaybe.hpp"
#include <sstream>
#include <locale>
namespace Star {
STAR_EXCEPTION(BadLexicalCast, StarException);
// Very simple basic lexical cast using stream input. Always operates in the
// "C" locale.
template <typename Type>
Maybe<Type> maybeLexicalCast(StringView s, std::ios_base::fmtflags flags = std::ios_base::boolalpha) {
Type result;
std::istringstream stream(std::string(s.utf8()));
stream.flags(flags);
stream.imbue(std::locale::classic());
if (!(stream >> result))
return {};
// Confirm that we read everything out of the stream
char ch;
if (stream >> ch)
return {};
return move(result);
}
template <typename Type>
Type lexicalCast(StringView s, std::ios_base::fmtflags flags = std::ios_base::boolalpha) {
auto m = maybeLexicalCast<Type>(s, flags);
if (m)
return m.take();
else
throw BadLexicalCast(strf("Lexical cast failed on '%s'", s));
}
template <class Type>
std::string toString(Type const& t, std::ios_base::fmtflags flags = std::ios_base::boolalpha) {
std::stringstream ss;
ss.flags(flags);
ss.imbue(std::locale::classic());
ss << t;
return ss.str();
}
}
#endif