From b204b3e85567ceab7a1140530acdda998da98b37 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 16 Mar 2022 16:58:47 +0100 Subject: [PATCH] Skip early if line doesn't look like a patch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows us to skip exception handling (substr) later. 😉 --- src/main.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 99cd211..d53f123 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,24 +53,27 @@ int main() while (std::getline(std::cin, line)) { const auto line_nc{remove_colors(line)}; - // Don't color +++ and --- lines - if (line_nc.length() >= 2 - && (line_nc.length() < 3 - || (line_nc[2] != '+' && line_nc[2] != '-'))) + // Only look at lines starting with + or - + if (line_nc.length() >= 2 && (line_nc[0] == '+' || line_nc[0] == '-')) { - if (line_nc[1] == '+') + // Don't color +++ and --- lines + if (line_nc.length() < 3 + || (line_nc[2] != '+' && line_nc[2] != '-')) { - const auto pos{get_start_pos(line)}; - std::cout << line.substr(0, pos) << green << line.substr(pos) - << reset << '\n'; - continue; - } - if (line_nc[1] == '-') - { - const auto pos{get_start_pos(line)}; - std::cout << line.substr(0, pos) << red << line.substr(pos) - << reset << '\n'; - continue; + if (line_nc[1] == '+') + { + const auto pos{get_start_pos(line)}; + std::cout << line.substr(0, pos) << green + << line.substr(pos) << reset << '\n'; + continue; + } + if (line_nc[1] == '-') + { + const auto pos{get_start_pos(line)}; + std::cout << line.substr(0, pos) << red << line.substr(pos) + << reset << '\n'; + continue; + } } } std::cout << line << '\n';