2016-05-15 10:42:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
//convert any (supported) type to a const char* without constructing a new nall::string
|
|
|
|
//this is used inside string{...} to build nall::string values
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
|
|
|
//booleans
|
|
|
|
|
|
|
|
template<> struct stringify<bool> {
|
|
|
|
stringify(bool value) : _value(value) {}
|
|
|
|
auto data() const -> const char* { return _value ? "true" : "false"; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return _value ? 4 : 5; }
|
2016-05-15 10:42:10 +00:00
|
|
|
bool _value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<Boolean> {
|
|
|
|
stringify(bool value) : _value(value) {}
|
|
|
|
auto data() const -> const char* { return _value ? "true" : "false"; }
|
|
|
|
auto size() const -> uint { return _value ? 4 : 5; }
|
|
|
|
bool _value;
|
|
|
|
};
|
|
|
|
|
|
|
|
//characters
|
|
|
|
|
|
|
|
template<> struct stringify<char> {
|
|
|
|
stringify(char source) { _data[0] = source; _data[1] = 0; }
|
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return 1; }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
//signed integers
|
|
|
|
|
|
|
|
template<> struct stringify<signed char> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(signed char source) { fromInteger(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[2 + sizeof(signed char) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<signed short> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(signed short source) { fromInteger(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[2 + sizeof(signed short) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<signed int> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(signed int source) { fromInteger(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[2 + sizeof(signed int) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<signed long> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(signed long source) { fromInteger(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[2 + sizeof(signed long) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<signed long long> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(signed long long source) { fromInteger(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[2 + sizeof(signed long long) * 3];
|
|
|
|
};
|
|
|
|
|
2016-08-09 18:27:40 +00:00
|
|
|
#if INTMAX_BITS >= 128
|
|
|
|
template<> struct stringify<int128_t> {
|
|
|
|
stringify(int128_t source) { fromInteger(_data, source); }
|
|
|
|
auto data() const -> const char* { return _data; }
|
|
|
|
auto size() const -> uint { return strlen(_data); }
|
|
|
|
char _data[2 + sizeof(int128_t) * 3];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-05-15 10:42:10 +00:00
|
|
|
template<uint Bits> struct stringify<Integer<Bits>> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(Integer<Bits> source) { fromInteger(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
|
|
|
auto size() const -> uint { return strlen(_data); }
|
|
|
|
char _data[2 + sizeof(int64_t) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
//unsigned integers
|
|
|
|
|
|
|
|
template<> struct stringify<unsigned char> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(unsigned char source) { fromNatural(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[1 + sizeof(unsigned char) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<unsigned short> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(unsigned short source) { fromNatural(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[1 + sizeof(unsigned short) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<unsigned int> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(unsigned int source) { fromNatural(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[1 + sizeof(unsigned int) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<unsigned long> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(unsigned long source) { fromNatural(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[1 + sizeof(unsigned long) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<unsigned long long> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(unsigned long long source) { fromNatural(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[1 + sizeof(unsigned long long) * 3];
|
|
|
|
};
|
|
|
|
|
2016-08-09 18:27:40 +00:00
|
|
|
#if INTMAX_BITS >= 128
|
|
|
|
template<> struct stringify<uint128_t> {
|
|
|
|
stringify(uint128_t source) { fromNatural(_data, source); }
|
|
|
|
auto data() const -> const char* { return _data; }
|
|
|
|
auto size() const -> uint { return strlen(_data); }
|
|
|
|
char _data[1 + sizeof(uint128_t) * 3];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-05-15 10:42:10 +00:00
|
|
|
template<uint Bits> struct stringify<Natural<Bits>> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(Natural<Bits> source) { fromNatural(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
|
|
|
auto size() const -> uint { return strlen(_data); }
|
|
|
|
char _data[1 + sizeof(uint64_t) * 3];
|
|
|
|
};
|
|
|
|
|
|
|
|
//floating-point
|
|
|
|
|
|
|
|
template<> struct stringify<float> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(float source) { fromReal(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<double> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(double source) { fromReal(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<long double> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(long double source) { fromReal(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
char _data[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<uint Bits> struct stringify<Real<Bits>> {
|
2016-07-09 07:51:02 +00:00
|
|
|
stringify(Real<Bits> source) { fromReal(_data, source); }
|
2016-05-15 10:42:10 +00:00
|
|
|
auto data() const -> const char* { return _data; }
|
|
|
|
auto size() const -> uint { return strlen(_data); }
|
|
|
|
char _data[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
//arrays
|
|
|
|
|
|
|
|
template<> struct stringify<vector<uint8_t>> {
|
|
|
|
stringify(vector<uint8_t> source) {
|
|
|
|
_text.resize(source.size());
|
|
|
|
memory::copy(_text.data(), source.data(), source.size());
|
|
|
|
}
|
|
|
|
auto data() const -> const char* { return _text.data(); }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return _text.size(); }
|
2016-05-15 10:42:10 +00:00
|
|
|
vector<char> _text;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<const vector<uint8_t>&> {
|
|
|
|
stringify(const vector<uint8_t>& source) {
|
|
|
|
_text.resize(source.size());
|
|
|
|
memory::copy(_text.data(), source.data(), source.size());
|
|
|
|
}
|
|
|
|
auto data() const -> const char* { return _text.data(); }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return _text.size(); }
|
2016-05-15 10:42:10 +00:00
|
|
|
vector<char> _text;
|
|
|
|
};
|
|
|
|
|
|
|
|
//char arrays
|
|
|
|
|
|
|
|
template<> struct stringify<char*> {
|
|
|
|
stringify(char* source) : _data(source ? source : "") {}
|
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
const char* _data;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<const char*> {
|
|
|
|
stringify(const char* source) : _data(source ? source : "") {}
|
|
|
|
auto data() const -> const char* { return _data; }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return strlen(_data); }
|
2016-05-15 10:42:10 +00:00
|
|
|
const char* _data;
|
|
|
|
};
|
|
|
|
|
|
|
|
//strings
|
|
|
|
|
|
|
|
template<> struct stringify<string> {
|
|
|
|
stringify(const string& source) : _text(source) {}
|
|
|
|
auto data() const -> const char* { return _text.data(); }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return _text.size(); }
|
2016-05-15 10:42:10 +00:00
|
|
|
const string& _text;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<const string&> {
|
|
|
|
stringify(const string& source) : _text(source) {}
|
|
|
|
auto data() const -> const char* { return _text.data(); }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return _text.size(); }
|
2016-05-15 10:42:10 +00:00
|
|
|
const string& _text;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<string_view> {
|
|
|
|
stringify(const string_view& source) : _view(source) {}
|
|
|
|
auto data() const -> const char* { return _view.data(); }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return _view.size(); }
|
2016-05-15 10:42:10 +00:00
|
|
|
const string_view& _view;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct stringify<const string_view&> {
|
|
|
|
stringify(const string_view& source) : _view(source) {}
|
|
|
|
auto data() const -> const char* { return _view.data(); }
|
2016-07-03 11:35:22 +00:00
|
|
|
auto size() const -> uint { return _view.size(); }
|
2016-05-15 10:42:10 +00:00
|
|
|
const string_view& _view;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
template<typename T> auto make_string(T value) -> stringify<T> {
|
|
|
|
return stringify<T>(forward<T>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|