-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilities for string manipulation (mainly inserting tabs)
- Loading branch information
1 parent
b778030
commit e3348fc
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <string> | ||
#include <vector> | ||
#include <sstream> | ||
|
||
#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<std::string> 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<size_t>(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 |
17 changes: 17 additions & 0 deletions
17
projects/utilities/public/SIREN/utilities/StringManipulation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
#ifndef SIREN_StringMapulation_H | ||
#define SIREN_StringMapulation_H | ||
|
||
#include <string> | ||
|
||
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 |