old-nall/string/format.hpp

156 lines
4 KiB
C++
Raw Permalink Normal View History

2016-05-15 10:42:10 +00:00
#pragma once
namespace nall {
//nall::format is a vector<string> of parameters that can be applied to a string
//each {#} token will be replaced with its appropriate format parameter
2016-07-09 07:51:02 +00:00
auto string::format(const nall::string_format& params) -> type& {
2016-05-15 10:42:10 +00:00
auto size = (int)this->size();
auto data = (char*)memory::allocate(size);
memory::copy(data, this->data(), size);
2016-07-03 11:35:22 +00:00
int x = 0;
2016-05-15 10:42:10 +00:00
while(x < size - 2) { //2 = minimum tag length
if(data[x] != '{') { x++; continue; }
2016-07-03 11:35:22 +00:00
int y = x + 1;
2016-05-15 10:42:10 +00:00
while(y < size - 1) { //-1 avoids going out of bounds on test after this loop
if(data[y] != '}') { y++; continue; }
break;
}
if(data[y++] != '}') { x++; continue; }
static auto isNumeric = [](char* s, char* e) -> bool {
if(s == e) return false; //ignore empty tags: {}
while(s < e) {
if(*s >= '0' && *s <= '9') { s++; continue; }
return false;
}
return true;
};
if(!isNumeric(&data[x + 1], &data[y - 1])) { x++; continue; }
2016-07-09 07:51:02 +00:00
uint index = toNatural(&data[x + 1]);
2016-05-15 10:42:10 +00:00
if(index >= params.size()) { x++; continue; }
uint sourceSize = y - x;
uint targetSize = params[index].size();
uint remaining = size - x;
if(sourceSize > targetSize) {
uint difference = sourceSize - targetSize;
memory::move(&data[x], &data[x + difference], remaining);
size -= difference;
} else if(targetSize > sourceSize) {
uint difference = targetSize - sourceSize;
data = (char*)realloc(data, size + difference);
size += difference;
memory::move(&data[x + difference], &data[x], remaining);
}
memory::copy(&data[x], params[index].data(), targetSize);
x += targetSize;
}
resize(size);
memory::copy(get(), data, size);
memory::free(data);
return *this;
}
2016-07-09 07:51:02 +00:00
template<typename T, typename... P> auto string_format::append(const T& value, P&&... p) -> string_format& {
2016-05-15 10:42:10 +00:00
vector<string>::append(value);
return append(forward<P>(p)...);
}
2016-07-09 07:51:02 +00:00
auto string_format::append() -> string_format& {
2016-05-15 10:42:10 +00:00
return *this;
}
template<typename... P> auto print(P&&... p) -> void {
string s{forward<P>(p)...};
2016-07-03 11:35:22 +00:00
fwrite(s.data(), 1, s.size(), stdout);
}
template<typename... P> auto print(FILE* fp, P&&... p) -> void {
string s{forward<P>(p)...};
fwrite(s.data(), 1, s.size(), fp);
2016-05-15 10:42:10 +00:00
}
2016-08-09 18:27:40 +00:00
template<typename T> auto pad(const T& value, long precision, char padchar) -> string {
2016-07-09 07:51:02 +00:00
string buffer{value};
if(precision) buffer.size(precision, padchar);
return buffer;
}
2016-08-09 18:27:40 +00:00
auto hex(uintmax value, long precision, char padchar) -> string {
2016-05-15 10:42:10 +00:00
string buffer;
2016-08-09 18:27:40 +00:00
buffer.resize(sizeof(uintmax) * 2);
2016-05-15 10:42:10 +00:00
char* p = buffer.get();
uint size = 0;
do {
uint n = value & 15;
p[size++] = n < 10 ? '0' + n : 'a' + n - 10;
value >>= 4;
} while(value);
buffer.resize(size);
buffer.reverse();
if(precision) buffer.size(precision, padchar);
return buffer;
}
2016-08-09 18:27:40 +00:00
auto octal(uintmax value, long precision, char padchar) -> string {
2016-05-15 10:42:10 +00:00
string buffer;
2016-08-09 18:27:40 +00:00
buffer.resize(sizeof(uintmax) * 3);
2016-05-15 10:42:10 +00:00
char* p = buffer.get();
uint size = 0;
do {
p[size++] = '0' + (value & 7);
value >>= 3;
} while(value);
buffer.resize(size);
buffer.reverse();
if(precision) buffer.size(precision, padchar);
return buffer;
}
2016-08-09 18:27:40 +00:00
auto binary(uintmax value, long precision, char padchar) -> string {
2016-05-15 10:42:10 +00:00
string buffer;
2016-08-09 18:27:40 +00:00
buffer.resize(sizeof(uintmax) * 8);
2016-05-15 10:42:10 +00:00
char* p = buffer.get();
uint size = 0;
do {
p[size++] = '0' + (value & 1);
value >>= 1;
} while(value);
buffer.resize(size);
buffer.reverse();
if(precision) buffer.size(precision, padchar);
return buffer;
}
template<typename T> auto pointer(const T* value, long precision) -> string {
if(value == nullptr) return "(nullptr)";
2016-08-09 18:27:40 +00:00
return {"0x", hex((uintptr)value, precision)};
2016-05-15 10:42:10 +00:00
}
2016-08-09 18:27:40 +00:00
auto pointer(uintptr value, long precision) -> string {
2016-05-15 10:42:10 +00:00
if(value == 0) return "(nullptr)";
return {"0x", hex(value, precision)};
}
2016-07-09 07:51:02 +00:00
/*
2016-05-15 10:42:10 +00:00
auto real(long double value) -> string {
string temp;
2016-07-09 07:51:02 +00:00
temp.resize(fromReal(nullptr, value));
fromReal(temp.get(), value);
2016-05-15 10:42:10 +00:00
return temp;
}
2016-07-09 07:51:02 +00:00
*/
2016-05-15 10:42:10 +00:00
}