From 48b68c024791be113bad7a220eebeb3773fba6cf Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Tue, 7 Apr 2026 16:05:05 +0100 Subject: [PATCH] chore: improve line cleanup from whitespace and comments --- src/main.cpp | 15 ++++++++------- src/utils.cpp | 24 +++++++++++++++++++++--- src/utils.hpp | 4 ++-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b919ca3..510201a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -13,21 +14,21 @@ int process(std::string inputPath, std::string outputPath, std::ios_base::openmode outputStreamMode) { std::ifstream ifs(inputPath); std::ofstream ofs(outputPath, outputStreamMode); - std::string fileName = getFileNameFromPath(inputPath); + std::string fileName = getFileNameFromFilePath(inputPath); std::string line; std::string output; std::vector commands; while (getline(ifs, line)) { - if (isComment(line) || isEmptyLine(line)) + if (cleanupLine(line) != 0) + exit(1); + + if (isEmptyLine(line)) continue; - int parseResult; - - if ((parseResult = parseCommand(commands, line)) != 0) { - return parseResult; - } + if (parseCommand(commands, line) != 0) + exit(1); } for (const Command &cmd : commands) { diff --git a/src/utils.cpp b/src/utils.cpp index 6b88acd..b57f524 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -10,11 +10,29 @@ bool isEmptyLine(const std::string &line) { return true; } -bool isComment(const std::string &line) { - return line[0] == '/' && line[1] == '/'; +int cleanupLine(std::string &line) { + size_t commentStartingPosition = line.find("//"); + + if (commentStartingPosition != std::string::npos) { + line = line.substr(0, commentStartingPosition); + } + + size_t firstNonWhiteSpacePosition = line.find_first_not_of(" \t\n\r"); + + if (firstNonWhiteSpacePosition == std::string::npos) { + line = ""; + return 0; + } + + size_t lastNonWhiteSpacePosition = line.find_last_not_of(" \t\n\r"); + line = + line.substr(firstNonWhiteSpacePosition, + (lastNonWhiteSpacePosition - firstNonWhiteSpacePosition + 1)); + + return 0; } -std::string getFileNameFromPath(const std::string &path) { +std::string getFileNameFromFilePath(const std::string &path) { std::string fileName = path.substr(path.find_last_of("/\\") + 1); return fileName.substr(0, fileName.find_last_of('.')); diff --git a/src/utils.hpp b/src/utils.hpp index b691e17..27df954 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,6 +1,6 @@ #include bool isEmptyLine(const std::string&); -bool isComment(const std::string&); -std::string getFileNameFromPath(const std::string&); +int cleanupLine(std::string&); +std::string getFileNameFromFilePath(const std::string&); std::string getOutputPath(const std::string&);