cprover
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 <cstdio>
15 
16 #ifdef _WIN32
17 #ifndef __MINGW32__
18 #define snprintf sprintf_s
19 #endif
20 #endif
21 
22 static void MetaChar(std::string &out, char c, bool inString)
23 {
24  switch(c)
25  {
26  case '\'':
27  if(inString)
28  out+="'";
29  else
30  out+="\\'";
31  break;
32 
33  case '"':
34  if(inString)
35  out+="\\\"";
36  else
37  out+="\"";
38  break;
39 
40  case '\0':
41  out+="\\0";
42  break;
43 
44  case '\\':
45  out+="\\\\";
46  break;
47 
48  case '\n':
49  out+="\\n";
50  break;
51 
52  case '\t':
53  out+="\\t";
54  break;
55 
56  case '\r':
57  out+="\\r";
58  break;
59 
60  case '\f':
61  out+="\\f";
62  break;
63 
64  case '\b':
65  out+="\\b";
66  break;
67 
68  case '\v':
69  out+="\\v";
70  break;
71 
72  case '\a':
73  out+="\\a";
74  break;
75 
76  default:
77  // Show low and certain high ascii as octal
78  if(((unsigned char)c < ' ') || (c == 127))
79  {
80  char octbuf[8];
81  snprintf(octbuf, sizeof(octbuf), "%03o", (unsigned char) c);
82  out+="\\";
83  out+=octbuf;
84  }
85  else
86  {
87  // leave everything else to permit UTF-8 and 8-bit codepages
88  out+=c;
89  }
90 
91  break;
92  }
93 }
94 
95 #if 0
96 static std::string MetaChar(char c)
97 {
98  std::string result;
99  MetaChar(result, c, false);
100  return result;
101 }
102 #endif
103 
104 std::string MetaString(const std::string &in)
105 {
106  std::string result;
107 
108  for(const auto &ch : in)
109  MetaChar(result, ch, true);
110 
111  return result;
112 }
ANSI-C Misc Utilities.
static void MetaChar(std::string &out, char c, bool inString)
Definition: c_misc.cpp:22
std::string MetaString(const std::string &in)
Definition: c_misc.cpp:104