mirror of
https://github.com/fmtlib/fmt.git
synced 2025-08-02 20:24:43 +02:00
Add a Buffer that stores initial data on stack.
This commit is contained in:
46
format.cc
46
format.cc
@@ -113,7 +113,7 @@ void fmt::Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
++size;
|
++size;
|
||||||
} while ((n /= 10) != 0);
|
} while ((n /= 10) != 0);
|
||||||
width = std::max(width, size);
|
width = std::max(width, size);
|
||||||
buffer_.resize(buffer_.size() + width, fill);
|
buffer_.resize(buffer_.size() + width);
|
||||||
p = &buffer_.back();
|
p = &buffer_.back();
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
do {
|
do {
|
||||||
@@ -129,7 +129,7 @@ void fmt::Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
++size;
|
++size;
|
||||||
} while ((n >>= 4) != 0);
|
} while ((n >>= 4) != 0);
|
||||||
width = std::max(width, size);
|
width = std::max(width, size);
|
||||||
buffer_.resize(buffer_.size() + width, fill);
|
buffer_.resize(buffer_.size() + width);
|
||||||
p = &buffer_.back();
|
p = &buffer_.back();
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
const char *digits = type == 'x' ? "0123456789abcdef" : "0123456789ABCDEF";
|
const char *digits = type == 'x' ? "0123456789abcdef" : "0123456789ABCDEF";
|
||||||
@@ -148,7 +148,7 @@ void fmt::Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
++size;
|
++size;
|
||||||
} while ((n >>= 3) != 0);
|
} while ((n >>= 3) != 0);
|
||||||
width = std::max(width, size);
|
width = std::max(width, size);
|
||||||
buffer_.resize(buffer_.size() + width, fill);
|
buffer_.resize(buffer_.size() + width);
|
||||||
p = &buffer_.back();
|
p = &buffer_.back();
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
do {
|
do {
|
||||||
@@ -162,10 +162,11 @@ void fmt::Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
}
|
}
|
||||||
if (sign) {
|
if (sign) {
|
||||||
if ((flags & ZERO_FLAG) != 0)
|
if ((flags & ZERO_FLAG) != 0)
|
||||||
buffer_[start] = sign;
|
buffer_[start++] = sign;
|
||||||
else
|
else
|
||||||
*p = sign;
|
*p-- = sign;
|
||||||
}
|
}
|
||||||
|
std::fill(&buffer_[start], p + 1, fill);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -205,9 +206,8 @@ void fmt::Formatter::FormatDouble(
|
|||||||
|
|
||||||
// Format using snprintf.
|
// Format using snprintf.
|
||||||
size_t offset = buffer_.size();
|
size_t offset = buffer_.size();
|
||||||
buffer_.resize(buffer_.capacity());
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
size_t size = buffer_.size() - offset;
|
size_t size = buffer_.capacity() - offset;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
if (width <= 0) {
|
if (width <= 0) {
|
||||||
n = precision < 0 ?
|
n = precision < 0 ?
|
||||||
@@ -218,29 +218,28 @@ void fmt::Formatter::FormatDouble(
|
|||||||
snprintf(&buffer_[offset], size, format, width, value) :
|
snprintf(&buffer_[offset], size, format, width, value) :
|
||||||
snprintf(&buffer_[offset], size, format, width, precision, value);
|
snprintf(&buffer_[offset], size, format, width, precision, value);
|
||||||
}
|
}
|
||||||
if (n >= 0 && offset + n < buffer_.size()) {
|
if (n >= 0 && offset + n < buffer_.capacity()) {
|
||||||
buffer_.resize(offset + n);
|
buffer_.resize(offset + n);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
buffer_.resize(n >= 0 ? offset + n + 1 : 2 * buffer_.size());
|
buffer_.reserve(n >= 0 ? offset + n + 1 : 2 * buffer_.capacity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fmt::Formatter::Format() {
|
void fmt::Formatter::Format() {
|
||||||
buffer_.reserve(500);
|
|
||||||
const char *start = format_;
|
const char *start = format_;
|
||||||
const char *s = start;
|
const char *s = start;
|
||||||
while (*s) {
|
while (*s) {
|
||||||
char c = *s++;
|
char c = *s++;
|
||||||
if (c != '{' && c != '}') continue;
|
if (c != '{' && c != '}') continue;
|
||||||
if (*s == c) {
|
if (*s == c) {
|
||||||
buffer_.insert(buffer_.end(), start, s);
|
buffer_.append(start, s);
|
||||||
start = ++s;
|
start = ++s;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (c == '}')
|
if (c == '}')
|
||||||
throw FormatError("unmatched '}' in format");
|
throw FormatError("unmatched '}' in format");
|
||||||
buffer_.insert(buffer_.end(), start, s - 1);
|
buffer_.append(start, s - 1);
|
||||||
|
|
||||||
// Parse argument index.
|
// Parse argument index.
|
||||||
if (*s < '0' || *s > '9')
|
if (*s < '0' || *s > '9')
|
||||||
@@ -248,7 +247,7 @@ void fmt::Formatter::Format() {
|
|||||||
unsigned arg_index = ParseUInt(s);
|
unsigned arg_index = ParseUInt(s);
|
||||||
if (arg_index >= args_.size())
|
if (arg_index >= args_.size())
|
||||||
ReportError(s, "argument index is out of range in format");
|
ReportError(s, "argument index is out of range in format");
|
||||||
Arg &arg = args_[arg_index];
|
const Arg &arg = args_[arg_index];
|
||||||
|
|
||||||
unsigned flags = 0;
|
unsigned flags = 0;
|
||||||
int width = 0;
|
int width = 0;
|
||||||
@@ -328,14 +327,17 @@ void fmt::Formatter::Format() {
|
|||||||
case LONG_DOUBLE:
|
case LONG_DOUBLE:
|
||||||
FormatDouble(arg.long_double_value, flags, width, precision, type);
|
FormatDouble(arg.long_double_value, flags, width, precision, type);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR: {
|
||||||
if (type && type != 'c')
|
if (type && type != 'c')
|
||||||
ReportUnknownType(type, "char");
|
ReportUnknownType(type, "char");
|
||||||
buffer_.reserve(std::max(width, 1));
|
std::size_t offset = buffer_.size();
|
||||||
buffer_.push_back(arg.int_value);
|
buffer_.resize(offset + std::max(width, 1));
|
||||||
|
char *out = &buffer_[offset];
|
||||||
|
*out++ = arg.int_value;
|
||||||
if (width > 1)
|
if (width > 1)
|
||||||
buffer_.resize(buffer_.size() + width - 1, ' ');
|
std::fill_n(out, width - 1, ' ');
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case STRING: {
|
case STRING: {
|
||||||
if (type && type != 's')
|
if (type && type != 's')
|
||||||
ReportUnknownType(type, "string");
|
ReportUnknownType(type, "string");
|
||||||
@@ -343,10 +345,12 @@ void fmt::Formatter::Format() {
|
|||||||
size_t size = arg.size;
|
size_t size = arg.size;
|
||||||
if (size == 0 && *str)
|
if (size == 0 && *str)
|
||||||
size = std::strlen(str);
|
size = std::strlen(str);
|
||||||
buffer_.reserve(buffer_.size() + std::max<size_t>(width, size));
|
size_t offset = buffer_.size();
|
||||||
buffer_.insert(buffer_.end(), str, str + size);
|
buffer_.resize(offset + std::max<size_t>(width, size));
|
||||||
|
char *out = &buffer_[offset];
|
||||||
|
std::copy(str, str + size, out);
|
||||||
if (width > size)
|
if (width > size)
|
||||||
buffer_.resize(buffer_.size() + width - size, ' ');
|
std::fill_n(out + size, width - size, ' ');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case POINTER:
|
case POINTER:
|
||||||
@@ -365,7 +369,7 @@ void fmt::Formatter::Format() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buffer_.insert(buffer_.end(), start, s + 1);
|
buffer_.append(start, s + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt::ArgFormatter::~ArgFormatter() {
|
fmt::ArgFormatter::~ArgFormatter() {
|
||||||
|
119
format.h
119
format.h
@@ -25,11 +25,89 @@ class ArgFormatter;
|
|||||||
template <typename Callback>
|
template <typename Callback>
|
||||||
class ArgFormatterWithCallback;
|
class ArgFormatterWithCallback;
|
||||||
|
|
||||||
|
// A buffer with the first SIZE elements stored in the object itself.
|
||||||
|
template <typename T, std::size_t SIZE>
|
||||||
|
class Buffer {
|
||||||
|
private:
|
||||||
|
std::size_t size_;
|
||||||
|
std::size_t capacity_;
|
||||||
|
T *ptr_;
|
||||||
|
T data_[SIZE];
|
||||||
|
|
||||||
|
void Grow(std::size_t size) {
|
||||||
|
capacity_ = std::max(size, capacity_ + capacity_ / 2);
|
||||||
|
T *p = new T[capacity_];
|
||||||
|
std::copy(ptr_, ptr_ + size_, p);
|
||||||
|
if (ptr_ != data_)
|
||||||
|
delete [] ptr_;
|
||||||
|
ptr_ = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not implement!
|
||||||
|
Buffer(const Buffer &);
|
||||||
|
void operator=(const Buffer &);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Buffer() : size_(0), capacity_(SIZE), ptr_(data_) {}
|
||||||
|
~Buffer() {
|
||||||
|
if (ptr_ != data_) delete [] ptr_;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t size() const { return size_; }
|
||||||
|
std::size_t capacity() const { return capacity_; }
|
||||||
|
|
||||||
|
void resize(std::size_t size) {
|
||||||
|
if (size > capacity_)
|
||||||
|
Grow(size);
|
||||||
|
size_ = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reserve(std::size_t capacity) {
|
||||||
|
if (capacity < capacity_)
|
||||||
|
Grow(capacity - capacity_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_back(const T &value) {
|
||||||
|
if (size_ == capacity_)
|
||||||
|
Grow(size_ + 1);
|
||||||
|
ptr_[size_++] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void append(const T *begin, const T *end) {
|
||||||
|
std::ptrdiff_t size = end - begin;
|
||||||
|
if (size_ + size > capacity_)
|
||||||
|
Grow(size);
|
||||||
|
std::copy(begin, end, ptr_ + size_);
|
||||||
|
size_ += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
T &operator[](std::size_t index) { return ptr_[index]; }
|
||||||
|
const T &operator[](std::size_t index) const { return ptr_[index]; }
|
||||||
|
|
||||||
|
const T &back() const { return ptr_[size_ - 1]; }
|
||||||
|
T &back() { return ptr_[size_ - 1]; }
|
||||||
|
|
||||||
|
void clear() { size_ = 0; }
|
||||||
|
|
||||||
|
void Take(Buffer &other) {
|
||||||
|
if (ptr_ != data_)
|
||||||
|
delete [] ptr_;
|
||||||
|
size_ = other.size_;
|
||||||
|
if (other.ptr_ != data_) {
|
||||||
|
ptr_ = other.ptr_;
|
||||||
|
other.ptr_ = 0;
|
||||||
|
} else {
|
||||||
|
ptr_ = data_;
|
||||||
|
std::copy(other.ptr_, other.ptr_ + size_, data_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// A sprintf-like formatter that automatically allocates enough storage to
|
// A sprintf-like formatter that automatically allocates enough storage to
|
||||||
// fit all the output.
|
// fit all the output.
|
||||||
class Formatter {
|
class Formatter {
|
||||||
private:
|
private:
|
||||||
std::vector<char> buffer_; // Output buffer.
|
Buffer<char, 500> buffer_; // Output buffer.
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
// Numeric types should go first.
|
// Numeric types should go first.
|
||||||
@@ -40,7 +118,8 @@ class Formatter {
|
|||||||
|
|
||||||
typedef void (Formatter::*FormatFunc)(const void *arg, int width);
|
typedef void (Formatter::*FormatFunc)(const void *arg, int width);
|
||||||
|
|
||||||
// An argument.
|
public:
|
||||||
|
// A format argument.
|
||||||
struct Arg {
|
struct Arg {
|
||||||
Type type;
|
Type type;
|
||||||
union {
|
union {
|
||||||
@@ -50,11 +129,9 @@ class Formatter {
|
|||||||
long long_value;
|
long long_value;
|
||||||
unsigned long ulong_value;
|
unsigned long ulong_value;
|
||||||
long double long_double_value;
|
long double long_double_value;
|
||||||
struct {
|
|
||||||
union {
|
|
||||||
const char *string_value;
|
|
||||||
const void *pointer_value;
|
const void *pointer_value;
|
||||||
};
|
struct {
|
||||||
|
const char *string_value;
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
};
|
};
|
||||||
struct {
|
struct {
|
||||||
@@ -63,6 +140,7 @@ class Formatter {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Arg() {}
|
||||||
explicit Arg(int value) : type(INT), int_value(value) {}
|
explicit Arg(int value) : type(INT), int_value(value) {}
|
||||||
explicit Arg(unsigned value) : type(UINT), uint_value(value) {}
|
explicit Arg(unsigned value) : type(UINT), uint_value(value) {}
|
||||||
explicit Arg(long value) : type(LONG), long_value(value) {}
|
explicit Arg(long value) : type(LONG), long_value(value) {}
|
||||||
@@ -78,15 +156,13 @@ class Formatter {
|
|||||||
: type(CUSTOM), custom_value(value), format(f) {}
|
: type(CUSTOM), custom_value(value), format(f) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<Arg> args_; // Format arguments.
|
Buffer<Arg, 10> args_; // Format arguments.
|
||||||
|
|
||||||
const char *format_; // Format string.
|
const char *format_; // Format string.
|
||||||
|
|
||||||
friend class ArgFormatter;
|
friend class ArgFormatter;
|
||||||
|
|
||||||
void Add(const Arg &arg) {
|
void Add(const Arg &arg) {
|
||||||
if (args_.empty())
|
|
||||||
args_.reserve(10);
|
|
||||||
args_.push_back(arg);
|
args_.push_back(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +181,11 @@ class Formatter {
|
|||||||
|
|
||||||
void Format();
|
void Format();
|
||||||
|
|
||||||
|
void Take(Formatter &f) {
|
||||||
|
buffer_.Take(f.buffer_);
|
||||||
|
args_.Take(f.args_);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Formatter() : format_(0) {}
|
Formatter() : format_(0) {}
|
||||||
|
|
||||||
@@ -113,14 +194,10 @@ class Formatter {
|
|||||||
template <typename Callback>
|
template <typename Callback>
|
||||||
ArgFormatterWithCallback<Callback> FormatWithCallback(const char *format);
|
ArgFormatterWithCallback<Callback> FormatWithCallback(const char *format);
|
||||||
|
|
||||||
const char *c_str() const { return &buffer_[0]; }
|
|
||||||
const char *data() const { return &buffer_[0]; }
|
|
||||||
std::size_t size() const { return buffer_.size(); }
|
std::size_t size() const { return buffer_.size(); }
|
||||||
|
|
||||||
void Swap(Formatter &f) {
|
const char *data() const { return &buffer_[0]; }
|
||||||
buffer_.swap(f.buffer_);
|
const char *c_str() const { return &buffer_[0]; }
|
||||||
args_.swap(f.args_);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ArgFormatter {
|
class ArgFormatter {
|
||||||
@@ -255,14 +332,17 @@ void Formatter::FormatCustomArg(const void *arg, int width) {
|
|||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << value;
|
os << value;
|
||||||
std::string str(os.str());
|
std::string str(os.str());
|
||||||
buffer_.reserve(buffer_.size() + std::max<std::size_t>(width, str.size()));
|
std::size_t offset = buffer_.size();
|
||||||
buffer_.insert(buffer_.end(), str.begin(), str.end());
|
buffer_.resize(offset + std::max<std::size_t>(width, str.size()));
|
||||||
|
char *out = &buffer_[offset];
|
||||||
|
std::copy(str.begin(), str.end(), out);
|
||||||
if (width > str.size())
|
if (width > str.size())
|
||||||
buffer_.resize(buffer_.size() + width - str.size(), ' ');
|
std::fill_n(out + str.size(), width - str.size(), ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
inline ArgFormatter Formatter::operator()(const char *format) {
|
inline ArgFormatter Formatter::operator()(const char *format) {
|
||||||
format_ = format;
|
format_ = format;
|
||||||
|
args_.clear();
|
||||||
return ArgFormatter(*this);
|
return ArgFormatter(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,6 +350,7 @@ template <typename Callback>
|
|||||||
ArgFormatterWithCallback<Callback>
|
ArgFormatterWithCallback<Callback>
|
||||||
Formatter::FormatWithCallback(const char *format) {
|
Formatter::FormatWithCallback(const char *format) {
|
||||||
format_ = format;
|
format_ = format;
|
||||||
|
args_.clear();
|
||||||
return ArgFormatterWithCallback<Callback>(*this);
|
return ArgFormatterWithCallback<Callback>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +367,7 @@ class FullFormat : public ArgFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FullFormat(const FullFormat& other) : ArgFormatter(other) {
|
FullFormat(const FullFormat& other) : ArgFormatter(other) {
|
||||||
formatter_.Swap(other.formatter_);
|
formatter_.Take(other.formatter_);
|
||||||
}
|
}
|
||||||
|
|
||||||
~FullFormat() {
|
~FullFormat() {
|
||||||
|
Reference in New Issue
Block a user