Simplify count_words, add trim_and_count_words

The original count_words function (before it was put into utils::) also trimmed
comments. For compatibility this behaviour was retained at first. However, due
to the name the trimming is not immediatly apparent and many times not
wanted.

Therefore, this commit replaces count_words with an implementation that
just does what it says. If a comment should be trimmed there is a
trim_comment function. For convenience, a trim_and_count_words function was
added and is now used where the old behaviour was needed.
This commit is contained in:
Richard Berger
2020-06-04 10:35:36 -04:00
parent 3c6ce73ccf
commit 9291d2a9d7
14 changed files with 40 additions and 20 deletions

View File

@ -2489,7 +2489,7 @@ void FixRigidSmall::readfile(int which, double **array, int *inbody)
buf = buffer; buf = buffer;
next = strchr(buf,'\n'); next = strchr(buf,'\n');
*next = '\0'; *next = '\0';
int nwords = utils::count_words(buf); int nwords = utils::trim_and_count_words(buf);
*next = '\n'; *next = '\n';
if (nwords != ATTRIBUTE_PERBODY) if (nwords != ATTRIBUTE_PERBODY)

View File

@ -568,7 +568,7 @@ void PairSNAP::read_files(char *coefffilename, char *paramfilename)
MPI_Bcast(&n,1,MPI_INT,0,world); MPI_Bcast(&n,1,MPI_INT,0,world);
MPI_Bcast(line,n,MPI_CHAR,0,world); MPI_Bcast(line,n,MPI_CHAR,0,world);
nwords = utils::count_words(line); nwords = utils::trim_and_count_words(line);
if (nwords != 3) if (nwords != 3)
error->all(FLERR,"Incorrect format in SNAP coefficient file"); error->all(FLERR,"Incorrect format in SNAP coefficient file");
@ -610,7 +610,7 @@ void PairSNAP::read_files(char *coefffilename, char *paramfilename)
MPI_Bcast(&n,1,MPI_INT,0,world); MPI_Bcast(&n,1,MPI_INT,0,world);
MPI_Bcast(line,n,MPI_CHAR,0,world); MPI_Bcast(line,n,MPI_CHAR,0,world);
nwords = utils::count_words(line); nwords = utils::trim_and_count_words(line);
if (nwords != 1) if (nwords != 1)
error->all(FLERR,"Incorrect format in SNAP coefficient file"); error->all(FLERR,"Incorrect format in SNAP coefficient file");

View File

@ -441,7 +441,7 @@ void NEBSpin::readfile(char *file, int flag)
buf = buffer; buf = buffer;
next = strchr(buf,'\n'); next = strchr(buf,'\n');
*next = '\0'; *next = '\0';
int nwords = utils::count_words(buf); int nwords = utils::trim_and_count_words(buf);
*next = '\n'; *next = '\n';
if (nwords != ATTRIBUTE_PERLINE) if (nwords != ATTRIBUTE_PERLINE)

View File

