From 5d4614b6260a4a9e7a27f8565326d5c86bc72b21 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 19 Mar 2021 14:10:20 -0400 Subject: [PATCH 1/6] Correct vformat for bigint in dump_custom.cpp --- src/dump_custom.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dump_custom.cpp b/src/dump_custom.cpp index 53bc9449a0..6f4d7c50b0 100644 --- a/src/dump_custom.cpp +++ b/src/dump_custom.cpp @@ -285,7 +285,7 @@ void DumpCustom::init_style() else if (vtype[i] == Dump::DOUBLE && format_float_user) vformat[i] = utils::strdup(std::string(format_float_user) + " "); else if (vtype[i] == Dump::BIGINT && format_bigint_user) - vformat[i] = utils::strdup(std::string(format_int_user) + " "); + vformat[i] = utils::strdup(std::string(format_bigint_user) + " "); else vformat[i] = utils::strdup(word + " "); // remove trailing blank on last column's format From a33a04a39297bcb38c48ee7f809e76ed9d1fd508 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 19 Mar 2021 14:12:09 -0400 Subject: [PATCH 2/6] Remove redundant has_next() check in Tokenizer --- src/tokenizer.cpp | 70 ++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 8467d07b95..03fe06080b 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -197,70 +197,60 @@ bool ValueTokenizer::contains(const std::string &value) const { * * \return string with next token */ std::string ValueTokenizer::next_string() { - if (has_next()) { - std::string value = tokens.next(); - return value; - } throw TokenizerException("Not enough tokens",""); + std::string value = tokens.next(); + return value; } /*! Retrieve next token and convert to int * * \return value of next token */ int ValueTokenizer::next_int() { - if (has_next()) { - std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); - if (!utils::is_integer(current)) { - throw InvalidIntegerException(current); - } - int value = atoi(current.c_str()); - return value; - } throw TokenizerException("Not enough tokens",""); + std::string current = tokens.next(); + if (utils::has_utf8(current)) current = utils::utf8_subst(current); + if (!utils::is_integer(current)) { + throw InvalidIntegerException(current); + } + int value = atoi(current.c_str()); + return value; } /*! Retrieve next token and convert to bigint * * \return value of next token */ bigint ValueTokenizer::next_bigint() { - if (has_next()) { - std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); - if (!utils::is_integer(current)) { - throw InvalidIntegerException(current); - } - bigint value = ATOBIGINT(current.c_str()); - return value; - } throw TokenizerException("Not enough tokens",""); + std::string current = tokens.next(); + if (utils::has_utf8(current)) current = utils::utf8_subst(current); + if (!utils::is_integer(current)) { + throw InvalidIntegerException(current); + } + bigint value = ATOBIGINT(current.c_str()); + return value; } /*! Retrieve next token and convert to tagint * * \return value of next token */ tagint ValueTokenizer::next_tagint() { - if (has_next()) { - std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); - if (!utils::is_integer(current)) { - throw InvalidIntegerException(current); - } - tagint value = ATOTAGINT(current.c_str()); - return value; - } throw TokenizerException("Not enough tokens",""); + std::string current = tokens.next(); + if (utils::has_utf8(current)) current = utils::utf8_subst(current); + if (!utils::is_integer(current)) { + throw InvalidIntegerException(current); + } + tagint value = ATOTAGINT(current.c_str()); + return value; } /*! Retrieve next token and convert to double * * \return value of next token */ double ValueTokenizer::next_double() { - if (has_next()) { - std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); - if (!utils::is_double(current)) { - throw InvalidFloatException(current); - } - double value = atof(current.c_str()); - return value; - } throw TokenizerException("Not enough tokens",""); + std::string current = tokens.next(); + if (utils::has_utf8(current)) current = utils::utf8_subst(current); + if (!utils::is_double(current)) { + throw InvalidFloatException(current); + } + double value = atof(current.c_str()); + return value; } /*! Skip over a given number of tokens From 1f50557b38b232a5bf12c132802a98c44b4d9f33 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 19 Mar 2021 15:33:00 -0400 Subject: [PATCH 3/6] Adopt utils::strdup in dump styles --- src/COMPRESS/dump_atom_gz.cpp | 6 ++--- src/COMPRESS/dump_atom_zstd.cpp | 6 ++--- src/COMPRESS/dump_cfg_gz.cpp | 6 ++--- src/COMPRESS/dump_cfg_zstd.cpp | 6 ++--- src/COMPRESS/dump_custom_gz.cpp | 6 ++--- src/COMPRESS/dump_custom_zstd.cpp | 6 ++--- src/COMPRESS/dump_local_gz.cpp | 6 ++--- src/COMPRESS/dump_local_zstd.cpp | 6 ++--- src/COMPRESS/dump_xyz_gz.cpp | 6 ++--- src/COMPRESS/dump_xyz_zstd.cpp | 6 ++--- src/dump.cpp | 35 ++++++++------------------ src/dump_cfg.cpp | 10 +++----- src/dump_custom.cpp | 41 +++++++++---------------------- src/dump_local.cpp | 8 ++---- src/dump_xyz.cpp | 20 +++++---------- 15 files changed, 52 insertions(+), 122 deletions(-) diff --git a/src/COMPRESS/dump_atom_gz.cpp b/src/COMPRESS/dump_atom_gz.cpp index 7c30d7c101..f252e32064 100644 --- a/src/COMPRESS/dump_atom_gz.cpp +++ b/src/COMPRESS/dump_atom_gz.cpp @@ -77,14 +77,12 @@ void DumpAtomGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_atom_zstd.cpp b/src/COMPRESS/dump_atom_zstd.cpp index f51a8b393b..3dde07bbf4 100644 --- a/src/COMPRESS/dump_atom_zstd.cpp +++ b/src/COMPRESS/dump_atom_zstd.cpp @@ -76,14 +76,12 @@ void DumpAtomZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_cfg_gz.cpp b/src/COMPRESS/dump_cfg_gz.cpp index 9beb606b24..378baf502f 100644 --- a/src/COMPRESS/dump_cfg_gz.cpp +++ b/src/COMPRESS/dump_cfg_gz.cpp @@ -81,14 +81,12 @@ void DumpCFGGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_cfg_zstd.cpp b/src/COMPRESS/dump_cfg_zstd.cpp index 13890334ba..459649c70a 100644 --- a/src/COMPRESS/dump_cfg_zstd.cpp +++ b/src/COMPRESS/dump_cfg_zstd.cpp @@ -79,14 +79,12 @@ void DumpCFGZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_custom_gz.cpp b/src/COMPRESS/dump_custom_gz.cpp index f345f4b111..4f03a4a232 100644 --- a/src/COMPRESS/dump_custom_gz.cpp +++ b/src/COMPRESS/dump_custom_gz.cpp @@ -79,14 +79,12 @@ void DumpCustomGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_custom_zstd.cpp b/src/COMPRESS/dump_custom_zstd.cpp index 81ace6172f..3aa3f874ea 100644 --- a/src/COMPRESS/dump_custom_zstd.cpp +++ b/src/COMPRESS/dump_custom_zstd.cpp @@ -76,14 +76,12 @@ void DumpCustomZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_local_gz.cpp b/src/COMPRESS/dump_local_gz.cpp index e0d2f0d2dd..e8065a848a 100644 --- a/src/COMPRESS/dump_local_gz.cpp +++ b/src/COMPRESS/dump_local_gz.cpp @@ -79,14 +79,12 @@ void DumpLocalGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_local_zstd.cpp b/src/COMPRESS/dump_local_zstd.cpp index c03670ffba..d26555d282 100644 --- a/src/COMPRESS/dump_local_zstd.cpp +++ b/src/COMPRESS/dump_local_zstd.cpp @@ -78,14 +78,12 @@ void DumpLocalZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_xyz_gz.cpp b/src/COMPRESS/dump_xyz_gz.cpp index 21a55f6b07..c63d354e80 100644 --- a/src/COMPRESS/dump_xyz_gz.cpp +++ b/src/COMPRESS/dump_xyz_gz.cpp @@ -78,14 +78,12 @@ void DumpXYZGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_xyz_zstd.cpp b/src/COMPRESS/dump_xyz_zstd.cpp index 9c220bdca7..7c5b73d0ba 100644 --- a/src/COMPRESS/dump_xyz_zstd.cpp +++ b/src/COMPRESS/dump_xyz_zstd.cpp @@ -76,14 +76,12 @@ void DumpXYZZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/dump.cpp b/src/dump.cpp index ab2a84ab22..031364dada 100644 --- a/src/dump.cpp +++ b/src/dump.cpp @@ -46,20 +46,14 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp) MPI_Comm_rank(world,&me); MPI_Comm_size(world,&nprocs); - int n = strlen(arg[0]) + 1; - id = new char[n]; - strcpy(id,arg[0]); + id = utils::strdup(arg[0]); igroup = group->find(arg[1]); groupbit = group->bitmask[igroup]; - n = strlen(arg[2]) + 1; - style = new char[n]; - strcpy(style,arg[2]); + style = utils::strdup(arg[2]); - n = strlen(arg[4]) + 1; - filename = new char[n]; - strcpy(filename,arg[4]); + filename = utils::strdup(arg[4]); comm_forward = comm_reverse = 0; @@ -140,9 +134,8 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp) filewriter = 1; fileproc = me; MPI_Comm_split(world,me,0,&clustercomm); - multiname = new char[strlen(filename) + 16]; *ptr = '\0'; - sprintf(multiname,"%s%d%s",filename,me,ptr+1); + multiname = utils::strdup(fmt::format("{}{}{}", filename, me, ptr+1)); *ptr = '%'; } @@ -573,14 +566,12 @@ void Dump::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } @@ -952,9 +943,7 @@ void Dump::modify_params(int narg, char **arg) int n; if (strstr(arg[iarg+1],"v_") == arg[iarg+1]) { delete [] output->var_dump[idump]; - n = strlen(&arg[iarg+1][2]) + 1; - output->var_dump[idump] = new char[n]; - strcpy(output->var_dump[idump],&arg[iarg+1][2]); + output->var_dump[idump] = utils::strdup(&arg[iarg+1][2]); n = 0; } else { n = utils::inumeric(FLERR,arg[iarg+1],false,lmp); @@ -984,10 +973,9 @@ void Dump::modify_params(int narg, char **arg) MPI_Comm_split(world,icluster,0,&clustercomm); delete [] multiname; - multiname = new char[strlen(filename) + 16]; char *ptr = strchr(filename,'%'); *ptr = '\0'; - sprintf(multiname,"%s%d%s",filename,icluster,ptr+1); + multiname = utils::strdup(fmt::format("{}{}{}", filename, icluster, ptr+1)); *ptr = '%'; iarg += 2; @@ -1028,9 +1016,7 @@ void Dump::modify_params(int narg, char **arg) if (strcmp(arg[iarg+1],"line") == 0) { delete [] format_line_user; - int n = strlen(arg[iarg+2]) + 1; - format_line_user = new char[n]; - strcpy(format_line_user,arg[iarg+2]); + format_line_user = utils::strdup(arg[iarg+2]); iarg += 3; } else { // pass other format options to child classes int n = modify_param(narg-iarg,&arg[iarg]); @@ -1085,10 +1071,9 @@ void Dump::modify_params(int narg, char **arg) MPI_Comm_split(world,icluster,0,&clustercomm); delete [] multiname; - multiname = new char[strlen(filename) + 16]; char *ptr = strchr(filename,'%'); *ptr = '\0'; - sprintf(multiname,"%s%d%s",filename,icluster,ptr+1); + multiname = utils::strdup(fmt::format("{}{}{}", filename, icluster, ptr+1)); *ptr = '%'; iarg += 2; diff --git a/src/dump_cfg.cpp b/src/dump_cfg.cpp index b4e6af90cf..0ff32720d4 100644 --- a/src/dump_cfg.cpp +++ b/src/dump_cfg.cpp @@ -74,14 +74,10 @@ DumpCFG::DumpCFG(LAMMPS *lmp, int narg, char **arg) : |ArgInfo::DNAME|ArgInfo::INAME); if (argi.get_dim() == 1) { - std::string newarg(std::to_string(earg[iarg][0])); - newarg += std::string("_") + argi.get_name(); - newarg += std::string("_") + std::to_string(argi.get_index1()); - auxname[i] = new char[newarg.size()+1]; - strcpy(auxname[i],newarg.c_str()); + std::string newarg = fmt::format("{}_{}_{}", earg[iarg][0], argi.get_name(), argi.get_index1()); + auxname[i] = utils::strdup(newarg); } else { - auxname[i] = new char[strlen(earg[iarg]) + 1]; - strcpy(auxname[i],earg[iarg]); + auxname[i] = utils::strdup(earg[iarg]); } } } diff --git a/src/dump_custom.cpp b/src/dump_custom.cpp index 6f4d7c50b0..53094849ac 100644 --- a/src/dump_custom.cpp +++ b/src/dump_custom.cpp @@ -1562,9 +1562,7 @@ int DumpCustom::add_compute(const char *id) delete [] compute; compute = new Compute*[ncompute+1]; - int n = strlen(id) + 1; - id_compute[ncompute] = new char[n]; - strcpy(id_compute[ncompute],id); + id_compute[ncompute] = utils::strdup(id); ncompute++; return ncompute-1; } @@ -1587,9 +1585,7 @@ int DumpCustom::add_fix(const char *id) delete [] fix; fix = new Fix*[nfix+1]; - int n = strlen(id) + 1; - id_fix[nfix] = new char[n]; - strcpy(id_fix[nfix],id); + id_fix[nfix] = utils::strdup(id); nfix++; return nfix-1; } @@ -1616,9 +1612,7 @@ int DumpCustom::add_variable(const char *id) vbuf = new double*[nvariable+1]; for (int i = 0; i <= nvariable; i++) vbuf[i] = nullptr; - int n = strlen(id) + 1; - id_variable[nvariable] = new char[n]; - strcpy(id_variable[nvariable],id); + id_variable[nvariable] = utils::strdup(id); nvariable++; return nvariable-1; } @@ -1642,9 +1636,7 @@ int DumpCustom::add_custom(const char *id, int flag) flag_custom = (int *) memory->srealloc(flag_custom,(ncustom+1)*sizeof(int),"dump:flag_custom"); - int n = strlen(id) + 1; - id_custom[ncustom] = new char[n]; - strcpy(id_custom[ncustom],id); + id_custom[ncustom] = utils::strdup(id); flag_custom[ncustom] = flag; ncustom++; @@ -1663,9 +1655,7 @@ int DumpCustom::modify_param(int narg, char **arg) if (iregion == -1) error->all(FLERR,"Dump_modify region ID does not exist"); delete [] idregion; - int n = strlen(arg[1]) + 1; - idregion = new char[n]; - strcpy(idregion,arg[1]); + idregion = utils::strdup(arg[1]); } return 2; } @@ -1686,11 +1676,9 @@ int DumpCustom::modify_param(int narg, char **arg) if (strcmp(arg[1],"int") == 0) { delete [] format_int_user; - int n = strlen(arg[2]) + 1; - format_int_user = new char[n]; - strcpy(format_int_user,arg[2]); + format_int_user = utils::strdup(arg[2]); delete [] format_bigint_user; - n = strlen(format_int_user) + 8; + int n = strlen(format_int_user) + 8; format_bigint_user = new char[n]; // replace "d" in format_int_user with bigint format specifier // use of &str[1] removes leading '%' from BIGINT_FORMAT string @@ -1706,18 +1694,14 @@ int DumpCustom::modify_param(int narg, char **arg) } else if (strcmp(arg[1],"float") == 0) { delete [] format_float_user; - int n = strlen(arg[2]) + 1; - format_float_user = new char[n]; - strcpy(format_float_user,arg[2]); + format_float_user = utils::strdup(arg[2]); } else { int i = utils::inumeric(FLERR,arg[1],false,lmp) - 1; if (i < 0 || i >= nfield) error->all(FLERR,"Illegal dump_modify command"); if (format_column_user[i]) delete [] format_column_user[i]; - int n = strlen(arg[2]) + 1; - format_column_user[i] = new char[n]; - strcpy(format_column_user[i],arg[2]); + format_column_user[i] = utils::strdup(arg[2]); } return 3; } @@ -1730,9 +1714,7 @@ int DumpCustom::modify_param(int narg, char **arg) delete [] typenames; typenames = new char*[ntypes+1]; for (int itype = 1; itype <= ntypes; itype++) { - int n = strlen(arg[itype]) + 1; - typenames[itype] = new char[n]; - strcpy(typenames[itype],arg[itype]); + typenames[itype] = utils::strdup(arg[itype]); } return ntypes+1; } @@ -1998,8 +1980,7 @@ int DumpCustom::modify_param(int narg, char **arg) memory->grow(thresh_first,(nthreshlast+1),"dump:thresh_first"); std::string threshid = fmt::format("{}{}_DUMP_STORE",id,nthreshlast); - thresh_fixID[nthreshlast] = new char[threshid.size()+1]; - strcpy(thresh_fixID[nthreshlast],threshid.c_str()); + thresh_fixID[nthreshlast] = utils::strdup(threshid); modify->add_fix(fmt::format("{} {} STORE peratom 1 1",threshid, group->names[igroup])); thresh_fix[nthreshlast] = (FixStore *) modify->fix[modify->nfix-1]; diff --git a/src/dump_local.cpp b/src/dump_local.cpp index beb9236d63..53a82b496f 100644 --- a/src/dump_local.cpp +++ b/src/dump_local.cpp @@ -472,9 +472,7 @@ int DumpLocal::add_compute(const char *id) delete [] compute; compute = new Compute*[ncompute+1]; - int n = strlen(id) + 1; - id_compute[ncompute] = new char[n]; - strcpy(id_compute[ncompute],id); + id_compute[ncompute] = utils::strdup(id); ncompute++; return ncompute-1; } @@ -497,9 +495,7 @@ int DumpLocal::add_fix(const char *id) delete [] fix; fix = new Fix*[nfix+1]; - int n = strlen(id) + 1; - id_fix[nfix] = new char[n]; - strcpy(id_fix[nfix],id); + id_fix[nfix] = utils::strdup(id); nfix++; return nfix-1; } diff --git a/src/dump_xyz.cpp b/src/dump_xyz.cpp index 108ea6f4d6..5f478a2443 100644 --- a/src/dump_xyz.cpp +++ b/src/dump_xyz.cpp @@ -40,10 +40,7 @@ DumpXYZ::DumpXYZ(LAMMPS *lmp, int narg, char **arg) : Dump(lmp, narg, arg), if (format_default) delete [] format_default; - char *str = (char *) "%s %g %g %g"; - int n = strlen(str) + 1; - format_default = new char[n]; - strcpy(format_default,str); + format_default = utils::strdup("%s %g %g %g"); ntypes = atom->ntypes; typenames = nullptr; @@ -71,14 +68,11 @@ void DumpXYZ::init_style() // format = copy of default or user-specified line format delete [] format; - char *str; - if (format_line_user) str = format_line_user; - else str = format_default; - int n = strlen(str) + 2; - format = new char[n]; - strcpy(format,str); - strcat(format,"\n"); + if (format_line_user) + format = utils::strdup(fmt::format("{}\n", format_line_user)); + else + format = utils::strdup(fmt::format("{}\n", format_default)); // initialize typenames array to be backward compatible by default // a 32-bit int can be maximally 10 digits plus sign @@ -119,9 +113,7 @@ int DumpXYZ::modify_param(int narg, char **arg) typenames = new char*[ntypes+1]; for (int itype = 1; itype <= ntypes; itype++) { - int n = strlen(arg[itype]) + 1; - typenames[itype] = new char[n]; - strcpy(typenames[itype],arg[itype]); + typenames[itype] = utils::strdup(arg[itype]); } return ntypes+1; From 6503a7c3bab1e5b37ba6680e1e60a5ca0cc7cca1 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 19 Mar 2021 15:52:27 -0400 Subject: [PATCH 4/6] skip explicit temporaries --- src/tokenizer.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 03fe06080b..0954a4a4cd 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -197,8 +197,7 @@ bool ValueTokenizer::contains(const std::string &value) const { * * \return string with next token */ std::string ValueTokenizer::next_string() { - std::string value = tokens.next(); - return value; + return tokens.next(); } /*! Retrieve next token and convert to int @@ -210,8 +209,7 @@ int ValueTokenizer::next_int() { if (!utils::is_integer(current)) { throw InvalidIntegerException(current); } - int value = atoi(current.c_str()); - return value; + return atoi(current.c_str()); } /*! Retrieve next token and convert to bigint @@ -223,8 +221,7 @@ bigint ValueTokenizer::next_bigint() { if (!utils::is_integer(current)) { throw InvalidIntegerException(current); } - bigint value = ATOBIGINT(current.c_str()); - return value; + return ATOBIGINT(current.c_str()); } /*! Retrieve next token and convert to tagint @@ -236,8 +233,7 @@ tagint ValueTokenizer::next_tagint() { if (!utils::is_integer(current)) { throw InvalidIntegerException(current); } - tagint value = ATOTAGINT(current.c_str()); - return value; + return ATOTAGINT(current.c_str()); } /*! Retrieve next token and convert to double @@ -249,8 +245,7 @@ double ValueTokenizer::next_double() { if (!utils::is_double(current)) { throw InvalidFloatException(current); } - double value = atof(current.c_str()); - return value; + return atof(current.c_str()); } /*! Skip over a given number of tokens From 9707771f1c50827ea21ebcdc51153b5f0c4844ef Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 19 Mar 2021 16:10:37 -0400 Subject: [PATCH 5/6] apply UTF-8 character replacement before creating tokens --- src/tokenizer.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 0954a4a4cd..d26a5199f9 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -35,12 +35,14 @@ TokenizerException::TokenizerException(const std::string &msg, const std::string /** Class for splitting text into words * * This tokenizer will break down a string into sub-strings (i.e words) - * separated by the given separator characters. + * separated by the given separator characters. If the string contains + * certain known UTF-8 characters they will be replaced by their ASCII + * equivalents processing the string. * \verbatim embed:rst *See also* - :cpp:class:`ValueTokenizer`, :cpp:func:`utils::split_words` + :cpp:class:`ValueTokenizer`, :cpp:func:`utils::split_words`, :cpp:func:`utils::utf8_subst` \endverbatim * @@ -50,6 +52,8 @@ TokenizerException::TokenizerException(const std::string &msg, const std::string Tokenizer::Tokenizer(const std::string &str, const std::string &separators) : text(str), separators(separators), start(0), ntokens(std::string::npos) { + // replace known UTF-8 characters with ASCII equivalents + if (utils::has_utf8(text)) text = utils::utf8_subst(text); reset(); } @@ -205,7 +209,6 @@ std::string ValueTokenizer::next_string() { * \return value of next token */ int ValueTokenizer::next_int() { std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); if (!utils::is_integer(current)) { throw InvalidIntegerException(current); } @@ -217,7 +220,6 @@ int ValueTokenizer::next_int() { * \return value of next token */ bigint ValueTokenizer::next_bigint() { std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); if (!utils::is_integer(current)) { throw InvalidIntegerException(current); } @@ -229,7 +231,6 @@ bigint ValueTokenizer::next_bigint() { * \return value of next token */ tagint ValueTokenizer::next_tagint() { std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); if (!utils::is_integer(current)) { throw InvalidIntegerException(current); } @@ -241,7 +242,6 @@ tagint ValueTokenizer::next_tagint() { * \return value of next token */ double ValueTokenizer::next_double() { std::string current = tokens.next(); - if (utils::has_utf8(current)) current = utils::utf8_subst(current); if (!utils::is_double(current)) { throw InvalidFloatException(current); } From 06ee5be2cef23eb23273c7f3fd64ee935178a13b Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 19 Mar 2021 18:51:21 -0400 Subject: [PATCH 6/6] Fix unhandled cases in docs LAMMPS syntax highlighting --- doc/src/fix_rigid_meso.rst | 4 ++-- doc/src/group.rst | 8 ++++---- doc/src/kim_commands.rst | 2 +- doc/utils/sphinx-config/LAMMPSLexer.py | 6 ++++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/doc/src/fix_rigid_meso.rst b/doc/src/fix_rigid_meso.rst index 2acd81a268..dea2354bad 100644 --- a/doc/src/fix_rigid_meso.rst +++ b/doc/src/fix_rigid_meso.rst @@ -48,8 +48,8 @@ Examples fix 1 ellipsoid rigid/meso single fix 1 rods rigid/meso molecule fix 1 spheres rigid/meso single force 1 off off on - fix 1 particles rigid/meso molecule force 1\*5 off off off force 6\*10 off off on - fix 2 spheres rigid/meso group 3 sphere1 sphere2 sphere3 torque \* off off off + fix 1 particles rigid/meso molecule force 1*5 off off off force 6*10 off off on + fix 2 spheres rigid/meso group 3 sphere1 sphere2 sphere3 torque * off off off Description """"""""""" diff --git a/doc/src/group.rst b/doc/src/group.rst index 6371a030ee..3ce885bdc5 100644 --- a/doc/src/group.rst +++ b/doc/src/group.rst @@ -120,12 +120,12 @@ specified atom types, atom IDs, or molecule IDs into the group. These The first format is a list of values (types or IDs). For example, the second command in the examples above puts all atoms of type 3 or 4 into the group named *water*\ . Each entry in the list can be a -colon-separated sequence A:B or A:B:C, as in two of the examples +colon-separated sequence ``A:B`` or ``A:B:C``, as in two of the examples above. A "sequence" generates a sequence of values (types or IDs), -with an optional increment. The first example with 500:1000 has the +with an optional increment. The first example with ``500:1000`` has the default increment of 1 and would add all atom IDs from 500 to 1000 (inclusive) to the group sub, along with 10,25,50 since they also -appear in the list of values. The second example with 100:10000:10 +appear in the list of values. The second example with ``100:10000:10`` uses an increment of 10 and would thus would add atoms IDs 100,110,120, ... 9990,10000 to the group sub. @@ -269,7 +269,7 @@ group and running further. .. code-block:: LAMMPS variable nsteps equal 5000 - variable rad equal 18-(step/v_nsteps)\*(18-5) + variable rad equal 18-(step/v_nsteps)*(18-5) region ss sphere 20 20 0 v_rad group mobile dynamic all region ss fix 1 mobile nve diff --git a/doc/src/kim_commands.rst b/doc/src/kim_commands.rst index 295c6a5e40..f116a70922 100644 --- a/doc/src/kim_commands.rst +++ b/doc/src/kim_commands.rst @@ -1205,7 +1205,7 @@ coordinates of atoms in the unit cell of the cubic crystal. In the case of, e.g. a conventional fcc unit cell, the "source-value" key in the map associated with this key should be assigned the following value: -.. code-block:: LAMMPS +.. code-block:: text [[0.0, 0.0, 0.0], [0.5, 0.5, 0.0], diff --git a/doc/utils/sphinx-config/LAMMPSLexer.py b/doc/utils/sphinx-config/LAMMPSLexer.py index ae40b094c2..6e611f9f74 100644 --- a/doc/utils/sphinx-config/LAMMPSLexer.py +++ b/doc/utils/sphinx-config/LAMMPSLexer.py @@ -40,7 +40,7 @@ class LAMMPSLexer(RegexLexer): (r'compute\s+', Keyword, 'compute'), (r'dump\s+', Keyword, 'dump'), (r'region\s+', Keyword, 'region'), - (r'variable\s+', Keyword, 'variable'), + (r'^\s*variable\s+', Keyword, 'variable_cmd'), (r'group\s+', Keyword, 'group'), (r'change_box\s+', Keyword, 'change_box'), (r'uncompute\s+', Keyword, 'uncompute'), @@ -51,6 +51,7 @@ class LAMMPSLexer(RegexLexer): (r'#.*?\n', Comment), ('"', String, 'string'), ('\'', String, 'single_quote_string'), + (r'[0-9]+:[0-9]+(:[0-9]+)?', Number), (r'[0-9]+(\.[0-9]+)?([eE]\-?[0-9]+)?', Number), ('\$?\(', Name.Variable, 'expression'), ('\$\{', Name.Variable, 'variable'), @@ -58,6 +59,7 @@ class LAMMPSLexer(RegexLexer): (r'\$[\w_]+', Name.Variable), (r'\s+', Whitespace), (r'[\+\-\*\^\|\/\!%&=<>]', Operator), + (r'[\~\.\w_:,@\-\/\\0-9]+', Text), ], 'keywords' : [ (words(LAMMPS_COMMANDS, suffix=r'\b', prefix=r'^'), Keyword) @@ -99,7 +101,7 @@ class LAMMPSLexer(RegexLexer): (r'[\w_\-\.\[\]]+', Name.Variable.Identifier), default('#pop') ], - 'variable' : [ + 'variable_cmd' : [ (r'[\w_\-\.\[\]]+', Name.Variable.Identifier), default('#pop') ],