Creating a common word_count functions for strings.

This commit is contained in:
Patrick Shriwise 2018-10-10 16:09:01 -05:00
parent 2bffc33452
commit fea2c3cc04
3 changed files with 14 additions and 18 deletions

View file

@ -14,5 +14,7 @@ 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,4 +1,5 @@
#include "openmc/string_functions.h"
#include <sstream>
namespace openmc {
@ -27,4 +28,15 @@ 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

View file

@ -35,24 +35,6 @@ std::map<int, int> surface_map;
// Helper functions for reading the "coeffs" node of an XML surface element
//==============================================================================
int word_count(const std::string &text)
{
bool in_word = false;
int count {0};
for (auto c = text.begin(); c != text.end(); c++) {
if (std::isspace(*c)) {
if (in_word) {
in_word = false;
count++;
}
} else {
in_word = true;
}
}
if (in_word) count++;
return count;
}
void read_coeffs(pugi::xml_node surf_node, int surf_id, double &c1)
{
// Check the given number of coefficients.