From: Petr Machata Date: Fri, 1 Oct 2010 13:55:45 +0000 (+0200) Subject: dwarflint: Add a function to format N spaces X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=451e8d4904b54d29048d250a95721a2bb53571b9;p=thirdparty%2Felfutils.git dwarflint: Add a function to format N spaces --- diff --git a/dwarflint/option.cc b/dwarflint/option.cc index 5312a7248..99cfd0f24 100644 --- a/dwarflint/option.cc +++ b/dwarflint/option.cc @@ -218,7 +218,6 @@ option_common::format () const { char buf[3] = {}; std::sprintf (buf, "-%c", _m_opt.key); - std::string xxx (buf); ret += buf; } diff --git a/dwarflint/wrap.cc b/dwarflint/wrap.cc index 37f47f6ed..a0da874e3 100644 --- a/dwarflint/wrap.cc +++ b/dwarflint/wrap.cc @@ -52,9 +52,7 @@ wrapline_t::build (std::string const &image) const { assert (_m_end <= image.size ()); std::string main = image.substr (_m_start, _m_end - _m_start); - char padding[_m_indent + 1]; - std::memset (padding, ' ', _m_indent); - padding[_m_indent] = 0; + char const *padding = spaces (_m_indent); return std::string (padding) + main; } @@ -142,6 +140,40 @@ wrap_str::build (wrap_str::const_iterator it) const return it->build (_m_image); } +namespace +{ + template + class spc + { + char *_m_buf; + char *_m_endp; + public: + spc () + : _m_buf (new char[Max]) + , _m_endp (_m_buf + Max - 1) + { + std::memset (_m_buf, ' ', Max - 1); + _m_buf[Max] = 0; + } + ~spc () + { + delete [] _m_buf; + } + char const *get (size_t n) + { + assert (n < Max); + return _m_endp - n; + } + }; +} + +char const * +spaces (size_t n) +{ + static spc<128> spaces; + return spaces.get (n); +} + namespace { class tests @@ -152,9 +184,19 @@ namespace return wrap_str (str, width).join (); } + std::string + spacess (size_t i) + { + return spaces (i); + } + public: tests () { + assert (spacess (0) == ""); + assert (spacess (1) == " "); + assert (spacess (2) == " "); + assert (spacess (10) == " "); assert (wrap ("a b c d", 1) == "a\nb\nc\nd\n"); assert (wrap ("a bbbbb c d", 1) == "a\nbbbbb\nc\nd\n"); assert (wrap ("a b", 3) == "a b\n"); diff --git a/dwarflint/wrap.hh b/dwarflint/wrap.hh index 1c009f526..c7e12fec5 100644 --- a/dwarflint/wrap.hh +++ b/dwarflint/wrap.hh @@ -61,4 +61,6 @@ public: std::string build (const_iterator it) const; }; +char const *spaces (size_t n); + #endif//DWARFLINT_WRAP_HH