MCPcopy Create free account
hub / github.com/apache/thrift / normalize_for_compare

Function normalize_for_compare

compiler/cpp/tests/cpp/t_cpp_generator_test_utils.h:70–116  ·  view source on GitHub ↗

Normalize generated code for comparison by removing comments and extra whitespace

Source from the content-addressed store, hash-verified

68
69// Normalize generated code for comparison by removing comments and extra whitespace
70inline std::string normalize_for_compare(std::string s) {
71 s.erase(std::remove(s.begin(), s.end(), '\r'), s.end());
72
73 std::istringstream in(s);
74 std::ostringstream out;
75 std::string line;
76 bool in_block_comment = false;
77 bool first = true;
78 while (std::getline(in, line)) {
79 while (!line.empty() && (line.back() == ' ' || line.back() == '\t')) {
80 line.pop_back();
81 }
82
83 const auto first_non_ws = line.find_first_not_of(" \t");
84 if (first_non_ws == std::string::npos) {
85 continue;
86 }
87
88 const std::string trimmed = line.substr(first_non_ws);
89
90 if (in_block_comment) {
91 if (trimmed.find("*/") != std::string::npos) {
92 in_block_comment = false;
93 }
94 continue;
95 }
96
97 if (trimmed.size() >= 2 && trimmed.compare(0, 2, "//") == 0) {
98 continue;
99 }
100
101 if (trimmed.size() >= 2 && trimmed.compare(0, 2, "/*") == 0) {
102 if (trimmed.find("*/") == std::string::npos) {
103 in_block_comment = true;
104 }
105 continue;
106 }
107
108 if (!first) {
109 out << '\n';
110 }
111 first = false;
112 out << line;
113 }
114
115 return out.str();
116}
117
118// Parse a thrift program for testing
119inline void parse_thrift_for_test(t_program* program) {

Calls 8

emptyMethod · 0.80
findMethod · 0.80
strMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
substrMethod · 0.45
sizeMethod · 0.45
compareMethod · 0.45

Tested by

no test coverage detected