diff --git a/CMakeLists.txt b/CMakeLists.txt index e76ed9a9e..bb5c45c42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -426,7 +426,7 @@ add_library(libopenmc SHARED src/simulation.cpp src/source.cpp src/state_point.cpp - src/string_functions.cpp + src/string_utils.cpp src/summary.cpp src/surface.cpp src/tallies/filter.cpp diff --git a/include/openmc/string_functions.h b/include/openmc/string_functions.h deleted file mode 100644 index a653d1e77..000000000 --- a/include/openmc/string_functions.h +++ /dev/null @@ -1,20 +0,0 @@ -//! \file string_functions.h -//! A collection of helper routines for C-strings and STL strings - -#ifndef OPENMC_STRING_FUNCTIONS_H -#define OPENMC_STRING_FUNCTIONS_H - -#include - -namespace openmc { - -std::string& strtrim(std::string& s); - -char* strtrim(char* c_str); - -void to_lower(std::string& str); - -int word_count(std::string const& str); - -} // namespace openmc -#endif // STRING_FUNCTIONS_H diff --git a/include/openmc/string_utils.h b/include/openmc/string_utils.h index 73b1a28ec..35c25e548 100644 --- a/include/openmc/string_utils.h +++ b/include/openmc/string_utils.h @@ -1,49 +1,24 @@ #ifndef OPENMC_STRING_UTILS_H #define OPENMC_STRING_UTILS_H -#include #include #include namespace openmc { -inline std::vector -split(const std::string& in) -{ - std::vector out; +std::string& strtrim(std::string& s); - for (int i = 0; i < in.size(); ) { - // Increment i until we find a non-whitespace character. - if (std::isspace(in[i])) { - i++; +char* strtrim(char* c_str); - } else { - // Find the next whitespace character at j. - int j = i + 1; - while (j < in.size() && std::isspace(in[j]) == 0) {j++;} +void to_lower(std::string& str); - // Push-back everything between i and j. - out.push_back(in.substr(i, j-i)); - i = j + 1; // j is whitespace so leapfrog to j+1 - } - } +int word_count(std::string const& str); - return out; -} +std::vector split(const std::string& in); -inline bool -ends_with(const std::string& value, const std::string& ending) -{ - if (ending.size() > value.size()) return false; - return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); -} +bool ends_with(const std::string& value, const std::string& ending); -inline bool -starts_with(const std::string& value, const std::string& beginning) -{ - if (beginning.size() > value.size()) return false; - return std::equal(beginning.begin(), beginning.end(), value.begin()); -} +bool starts_with(const std::string& value, const std::string& beginning); } // namespace openmc #endif // OPENMC_STRING_UTILS_H diff --git a/src/cross_sections.cpp b/src/cross_sections.cpp index aca46a5b9..dbfe41679 100644 --- a/src/cross_sections.cpp +++ b/src/cross_sections.cpp @@ -5,7 +5,6 @@ #include "openmc/error.h" #include "openmc/file_utils.h" #include "openmc/settings.h" -#include "openmc/string_functions.h" #include "openmc/string_utils.h" #include "openmc/xml_interface.h" diff --git a/src/dagmc.cpp b/src/dagmc.cpp index d4e113977..7c1127867 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -1,7 +1,7 @@ #include "openmc/dagmc.h" #include "openmc/error.h" -#include "openmc/string_functions.h" +#include "openmc/string_utils.h" #include "openmc/settings.h" #include "openmc/geometry.h" diff --git a/src/mgxs.cpp b/src/mgxs.cpp index 1b6e85ffb..8c14f324e 100644 --- a/src/mgxs.cpp +++ b/src/mgxs.cpp @@ -17,7 +17,7 @@ #include "openmc/error.h" #include "openmc/math_functions.h" #include "openmc/random_lcg.h" -#include "openmc/string_functions.h" +#include "openmc/string_utils.h" namespace openmc { diff --git a/src/plot.cpp b/src/plot.cpp index 7b0cddb63..9a8aba139 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -9,7 +9,7 @@ #include "openmc/geometry.h" #include "openmc/cell.h" #include "openmc/material.h" -#include "openmc/string_functions.h" +#include "openmc/string_utils.h" #include "openmc/mesh.h" #include "openmc/output.h" #include "openmc/hdf5_interface.h" diff --git a/src/string_functions.cpp b/src/string_functions.cpp deleted file mode 100644 index 27e14c9ba..000000000 --- a/src/string_functions.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "openmc/string_functions.h" -#include - -namespace openmc { - -std::string& strtrim(std::string& s) -{ - const char* t = " \t\n\r\f\v"; - s.erase(s.find_last_not_of(t) + 1); - s.erase(0, s.find_first_not_of(t)); - return s; -} - - -char* strtrim(char* c_str) -{ - std::string std_str; - std_str.assign(c_str); - strtrim(std_str); - int length = std_str.copy(c_str, std_str.size()); - c_str[length] = '\0'; - return c_str; -} - - -void to_lower(std::string& str) -{ - for (int i = 0; i < str.size(); i++) str[i] = std::tolower(str[i]); -} - -int word_count(std::string const& str) -{ - std::stringstream stream(str); - std::string dum; - int count = 0; - while (stream >> dum) {count++;} - return count; -} - -} // namespace openmc diff --git a/src/string_utils.cpp b/src/string_utils.cpp new file mode 100644 index 000000000..c1a203e48 --- /dev/null +++ b/src/string_utils.cpp @@ -0,0 +1,78 @@ +#include "openmc/string_utils.h" + +#include // for equal +#include // for tolower, isspace +#include + +namespace openmc { + +std::string& strtrim(std::string& s) +{ + const char* t = " \t\n\r\f\v"; + s.erase(s.find_last_not_of(t) + 1); + s.erase(0, s.find_first_not_of(t)); + return s; +} + + +char* strtrim(char* c_str) +{ + std::string std_str; + std_str.assign(c_str); + strtrim(std_str); + int length = std_str.copy(c_str, std_str.size()); + c_str[length] = '\0'; + return c_str; +} + + +void to_lower(std::string& str) +{ + for (int i = 0; i < str.size(); i++) str[i] = std::tolower(str[i]); +} + +int word_count(std::string const& str) +{ + std::stringstream stream(str); + std::string dum; + int count = 0; + while (stream >> dum) {count++;} + return count; +} + +std::vector split(const std::string& in) +{ + std::vector out; + + for (int i = 0; i < in.size(); ) { + // Increment i until we find a non-whitespace character. + if (std::isspace(in[i])) { + i++; + + } else { + // Find the next whitespace character at j. + int j = i + 1; + while (j < in.size() && std::isspace(in[j]) == 0) {j++;} + + // Push-back everything between i and j. + out.push_back(in.substr(i, j-i)); + i = j + 1; // j is whitespace so leapfrog to j+1 + } + } + + return out; +} + +bool ends_with(const std::string& value, const std::string& ending) +{ + if (ending.size() > value.size()) return false; + return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); +} + +bool starts_with(const std::string& value, const std::string& beginning) +{ + if (beginning.size() > value.size()) return false; + return std::equal(beginning.begin(), beginning.end(), value.begin()); +} + +} // namespace openmc diff --git a/src/surface.cpp b/src/surface.cpp index 265a2f02c..66217a58b 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -8,7 +8,7 @@ #include "openmc/error.h" #include "openmc/hdf5_interface.h" #include "openmc/xml_interface.h" -#include "openmc/string_functions.h" +#include "openmc/string_utils.h" namespace openmc {