@ -1089,7 +1089,7 @@ void Atom::data_atoms(int n, char *buf, tagint id_offset, tagint mol_offset,
next = strchr(buf,'\n'); next = strchr(buf,'\n');
*next = '\0'; *next = '\0';
int nwords = utils::count_words(buf); int nwords = utils::trim_and_count_words(buf);
*next = '\n'; *next = '\n';
if (nwords != avec->size_data_atom && nwords != avec->size_data_atom + 3) if (nwords != avec->size_data_atom && nwords != avec->size_data_atom + 3)
@ -1239,7 +1239,7 @@ void Atom::data_vels(int n, char *buf, tagint id_offset)
next = strchr(buf,'\n'); next = strchr(buf,'\n');
*next = '\0'; *next = '\0';
int nwords = utils::count_words(buf); int nwords = utils::trim_and_count_words(buf);
*next = '\n'; *next = '\n';
if (nwords != avec->size_data_vel) if (nwords != avec->size_data_vel)
@ -1591,7 +1591,7 @@ void Atom::data_bonus(int n, char *buf, AtomVec *avec_bonus, tagint id_offset)
next = strchr(buf,'\n'); next = strchr(buf,'\n');
*next = '\0'; *next = '\0';
int nwords = utils::count_words(buf); int nwords = utils::trim_and_count_words(buf);
*next = '\n'; *next = '\n';
if (nwords != avec_bonus->size_data_bonus) if (nwords != avec_bonus->size_data_bonus)

View File

@ -218,7 +218,7 @@ void FixPropertyAtom::read_data_section(char *keyword, int n, char *buf,
next = strchr(buf,'\n'); next = strchr(buf,'\n');
*next = '\0'; *next = '\0';
int nwords = utils::count_words(buf); int nwords = utils::trim_and_count_words(buf);
*next = '\n'; *next = '\n';
if (nwords != nvalue+1) { if (nwords != nvalue+1) {

View File

@ -447,12 +447,12 @@ void FixTMD::readfile(char *file)
zprd = hi - lo; zprd = hi - lo;
bufptr = next + 1; bufptr = next + 1;
continue; continue;
} else if (utils::count_words(bufptr) == 4) { } else if (utils::trim_and_count_words(bufptr) == 4) {
if (xprd >= 0.0 || yprd >= 0.0 || zprd >= 0.0) if (xprd >= 0.0 || yprd >= 0.0 || zprd >= 0.0)
error->all(FLERR,"Incorrect format in TMD target file"); error->all(FLERR,"Incorrect format in TMD target file");
imageflag = 0; imageflag = 0;
firstline = 0; firstline = 0;
} else if (utils::count_words(bufptr) == 7) { } else if (utils::trim_and_count_words(bufptr) == 7) {
if (xprd < 0.0 || yprd < 0.0 || zprd < 0.0) if (xprd < 0.0 || yprd < 0.0 || zprd < 0.0)
error->all(FLERR,"Incorrect format in TMD target file"); error->all(FLERR,"Incorrect format in TMD target file");
imageflag = 1; imageflag = 1;

View File

@ -1401,7 +1401,7 @@ void Molecule::body(int flag, int pflag, char *line)
while (nword < nparam) { while (nword < nparam) {
readline(line); readline(line);
ncount = utils::count_words(line); ncount = utils::trim_and_count_words(line);
if (ncount == 0) if (ncount == 0)
error->one(FLERR,"Too few values in body section of molecule file"); error->one(FLERR,"Too few values in body section of molecule file");
if (nword+ncount > nparam) if (nword+ncount > nparam)

View File

@ -1734,7 +1734,7 @@ void ReadData::bodies(int firstpass, AtomVec *ptr)
while (nword < ninteger) { while (nword < ninteger) {
eof = fgets(&buffer[m],MAXLINE,fp); eof = fgets(&buffer[m],MAXLINE,fp);
if (eof == NULL) error->one(FLERR,"Unexpected end of data file"); if (eof == NULL) error->one(FLERR,"Unexpected end of data file");
ncount = utils::count_words(&buffer[m]); ncount = utils::trim_and_count_words(&buffer[m]);
if (ncount == 0) if (ncount == 0)
error->one(FLERR,"Too few values in body lines in data file"); error->one(FLERR,"Too few values in body lines in data file");
nword += ncount; nword += ncount;
@ -1748,7 +1748,7 @@ void ReadData::bodies(int firstpass, AtomVec *ptr)
while (nword < ndouble) { while (nword < ndouble) {
eof = fgets(&buffer[m],MAXLINE,fp); eof = fgets(&buffer[m],MAXLINE,fp);
if (eof == NULL) error->one(FLERR,"Unexpected end of data file"); if (eof == NULL) error->one(FLERR,"Unexpected end of data file");
ncount = utils::count_words(&buffer[m]); ncount = utils::trim_and_count_words(&buffer[m]);
if (ncount == 0) if (ncount == 0)
error->one(FLERR,"Too few values in body lines in data file"); error->one(FLERR,"Too few values in body lines in data file");
nword += ncount; nword += ncount;

View File

@ -162,7 +162,7 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
char *labelline = &line[strlen("ITEM: ATOMS ")]; char *labelline = &line[strlen("ITEM: ATOMS ")];
nwords = utils::count_words(labelline); nwords = utils::trim_and_count_words(labelline);
char **labels = new char*[nwords]; char **labels = new char*[nwords];
labels[0] = strtok(labelline," \t\n\r\f"); labels[0] = strtok(labelline," \t\n\r\f");
if (labels[0] == NULL) { if (labels[0] == NULL) {

View File

@ -29,7 +29,7 @@
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype) TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype)
: filename(filename), filetype(filetype) : filename(filename), filetype(filetype), ignore_comments(true)
{ {
fp = fopen(filename.c_str(), "r"); fp = fopen(filename.c_str(), "r");

View File

@ -160,7 +160,7 @@ Thermo::Thermo(LAMMPS *lmp, int narg, char **arg) : Pointers(lmp)
// allocate per-field memory // allocate per-field memory
// process line of keywords // process line of keywords
nfield_initial = utils::count_words(line); nfield_initial = utils::trim_and_count_words(line);
allocate(); allocate();
parse_fields(line); parse_fields(line);

View File

@ -355,14 +355,22 @@ std::string utils::trim_comment(const std::string & line) {
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
Trim comment from string and return number of words Return number of words
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
size_t utils::count_words(const std::string & text, const std::string & seperators) { size_t utils::count_words(const std::string & text, const std::string & seperators) {
Tokenizer words(utils::trim_comment(text), seperators); ValueTokenizer words(text, seperators);
return words.count(); return words.count();
} }
/* ----------------------------------------------------------------------
Trim comment from string and return number of words
------------------------------------------------------------------------- */
size_t utils::trim_and_count_words(const std::string & text, const std::string & seperators) {
return utils::count_words(utils::trim_comment(text), seperators);
}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
Return whether string is a valid integer number Return whether string is a valid integer number
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */

View File

@ -142,13 +142,21 @@ namespace LAMMPS_NS {
*/ */
std::string trim_comment(const std::string & line); std::string trim_comment(const std::string & line);
/**
* \brief Count words in string
* \param text string that should be searched
* \param seperators string containing characters that will be treated as whitespace
* \return number of words found
*/
size_t count_words(const std::string & text, const std::string & seperators = " \t\r\n\f");
/** /**
* \brief Count words in a single line, trim anything from '#' onward * \brief Count words in a single line, trim anything from '#' onward
* \param text string that should be trimmed and searched * \param text string that should be trimmed and searched
* \param seperators string containing characters that will be treated as whitespace * \param seperators string containing characters that will be treated as whitespace
* \return number of words found * \return number of words found
*/ */
size_t count_words(const std::string & text, const std::string & seperators = " \t\r\n\f"); size_t trim_and_count_words(const std::string & text, const std::string & seperators = " \t\r\n\f");
/** /**
* \brief Check if string can be converted to valid integer * \brief Check if string can be converted to valid integer

View File

@ -25,7 +25,11 @@ TEST(Utils, trim_comment) {
} }
TEST(Utils, count_words) { TEST(Utils, count_words) {
ASSERT_EQ(utils::count_words("some text # comment"), 2); ASSERT_EQ(utils::count_words("some text # comment"), 4);
}
TEST(Utils, trim_and_count_words) {
ASSERT_EQ(utils::trim_and_count_words("some text # comment"), 2);
} }
TEST(Utils, valid_integer1) { TEST(Utils, valid_integer1) {