CBMC
c_misc.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: ANSI-C Misc Utilities
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "c_misc.h"
13 
14 #include <sstream>
15 
16 static void MetaChar(std::ostringstream &out, char c, bool inString)
17 {
18  switch(c)
19  {
20  case '\'':
21  if(inString)
22  out << "'";
23  else
24  out << "\\'";
25  break;
26 
27  case '"':
28  if(inString)
29  out << "\\\"";
30  else
31  out << "\"";
32  break;
33 
34  case '\0':
35  out << "\\0";
36  break;
37 
38  case '\\':
39  out << "\\\\";
40  break;
41 
42  case '\n':
43  out << "\\n";
44  break;
45 
46  case '\t':
47  out << "\\t";
48  break;
49 
50  case '\r':
51  out << "\\r";
52  break;
53 
54  case '\f':
55  out << "\\f";
56  break;
57 
58  case '\b':
59  out << "\\b";
60  break;
61 
62  case '\v':
63  out << "\\v";
64  break;
65 
66  case '\a':
67  out << "\\a";
68  break;
69 
70  default:
71  // Show low and certain high ascii as octal
72  if((static_cast<unsigned char>(c)<' ') || (c==127))
73  {
74  out << "\\" << std::oct << static_cast<unsigned char>(c);
75  }
76  else
77  {
78  // leave everything else to permit UTF-8 and 8-bit codepages
79  out << c;
80  }
81 
82  break;
83  }
84 }
85 
86 #if 0
87 static std::string MetaChar(char c)
88 {
89  std::string result;
90  MetaChar(result, c, false);
91  return result;
92 }
93 #endif
94 
95 std::string MetaString(const std::string &in)
96 {
97  std::ostringstream result;
98 
99  for(const auto &ch : in)
100  MetaChar(result, ch, true);
101 
102  return result.str();
103 }
std::string MetaString(const std::string &in)
Definition: c_misc.cpp:95
static void MetaChar(std::ostringstream &out, char c, bool inString)
Definition: c_misc.cpp:16
ANSI-C Misc Utilities.