8#include "wvbackslash.h"
10static const char *escapein =
"\a\b\f\n\r\t\v";
11static const char *escapeout =
"abfnrtv";
13static inline char tohex(
int digit,
char alphabase = (
'a' - 10))
15 return (digit < 10 ?
'0' : alphabase) + digit;
18static inline int fromhex(
char digit)
22 if (digit >=
'A' && digit <=
'F')
23 return digit -
'A' + 10;
24 if (digit >=
'a' && digit <=
'f')
25 return digit -
'a' + 10;
29static inline int fromoctal(
char digit)
31 if (digit >=
'0' && digit <=
'7')
48 size_t avail = outbuf.free();
50 while ((len = inbuf.optgettable()) != 0)
52 const unsigned char *datain = inbuf.get(len);
53 for (
size_t i = 0; i < len; ++i)
59 { outbuf.unget(len - i);
return !
flush; }
60 const char *foundnasty = NULL;
61 const char *foundspecial = NULL;
64 foundnasty = strchr(nasties.
cstr(), c);
67 foundspecial = strchr(escapein, c);
68 if (! foundspecial && isprint(c))
79 { outbuf.unget(len - i);
return !
flush; }
80 if (foundnasty != NULL)
87 if (foundspecial != NULL)
90 outbuf.putch(escapeout[foundspecial - escapein]);
97 { outbuf.unget(len - i);
return !
flush; }
99 outbuf.putch(tohex(c >> 4));
100 outbuf.putch(tohex(c & 15));
125 if (outbuf.free() == 0)
126 return inbuf.used() == 0;
127 if (! flushtmpbuf(outbuf))
131 while ((len = inbuf.optgettable()) != 0)
133 const unsigned char *datain = inbuf.get(len);
134 for (
size_t i = 0; i < len; ++i)
147 if (c >=
'0' && c <=
'3')
167 const char *found = strchr(escapeout, c);
170 c = escapein[found - escapeout];
179 int digit = fromhex(c);
190 value = (value << 4) | digit;
205 int digit = fromoctal(c);
208 value = (value << 3) | digit;
210 state = State(state + 1);
224 if (outbuf.free() == 0)
226 inbuf.unget(len - i);
233 if (inbuf.used() != 0)
236 return flushtmpbuf(outbuf);
252bool WvBackslashDecoder::flushtmpbuf(WvBuf &outbuf)
254 if (state != Initial)
263 size_t len = tmpbuf.
used();
266 size_t avail = outbuf.free();
269 outbuf.merge(tmpbuf, avail);
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
Template method implementation of encode().
WvBackslashDecoder()
Creates a C-style backslash decoder.
virtual bool _reset()
Template method implementation of reset().
WvBackslashEncoder(WvStringParm _nasties="\\\"")
Creates a C-style backslash encoder.
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
Template method implementation of encode().
virtual bool _reset()
Template method implementation of reset().
void unalloc(size_t count)
Unallocates exactly the specified number of elements by removing them from the buffer and releasing t...
void zap()
Clears the buffer.
size_t used() const
Returns the number of elements in the buffer currently available for reading.
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
const char * cstr() const
return a (const char *) for this string.