From 96ea4f71bd41aec453e5cd7bcccaba10ee76d190 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Tue, 9 Nov 2021 03:16:09 +0100 Subject: [PATCH] Utilities: cut: Split up into file and line processing --- Userland/Utilities/cut.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Userland/Utilities/cut.cpp b/Userland/Utilities/cut.cpp index fdfba1321d..eba5826ef1 100644 --- a/Userland/Utilities/cut.cpp +++ b/Userland/Utilities/cut.cpp @@ -120,7 +120,7 @@ static bool expand_list(String& list, Vector& ranges) return true; } -static void cut_file(const String& file, const Vector& byte_vector) +static void cut_file(const String& file, const Vector& ranges, void (*process_line)(char*, size_t, const Vector&)) { FILE* fp = stdin; if (!file.is_null()) { @@ -142,16 +142,7 @@ static void cut_file(const String& file, const Vector& byte_vector) line[line_length - 1] = '\0'; line_length--; - for (auto& i : byte_vector) { - if (i.m_from >= (size_t) line_length) - continue; - - - auto to = min(i.m_to, line_length); - auto sub_string = String(line).substring(i.m_from - 1, to - i.m_from + 1); - out("{}", sub_string); - } - outln(); + process_line(line, line_length, ranges); } if (line) @@ -161,6 +152,20 @@ static void cut_file(const String& file, const Vector& byte_vector) fclose(fp); } +static void process_line_bytes(char* line, size_t length, const Vector& ranges) +{ + for (auto& i : ranges) { + if (i.m_from >= length) + continue; + + + auto to = min(i.m_to, length); + auto sub_string = String(line).substring(i.m_from - 1, to - i.m_from + 1); + out("{}", sub_string); + } + outln(); +} + int main(int argc, char** argv) { String byte_list = ""; @@ -210,7 +215,7 @@ int main(int argc, char** argv) /* Process each file */ for (auto& file : files) - cut_file(file, disjoint_ranges); + cut_file(file, disjoint_ranges, process_line_bytes); return 0; }