From a04b5232f4ea703eee0210b16b4fb50ea53730db Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 2 Oct 2021 12:10:42 +0200 Subject: [PATCH] Util: Add delimiter support to the cut function --- src/util/system.cpp | 10 +++++----- src/util/system.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/util/system.cpp b/src/util/system.cpp index 5c3e39a..e01909e 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -133,22 +133,22 @@ System System::operator||(System rhs) return rhs; } -// cut -f -System& System::cut(uint32_t field) +// cut -f -d +System& System::cut(uint32_t field, char delimiter) { exec(); - return apply([&field](std::vector& lines) { + return apply([&field, &delimiter](std::vector& lines) { for (auto& line : lines) { size_t count = 1; size_t index = 0; while (index != std::string::npos) { if (count == field) { - line = line.substr(0, line.find_first_of(" ")); + line = line.substr(0, line.find_first_of(delimiter)); break; } - index = line.find_first_of(" "); + index = line.find_first_of(delimiter); line = line.substr(index + 1); count++; } diff --git a/src/util/system.h b/src/util/system.h index 5b71ffe..890d1ee 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -36,7 +36,7 @@ public: System operator&&(System rhs); System operator||(System rhs); - System& cut(uint32_t field); + System& cut(uint32_t field, char delimiter = '\t'); System& sort(bool unique = false); System& tail(int32_t number, bool starting = false); System& apply(LineCallback callback);