$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r60479 - trunk/libs/spirit/example/scheme
From: joel_at_[hidden]
Date: 2010-03-11 08:37:33
Author: djowel
Date: 2010-03-11 08:37:32 EST (Thu, 11 Mar 2010)
New Revision: 60479
URL: http://svn.boost.org/trac/boost/changeset/60479
Log:
binary strings
Text files modified: 
   trunk/libs/spirit/example/scheme/out.txt          |     2 +-                                      
   trunk/libs/spirit/example/scheme/sexpr.hpp        |    20 ++++++++++++++++++++                    
   trunk/libs/spirit/example/scheme/sexpr_test.txt   |     1 +                                       
   trunk/libs/spirit/example/scheme/simple_print.hpp |    27 +++++++++++++++++++--------             
   4 files changed, 41 insertions(+), 9 deletions(-)
Modified: trunk/libs/spirit/example/scheme/out.txt
==============================================================================
--- trunk/libs/spirit/example/scheme/out.txt	(original)
+++ trunk/libs/spirit/example/scheme/out.txt	2010-03-11 08:37:32 EST (Thu, 11 Mar 2010)
@@ -1,2 +1,2 @@
-success: (123.45 true false 255 63 "this is a ⬠string" "Τη γλÏÏÏα μοÏ
 ÎδÏÏαν ελληνική" (92 ("another string" apple Sîne)))
+success: (123.45 true false 255 63 "this is a ⬠string" "Τη γλÏÏÏα μοÏ
 ÎδÏÏαν ελληνική" b0123456789abcdef0123456789abcdef (92 ("another string" apple Sîne)))
 
Modified: trunk/libs/spirit/example/scheme/sexpr.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/sexpr.hpp	(original)
+++ trunk/libs/spirit/example/scheme/sexpr.hpp	2010-03-11 08:37:32 EST (Thu, 11 Mar 2010)
@@ -93,6 +93,20 @@
             }
         };
 
+        struct push_binary
+        {
+            template <typename S, typename C>
+            struct result { typedef void type; };
+
+            void operator()(std::string& utf8, char byte) const
+            {
+                if (utf8.size() == 0)
+                    utf8 += '\1';   //  mark a symbol with prefix 1
+                                    //  (a 1 byte at the beginning signifies a binary stream)
+                utf8 += byte;
+            }
+        };
+
         struct push_esc
         {
             template <typename S, typename C>
@@ -149,7 +163,9 @@
         sexpr() : sexpr::base_type(start)
         {
             real_parser<double, strict_real_policies<double> > strict_double;
+            uint_parser<unsigned char, 16, 2, 2> hex2;
             function<detail::push_symbol_utf8> push_symbol_utf8;
+            function<detail::push_binary> push_binary;
 
             start   = atom | list;
 
@@ -158,6 +174,7 @@
             atom    = number                            [_val = _1]
                     | bool_                             [_val = _1]
                     | string                            [_val = _1]
+                    | byte_str                          [_val = _1]
                     | symbol                            [_val = _1]
                     ;
 
@@ -169,11 +186,14 @@
                     | lexeme['0' >> oct]                [_val = _1]
                     | int_                              [_val = _1]
                     ;
+
+            byte_str = lexeme[no_case['b'] >> +(hex2    [push_binary(_val, _1)])];
         }
 
         rule<Iterator, unicode, white_space<Iterator>, utree()> start, list;
         rule<Iterator, unicode, utree()> atom, number;
         rule<Iterator, unicode, std::string()> symbol;
+        rule<Iterator, unicode, std::string()> byte_str;
         scheme::string<Iterator> string;
     };
 }
Modified: trunk/libs/spirit/example/scheme/sexpr_test.txt
==============================================================================
--- trunk/libs/spirit/example/scheme/sexpr_test.txt	(original)
+++ trunk/libs/spirit/example/scheme/sexpr_test.txt	2010-03-11 08:37:32 EST (Thu, 11 Mar 2010)
@@ -6,6 +6,7 @@
     077
     "this is a \u20AC string"           ; A UTF-8 string
     "Τη γλÏÏÏα μοÏ
 ÎδÏÏαν ελληνική"     ; Another UTF-8 string
+	b0123456789ABCDEF0123456789abcdef	; A binary stream
     (
         92 ("another string" apple Sîne)
     )
Modified: trunk/libs/spirit/example/scheme/simple_print.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/simple_print.hpp	(original)
+++ trunk/libs/spirit/example/scheme/simple_print.hpp	2010-03-11 08:37:32 EST (Thu, 11 Mar 2010)
@@ -56,15 +56,26 @@
         {
             typedef typename Range::const_iterator iterator;
             iterator i = range.begin();
-            bool const is_symbol = *i == '\0';  // a 0 byte at the beginning signifies a symbol
-            if (!is_symbol)
-                (*this)('"');
+            if (*i == '\1') // a 1 byte at the beginning signifies a byte stream
+            {
+                out << "b"; ++i;
+                for (; i != range.end(); ++i)
+                    out << std::setw(2) << std::setfill('0')
+                        << std::hex << int((unsigned char)*i);
+                out << std::dec;
+            }
             else
-                ++i;
-            for (; i != range.end(); ++i)
-                (*this)(*i);
-            if (!is_symbol)
-                (*this)('"');
+            {
+                bool const is_symbol = *i == '\0';  // a 0 byte at the beginning signifies a symbol
+                if (!is_symbol)
+                    out << '"';
+                else
+                    ++i;
+                for (; i != range.end(); ++i)
+                    out << *i;
+                if (!is_symbol)
+                    out << '"';
+            }
         }
 
         template <typename Iterator>