diff options
author | 2022-10-10 20:36:14 +0200 | |
---|---|---|
committer | 2022-10-10 11:36:14 -0700 | |
commit | e9cc65ffeb0684a97618b67c2164d95ea497226c (patch) | |
tree | ed65f7ac86cc4e8945021dc36a79c8bc246c150d /Source/Utils/Strings/StringUtils.H | |
parent | 56e04c1b911f9399662c4ff9ecf6630d686cc220 (diff) | |
download | WarpX-e9cc65ffeb0684a97618b67c2164d95ea497226c.tar.gz WarpX-e9cc65ffeb0684a97618b67c2164d95ea497226c.tar.zst WarpX-e9cc65ffeb0684a97618b67c2164d95ea497226c.zip |
Partial refactoring of the utils directory (#3404)
* initial work to clean WarpX Utils
* remove AMRCore from Ionization tables
* progress
* refactoring of a part of the utils directory
* fix bug
* fixed bug
* fixed bug
* remove debug line accidentally slipped into the code
* remove debug line accidentally slipped into the code
* remove debug line accidentally slipped into the code
* cleaning
* fixed bug
Diffstat (limited to 'Source/Utils/Strings/StringUtils.H')
-rw-r--r-- | Source/Utils/Strings/StringUtils.H | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Source/Utils/Strings/StringUtils.H b/Source/Utils/Strings/StringUtils.H new file mode 100644 index 000000000..5c21d1be1 --- /dev/null +++ b/Source/Utils/Strings/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 WARPX_UTILS_STRINGS_STRINGUTILS_H_ +#define WARPX_UTILS_STRINGS_STRINGUTILS_H_ + +#include <AMReX_Utility.H> + +#include <string> +#include <vector> + +namespace utils::strings +{ + /** \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 (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 //WARPX_UTILS_STRINGS_STRINGUTILS_H_ |