canonical_string.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // canonical_string: Transform a string into a canonical form
4 //
5 // ======================================================================
6 
8 
9 #include "cetlib_except/exception.h"
10 
11 #include <cctype>
12 
13 // ----------------------------------------------------------------------
14 
17 {
19  for (std::string::const_iterator it = str.begin(), e = str.end(); it != e;
20  ++it) {
21  switch (*it) {
22  case '\"':
23  result.append("\\\"");
24  break;
25  case '\'':
26  result.append("\\\'");
27  break;
28  case '\\':
29  result.append("\\\\");
30  break;
31  case '\n':
32  result.append("\\n");
33  break;
34  case '\t':
35  result.append("\\t");
36  break;
37  default:
38  if (std::isprint(*it))
39  result.append(1, *it);
40  else
41  throw cet::exception("unprintable character");
42  }
43  }
44  return result;
45 } // escape()
46 
47 // ----------------------------------------------------------------------
48 
51 {
53  for (std::string::const_iterator it = str.begin(), e = str.end(); it != e;
54  ++it) {
55  char ch = *it;
56  if (ch == '\\' && it != e - 1) {
57  switch (*++it) {
58  case '\"':
59  ch = '\"';
60  break;
61  case '\'':
62  ch = '\'';
63  break;
64  case '\\':
65  ch = '\\';
66  break;
67  case 'n':
68  ch = '\n';
69  break;
70  case 't':
71  ch = '\t';
72  break;
73  default:
74  throw cet::exception("unknown escape: \\") << *it;
75  }
76  }
77  result.append(1, ch);
78  }
79  return result;
80 } // unescape()
81 
82 // ----------------------------------------------------------------------
83 
86 {
88  if (!s.empty()) {
90  if (is_double_quoted_string(s)) {
91  value = cet::unescape(s.substr(1, s.size() - 2));
92  } else if (is_single_quoted_string(s)) {
93  value = s.substr(1, s.size() - 2);
94  } else {
95  value = s;
96  }
97 
98  result.append(1, '"').append(cet::escape(value)).append(1, '"');
99  }
100  return result;
101 }
102 
103 bool
105 {
106  bool result = false;
107  auto const sz = s.size();
108  if ((sz >= 2) && (s[0] == quot) && (s.back() == quot)) {
109  result = true;
110  }
111  return result;
112 }
113 
114 // ======================================================================
static QCString result
std::string string
Definition: nybbler.cc:12
intermediate_table::const_iterator const_iterator
uint size() const
Definition: qcstring.h:201
std::string unescape(std::string const &str)
bool is_double_quoted_string(std::string const &str)
const double e
bool canonical_string(std::string const &str, std::string &result)
bool is_quoted_string(std::string const &s, char quot)
bool is_single_quoted_string(std::string const &str)
std::string escape(std::string const &str)
static QCString * s
Definition: config.cpp:1042
static QCString str
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QCString & append(const char *s)
Definition: qcstring.cpp:383