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;
}
}
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;
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
// 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
if (space <= last + (newline ? indent : 0))
{
space = pos;
- while (space < str.size () && !isspace (str[space]))
+ while (space < str_size && !isspace (str[space]))
space++;
}
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')
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 ();
}
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<wrapline_t>
{
- std::string const &_m_image;
+ char const *_m_image;
public:
typedef std::vector<wrapline_t> super_t;
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;