micro optimize scoped_padder

This commit is contained in:
gabime 2018-11-11 00:31:14 +02:00
parent 3ff541cf77
commit 3cdf2b7f04

View File

@ -48,31 +48,29 @@ struct padding_info
class scoped_pad class scoped_pad
{ {
public: public:
static const size_t max_width = 128;
scoped_pad(size_t wrapped_size, padding_info &padinfo, fmt::memory_buffer &dest): scoped_pad(size_t wrapped_size, padding_info &padinfo, fmt::memory_buffer &dest):
padinfo_(padinfo) padinfo_(padinfo)
, dest_(dest) , dest_(dest)
, nchars_(padinfo_.width_ > wrapped_size ? padinfo_.width_ - wrapped_size : 0)
{ {
if(nchars_ == 0) if(padinfo_.width_ <= wrapped_size)
{ {
nchars_ = 0;
return; return;
} }
nchars_= padinfo.width_ - wrapped_size;
if (padinfo_.side_ == padding_info::left) if (padinfo_.side_ == padding_info::left)
{ {
pad_it(nchars_); pad_it(nchars_);
nchars_ = 0; nchars_ = 0;
return;
} }
else if(padinfo_.side_ == padding_info::center) else if(padinfo_.side_ == padding_info::center)
{ {
auto half_chars = nchars_/ 2;
pad_it(nchars_/ 2); auto reminder = nchars_ % 2;
return; pad_it(half_chars);
nchars_= half_chars + reminder; //for the right side
} }
} }
@ -83,27 +81,17 @@ public:
~scoped_pad() ~scoped_pad()
{ {
if(nchars_)
if(nchars_ == 0)
{
return;
}
if (padinfo_.side_ == padding_info::right)
{ {
pad_it(nchars_); pad_it(nchars_);
} }
else // padinfo_.side_ == padding_info::center
{
pad_it( (nchars_/ 2) + (nchars_% 2) ) ;
}
} }
private: private:
void pad_it(size_t count) void pad_it(size_t count)
{ {
//count = std::min(count, spaces_.size()); //count = std::min(count, spaces_.size());
assert(count <= spaces_.size());
fmt_helper::append_string_view(string_view_t(spaces_.data(), count), dest_); fmt_helper::append_string_view(string_view_t(spaces_.data(), count), dest_);
} }
@ -1083,7 +1071,7 @@ private:
{ {
using details::padding_info; using details::padding_info;
using details::scoped_pad; using details::scoped_pad;
const size_t max_width = 128;
if (it == end) if (it == end)
{ {
return padding_info(); return padding_info();
@ -1116,7 +1104,7 @@ private:
auto digit = static_cast<size_t>(*it - '0'); auto digit = static_cast<size_t>(*it - '0');
width = width * 10 + digit; width = width * 10 + digit;
} }
return details::padding_info{std::min(width, scoped_pad::max_width), side}; return details::padding_info{std::min(width, max_width), side};
} }
void compile_pattern_(const std::string &pattern) void compile_pattern_(const std::string &pattern)