Normalize generated code for comparison by removing comments and extra whitespace
| 68 | |
| 69 | // Normalize generated code for comparison by removing comments and extra whitespace |
| 70 | inline 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 |
| 119 | inline void parse_thrift_for_test(t_program* program) { |
no test coverage detected