add utils::strdup() convenience function
This commit is contained in:
@ -71,6 +71,9 @@ and parsing files or arguments.
|
|||||||
|
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
.. doxygenfunction:: strdup
|
||||||
|
:project: progguide
|
||||||
|
|
||||||
.. doxygenfunction:: trim
|
.. doxygenfunction:: trim
|
||||||
:project: progguide
|
:project: progguide
|
||||||
|
|
||||||
|
|||||||
@ -546,6 +546,18 @@ int utils::expand_args(const char *file, int line, int narg, char **arg,
|
|||||||
return newarg;
|
return newarg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------
|
||||||
|
Make copy of string in new storage. Works like the (non-portable)
|
||||||
|
C-style strdup() but also accepts a C++ string as argument.
|
||||||
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
char *utils::strdup(const std::string &text)
|
||||||
|
{
|
||||||
|
char *tmp = new char[text.size()+1];
|
||||||
|
strcpy(tmp,text.c_str());
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
Return string without leading or trailing whitespace
|
Return string without leading or trailing whitespace
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|||||||
11
src/utils.h
11
src/utils.h
@ -195,6 +195,17 @@ namespace LAMMPS_NS {
|
|||||||
int expand_args(const char *file, int line, int narg, char **arg,
|
int expand_args(const char *file, int line, int narg, char **arg,
|
||||||
int mode, char **&earg, LAMMPS *lmp);
|
int mode, char **&earg, LAMMPS *lmp);
|
||||||
|
|
||||||
|
/** Make C-style copy of string in new storage
|
||||||
|
*
|
||||||
|
* This allocates a storage buffer and copies the C-style or
|
||||||
|
* C++ style string into it. The buffer is allocated with "new"
|
||||||
|
* and thus needs to be deallocated with "delete[]".
|
||||||
|
*
|
||||||
|
* \param text string that should be copied
|
||||||
|
* \return new buffer with copy of string */
|
||||||
|
|
||||||
|
char *strdup(const std::string &text);
|
||||||
|
|
||||||
/** Trim leading and trailing whitespace. Like TRIM() in Fortran.
|
/** Trim leading and trailing whitespace. Like TRIM() in Fortran.
|
||||||
*
|
*
|
||||||
* \param line string that should be trimmed
|
* \param line string that should be trimmed
|
||||||
|
|||||||
@ -30,6 +30,21 @@ using ::testing::StrEq;
|
|||||||
#define FLERR __FILE__, __LINE__
|
#define FLERR __FILE__, __LINE__
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
TEST(Utils, strdup)
|
||||||
|
{
|
||||||
|
std::string original("some_text");
|
||||||
|
const char *copy = utils::strdup(original);
|
||||||
|
ASSERT_THAT(original, StrEq(copy));
|
||||||
|
ASSERT_NE(copy,original.c_str());
|
||||||
|
|
||||||
|
const char *copy2 = utils::strdup(copy);
|
||||||
|
ASSERT_THAT(original, StrEq(copy2));
|
||||||
|
ASSERT_NE(copy,copy2);
|
||||||
|
|
||||||
|
delete[] copy;
|
||||||
|
delete[] copy2;
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Utils, trim)
|
TEST(Utils, trim)
|
||||||
{
|
{
|
||||||
auto trimmed = utils::trim("\t some text");
|
auto trimmed = utils::trim("\t some text");
|
||||||
|
|||||||
Reference in New Issue
Block a user