diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/spirit/example/karma/escaped_string.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/spirit/example/karma/escaped_string.cpp')
-rw-r--r-- | src/boost/libs/spirit/example/karma/escaped_string.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/example/karma/escaped_string.cpp b/src/boost/libs/spirit/example/karma/escaped_string.cpp new file mode 100644 index 00000000..f3c2fe6f --- /dev/null +++ b/src/boost/libs/spirit/example/karma/escaped_string.cpp @@ -0,0 +1,66 @@ +// Copyright (c) 2001-2010 Hartmut Kaiser +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// The purpose of this example is to show how any character sequence can be +// printed while being properly quoted. + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/include/karma.hpp> + +namespace client +{ + namespace karma = boost::spirit::karma; + + template <typename OutputIterator> + struct escaped_string + : karma::grammar<OutputIterator, std::string(char const*)> + { + escaped_string() + : escaped_string::base_type(esc_str) + { + esc_char.add('\a', "\\a")('\b', "\\b")('\f', "\\f")('\n', "\\n") + ('\r', "\\r")('\t', "\\t")('\v', "\\v")('\\', "\\\\") + ('\'', "\\\'")('\"', "\\\"") + ; + + esc_str = karma::lit(karma::_r1) + << *(esc_char | karma::print | "\\x" << karma::hex) + << karma::lit(karma::_r1) + ; + } + + karma::rule<OutputIterator, std::string(char const*)> esc_str; + karma::symbols<char, char const*> esc_char; + }; +} + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + namespace karma = boost::spirit::karma; + + typedef std::back_insert_iterator<std::string> sink_type; + + std::string generated; + sink_type sink(generated); + + std::string str("string to escape: \n\r\t\"'\x19"); + char const* quote = "'''"; + + client::escaped_string<sink_type> g; + if (!karma::generate(sink, g(quote), str)) + { + std::cout << "-------------------------\n"; + std::cout << "Generating failed\n"; + std::cout << "-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Generated: " << generated << "\n"; + std::cout << "-------------------------\n"; + } + return 0; +} |