-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
160 lines (122 loc) · 2.77 KB
/
Copy pathstring.cpp
File metadata and controls
160 lines (122 loc) · 2.77 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* File: string.cpp
*
* Description: This file contains the function definitions for parsing and
* escaping C-style escape sequences in strings.
*/
# include <climits>
# include "string.h"
using namespace std;
/*
* Function: parseString
*
* Description: Parse a string contains C-style escape sequences. An
* invalid escape sequence is detected, as is an overflow in
* an octal or hexadecimal escape sequence.
*/
string parseString(const string &s, bool &invalid, bool &overflow)
{
unsigned start, val;
string result;
invalid = false;
overflow = false;
for (unsigned i = 0; i < s.size(); i ++) {
if (s[i] == '\\') {
i ++;
switch(s[i]) {
case 'a':
result += '\a';
break;
case 'b':
result += '\b';
break;
case 'f':
result += '\f';
break;
case 'n':
result += '\n';
break;
case 'r':
result += '\r';
break;
case 't':
result += '\t';
break;
case 'v':
result += '\v';
break;
case '\n':
break;
case '\\': case '\?': case '\'': case '\"':
result += s[i];
break;
case 'x':
val = 0;
start = i;
while (1) {
if (s[i + 1] >= '0' && s[i + 1] <= '9')
val = val * 16 + (s[++ i] - '0');
else if (s[i + 1] >= 'a' && s[i + 1] <= 'f')
val = val * 16 + (s[++ i] - 'a' + 10);
else if (s[i + 1] >= 'A' && s[i + 1] <= 'F')
val = val * 16 + (s[++ i] - 'A' + 10);
else
break;
}
if (start == i) {
invalid = true;
val = 'x';
} else if (val > UCHAR_MAX)
overflow = true;
result += val;
break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
val = s[i] - '0';
if (s[i + 1] >= '0' && s[i + 1] <= '7')
val = val * 8 + (s[++ i] - '0');
if (s[i + 1] >= '0' && s[i + 1] <= '7')
val = val * 8 + (s[++ i] - '0');
if (val > UCHAR_MAX)
overflow = true;
result += val;
break;
default:
invalid = true;
result += s[i];
break;
}
} else
result += s[i];
}
return result;
}
/*
* Function: parseString
*
* Description: Parse a string contains C-style escape sequences. Any
* invalid escape sequence is silently ignored.
*/
string parseString(const string &s)
{
bool invalid, overflow;
return parseString(s, invalid, overflow);
}
/*
* Function: escapeString
*
* Description: Return a copy of the given string but with any unprintable
* character replaced with an octal escape sequence.
*/
string escapeString(const string &s, const string &meta)
{
char buf[5];
string result;
for (unsigned i = 0; i < s.size(); i ++)
if (!isprint(s[i]) || meta.find(s[i]) != string::npos) {
snprintf(buf, sizeof(buf), "\\%03o", (unsigned char) s[i]);
result += buf;
} else
result += s[i];
return result;
}