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();
52 const unsigned char *datain = inbuf.
get(len);
53 for (
size_t i = 0; i < len; ++i)
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))
80 if (foundnasty != NULL)
87 if (foundspecial != NULL)
90 outbuf.
putch(escapeout[foundspecial - escapein]);
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))
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 merge(Buffer &inbuf, size_t count)
Efficiently moves count bytes from the specified buffer into this one.
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
void unget(size_t count)
Ungets exactly the specified number of elements by returning them to the buffer for subsequent reads.
void unalloc(size_t count)
Unallocates exactly the specified number of elements by removing them from the buffer and releasing t...
size_t free() const
Returns the number of elements that the buffer can currently accept for writing.
void zap()
Clears the buffer.
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
void putch(int ch)
Puts a single character into the buffer as an int.
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.