Check-in [87f57f1b7b]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:87f57f1b7b391f19f91a15971658cb80116bbc33
Date: 2008-06-12 01:14:30
User: stephan
Comment:egg
Tags And Properties
Changes
hide diffs unified diffs patch

Added parsepp_strings.hpp

Old () New (23eb7bbda4c8ed05)
> 1 #ifndef S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED
> 2 #define S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED
> 3
> 4 #define RL rule_list
> 5 #define CL char_list
> 6 namespace Ps {
> 7 namespace strings {
> 8 using namespace Ps;
> 9
> 10 //! Starts reading of a string.
> 11 struct op_startstring
> 12 {
> 13 template< typename State >
> 14 static void matched( parser_state &, const std::string &, State & s)
> 15 {
> 16 //COUT << "start "<<CH<<"-style string\n";
> 17 s = std::string();
> 18 }
> 19 };
> 20
> 21 //! Appends a char to a string.
> 22 struct op_appendchar
> 23 {
> 24 template< typename State >
> 25 static void matched( parser_state &, const std::string & m, State & s )
> 26 {
> 27 if( m.empty() ) return;
> 28 s += m;
> 29 //COUT << "appendchar"<<m<<". state == [" << s << "]\n";
> 30 }
> 31 };
> 32
> 33 //! Appends an escaped char (unescaped) to a string.
> 34 template <char CH>
> 35 struct op_appendesc
> 36 {
> 37 template< typename State >
> 38 static void matched( parser_state &, const std::string &, State & s )
> 39 {
> 40 s.push_back(CH);
> 41 }
> 42 };
> 43
> 44 //! Ends reading of a string.
> 45 struct op_endstring
> 46 {
> 47 template< typename State >
> 48 static void matched( parser_state &, const std::string &, State & )
> 49 {
> 50 //s.str = s.buf;
> 51 //s.buf = "";
> 52 }
> 53 };
> 54
> 55
> 56
> 57 //! Reads an escape sequence. QC == QuoteChar (either " or ')
> 58 template <char QC>
> 59 struct read_escchar :
> 60 r_and< RL<
> 61 r_ch<'\\'>,
> 62 r_or< RL< r_action< r_ch<QC>, op_appendesc<QC> >,
> 63 r_action< r_ch<'\\'>, op_appendesc<'\\'> >,
> 64 r_action< r_ch<'t'>, op_appendesc<'\t'> >,
> 65 r_action< r_ch<'b'>, op_appendesc<'\b'> >,
> 66 r_action< r_ch<'n'>, op_appendesc<'\n'> >,
> 67 r_action< r_ch<'r'>, op_appendesc<'\r'> >,
> 68 r_action< r_ch<'v'>, op_appendesc<'\v'> > > >
> 69 > >
> 70 {};
> 71
> 72
> 73
> 74 //! Reads a single char or known escape sequence for a string.
> 75 template <char QC>
> 76 struct read_char :
> 77 r_or< RL<
> 78 read_escchar<QC>, r_action< r_notch<QC>, op_appendchar >
> 79 > >
> 80 //r_action< r_action<r_not<r_ch<QC>>,op_dump>, op_appendchar >
> 81 //r_action< r_action<r_any,op_dump>, op_appendchar >
> 82 //r_action< r_action<r_not<r_oneof<'x','y','n'>>,op_dump>, op_appendchar >
> 83 //r_action< r_action<r_notch<QC>,op_dump>, op_appendchar >
> 84 {};
> 85
> 86 /**
> 87 Reads a string enclosed in QC characters. QC should be one of ' or "
> 88 and if that character is in the string content, it must be escaped
> 89 with a backslash character.
> 90
> 91 At the opening quote the State is cleared. Characters are
> 92 appended until an unescaped quote char is found, then the State
> 93 is "finalized". If the rule does not match, State.str will not
> 94 be modified, but State.buf might be (containing content up until
> 95 the point of the parse error).
> 96 */
> 97 template <char QC>
> 98 struct read_qstring :
> 99 r_and< RL<
> 100 r_action< r_ch<QC>, op_startstring >,
> 101 r_star< read_char<QC> >,
> 102 r_action< r_ch<QC>, op_endstring >
> 103 > >
> 104 {};
> 105
> 106 //! Matches a double-quoted string.
> 107 struct read_dqstring : read_qstring<'"'> {};
> 108 //! Matches a single-quoted string.
> 109 struct read_sqstring : read_qstring<'\''> {};
> 110
> 111 /**
> 112 Matches single- or double-quoted strings. It expects a
> 113 std::string State object, to which it writes the output
> 114 (overwriting any existing content). This routine unescapes
> 115 common C-style escape constructs when building the target
> 116 string.
> 117 */
> 118 struct parse_string :
> 119 r_or< RL< read_dqstring, read_sqstring > >
> 120 {};
> 121
> 122 /**
> 123 A convenience form of parse<parse_string>(). It reads from
> 124 src, which is expected to be string data (possibly escaped
> 125 using C-style escaped) enclosed in either single or double
> 126 quotes. If the parse is successful tgt is overwritten with the
> 127 unescaped content of the string (minus the enclosing quotes)
> 128 and true is returned. If it fails to parse then false is
> 129 returned and tgt is not modified.
> 130 */
> 131 inline bool parse_quoted_string( std::string const & src,
> 132 std::string & tgt )
> 133 {
> 134 std::string ss;
> 135 return parse<parse_string>( src, ss )
> 136 ? (tgt = ss,true)
> 137 : false;
> 138 };
> 139
> 140 }} // namespaces
> 141 #undef RL
> 142 #undef CL
> 143 #endif // S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED