Skip to content

Commit 3fdcae4

Browse files
author
Daniel Kroening
authored
Merge pull request #1331 from tautschnig/cleanup-metachar
MetaChar: use std::stringstream for incremental escaped-string construction
2 parents 4349d91 + 003482a commit 3fdcae4

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

src/ansi-c/c_misc.cpp

+20-29
Original file line numberDiff line numberDiff line change
@@ -11,81 +11,72 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "c_misc.h"
1313

14-
#include <cstdio>
14+
#include <sstream>
1515

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)
16+
static void MetaChar(std::ostringstream &out, char c, bool inString)
2317
{
2418
switch(c)
2519
{
2620
case '\'':
2721
if(inString)
28-
out+="'";
22+
out << "'";
2923
else
30-
out+="\\'";
24+
out << "\\'";
3125
break;
3226

3327
case '"':
3428
if(inString)
35-
out+="\\\"";
29+
out << "\\\"";
3630
else
37-
out+="\"";
31+
out << "\"";
3832
break;
3933

4034
case '\0':
41-
out+="\\0";
35+
out << "\\0";
4236
break;
4337

4438
case '\\':
45-
out+="\\\\";
39+
out << "\\\\";
4640
break;
4741

4842
case '\n':
49-
out+="\\n";
43+
out << "\\n";
5044
break;
5145

5246
case '\t':
53-
out+="\\t";
47+
out << "\\t";
5448
break;
5549

5650
case '\r':
57-
out+="\\r";
51+
out << "\\r";
5852
break;
5953

6054
case '\f':
61-
out+="\\f";
55+
out << "\\f";
6256
break;
6357

6458
case '\b':
65-
out+="\\b";
59+
out << "\\b";
6660
break;
6761

6862
case '\v':
69-
out+="\\v";
63+
out << "\\v";
7064
break;
7165

7266
case '\a':
73-
out+="\\a";
67+
out << "\\a";
7468
break;
7569

7670
default:
7771
// Show low and certain high ascii as octal
78-
if(((unsigned char)c < ' ') || (c == 127))
72+
if((static_cast<unsigned char>(c)<' ') || (c==127))
7973
{
80-
char octbuf[8];
81-
snprintf(octbuf, sizeof(octbuf), "%03o", (unsigned char) c);
82-
out+="\\";
83-
out+=octbuf;
74+
out << "\\" << std::oct << static_cast<unsigned char>(c);
8475
}
8576
else
8677
{
8778
// leave everything else to permit UTF-8 and 8-bit codepages
88-
out+=c;
79+
out << c;
8980
}
9081

9182
break;
@@ -103,10 +94,10 @@ static std::string MetaChar(char c)
10394

10495
std::string MetaString(const std::string &in)
10596
{
106-
std::string result;
97+
std::ostringstream result;
10798

10899
for(const auto &ch : in)
109100
MetaChar(result, ch, true);
110101

111-
return result;
102+
return result.str();
112103
}

0 commit comments

Comments
 (0)