summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/example/karma/escaped_string.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/spirit/example/karma/escaped_string.cpp')
-rw-r--r--src/boost/libs/spirit/example/karma/escaped_string.cpp66
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 000000000..f3c2fe6f4
--- /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;
+}