From e3348fc42bc38d28ef281a8fe9bd66e57e56ce4c Mon Sep 17 00:00:00 2001 From: Austin Schneider Date: Fri, 13 Sep 2024 19:31:03 -0600 Subject: [PATCH] Utilities for string manipulation (mainly inserting tabs) --- projects/utilities/CMakeLists.txt | 1 + .../utilities/private/StringManipulation.cxx | 41 +++++++++++++++++++ .../SIREN/utilities/StringManipulation.h | 17 ++++++++ 3 files changed, 59 insertions(+) create mode 100644 projects/utilities/private/StringManipulation.cxx create mode 100644 projects/utilities/public/SIREN/utilities/StringManipulation.h diff --git a/projects/utilities/CMakeLists.txt b/projects/utilities/CMakeLists.txt index f0dec7b9..5ad24dfa 100644 --- a/projects/utilities/CMakeLists.txt +++ b/projects/utilities/CMakeLists.txt @@ -3,6 +3,7 @@ LIST (APPEND utilities_SOURCES ${PROJECT_SOURCE_DIR}/projects/utilities/private/Interpolator.cxx ${PROJECT_SOURCE_DIR}/projects/utilities/private/Random.cxx + ${PROJECT_SOURCE_DIR}/projects/utilities/private/StringManipulation.cxx ) add_library(SIREN_utilities OBJECT ${utilities_SOURCES}) set_property(TARGET SIREN_utilities PROPERTY POSITION_INDEPENDENT_CODE ON) diff --git a/projects/utilities/private/StringManipulation.cxx b/projects/utilities/private/StringManipulation.cxx new file mode 100644 index 00000000..96f009f6 --- /dev/null +++ b/projects/utilities/private/StringManipulation.cxx @@ -0,0 +1,41 @@ +#include +#include +#include + +#include "SIREN/utilities/StringManipulation.h" + +namespace siren { +namespace utilities { + +std::string add_prefix(std::string const & input, std::string const & prefix) { + std::istringstream iss(input); + std::vector lines; + std::string line; + ssize_t last_non_empty_line = -1; + size_t line_number = 0; + + // Read each line and track the last non-empty line + while (std::getline(iss, line)) { + lines.push_back(line); + if (!line.empty()) { + last_non_empty_line = line_number; + } + line_number++; + } + + std::ostringstream oss; + + // Add prefix to each line up to the last non-empty line + if (last_non_empty_line >= 0) { + for (size_t i = 0; i <= static_cast(last_non_empty_line); ++i) { + oss << prefix << lines[i] << '\n'; + } + // Ensure the string ends with an empty newline + oss << '\n'; + } + + return oss.str(); +} + +} // namespace utilities +} // namespace siren diff --git a/projects/utilities/public/SIREN/utilities/StringManipulation.h b/projects/utilities/public/SIREN/utilities/StringManipulation.h new file mode 100644 index 00000000..ef3d90a7 --- /dev/null +++ b/projects/utilities/public/SIREN/utilities/StringManipulation.h @@ -0,0 +1,17 @@ +#pragma once +#ifndef SIREN_StringMapulation_H +#define SIREN_StringMapulation_H + +#include + +namespace siren { +namespace utilities { + +constexpr char const * tab = " "; + +std::string add_prefix(std::string const & input, std::string const & prefix); + +} // namespace utilities +} // namespace siren + +#endif // SIREN_StringMapulation_H