hyphenate_string.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_UTIL_HYPHENATE_STRING_HPP
13 #define MLPACK_CORE_UTIL_HYPHENATE_STRING_HPP
14 
15 namespace mlpack {
16 namespace util {
17 
25 inline std::string HyphenateString(const std::string& str, int padding)
26 {
27  size_t margin = 80 - padding;
28  if (str.length() < margin)
29  return str;
30  std::string out("");
31  unsigned int pos = 0;
32  // First try to look as far as possible.
33  while (pos < str.length())
34  {
35  size_t splitpos;
36  // Check that we don't have a newline first.
37  splitpos = str.find('\n', pos);
38  if (splitpos == std::string::npos || splitpos > (pos + margin))
39  {
40  // We did not find a newline.
41  if (str.length() - pos < margin)
42  {
43  splitpos = str.length(); // The rest fits on one line.
44  }
45  else
46  {
47  splitpos = str.rfind(' ', margin + pos); // Find nearest space.
48  if (splitpos <= pos || splitpos == std::string::npos) // Not found.
49  splitpos = pos + margin;
50  }
51  }
52  out += str.substr(pos, (splitpos - pos));
53  if (splitpos < str.length())
54  {
55  out += '\n';
56  out += std::string(padding, ' ');
57  }
58 
59  pos = splitpos;
60  if (str[pos] == ' ' || str[pos] == '\n')
61  pos++;
62  }
63  return out;
64 }
65 
66 } // namespace util
67 } // namespace mlpack
68 
69 #endif
.hpp
Definition: add_to_po.hpp:21
std::string HyphenateString(const std::string &str, int padding)
 * Hyphenate a string or split it onto multiple 80-character lines, with some  * amount of padding on...