1 1.1 jmmv // Copyright 2010 Google Inc. 2 1.1 jmmv // All rights reserved. 3 1.1 jmmv // 4 1.1 jmmv // Redistribution and use in source and binary forms, with or without 5 1.1 jmmv // modification, are permitted provided that the following conditions are 6 1.1 jmmv // met: 7 1.1 jmmv // 8 1.1 jmmv // * Redistributions of source code must retain the above copyright 9 1.1 jmmv // notice, this list of conditions and the following disclaimer. 10 1.1 jmmv // * Redistributions in binary form must reproduce the above copyright 11 1.1 jmmv // notice, this list of conditions and the following disclaimer in the 12 1.1 jmmv // documentation and/or other materials provided with the distribution. 13 1.1 jmmv // * Neither the name of Google Inc. nor the names of its contributors 14 1.1 jmmv // may be used to endorse or promote products derived from this software 15 1.1 jmmv // without specific prior written permission. 16 1.1 jmmv // 17 1.1 jmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 1.1 jmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 1.1 jmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 1.1 jmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 1.1 jmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 1.1 jmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 1.1 jmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 1.1 jmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 1.1 jmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 1.1 jmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 1.1 jmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 jmmv 29 1.1 jmmv #include "utils/format/formatter.hpp" 30 1.1 jmmv 31 1.1 jmmv #include <memory> 32 1.1 jmmv #include <string> 33 1.1 jmmv #include <utility> 34 1.1 jmmv 35 1.1 jmmv #include "utils/format/exceptions.hpp" 36 1.1 jmmv #include "utils/sanity.hpp" 37 1.1 jmmv #include "utils/text/exceptions.hpp" 38 1.1 jmmv #include "utils/text/operations.ipp" 39 1.1 jmmv 40 1.1 jmmv namespace format = utils::format; 41 1.1 jmmv namespace text = utils::text; 42 1.1 jmmv 43 1.1 jmmv 44 1.1 jmmv namespace { 45 1.1 jmmv 46 1.1 jmmv 47 1.1 jmmv /// Finds the next placeholder in a string. 48 1.1 jmmv /// 49 1.1 jmmv /// \param format The original format string provided by the user; needed for 50 1.1 jmmv /// error reporting purposes only. 51 1.1 jmmv /// \param expansion The string containing the placeholder to look for. Any 52 1.1 jmmv /// '%%' in the string will be skipped, and they must be stripped later by 53 1.1 jmmv /// strip_double_percent(). 54 1.1 jmmv /// \param begin The position from which to start looking for the next 55 1.1 jmmv /// placeholder. 56 1.1 jmmv /// 57 1.1 jmmv /// \return The position in the string in which the placeholder is located and 58 1.1 jmmv /// the placeholder itself. If there are no placeholders left, this returns 59 1.1 jmmv /// the length of the string and an empty string. 60 1.1 jmmv /// 61 1.1 jmmv /// \throw bad_format_error If the input string contains a trailing formatting 62 1.1 jmmv /// character. We cannot detect any other kind of invalid formatter because 63 1.1 jmmv /// we do not implement a full parser for them. 64 1.1 jmmv static std::pair< std::string::size_type, std::string > 65 1.1 jmmv find_next_placeholder(const std::string& format, 66 1.1 jmmv const std::string& expansion, 67 1.1 jmmv std::string::size_type begin) 68 1.1 jmmv { 69 1.1 jmmv begin = expansion.find('%', begin); 70 1.1 jmmv while (begin != std::string::npos && expansion[begin + 1] == '%') 71 1.1 jmmv begin = expansion.find('%', begin + 2); 72 1.1 jmmv if (begin == std::string::npos) 73 1.1 jmmv return std::make_pair(expansion.length(), ""); 74 1.1 jmmv if (begin == expansion.length() - 1) 75 1.1 jmmv throw format::bad_format_error(format, "Trailing %"); 76 1.1 jmmv 77 1.1 jmmv std::string::size_type end = begin + 1; 78 1.1 jmmv while (end < expansion.length() && expansion[end] != 's') 79 1.1 jmmv end++; 80 1.1 jmmv const std::string placeholder = expansion.substr(begin, end - begin + 1); 81 1.1 jmmv if (end == expansion.length() || 82 1.1 jmmv placeholder.find('%', 1) != std::string::npos) 83 1.1 jmmv throw format::bad_format_error(format, "Unterminated placeholder '" + 84 1.1 jmmv placeholder + "'"); 85 1.1 jmmv return std::make_pair(begin, placeholder); 86 1.1 jmmv } 87 1.1 jmmv 88 1.1 jmmv 89 1.1 jmmv /// Converts a string to an integer. 90 1.1 jmmv /// 91 1.1 jmmv /// \param format The format string; for error reporting purposes only. 92 1.1 jmmv /// \param str The string to conver. 93 1.1 jmmv /// \param what The name of the field this integer belongs to; for error 94 1.1 jmmv /// reporting purposes only. 95 1.1 jmmv /// 96 1.1 jmmv /// \return An integer representing the input string. 97 1.1 jmmv inline int 98 1.1 jmmv to_int(const std::string& format, const std::string& str, const char* what) 99 1.1 jmmv { 100 1.1 jmmv try { 101 1.1 jmmv return text::to_type< int >(str); 102 1.1 jmmv } catch (const text::value_error& e) { 103 1.1 jmmv throw format::bad_format_error(format, "Invalid " + std::string(what) + 104 1.1 jmmv "specifier"); 105 1.1 jmmv } 106 1.1 jmmv } 107 1.1 jmmv 108 1.1 jmmv 109 1.1 jmmv /// Constructs an std::ostringstream based on a formatting placeholder. 110 1.1 jmmv /// 111 1.1 jmmv /// \param format The format placeholder; may be empty. 112 1.1 jmmv /// 113 1.1 jmmv /// \return A new std::ostringstream that is prepared to format a single 114 1.1 jmmv /// object in the manner specified by the format placeholder. 115 1.1 jmmv /// 116 1.1 jmmv /// \throw bad_format_error If the format string is bad. We do minimal 117 1.1 jmmv /// validation on this string though. 118 1.1 jmmv static std::ostringstream* 119 1.1 jmmv new_ostringstream(const std::string& format) 120 1.1 jmmv { 121 1.2 lukem std::unique_ptr< std::ostringstream > output(new std::ostringstream()); 122 1.1 jmmv 123 1.1 jmmv if (format.length() <= 2) { 124 1.1 jmmv // If the format is empty, we create a new stream so that we don't have 125 1.1 jmmv // to check for NULLs later on. We rarely should hit this condition 126 1.1 jmmv // (and when we do it's a bug in the caller), so this is not a big deal. 127 1.1 jmmv // 128 1.1 jmmv // Otherwise, if the format is a regular '%s', then we don't have to do 129 1.1 jmmv // any processing for additional formatters. So this is just a "fast 130 1.1 jmmv // path". 131 1.1 jmmv } else { 132 1.1 jmmv std::string partial = format.substr(1, format.length() - 2); 133 1.1 jmmv if (partial[0] == '0') { 134 1.1 jmmv output->fill('0'); 135 1.1 jmmv partial.erase(0, 1); 136 1.1 jmmv } 137 1.1 jmmv if (!partial.empty()) { 138 1.1 jmmv const std::string::size_type dot = partial.find('.'); 139 1.1 jmmv if (dot != 0) 140 1.1 jmmv output->width(to_int(format, partial.substr(0, dot), "width")); 141 1.1 jmmv if (dot != std::string::npos) { 142 1.1 jmmv output->setf(std::ios::fixed, std::ios::floatfield); 143 1.1 jmmv output->precision(to_int(format, partial.substr(dot + 1), 144 1.1 jmmv "precision")); 145 1.1 jmmv } 146 1.1 jmmv } 147 1.1 jmmv } 148 1.1 jmmv 149 1.1 jmmv return output.release(); 150 1.1 jmmv } 151 1.1 jmmv 152 1.1 jmmv 153 1.1 jmmv /// Replaces '%%' by '%' in a given string range. 154 1.1 jmmv /// 155 1.1 jmmv /// \param in The input string to be rewritten. 156 1.1 jmmv /// \param begin The position at which to start the replacement. 157 1.1 jmmv /// \param end The position at which to end the replacement. 158 1.1 jmmv /// 159 1.1 jmmv /// \return The modified string and the amount of characters removed. 160 1.1 jmmv static std::pair< std::string, int > 161 1.1 jmmv strip_double_percent(const std::string& in, const std::string::size_type begin, 162 1.1 jmmv std::string::size_type end) 163 1.1 jmmv { 164 1.1 jmmv std::string part = in.substr(begin, end - begin); 165 1.1 jmmv 166 1.1 jmmv int removed = 0; 167 1.1 jmmv std::string::size_type pos = part.find("%%"); 168 1.1 jmmv while (pos != std::string::npos) { 169 1.1 jmmv part.erase(pos, 1); 170 1.1 jmmv ++removed; 171 1.1 jmmv pos = part.find("%%", pos + 1); 172 1.1 jmmv } 173 1.1 jmmv 174 1.1 jmmv return std::make_pair(in.substr(0, begin) + part + in.substr(end), removed); 175 1.1 jmmv } 176 1.1 jmmv 177 1.1 jmmv 178 1.1 jmmv } // anonymous namespace 179 1.1 jmmv 180 1.1 jmmv 181 1.1 jmmv /// Performs internal initialization of the formatter. 182 1.1 jmmv /// 183 1.1 jmmv /// This is separate from the constructor just because it is shared by different 184 1.1 jmmv /// overloaded constructors. 185 1.1 jmmv void 186 1.1 jmmv format::formatter::init(void) 187 1.1 jmmv { 188 1.1 jmmv const std::pair< std::string::size_type, std::string > placeholder = 189 1.1 jmmv find_next_placeholder(_format, _expansion, _last_pos); 190 1.1 jmmv const std::pair< std::string, int > no_percents = 191 1.1 jmmv strip_double_percent(_expansion, _last_pos, placeholder.first); 192 1.1 jmmv 193 1.1 jmmv _oss = new_ostringstream(placeholder.second); 194 1.1 jmmv 195 1.1 jmmv _expansion = no_percents.first; 196 1.1 jmmv _placeholder_pos = placeholder.first - no_percents.second; 197 1.1 jmmv _placeholder = placeholder.second; 198 1.1 jmmv } 199 1.1 jmmv 200 1.1 jmmv 201 1.1 jmmv /// Constructs a new formatter object (internal). 202 1.1 jmmv /// 203 1.1 jmmv /// \param format The format string. 204 1.1 jmmv /// \param expansion The format string with any replacements performed so far. 205 1.1 jmmv /// \param last_pos The position from which to start looking for formatting 206 1.1 jmmv /// placeholders. This must be maintained in case one of the replacements 207 1.1 jmmv /// introduced a new placeholder, which must be ignored. Think, for 208 1.1 jmmv /// example, replacing a "%s" string with "foo %s". 209 1.1 jmmv format::formatter::formatter(const std::string& format, 210 1.1 jmmv const std::string& expansion, 211 1.1 jmmv const std::string::size_type last_pos) : 212 1.1 jmmv _format(format), 213 1.1 jmmv _expansion(expansion), 214 1.1 jmmv _last_pos(last_pos), 215 1.1 jmmv _oss(NULL) 216 1.1 jmmv { 217 1.1 jmmv init(); 218 1.1 jmmv } 219 1.1 jmmv 220 1.1 jmmv 221 1.1 jmmv /// Constructs a new formatter object. 222 1.1 jmmv /// 223 1.1 jmmv /// \param format The format string. The formatters in the string are not 224 1.1 jmmv /// validated during construction, but will cause errors when used later if 225 1.1 jmmv /// they are invalid. 226 1.1 jmmv format::formatter::formatter(const std::string& format) : 227 1.1 jmmv _format(format), 228 1.1 jmmv _expansion(format), 229 1.1 jmmv _last_pos(0), 230 1.1 jmmv _oss(NULL) 231 1.1 jmmv { 232 1.1 jmmv init(); 233 1.1 jmmv } 234 1.1 jmmv 235 1.1 jmmv 236 1.1 jmmv format::formatter::~formatter(void) 237 1.1 jmmv { 238 1.1 jmmv delete _oss; 239 1.1 jmmv } 240 1.1 jmmv 241 1.1 jmmv 242 1.1 jmmv /// Returns the formatted string. 243 1.1 jmmv const std::string& 244 1.1 jmmv format::formatter::str(void) const 245 1.1 jmmv { 246 1.1 jmmv return _expansion; 247 1.1 jmmv } 248 1.1 jmmv 249 1.1 jmmv 250 1.1 jmmv /// Automatic conversion of formatter objects to strings. 251 1.1 jmmv /// 252 1.1 jmmv /// This is provided to allow painless injection of formatter objects into 253 1.1 jmmv /// streams, without having to manually call the str() method. 254 1.1 jmmv format::formatter::operator const std::string&(void) const 255 1.1 jmmv { 256 1.1 jmmv return _expansion; 257 1.1 jmmv } 258 1.1 jmmv 259 1.1 jmmv 260 1.1 jmmv /// Specialization of operator% for booleans. 261 1.1 jmmv /// 262 1.1 jmmv /// \param value The boolean to inject into the format string. 263 1.1 jmmv /// 264 1.1 jmmv /// \return A new formatter that has one less format placeholder. 265 1.1 jmmv format::formatter 266 1.1 jmmv format::formatter::operator%(const bool& value) const 267 1.1 jmmv { 268 1.1 jmmv (*_oss) << (value ? "true" : "false"); 269 1.1 jmmv return replace(_oss->str()); 270 1.1 jmmv } 271 1.1 jmmv 272 1.1 jmmv 273 1.1 jmmv /// Replaces the first formatting placeholder with a value. 274 1.1 jmmv /// 275 1.1 jmmv /// \param arg The replacement string. 276 1.1 jmmv /// 277 1.1 jmmv /// \return A new formatter in which the first formatting placeholder has been 278 1.1 jmmv /// replaced by arg and is ready to replace the next item. 279 1.1 jmmv /// 280 1.1 jmmv /// \throw utils::format::extra_args_error If there are no more formatting 281 1.1 jmmv /// placeholders in the input string, or if the placeholder is invalid. 282 1.1 jmmv format::formatter 283 1.1 jmmv format::formatter::replace(const std::string& arg) const 284 1.1 jmmv { 285 1.1 jmmv if (_placeholder_pos == _expansion.length()) 286 1.1 jmmv throw format::extra_args_error(_format, arg); 287 1.1 jmmv 288 1.1 jmmv const std::string expansion = _expansion.substr(0, _placeholder_pos) 289 1.1 jmmv + arg + _expansion.substr(_placeholder_pos + _placeholder.length()); 290 1.1 jmmv return formatter(_format, expansion, _placeholder_pos + arg.length()); 291 1.1 jmmv } 292