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