69#include "InternalErr.h"
82hexstring(
unsigned char val)
85 buf << hex << setw(2) << setfill('0') << static_cast<unsigned int>(val);
97 tmp_str[0] =
static_cast<char>(val);
99 return string(tmp_str);
103octstring(
unsigned char val)
106 buf << oct << setw(3) << setfill(
'0')
107 <<
static_cast<unsigned int>(val);
120 DBG(cerr <<
"unoctstring: " << val << endl);
123 tmp_str[0] =
static_cast<char>(val);
125 return string(tmp_str);
153id2www(
string in,
const string &allowable)
155 string::size_type i = 0;
156 DBG(cerr<<
"Input string: [" << in <<
"]" << endl);
157 while ((i = in.find_first_not_of(allowable, i)) != string::npos) {
158 DBG(cerr<<
"Found escapee: [" << in[i] <<
"]");
159 in.replace(i, 1,
"%" + hexstring(in[i]));
160 DBGN(cerr<<
" now the string is: " << in << endl);
180 return id2www(in, allowable);
220www2id(
const string &in,
const string &escape,
const string &except)
222 string::size_type i = 0;
224 while ((i = res.find_first_of(escape, i)) != string::npos) {
225 if (except.find(res.substr(i, 3)) != string::npos) {
229 res.replace(i, 3, unhexstring(res.substr(i + 1, 2)));
240 case '>':
return ">";
241 case '<':
return "<";
242 case '&':
return "&";
243 case '\'':
return "'";
244 case '\"':
return """;
246 throw InternalErr(__FILE__, __LINE__,
"Unrecognized character.");
253octal_to_hex(
const string &octal_digits)
257 istringstream ss(octal_digits);
261 ds << hex << setw(2) << setfill(
'0') << val;
272id2xml(
string in,
const string ¬_allowed)
274 string::size_type i = 0;
276 while ((i = in.find_first_of(not_allowed, i)) != string::npos) {
277 in.replace(i, 1, entity(in[i]));
292 string octal_escape =
"\\\\";
294 string::size_type length = in.length();
295 while ((i = in.find(octal_escape, i)) != string::npos) {
297 string::size_type j = i + 2;
300 string octal_digits = in.substr(j, 3);
302 string hex_escape = string(
"&#x");
303 hex_escape.append(octal_to_hex(octal_digits));
304 hex_escape.append(
string(
";"));
307 in.replace(i, 5, hex_escape);
324 string::size_type i = 0;
326 while ((i = in.find(
">", i)) != string::npos)
327 in.replace(i, 4,
">");
330 while ((i = in.find(
"<", i)) != string::npos)
331 in.replace(i, 4,
"<");
334 while ((i = in.find(
"&", i)) != string::npos)
335 in.replace(i, 5,
"&");
338 while ((i = in.find(
"'", i)) != string::npos)
339 in.replace(i, 6,
"'");
342 while ((i = in.find(
""", i)) != string::npos)
343 in.replace(i, 6,
"\"");
356 string::size_type pos;
357 while ((pos = s.find(
'%')) != string::npos)
358 s.replace(pos, 3,
"_");
370 const string printable =
" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_-+={[}]|\\:;<,>.?/'\"";
371 const string ESC =
"\\";
372 const string DOUBLE_ESC = ESC + ESC;
373 const string QUOTE =
"\"";
374 const string ESCQUOTE = ESC + QUOTE;
377 string::size_type ind = 0;
378 while ((ind = s.find(ESC, ind)) != s.npos) {
379 s.replace(ind, 1, DOUBLE_ESC);
380 ind += DOUBLE_ESC.length();
385 while ((ind = s.find_first_not_of(printable, ind)) != s.npos)
386 s.replace(ind, 1, ESC + octstring(s[ind]));
390 while ((ind = s.find(QUOTE, ind)) != s.npos) {
391 s.replace(ind, 1, ESCQUOTE);
392 ind += ESCQUOTE.length();
409 const Regex octal(
"\\\\[0-3][0-7][0-7]");
410 const Regex esc_quote(
"\\\\\"");
411 const Regex esc_esc(
"\\\\\\\\");
412 const string ESC =
"\\";
413 const string QUOTE =
"\"";
417 DBG(cerr <<
"0XX" << s <<
"XXX" << endl);
419 index = esc_esc.
search(s.c_str(), s.length(), matchlen, 0);
420 while (index < s.length()) {
421 DBG(cerr <<
"1aXX" << s <<
"XXX index: " << index << endl);
422 s.replace(index, 2, ESC);
423 DBG(cerr <<
"1bXX" << s <<
"XXX index: " << index << endl);
424 index = esc_esc.
search(s.c_str(), s.length(), matchlen, 0);
428 index = esc_quote.
search(s.c_str(), s.length(), matchlen, 0);
429 while (index < s.length()) {
430 s.replace(index, 2, QUOTE);
431 DBG(cerr <<
"2XX" << s <<
"XXX index: " << index << endl);
432 index = esc_quote.
search(s.c_str(), s.length(), matchlen, 0);
436 index = octal.
search(s.c_str(), s.length(), matchlen, 0);
437 while (index < s.length()) {
438 s.replace(index, 4, unoctstring(s.substr(index + 1, 3)));
439 DBG(cerr <<
"3XX" << s <<
"XXX index: " << index << endl);
440 index = octal.
search(s.c_str(), s.length(), matchlen, 0);
443 DBG(cerr <<
"4XX" << s <<
"XXX" << endl);
448munge_error_message(
string msg)
451 if (*msg.begin() !=
'"')
452 msg.insert(msg.begin(),
'"');
453 if (*(msg.end() - 1) !=
'"')
457 string::iterator miter;
458 for (miter = msg.begin() + 1; miter != msg.end() - 1; miter++)
459 if (*miter ==
'"' && *(miter - 1) !=
'\\')
460 miter = msg.insert(miter,
'\\');
472 string::size_type idx = 0;
473 while((idx = source.find(
'\"', idx)) != string::npos) {
474 source.replace(idx, 1,
"\\\"");
489 string::size_type idx = 0;
490 while((idx = source.find(
"\\\"", idx)) != string::npos) {
491 source.replace(idx, 2,
"\"");
Regular expression matching.
int search(const char *s, int len, int &matchlen, int pos=0) const
How much of the string does the pattern match.
top level DAP object to house generic methods
string esc2underscore(string s)
string www2id(const string &in, const string &escape, const string &except)
string unescape_double_quotes(string source)
string id2xml(string in, const string ¬_allowed)
string unescattr(string s)
string escape_double_quotes(string source)
string id2www_ce(string in, const string &allowable)
string id2www(string in, const string &allowable)