Skip to content

Commit 6376250

Browse files
committed
Refactoring only: move conversion to separate function
In preparation of an upcoming change, move the large loop bodies to a function of their own.
1 parent 004fd96 commit 6376250

File tree

2 files changed

+66
-59
lines changed

2 files changed

+66
-59
lines changed

src/ansi-c/file_converter.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,35 @@ Author: Daniel Kroening, [email protected]
1212
#include <iostream>
1313
#include <string>
1414

15-
int main()
15+
static void convert_line(const std::string &line)
1616
{
17-
std::string line;
17+
std::cout << "\"";
1818

19-
while(getline(std::cin, line))
19+
for(std::size_t i = 0; i < line.size(); i++)
2020
{
21-
std::cout << "\"";
22-
23-
for(std::size_t i=0; i<line.size(); i++)
21+
const char ch = line[i];
22+
if(ch == '\\')
23+
std::cout << "\\\\";
24+
else if(ch == '"')
25+
std::cout << "\\\"";
26+
else if(ch == '\r' || ch == '\n')
2427
{
25-
const char ch=line[i];
26-
if(ch=='\\')
27-
std::cout << "\\\\";
28-
else if(ch=='"')
29-
std::cout << "\\\"";
30-
else if(ch=='\r' || ch=='\n')
31-
{
32-
}
33-
else if((ch&0x80)!=0)
34-
{
35-
std::cout << "\\x"
36-
<< std::hex << (unsigned(ch)&0xff)
37-
<< std::dec;
38-
}
39-
else
40-
std::cout << ch;
4128
}
42-
43-
std::cout << "\\n\"\n";
29+
else if((ch & 0x80) != 0)
30+
{
31+
std::cout << "\\x" << std::hex << (unsigned(ch) & 0xff) << std::dec;
32+
}
33+
else
34+
std::cout << ch;
4435
}
36+
37+
std::cout << "\\n\"\n";
38+
}
39+
40+
int main()
41+
{
42+
std::string line;
43+
44+
while(getline(std::cin, line))
45+
convert_line(line);
4546
}

src/ansi-c/library/converter.cpp

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,57 @@ bool has_prefix(const std::string &s, const std::string &prefix)
1515
return std::string(s, 0, prefix.size())==prefix;
1616
}
1717

18-
int main()
18+
static void convert_line(const std::string &line, bool first)
1919
{
20-
std::string line;
21-
bool first=true;
20+
if(has_prefix(line, "/* FUNCTION: "))
21+
{
22+
if(!first)
23+
std::cout << "},\n";
2224

23-
std::cout << "{\n";
25+
std::string function = std::string(line, 13, std::string::npos);
26+
std::size_t pos = function.find(' ');
27+
if(pos != std::string::npos)
28+
function = std::string(function, 0, pos);
2429

25-
while(getline(std::cin, line))
30+
std::cout << "{ \"" << function << "\",\n";
31+
std::cout << " \"#line 1 \\\"<builtin-library-" << function
32+
<< ">\\\"\\n\"\n";
33+
}
34+
else if(!first)
2635
{
27-
if(has_prefix(line, "/* FUNCTION: "))
36+
std::cout << " \"";
37+
38+
for(unsigned i = 0; i < line.size(); i++)
2839
{
29-
if(first)
30-
first=false;
40+
const char ch = line[i];
41+
if(ch == '\\')
42+
std::cout << "\\\\";
43+
else if(ch == '"')
44+
std::cout << "\\\"";
45+
else if(ch == '\r' || ch == '\n')
46+
{
47+
}
3148
else
32-
std::cout << "},\n";
49+
std::cout << ch;
50+
}
3351

34-
std::string function=std::string(line, 13, std::string::npos);
35-
std::size_t pos=function.find(' ');
36-
if(pos!=std::string::npos)
37-
function=std::string(function, 0, pos);
52+
std::cout << "\\n\"\n";
53+
}
54+
}
3855

39-
std::cout << "{ \"" << function << "\",\n";
40-
std::cout << " \"#line 1 \\\"<builtin-library-"
41-
<< function << ">\\\"\\n\"\n";
42-
}
43-
else if(!first)
44-
{
45-
std::cout << " \"";
56+
int main()
57+
{
58+
std::string line;
59+
bool first = true;
4660

47-
for(unsigned i=0; i<line.size(); i++)
48-
{
49-
const char ch=line[i];
50-
if(ch=='\\')
51-
std::cout << "\\\\";
52-
else if(ch=='"')
53-
std::cout << "\\\"";
54-
else if(ch=='\r' || ch=='\n')
55-
{
56-
}
57-
else
58-
std::cout << ch;
59-
}
61+
std::cout << "{\n";
6062

61-
std::cout << "\\n\"\n";
62-
}
63+
if(getline(std::cin, line))
64+
{
65+
convert_line(line, true);
66+
first = false;
67+
while(getline(std::cin, line))
68+
convert_line(line, false);
6369
}
6470

6571
if(!first)

0 commit comments

Comments
 (0)