trim space

This commit is contained in:
Zengtudor 2024-08-18 12:52:44 +08:00
parent 5332ef1a8b
commit e74c692fe6

View File

@ -254,26 +254,32 @@ int main(int argc, char* argv[]) {
exit(0); exit(0);
} }
void trimTrailingWhitespace(std::string& str) {
str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), str.end());
}
int compareFiles(const std::string& path1, const std::string& path2) { int compareFiles(const std::string& path1, const std::string& path2) {
std::ifstream file1(path1); std::ifstream file1(path1);
std::ifstream file2(path2); std::ifstream file2(path2);
string s("cmping ");
LOG(s+path1+" "+path2)
if (!file1.is_open()) { if (!file1.is_open()) {
std::cerr << "cannot open the file at: " << path1 << std::endl; std::cerr << "cannot open the file at: " << path1 << std::endl;
exit(1); exit(-1);
} }
if (!file2.is_open()) { if (!file2.is_open()) {
std::cerr << "cannot open the file at: " << path2 << std::endl; std::cerr << "cannot open the file at: " << path2 << std::endl;
exit(1); exit(-1);
} }
std::string line1, line2; std::string line1, line2;
int lineNumber = 1; int lineNumber = 1;
while (std::getline(file1, line1) && std::getline(file2, line2)) { while (std::getline(file1, line1) && std::getline(file2, line2)) {
trimTrailingWhitespace(line1);
trimTrailingWhitespace(line2);
if (line1 != line2) { if (line1 != line2) {
return lineNumber; // 返回第一个不同的行号 return lineNumber; // 返回第一个不同的行号
} }
@ -287,7 +293,6 @@ int compareFiles(const std::string& path1, const std::string& path2) {
return 0; // 文件内容完全一致 return 0; // 文件内容完全一致
} }
string getProtectPath(const std::filesystem::path &p){ string getProtectPath(const std::filesystem::path &p){
return "\""+p.string()+"\""; return "\""+p.string()+"\"";
} }