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:
@ -2489,7 +2489,7 @@ void FixRigidSmall::readfile(int which, double **array, int *inbody)
|
||||
buf = buffer;
|
||||
next = strchr(buf,'\n');
|
||||
*next = '\0';
|
||||
int nwords = utils::count_words(buf);
|
||||
int nwords = utils::trim_and_count_words(buf);
|
||||
*next = '\n';
|
||||
|
||||
if (nwords != ATTRIBUTE_PERBODY)
|
||||
|
||||
@ -568,7 +568,7 @@ void PairSNAP::read_files(char *coefffilename, char *paramfilename)
|
||||
MPI_Bcast(&n,1,MPI_INT,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)
|
||||
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(line,n,MPI_CHAR,0,world);
|
||||
|
||||
nwords = utils::count_words(line);
|
||||
nwords = utils::trim_and_count_words(line);
|
||||
if (nwords != 1)
|
||||
error->all(FLERR,"Incorrect format in SNAP coefficient file");
|
||||
|
||||
|
||||
@ -441,7 +441,7 @@ void NEBSpin::readfile(char *file, int flag)
|
||||
buf = buffer;
|
||||
next = strchr(buf,'\n');
|
||||
*next = '\0';
|
||||
int nwords = utils::count_words(buf);
|
||||
int nwords = utils::trim_and_count_words(buf);
|
||||
*next = '\n';
|
||||
|
||||
if (nwords != ATTRIBUTE_PERLINE)
|
||||
|
||||
@ -1089,7 +1089,7 @@ void Atom::data_atoms(int n, char *buf, tagint id_offset, tagint mol_offset,
|
||||
|
||||
next = strchr(buf,'\n');
|
||||
*next = '\0';
|
||||
int nwords = utils::count_words(buf);
|
||||
int nwords = utils::trim_and_count_words(buf);
|
||||
*next = '\n';
|
||||
|
||||
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 = '\0';
|
||||
int nwords = utils::count_words(buf);
|
||||
int nwords = utils::trim_and_count_words(buf);
|
||||
*next = '\n';
|
||||
|
||||
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 = '\0';
|
||||
int nwords = utils::count_words(buf);
|
||||
int nwords = utils::trim_and_count_words(buf);
|
||||
*next = '\n';
|
||||
|
||||
if (nwords != avec_bonus->size_data_bonus)
|
||||
|
||||
@ -218,7 +218,7 @@ void FixPropertyAtom::read_data_section(char *keyword, int n, char *buf,
|
||||
|
||||
next = strchr(buf,'\n');
|
||||
*next = '\0';
|
||||
int nwords = utils::count_words(buf);
|
||||
int nwords = utils::trim_and_count_words(buf);
|
||||
*next = '\n';
|
||||
|
||||
if (nwords != nvalue+1) {
|
||||
|
||||
@ -447,12 +447,12 @@ void FixTMD::readfile(char *file)
|
||||
zprd = hi - lo;
|
||||
bufptr = next + 1;
|
||||
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)
|
||||
error->all(FLERR,"Incorrect format in TMD target file");
|
||||
imageflag = 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)
|
||||
error->all(FLERR,"Incorrect format in TMD target file");
|
||||
imageflag = 1;
|
||||
|
||||
@ -1401,7 +1401,7 @@ void Molecule::body(int flag, int pflag, char *line)
|
||||
while (nword < nparam) {
|
||||
readline(line);
|
||||
|
||||
ncount = utils::count_words(line);
|
||||
ncount = utils::trim_and_count_words(line);
|
||||
if (ncount == 0)
|
||||
error->one(FLERR,"Too few values in body section of molecule file");
|
||||
if (nword+ncount > nparam)
|
||||
|
||||
@ -1734,7 +1734,7 @@ void ReadData::bodies(int firstpass, AtomVec *ptr)
|
||||
while (nword < ninteger) {
|
||||
eof = fgets(&buffer[m],MAXLINE,fp);
|
||||
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)
|
||||
error->one(FLERR,"Too few values in body lines in data file");
|
||||
nword += ncount;
|
||||
@ -1748,7 +1748,7 @@ void ReadData::bodies(int firstpass, AtomVec *ptr)
|
||||
while (nword < ndouble) {
|
||||
eof = fgets(&buffer[m],MAXLINE,fp);
|
||||
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)
|
||||
error->one(FLERR,"Too few values in body lines in data file");
|
||||
nword += ncount;
|
||||
|
||||
@ -162,7 +162,7 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
|
||||
|
||||
char *labelline = &line[strlen("ITEM: ATOMS ")];
|
||||
|
||||
nwords = utils::count_words(labelline);
|
||||
nwords = utils::trim_and_count_words(labelline);
|
||||
char **labels = new char*[nwords];
|
||||
labels[0] = strtok(labelline," \t\n\r\f");
|
||||
if (labels[0] == NULL) {
|
||||
|
||||
@ -29,7 +29,7 @@
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
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");
|
||||
|
||||
|
||||
@ -160,7 +160,7 @@ Thermo::Thermo(LAMMPS *lmp, int narg, char **arg) : Pointers(lmp)
|
||||
// allocate per-field memory
|
||||
// process line of keywords
|
||||
|
||||
nfield_initial = utils::count_words(line);
|
||||
nfield_initial = utils::trim_and_count_words(line);
|
||||
allocate();
|
||||
parse_fields(line);
|
||||
|
||||
|
||||
@ -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) {
|
||||
Tokenizer words(utils::trim_comment(text), seperators);
|
||||
ValueTokenizer words(text, seperators);
|
||||
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
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
10
src/utils.h
10
src/utils.h
@ -142,13 +142,21 @@ namespace LAMMPS_NS {
|
||||
*/
|
||||
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
|
||||
* \param text string that should be trimmed and 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");
|
||||
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
|
||||
|
||||
@ -25,7 +25,11 @@ TEST(Utils, trim_comment) {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user