aboutsummaryrefslogtreecommitdiff
path: root/Source/ablastr/utils
diff options
context:
space:
mode:
Diffstat (limited to 'Source/ablastr/utils')
-rw-r--r--Source/ablastr/utils/CMakeLists.txt1
-rw-r--r--Source/ablastr/utils/Make.package1
-rw-r--r--Source/ablastr/utils/TextMsg.H20
-rw-r--r--Source/ablastr/utils/TextMsg.cpp47
-rw-r--r--Source/ablastr/utils/text/CMakeLists.txt4
-rw-r--r--Source/ablastr/utils/text/Make.package3
-rw-r--r--Source/ablastr/utils/text/StringUtils.H68
-rw-r--r--Source/ablastr/utils/text/StringUtils.cpp52
8 files changed, 134 insertions, 62 deletions
diff --git a/Source/ablastr/utils/CMakeLists.txt b/Source/ablastr/utils/CMakeLists.txt
index 6c1f0302d..e4f1cdaee 100644
--- a/Source/ablastr/utils/CMakeLists.txt
+++ b/Source/ablastr/utils/CMakeLists.txt
@@ -7,4 +7,5 @@ target_sources(ablastr
)
add_subdirectory(msg_logger)
+add_subdirectory(text)
add_subdirectory(timer)
diff --git a/Source/ablastr/utils/Make.package b/Source/ablastr/utils/Make.package
index 00258162c..24ae539eb 100644
--- a/Source/ablastr/utils/Make.package
+++ b/Source/ablastr/utils/Make.package
@@ -6,4 +6,5 @@ CEXE_sources += UsedInputsFile.cpp
VPATH_LOCATIONS += $(WARPX_HOME)/Source/ablastr/utils
include $(WARPX_HOME)/Source/ablastr/utils/msg_logger/Make.package
+include $(WARPX_HOME)/Source/ablastr/utils/text/Make.package
include $(WARPX_HOME)/Source/ablastr/utils/timer/Make.package
diff --git a/Source/ablastr/utils/TextMsg.H b/Source/ablastr/utils/TextMsg.H
index a36b39364..9dc55cac6 100644
--- a/Source/ablastr/utils/TextMsg.H
+++ b/Source/ablastr/utils/TextMsg.H
@@ -12,22 +12,7 @@
#include <vector>
-namespace ablastr::utils
-{
- /** \brief This function performs automatic text wrapping on a string,
- * returning an array of strings each not exceeding the maximum line length
- * (unless the text contains a word exceeding the maximum line length).
- *
- * @param[in] text the string containing the text to be wrapped
- * @param[in] max_line_length the maximum line length
- * @return an std::vector containing the lines of the wrapped text
- */
- std::vector< std::string >
- automatic_text_wrap (
- const std::string& text,
- const int max_line_length);
-
-namespace TextMsg
+namespace ablastr::utils::TextMsg
{
/** \brief This function formats a text message as an error message,
* adding the '### ERROR: ' prefix and (by default) performing text wrapping
@@ -67,8 +52,7 @@ namespace TextMsg
void
Assert (const char *ex, const char *file, const int line, const std::string &msg);
-} // namespace TextMsg
-} // namespace ablastr::utils
+} // namespace ablastr::utils::TextMsg
#define ABLASTR_ALWAYS_ASSERT_WITH_MESSAGE(EX,MSG) (EX)?((void)0) : ablastr::utils::TextMsg::Assert( # EX , __FILE__, __LINE__ , MSG)
diff --git a/Source/ablastr/utils/TextMsg.cpp b/Source/ablastr/utils/TextMsg.cpp
index 4dab5b508..9eea1d8f0 100644
--- a/Source/ablastr/utils/TextMsg.cpp
+++ b/Source/ablastr/utils/TextMsg.cpp
@@ -7,6 +7,8 @@
#include "TextMsg.H"
+#include "ablastr/utils/text/StringUtils.H"
+
#include <AMReX.H>
#include <algorithm>
@@ -35,7 +37,7 @@ namespace
return msg_prefix + msg + "\n";
}
- const auto wrapped_text = ablastr::utils::automatic_text_wrap(
+ const auto wrapped_text = ablastr::utils::text::automatic_text_wrap(
msg, msg_line_length);
std::stringstream ss_out;
@@ -77,46 +79,3 @@ ablastr::utils::TextMsg::Assert (const char* ex, const char* file, const int lin
const auto n_msg = "\n" + Err(msg);
amrex::Assert(ex , file, line , n_msg.c_str());
}
-
-std::vector< std::string >
-ablastr::utils::automatic_text_wrap (
- const std::string& text, const int max_line_length)
-{
-
- auto ss_text = std::stringstream{text};
- auto wrapped_text_lines = std::vector< std::string >{};
-
- std::string line;
- while(std::getline(ss_text, line,'\n')){
-
- auto ss_line = std::stringstream{line};
- int counter = 0;
- std::stringstream ss_line_out;
- std::string word;
-
- while (ss_line >> word){
- const auto wlen = static_cast<int>(word.length());
-
- if(counter == 0){
- ss_line_out << word;
- counter += wlen;
- }
- else{
- if (counter + wlen < max_line_length){
- ss_line_out << " " << word;
- counter += (wlen+1);
- }
- else{
- wrapped_text_lines.push_back(ss_line_out.str());
- ss_line_out.str("");
- ss_line_out << word;
- counter = wlen;
- }
- }
- }
-
- wrapped_text_lines.push_back(ss_line_out.str());
- }
-
- return wrapped_text_lines;
-}
diff --git a/Source/ablastr/utils/text/CMakeLists.txt b/Source/ablastr/utils/text/CMakeLists.txt
new file mode 100644
index 000000000..516d184b3
--- /dev/null
+++ b/Source/ablastr/utils/text/CMakeLists.txt
@@ -0,0 +1,4 @@
+target_sources(ablastr
+ PRIVATE
+ StringUtils.cpp
+)
diff --git a/Source/ablastr/utils/text/Make.package b/Source/ablastr/utils/text/Make.package
new file mode 100644
index 000000000..1cd8ee593
--- /dev/null
+++ b/Source/ablastr/utils/text/Make.package
@@ -0,0 +1,3 @@
+CEXE_sources += StringUtils.cpp
+
+VPATH_LOCATIONS += $(WARPX_HOME)/Source/ablastr/utils/text
diff --git a/Source/ablastr/utils/text/StringUtils.H b/Source/ablastr/utils/text/StringUtils.H
new file mode 100644
index 000000000..35c280e17
--- /dev/null
+++ b/Source/ablastr/utils/text/StringUtils.H
@@ -0,0 +1,68 @@
+/* Copyright 2022 Andrew Myers, Luca Fedeli, Maxence Thevenet
+ * Revathi Jambunathan
+ *
+ * This file is part of WarpX.
+ *
+ * License: BSD-3-Clause-LBNL
+ */
+
+#ifndef ABLASTR_UTILS_TEXT_STRINGUTILS_H_
+#define ABLASTR_UTILS_TEXT_STRINGUTILS_H_
+
+#include <AMReX_Utility.H>
+
+#include <string>
+#include <vector>
+
+namespace ablastr::utils::text
+{
+ /** \brief Splits a string using a string separator. This is somewhat similar to
+ * amrex::Tokenize. The main difference is that, if the separator ":" is used,
+ * amrex::Tokenize will split ":3::2" into ["3","2"] while this functio will
+ * split ":3::2" into ["","3","","2"]. This function can also perform a trimming to
+ * remove whitespaces (or any other arbitrary string) from the split string.
+ *
+ * @tparam Container the type of the split string.
+ *
+ * @param[in] instr the input string
+ * @param[in] separator the separator string
+ * @param[in] trim true to trim the split string, false otherwise.
+ * @param[in] trim_space the string to trim if trim is true.
+ * @return cont the split string
+ */
+ template <typename Container>
+ auto split_string (std::string const& instr, std::string const& separator,
+ bool const trim = false, std::string const& trim_space = " \t")
+ {
+ Container cont;
+ std::size_t current = instr.find(separator);
+ std::size_t previous = 0;
+ while (current != std::string::npos) {
+ if (trim){
+ cont.push_back(amrex::trim(instr.substr(previous, current - previous),trim_space));}
+ else{
+ cont.push_back(instr.substr(previous, current - previous));}
+ previous = current + separator.size();
+ current = instr.find(separator, previous);
+ }
+ if (trim){
+ cont.push_back(amrex::trim(instr.substr(previous, current - previous),trim_space));}
+ else{
+ cont.push_back(instr.substr(previous, current - previous));}
+ return cont;
+ }
+
+ /** \brief This function performs automatic text wrapping on a string,
+ * returning an array of strings each not exceeding the maximum line length
+ * (unless the text contains a word exceeding the maximum line length).
+ *
+ * @param[in] text the string containing the text to be wrapped
+ * @param[in] max_line_length the maximum line length
+ * @return an std::vector containing the lines of the wrapped text
+ */
+ std::vector<std::string> automatic_text_wrap(
+ const std::string& text, const int max_line_length);
+
+}
+
+#endif // ABLASTR_UTILS_TEXT_STRINGUTILS_H_
diff --git a/Source/ablastr/utils/text/StringUtils.cpp b/Source/ablastr/utils/text/StringUtils.cpp
new file mode 100644
index 000000000..67a269bbb
--- /dev/null
+++ b/Source/ablastr/utils/text/StringUtils.cpp
@@ -0,0 +1,52 @@
+/* Copyright 2022 Andrew Myers, Luca Fedeli, Maxence Thevenet
+ * Revathi Jambunathan
+ *
+ * This file is part of WarpX.
+ *
+ * License: BSD-3-Clause-LBNL
+ */
+
+#include "StringUtils.H"
+
+#include <sstream>
+
+std::vector<std::string>
+ablastr::utils::text::automatic_text_wrap(
+ const std::string& text, const int max_line_length){
+
+ auto ss_text = std::stringstream{text};
+ auto wrapped_text_lines = std::vector<std::string>{};
+
+ std::string line;
+ while(std::getline(ss_text, line,'\n')){
+
+ auto ss_line = std::stringstream{line};
+ int counter = 0;
+ std::stringstream ss_line_out;
+ std::string word;
+
+ while (ss_line >> word){
+ const auto wlen = static_cast<int>(word.length());
+
+ if(counter == 0){
+ ss_line_out << word;
+ counter += wlen;
+ }
+ else{
+ if (counter + wlen < max_line_length){
+ ss_line_out << " " << word;
+ counter += (wlen+1);
+ }
+ else{
+ wrapped_text_lines.push_back(ss_line_out.str());
+ ss_line_out = std::stringstream{word};
+ counter = wlen;
+ }
+ }
+ }
+
+ wrapped_text_lines.push_back(ss_line_out.str());
+ }
+
+ return wrapped_text_lines;
+}