83 buf << hex << setw(2) << setfill('0') << static_cast<unsigned int>(val);
93 tmp_str[0] =
static_cast<char>(val);
95 return string(tmp_str);
100 buf << oct << setw(3) << setfill('0') << static_cast<unsigned int>(val);
111 DBG(cerr <<
"unoctstring: " << val << endl);
114 tmp_str[0] =
static_cast<char>(val);
116 return string(tmp_str);
143string id2www(
string in,
const string &allowable) {
144 string::size_type i = 0;
145 DBG(cerr <<
"Input string: [" << in <<
"]" << endl);
146 while ((i = in.find_first_not_of(allowable, i)) != string::npos) {
147 DBG(cerr <<
"Found escapee: [" << in[i] <<
"]");
148 in.replace(i, 1,
"%" +
hexstring(in[i]));
149 DBGN(cerr <<
" now the string is: " << in << endl);
202string www2id(
const string &in,
const string &escape,
const string &except) {
203 string::size_type i = 0;
205 while ((i = res.find_first_of(escape, i)) != string::npos) {
206 if (except.find(res.substr(i, 3)) != string::npos) {
210 res.replace(i, 3,
unhexstring(res.substr(i + 1, 2)));
217static string entity(
char c) {
230 throw InternalErr(__FILE__, __LINE__,
"Unrecognized character.");
239 istringstream ss(octal_digits);
243 ds << hex << setw(2) << setfill(
'0') << val;
253string id2xml(
string in,
const string ¬_allowed) {
254 string::size_type i = 0;
256 while ((i = in.find_first_of(not_allowed, i)) != string::npos) {
257 in.replace(i, 1, entity(in[i]));
272 string octal_escape =
"\\\\";
274 string::size_type length = in.length();
275 while ((i = in.find(octal_escape, i)) != string::npos) {
277 string::size_type j = i + 2;
280 string octal_digits = in.substr(j, 3);
282 string hex_escape = string(
"&#x");
284 hex_escape.append(
string(
";"));
287 in.replace(i, 5, hex_escape);
302 string::size_type i = 0;
304 while ((i = in.find(
">", i)) != string::npos)
305 in.replace(i, 4,
">");
308 while ((i = in.find(
"<", i)) != string::npos)
309 in.replace(i, 4,
"<");
312 while ((i = in.find(
"&", i)) != string::npos)
313 in.replace(i, 5,
"&");
316 while ((i = in.find(
"'", i)) != string::npos)
317 in.replace(i, 6,
"'");
320 while ((i = in.find(
""", i)) != string::npos)
321 in.replace(i, 6,
"\"");
332 string::size_type pos;
333 while ((pos = s.find(
'%')) != string::npos)
334 s.replace(pos, 3,
"_");
343 const string printable =
344 " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_-+={[}]|\\:;<,>.?/'\"\n\t\r";
345 const string ESC =
"\\";
346 const string DOUBLE_ESC = ESC + ESC;
347 const string QUOTE =
"\"";
348 const string ESCQUOTE = ESC + QUOTE;
351 string::size_type ind = 0;
352 while ((ind = s.find(ESC, ind)) != string::npos) {
353 s.replace(ind, 1, DOUBLE_ESC);
354 ind += DOUBLE_ESC.length();
359 while ((ind = s.find_first_not_of(printable, ind)) != string::npos)
360 s.replace(ind, 1, ESC +
octstring(s[ind]));
364 while ((ind = s.find(QUOTE, ind)) != string::npos) {
365 s.replace(ind, 1, ESCQUOTE);
366 ind += ESCQUOTE.length();
376 const string printable =
377 " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_-+={[}]|\\:;<,>.?/'\"\n\t\r";
379 const string ESC =
"\\";
380 const char null_char =
'\0';
382 string::size_type ind = 0;
389 while ((ind = s.find_first_not_of(printable, ind)) != string::npos) {
390 if (s[ind] != null_char)
391 s.replace(ind, 1, ESC +
octstring(s[ind]));
408 const Regex octal(
"\\\\[0-3][0-7][0-7]");
409 const Regex esc_quote(
"\\\\\"");
410 const Regex esc_esc(
"\\\\\\\\");
411 const string ESC =
"\\";
412 const string QUOTE =
"\"";
416 DBG(cerr <<
"0XX" << s <<
"XXX" << endl);
418 index = esc_esc.
search(s.c_str(), s.length(), matchlen, 0);
419 while (index < s.length()) {
420 DBG(cerr <<
"1aXX" << s <<
"XXX index: " << index << endl);
421 s.replace(index, 2, ESC);
422 DBG(cerr <<
"1bXX" << s <<
"XXX index: " << index << endl);
423 index = esc_esc.
search(s.c_str(), s.length(), matchlen, 0);
427 index = esc_quote.
search(s.c_str(), s.length(), matchlen, 0);
428 while (index < s.length()) {
429 s.replace(index, 2, QUOTE);
430 DBG(cerr <<
"2XX" << s <<
"XXX index: " << index << endl);
431 index = esc_quote.
search(s.c_str(), s.length(), matchlen, 0);
435 index = octal.
search(s.c_str(), s.length(), matchlen, 0);
436 while (index < s.length()) {
437 s.replace(index, 4,
unoctstring(s.substr(index + 1, 3)));
438 DBG(cerr <<
"3XX" << s <<
"XXX index: " << index << endl);
439 index = octal.
search(s.c_str(), s.length(), matchlen, 0);
442 DBG(cerr <<
"4XX" << s <<
"XXX" << endl);
448 if (*msg.begin() !=
'"')
449 msg.insert(msg.begin(),
'"');
450 if (*(msg.end() - 1) !=
'"')
454 string::iterator miter;
455 for (miter = msg.begin() + 1; miter != msg.end() - 1; miter++)
456 if (*miter ==
'"' && *(miter - 1) !=
'\\')
457 miter = msg.insert(miter,
'\\');
467 string::size_type idx = 0;
468 while ((idx = source.find(
'\"', idx)) != string::npos) {
469 source.replace(idx, 1,
"\\\"");
482 string::size_type idx = 0;
483 while ((idx = source.find(
"\\\"", idx)) != string::npos) {
484 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 escattr_xml(string s)
string www2id(const string &in, const string &escape, const string &except)
string unescape_double_quotes(string source)
string hexstring(unsigned char val)
string octstring(unsigned char val)
string octal_to_hex(const string &octal_digits)
string id2xml(string in, const string ¬_allowed)
string unescattr(string s)
string munge_error_message(string msg)
string unhexstring(string s)
string escape_double_quotes(string source)
string id2www_ce(string in, const string &allowable)
string unoctstring(string s)
string id2www(string in, const string &allowable)