From b99dc1e7c7c691ceaf8a76c7c23f689b8ffc0b53 Mon Sep 17 00:00:00 2001 From: Petr Machata Date: Fri, 1 Oct 2010 16:21:07 +0200 Subject: [PATCH] dwarflint: Avoid constructing temporary std::string in line wrapper --- dwarflint/wrap.cc | 27 ++++++++++++++------------- dwarflint/wrap.hh | 6 +++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/dwarflint/wrap.cc b/dwarflint/wrap.cc index e4b3f8ab5..3295179ea 100644 --- a/dwarflint/wrap.cc +++ b/dwarflint/wrap.cc @@ -31,9 +31,9 @@ namespace { size_t - skip_blank (std::string const &str, size_t pos) + skip_blank (char const *str, size_t pos) { - while (pos < str.size () && isblank (str[pos])) + while (isblank (str[pos])) pos++; return pos; } @@ -48,21 +48,22 @@ wrapline_t::wrapline_t (size_t start, size_t end, size_t indent) } std::string -wrapline_t::build (std::string const &image) const +wrapline_t::build (char const *image) const { - assert (_m_end <= image.size ()); - std::string main = image.substr (_m_start, _m_end - _m_start); + assert (_m_end <= std::strlen (image)); + std::string main (image, _m_start, _m_end - _m_start); char const *padding = spaces (_m_indent); return std::string (padding) + main; } -wrap_str::wrap_str (std::string const &str, unsigned width) +wrap_str::wrap_str (char const *str, unsigned width) : _m_image (str) { size_t pos = 0; bool newline = true; size_t indent = 0; - while (pos < str.size ()) + size_t str_size = std::strlen (str); + while (pos < str_size) { size_t last = pos; @@ -74,14 +75,14 @@ wrap_str::wrap_str (std::string const &str, unsigned width) if (newline) { pos = skip_blank (str, pos); - if (pos < str.size () && str[pos] == '-') + if (pos < str_size && str[pos] == '-') pos = skip_blank (str, pos + 1); indent = pos - last; } length -= indent; // Take the remainder of the line, but don't cross hard EOLs. - for (; length > 0 && pos < str.size (); --length) + for (; length > 0 && pos < str_size; --length) if (str[pos] == '\n') break; else @@ -91,7 +92,7 @@ wrap_str::wrap_str (std::string const &str, unsigned width) // Look as far back as the end of previous line. size_t space = pos; for (; space > last; --space) - if (space == str.size () || isspace (str[space])) + if (space == str_size || isspace (str[space])) break; // While skipping back, we might end at the very beginning. If @@ -101,7 +102,7 @@ wrap_str::wrap_str (std::string const &str, unsigned width) if (space <= last + (newline ? indent : 0)) { space = pos; - while (space < str.size () && !isspace (str[space])) + while (space < str_size && !isspace (str[space])) space++; } @@ -109,7 +110,7 @@ wrap_str::wrap_str (std::string const &str, unsigned width) push_back (wrapline_t (last, space, newline ? 0 : indent)); // Skip useless white space at the end of the line, up to EOL. - while (space < str.size () && isspace (str[space]) && str[space] != '\n') + while (space < str_size && isspace (str[space]) && str[space] != '\n') space++; if (str[space] == '\n') @@ -179,7 +180,7 @@ namespace class tests { std::string - wrap (std::string const &str, size_t width) + wrap (char const *str, size_t width) { return wrap_str (str, width).join (); } diff --git a/dwarflint/wrap.hh b/dwarflint/wrap.hh index c7e12fec5..f454ea778 100644 --- a/dwarflint/wrap.hh +++ b/dwarflint/wrap.hh @@ -38,13 +38,13 @@ class wrapline_t public: wrapline_t (size_t start, size_t end, size_t indent); - std::string build (std::string const &image) const; + std::string build (char const *image) const; }; class wrap_str : private std::vector { - std::string const &_m_image; + char const *_m_image; public: typedef std::vector super_t; @@ -55,7 +55,7 @@ public: using super_t::end; using super_t::empty; - wrap_str (std::string const &str, unsigned width); + wrap_str (char const *str, unsigned width); std::string join () const; std::string build (const_iterator it) const; -- 2.47.2