mlpack 3.4.2
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
15namespace mlpack {
16namespace util {
17
27inline std::string HyphenateString(const std::string& str,
28 const std::string& prefix,
29 const bool force = false)
30{
31 if (prefix.size() >= 80)
32 {
33 throw std::invalid_argument("Prefix size must be less than 80");
34 }
35
36 size_t margin = 80 - prefix.size();
37 if (str.length() < margin && !force)
38 return str;
39 std::string out("");
40 unsigned int pos = 0;
41 // First try to look as far as possible.
42 while (pos < str.length())
43 {
44 size_t splitpos;
45 // Check that we don't have a newline first.
46 splitpos = str.find('\n', pos);
47 if (splitpos == std::string::npos || splitpos > (pos + margin))
48 {
49 // We did not find a newline.
50 if (str.length() - pos < margin)
51 {
52 splitpos = str.length(); // The rest fits on one line.
53 }
54 else
55 {
56 splitpos = str.rfind(' ', margin + pos); // Find nearest space.
57 if (splitpos <= pos || splitpos == std::string::npos) // Not found.
58 splitpos = pos + margin;
59 }
60 }
61 out += str.substr(pos, (splitpos - pos));
62 if (splitpos < str.length())
63 {
64 out += '\n';
65 out += prefix;
66 }
67
68 pos = splitpos;
69 if (str[pos] == ' ' || str[pos] == '\n')
70 pos++;
71 }
72 return out;
73}
74
82inline std::string HyphenateString(const std::string& str, int padding)
83{
84 return HyphenateString(str, std::string(padding, ' '));
85}
86
87} // namespace util
88} // namespace mlpack
89
90#endif
std::string HyphenateString(const std::string &str, const std::string &prefix, const bool force=false)
Hyphenate a string or split it onto multiple 80-character lines, with some amount of padding on each ...
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1