1 // Copyright 2012 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "utils/text/operations.ipp"
30
31 #include <sstream>
32
33 #include "utils/format/macros.hpp"
34 #include "utils/sanity.hpp"
35
36 namespace text = utils::text;
37
38
39 /// Surrounds a string with quotes, escaping the quote itself if needed.
40 ///
41 /// \param text The string to quote.
42 /// \param quote The quote character to use.
43 ///
44 /// \return The quoted string.
45 std::string
quote(const std::string & text,const char quote)46 text::quote(const std::string& text, const char quote)
47 {
48 std::ostringstream quoted;
49 quoted << quote;
50
51 std::string::size_type start_pos = 0;
52 std::string::size_type last_pos = text.find(quote);
53 while (last_pos != std::string::npos) {
54 quoted << text.substr(start_pos, last_pos - start_pos) << '\\';
55 start_pos = last_pos;
56 last_pos = text.find(quote, start_pos + 1);
57 }
58 quoted << text.substr(start_pos);
59
60 quoted << quote;
61 return quoted.str();
62 }
63
64
65 /// Fills a paragraph to the specified length.
66 ///
67 /// This preserves any sequence of spaces in the input and any possible
68 /// newlines. Sequences of spaces may be split in half (and thus one space is
69 /// lost), but the rest of the spaces will be preserved as either trailing or
70 /// leading spaces.
71 ///
72 /// \param input The string to refill.
73 /// \param target_width The width to refill the paragraph to.
74 ///
75 /// \return The refilled paragraph as a sequence of independent lines.
76 std::vector< std::string >
refill(const std::string & input,const std::size_t target_width)77 text::refill(const std::string& input, const std::size_t target_width)
78 {
79 std::vector< std::string > output;
80
81 std::string::size_type start = 0;
82 while (start < input.length()) {
83 std::string::size_type width;
84 if (start + target_width >= input.length())
85 width = input.length() - start;
86 else {
87 if (input[start + target_width] == ' ') {
88 width = target_width;
89 } else {
90 const std::string::size_type pos = input.find_last_of(
91 " ", start + target_width - 1);
92 if (pos == std::string::npos || pos < start + 1) {
93 width = input.find_first_of(" ", start + target_width);
94 if (width == std::string::npos)
95 width = input.length() - start;
96 else
97 width -= start;
98 } else {
99 width = pos - start;
100 }
101 }
102 }
103 INV(width != std::string::npos);
104 INV(start + width <= input.length());
105 INV(input[start + width] == ' ' || input[start + width] == '\0');
106 output.push_back(input.substr(start, width));
107
108 start += width + 1;
109 }
110
111 if (input.empty()) {
112 INV(output.empty());
113 output.push_back("");
114 }
115
116 return output;
117 }
118
119
120 /// Fills a paragraph to the specified length.
121 ///
122 /// See the documentation for refill() for additional details.
123 ///
124 /// \param input The string to refill.
125 /// \param target_width The width to refill the paragraph to.
126 ///
127 /// \return The refilled paragraph as a string with embedded newlines.
128 std::string
refill_as_string(const std::string & input,const std::size_t target_width)129 text::refill_as_string(const std::string& input, const std::size_t target_width)
130 {
131 return join(refill(input, target_width), "\n");
132 }
133
134
135 /// Splits a string into different components.
136 ///
137 /// \param str The string to split.
138 /// \param delimiter The separator to use to split the words.
139 ///
140 /// \return The different words in the input string as split by the provided
141 /// delimiter.
142 std::vector< std::string >
split(const std::string & str,const char delimiter)143 text::split(const std::string& str, const char delimiter)
144 {
145 std::vector< std::string > words;
146 if (!str.empty()) {
147 std::string::size_type pos = str.find(delimiter);
148 words.push_back(str.substr(0, pos));
149 while (pos != std::string::npos) {
150 ++pos;
151 const std::string::size_type next = str.find(delimiter, pos);
152 words.push_back(str.substr(pos, next - pos));
153 pos = next;
154 }
155 }
156 return words;
157 }
158
159
160 /// Converts a string to a boolean.
161 ///
162 /// \param str The string to convert.
163 ///
164 /// \return The converted string, if the input string was valid.
165 ///
166 /// \throw std::value_error If the input string does not represent a valid
167 /// boolean value.
168 template<>
169 bool
to_type(const std::string & str)170 text::to_type(const std::string& str)
171 {
172 if (str == "true")
173 return true;
174 else if (str == "false")
175 return false;
176 else
177 throw value_error(F("Invalid boolean value '%s'") % str);
178 }
179
180
181 /// Identity function for to_type, for genericity purposes.
182 ///
183 /// \param str The string to convert.
184 ///
185 /// \return The input string.
186 template<>
187 std::string
to_type(const std::string & str)188 text::to_type(const std::string& str)
189 {
190 return str;
191 }
192