]> git.sesse.net Git - casparcg/blob - core/help/util.cpp
Fix a few Clang warnings.
[casparcg] / core / help / util.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "util.h"
23
24 #include <boost/algorithm/string/replace.hpp>
25 #include <boost/algorithm/string/split.hpp>
26 #include <boost/algorithm/string/classification.hpp>
27
28 #include <list>
29 #include <vector>
30 #include <sstream>
31
32 namespace caspar { namespace core {
33
34 void wordwrap_line(const std::wstring& line, int width, std::wostream& result)
35 {
36         std::list<std::wstring> words;
37         boost::split(words, line, boost::is_any_of(L" "), boost::algorithm::token_compress_on);
38
39         size_t current_width = 0;
40         bool first = true;
41
42         while (!words.empty())
43         {
44                 auto word = std::move(words.front());
45                 words.pop_front();
46
47                 if (first)
48                 {
49                         current_width = word.length();
50                         result << std::move(word);
51                         first = false;
52                 }
53                 else if (current_width + 1 + word.length() > width)
54                 {
55                         result << L"\n";
56                         current_width = word.length();
57                         result << std::move(word);
58                 }
59                 else
60                 {
61                         current_width += 1 + word.length();
62                         result << L" " << std::move(word);
63                 }
64         }
65 }
66
67 std::wstring wordwrap(const std::wstring& text, int width)
68 {
69         std::vector<std::wstring> lines;
70         boost::split(lines, text, boost::is_any_of(L"\n"));
71
72         std::wstringstream result;
73
74         for (auto& line : lines)
75         {
76                 wordwrap_line(line, width, result);
77                 result << L"\n";
78         }
79
80         return result.str();
81 }
82
83 std::wstring indent(std::wstring text, const std::wstring& indent)
84 {
85         text.insert(0, indent);
86         bool last_is_newline = text.at(text.length() - 1) == L'\n';
87
88         if (last_is_newline)
89                 text.erase(text.length() - 1);
90
91         boost::replace_all(text, L"\n", L"\n" + indent);
92
93         if (last_is_newline)
94                 text += L'\n';
95
96         return text;
97 }
98
99 }}