[ci] Test Tcl bindings for dragonfly/freebsd
[xapian.git] / xapian-applications / omega / csvescape.h
blob26d4ca7560c75fcf93392d28bf7af76f48c98659
1 /** @file
2 * @brief Escape a string for use in a CSV file
3 */
4 /* Copyright (C) 2015 Olly Betts
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
25 #ifndef OMEGA_INCLUDED_CSVESCAPE_H
26 #define OMEGA_INCLUDED_CSVESCAPE_H
28 #include <string>
30 /** @internal Helper function. */
31 void csv_escape_(std::string &s, std::string::size_type i);
33 /** Escape @a s for use as a field in a CSV file.
35 * Escaping is done as described in RFC4180, except that we treat any
36 * byte value not otherwise mentioned as being 'TEXTDATA' (so %x00-%x09,
37 * %x0B-%x0C, %x0E-%x1F, %x7F-%xFF are also permitted there).
39 inline void csv_escape(std::string &s) {
40 // Check if the string needs any escaping or quoting first.
41 std::string::size_type i = s.find_first_of(",\"\r\n");
42 if (i != std::string::npos) {
43 csv_escape_(s, i);
47 /** Escape @a s for use as a field in a CSV file.
49 * All values are escaped, whether or not RFC4180 says they need to be.
51 inline void csv_escape_always(std::string &s) {
52 return csv_escape_(s, 0);
55 #endif // OMEGA_INCLUDED_CSVESCAPE_H