Combined string_functions.h and string_utils.h

This commit is contained in:
Paul Romano 2018-11-06 10:23:11 -06:00
parent 7d6ab8d72d
commit 97634d59ad
10 changed files with 90 additions and 98 deletions

View file

@ -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

View file

@ -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 <string>
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

View file

@ -1,49 +1,24 @@
#ifndef OPENMC_STRING_UTILS_H
#define OPENMC_STRING_UTILS_H
#include <algorithm>
#include <string>
#include <vector>
namespace openmc {
inline std::vector<std::string>
split(const std::string& in)
{
std::vector<std::string> 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<std::string> 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

View file

@ -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"

View file

@ -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"

View file

@ -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 {

View file

@ -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"

View file

@ -1,40 +0,0 @@
#include "openmc/string_functions.h"
#include <sstream>
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

78
src/string_utils.cpp Normal file
View file

@ -0,0 +1,78 @@
#include "openmc/string_utils.h"
#include <algorithm> // for equal
#include <cctype> // for tolower, isspace
#include <sstream>
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<std::string> split(const std::string& in)
{
std::vector<std::string> 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

View file

@ -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 {