Move std::stou to integer.h, add intToHex util
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
#include <limits> // std::numeric_limits
|
#include <limits> // std::numeric_limits
|
||||||
#include <string> // std::getline
|
#include <string> // std::getline, std::stoi
|
||||||
|
|
||||||
#include "inferno/assert.h"
|
#include "inferno/assert.h"
|
||||||
#include "inferno/io/file.h"
|
#include "inferno/io/file.h"
|
||||||
#include "inferno/render/font.h"
|
#include "inferno/render/font.h"
|
||||||
#include "inferno/render/texture.h"
|
#include "inferno/render/texture.h"
|
||||||
#include "inferno/util/string.h"
|
#include "inferno/util/integer.h"
|
||||||
|
|
||||||
namespace Inferno {
|
namespace Inferno {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef INTEGER_UTIL_H
|
||||||
|
#define INTEGER_UTIL_H
|
||||||
|
|
||||||
|
#include <limits> // std::numeric_limits
|
||||||
|
#include <string> // std::string, std::stoul
|
||||||
|
|
||||||
|
#include "inferno/assert.h"
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
|
||||||
|
// Can't believe this is not in the standard library
|
||||||
|
|
||||||
|
inline uint32_t stou(const std::string& string)
|
||||||
|
{
|
||||||
|
unsigned long size = std::stoul(string);
|
||||||
|
ASSERT(size <= std::numeric_limits<uint32_t>::max(), "String util not in uint32_t range '{}'", string);
|
||||||
|
return static_cast<uint32_t>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint32_t stou(const char* string)
|
||||||
|
{
|
||||||
|
return stou(std::string(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
|
#endif // INTEGER_UTIL_H
|
||||||
@@ -1,27 +1,22 @@
|
|||||||
#ifndef UTIL_STRING_H
|
#ifndef STRING_UTIL_H
|
||||||
#define UTIL_STRING_H
|
#define STRING_UTIL_H
|
||||||
|
|
||||||
#include <limits> // std::numeric_limits
|
#include <iomanip> // std::setfill, std::setw
|
||||||
#include <string> // std::string, std::stoul
|
#include <sstream> // std::stringstream
|
||||||
|
|
||||||
#include "inferno/assert.h"
|
namespace Inferno {
|
||||||
|
|
||||||
namespace std {
|
template<typename T>
|
||||||
|
std::string intToHex(T i)
|
||||||
// Can't believe this is not in the standard library
|
|
||||||
|
|
||||||
inline uint32_t stou(const std::string& string)
|
|
||||||
{
|
{
|
||||||
unsigned long size = std::stoul(string);
|
std::stringstream stream;
|
||||||
ASSERT(size <= std::numeric_limits<uint32_t>::max(), "String util not in uint32_t range '{}'", string);
|
stream << "0x"
|
||||||
return static_cast<uint32_t>(size);
|
<< std::setfill('0') << std::setw(sizeof(T) * 2)
|
||||||
|
<< std::hex << i;
|
||||||
|
|
||||||
|
return stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint32_t stou(const char* string)
|
} // namespace Inferno
|
||||||
{
|
|
||||||
return stou(std::string(string));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace std
|
#endif // STRING_UTIL_H
|
||||||
|
|
||||||
#endif // UTIL_STRING_H
|
|
||||||
|
|||||||
Reference in New Issue
Block a user