enable and apply clang-format
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
// clang-format off
|
||||
/* -*- c++ -*- ----------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
https://www.lammps.org/, Sandia National Laboratories
|
||||
@ -17,19 +16,20 @@
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#include "tokenizer.h"
|
||||
#include "utils.h"
|
||||
#include "fmt/format.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
TokenizerException::TokenizerException(const std::string &msg, const std::string &token) {
|
||||
if (token.empty()) {
|
||||
message = msg;
|
||||
} else {
|
||||
message = fmt::format("{}: '{}'", msg, token);
|
||||
}
|
||||
TokenizerException::TokenizerException(const std::string &msg, const std::string &token)
|
||||
{
|
||||
if (token.empty()) {
|
||||
message = msg;
|
||||
} else {
|
||||
message = fmt::format("{}: '{}'", msg, token);
|
||||
}
|
||||
}
|
||||
|
||||
/** Class for splitting text into words
|
||||
@ -52,117 +52,122 @@ TokenizerException::TokenizerException(const std::string &msg, const std::string
|
||||
Tokenizer::Tokenizer(std::string str, std::string _separators) :
|
||||
text(std::move(str)), separators(std::move(_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();
|
||||
// replace known UTF-8 characters with ASCII equivalents
|
||||
if (utils::has_utf8(text)) text = utils::utf8_subst(text);
|
||||
reset();
|
||||
}
|
||||
|
||||
Tokenizer::Tokenizer(const Tokenizer &rhs) :
|
||||
text(rhs.text), separators(rhs.separators), ntokens(rhs.ntokens)
|
||||
{
|
||||
reset();
|
||||
reset();
|
||||
}
|
||||
|
||||
Tokenizer::Tokenizer(Tokenizer && rhs) :
|
||||
Tokenizer::Tokenizer(Tokenizer &&rhs) :
|
||||
text(std::move(rhs.text)), separators(std::move(rhs.separators)), ntokens(rhs.ntokens)
|
||||
{
|
||||
reset();
|
||||
reset();
|
||||
}
|
||||
|
||||
Tokenizer& Tokenizer::operator=(const Tokenizer& other)
|
||||
Tokenizer &Tokenizer::operator=(const Tokenizer &other)
|
||||
{
|
||||
Tokenizer tmp(other);
|
||||
swap(tmp);
|
||||
return *this;
|
||||
Tokenizer tmp(other);
|
||||
swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tokenizer& Tokenizer::operator=(Tokenizer&& other)
|
||||
Tokenizer &Tokenizer::operator=(Tokenizer &&other)
|
||||
{
|
||||
Tokenizer tmp(std::move(other));
|
||||
swap(tmp);
|
||||
return *this;
|
||||
Tokenizer tmp(std::move(other));
|
||||
swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Tokenizer::swap(Tokenizer& other)
|
||||
void Tokenizer::swap(Tokenizer &other)
|
||||
{
|
||||
std::swap(text, other.text);
|
||||
std::swap(separators, other.separators);
|
||||
std::swap(start, other.start);
|
||||
std::swap(ntokens, other.ntokens);
|
||||
std::swap(text, other.text);
|
||||
std::swap(separators, other.separators);
|
||||
std::swap(start, other.start);
|
||||
std::swap(ntokens, other.ntokens);
|
||||
}
|
||||
|
||||
/*! Re-position the tokenizer state to the first word,
|
||||
* i.e. the first non-separator character */
|
||||
void Tokenizer::reset() {
|
||||
start = text.find_first_not_of(separators);
|
||||
void Tokenizer::reset()
|
||||
{
|
||||
start = text.find_first_not_of(separators);
|
||||
}
|
||||
|
||||
/*! Search the text to be processed for a sub-string.
|
||||
*
|
||||
* \param str string to be searched for
|
||||
* \return true if string was found, false if not */
|
||||
bool Tokenizer::contains(const std::string &str) const {
|
||||
return text.find(str) != std::string::npos;
|
||||
bool Tokenizer::contains(const std::string &str) const
|
||||
{
|
||||
return text.find(str) != std::string::npos;
|
||||
}
|
||||
|
||||
/*! Skip over a given number of tokens
|
||||
*
|
||||
* \param n number of tokens to skip over */
|
||||
void Tokenizer::skip(int n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!has_next()) throw TokenizerException("No more tokens", "");
|
||||
|
||||
size_t end = text.find_first_of(separators, start);
|
||||
|
||||
if (end == std::string::npos) {
|
||||
start = end;
|
||||
} else {
|
||||
start = text.find_first_not_of(separators, end+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*! Indicate whether more tokens are available
|
||||
*
|
||||
* \return true if there are more tokens, false if not */
|
||||
bool Tokenizer::has_next() const {
|
||||
return start != std::string::npos;
|
||||
}
|
||||
|
||||
/*! Retrieve next token.
|
||||
*
|
||||
* \return string with the next token */
|
||||
std::string Tokenizer::next() {
|
||||
void Tokenizer::skip(int n)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!has_next()) throw TokenizerException("No more tokens", "");
|
||||
|
||||
size_t end = text.find_first_of(separators, start);
|
||||
|
||||
if (end == std::string::npos) {
|
||||
std::string token = text.substr(start);
|
||||
start = end;
|
||||
return token;
|
||||
start = end;
|
||||
} else {
|
||||
start = text.find_first_not_of(separators, end + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string token = text.substr(start, end-start);
|
||||
start = text.find_first_not_of(separators, end+1);
|
||||
/*! Indicate whether more tokens are available
|
||||
*
|
||||
* \return true if there are more tokens, false if not */
|
||||
bool Tokenizer::has_next() const
|
||||
{
|
||||
return start != std::string::npos;
|
||||
}
|
||||
|
||||
/*! Retrieve next token.
|
||||
*
|
||||
* \return string with the next token */
|
||||
std::string Tokenizer::next()
|
||||
{
|
||||
if (!has_next()) throw TokenizerException("No more tokens", "");
|
||||
|
||||
size_t end = text.find_first_of(separators, start);
|
||||
|
||||
if (end == std::string::npos) {
|
||||
std::string token = text.substr(start);
|
||||
start = end;
|
||||
return token;
|
||||
}
|
||||
|
||||
std::string token = text.substr(start, end - start);
|
||||
start = text.find_first_not_of(separators, end + 1);
|
||||
return token;
|
||||
}
|
||||
|
||||
/*! Count number of tokens in text.
|
||||
*
|
||||
* \return number of counted tokens */
|
||||
size_t Tokenizer::count() {
|
||||
// lazy evaluation
|
||||
if (ntokens == std::string::npos) {
|
||||
ntokens = utils::count_words(text, separators);
|
||||
}
|
||||
return ntokens;
|
||||
size_t Tokenizer::count()
|
||||
{
|
||||
// lazy evaluation
|
||||
if (ntokens == std::string::npos) { ntokens = utils::count_words(text, separators); }
|
||||
return ntokens;
|
||||
}
|
||||
|
||||
/*! Retrieve the entire text converted to an STL vector of tokens.
|
||||
*
|
||||
* \return The STL vector */
|
||||
std::vector<std::string> Tokenizer::as_vector() {
|
||||
std::vector<std::string> Tokenizer::as_vector()
|
||||
{
|
||||
// store current state
|
||||
size_t current = start;
|
||||
|
||||
@ -171,9 +176,7 @@ std::vector<std::string> Tokenizer::as_vector() {
|
||||
// generate vector
|
||||
std::vector<std::string> tokens;
|
||||
|
||||
while (has_next()) {
|
||||
tokens.emplace_back(next());
|
||||
}
|
||||
while (has_next()) { tokens.emplace_back(next()); }
|
||||
|
||||
// restore state
|
||||
start = current;
|
||||
@ -195,107 +198,109 @@ std::vector<std::string> Tokenizer::as_vector() {
|
||||
*
|
||||
* \see Tokenizer InvalidIntegerException InvalidFloatException */
|
||||
|
||||
ValueTokenizer::ValueTokenizer(const std::string &str, const std::string &separators) : tokens(str, separators) {
|
||||
}
|
||||
|
||||
ValueTokenizer::ValueTokenizer(ValueTokenizer &&rhs) : tokens(std::move(rhs.tokens)) {
|
||||
}
|
||||
|
||||
ValueTokenizer& ValueTokenizer::operator=(const ValueTokenizer& other)
|
||||
ValueTokenizer::ValueTokenizer(const std::string &str, const std::string &separators) :
|
||||
tokens(str, separators)
|
||||
{
|
||||
ValueTokenizer tmp(other);
|
||||
swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ValueTokenizer& ValueTokenizer::operator=(ValueTokenizer&& other)
|
||||
ValueTokenizer::ValueTokenizer(ValueTokenizer &&rhs) : tokens(std::move(rhs.tokens)) {}
|
||||
|
||||
ValueTokenizer &ValueTokenizer::operator=(const ValueTokenizer &other)
|
||||
{
|
||||
ValueTokenizer tmp(std::move(other));
|
||||
swap(tmp);
|
||||
return *this;
|
||||
ValueTokenizer tmp(other);
|
||||
swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ValueTokenizer::swap(ValueTokenizer& other)
|
||||
ValueTokenizer &ValueTokenizer::operator=(ValueTokenizer &&other)
|
||||
{
|
||||
std::swap(tokens, other.tokens);
|
||||
ValueTokenizer tmp(std::move(other));
|
||||
swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ValueTokenizer::swap(ValueTokenizer &other)
|
||||
{
|
||||
std::swap(tokens, other.tokens);
|
||||
}
|
||||
|
||||
/*! Indicate whether more tokens are available
|
||||
*
|
||||
* \return true if there are more tokens, false if not */
|
||||
bool ValueTokenizer::has_next() const {
|
||||
return tokens.has_next();
|
||||
bool ValueTokenizer::has_next() const
|
||||
{
|
||||
return tokens.has_next();
|
||||
}
|
||||
|
||||
/*! Search the text to be processed for a sub-string.
|
||||
*
|
||||
* \param value string with value to be searched for
|
||||
* \return true if string was found, false if not */
|
||||
bool ValueTokenizer::contains(const std::string &value) const {
|
||||
return tokens.contains(value);
|
||||
bool ValueTokenizer::contains(const std::string &value) const
|
||||
{
|
||||
return tokens.contains(value);
|
||||
}
|
||||
|
||||
/*! Retrieve next token
|
||||
*
|
||||
* \return string with next token */
|
||||
std::string ValueTokenizer::next_string() {
|
||||
return tokens.next();
|
||||
std::string ValueTokenizer::next_string()
|
||||
{
|
||||
return tokens.next();
|
||||
}
|
||||
|
||||
/*! Retrieve next token and convert to int
|
||||
*
|
||||
* \return value of next token */
|
||||
int ValueTokenizer::next_int() {
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_integer(current)) {
|
||||
throw InvalidIntegerException(current);
|
||||
}
|
||||
return atoi(current.c_str());
|
||||
int ValueTokenizer::next_int()
|
||||
{
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_integer(current)) { throw InvalidIntegerException(current); }
|
||||
return atoi(current.c_str());
|
||||
}
|
||||
|
||||
/*! Retrieve next token and convert to bigint
|
||||
*
|
||||
* \return value of next token */
|
||||
bigint ValueTokenizer::next_bigint() {
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_integer(current)) {
|
||||
throw InvalidIntegerException(current);
|
||||
}
|
||||
return ATOBIGINT(current.c_str());
|
||||
bigint ValueTokenizer::next_bigint()
|
||||
{
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_integer(current)) { throw InvalidIntegerException(current); }
|
||||
return ATOBIGINT(current.c_str());
|
||||
}
|
||||
|
||||
/*! Retrieve next token and convert to tagint
|
||||
*
|
||||
* \return value of next token */
|
||||
tagint ValueTokenizer::next_tagint() {
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_integer(current)) {
|
||||
throw InvalidIntegerException(current);
|
||||
}
|
||||
return ATOTAGINT(current.c_str());
|
||||
tagint ValueTokenizer::next_tagint()
|
||||
{
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_integer(current)) { throw InvalidIntegerException(current); }
|
||||
return ATOTAGINT(current.c_str());
|
||||
}
|
||||
|
||||
/*! Retrieve next token and convert to double
|
||||
*
|
||||
* \return value of next token */
|
||||
double ValueTokenizer::next_double() {
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_double(current)) {
|
||||
throw InvalidFloatException(current);
|
||||
}
|
||||
return atof(current.c_str());
|
||||
double ValueTokenizer::next_double()
|
||||
{
|
||||
std::string current = tokens.next();
|
||||
if (!utils::is_double(current)) { throw InvalidFloatException(current); }
|
||||
return atof(current.c_str());
|
||||
}
|
||||
|
||||
/*! Skip over a given number of tokens
|
||||
*
|
||||
* \param n number of tokens to skip over */
|
||||
void ValueTokenizer::skip(int n) {
|
||||
tokens.skip(n);
|
||||
void ValueTokenizer::skip(int n)
|
||||
{
|
||||
tokens.skip(n);
|
||||
}
|
||||
|
||||
/*! Count number of tokens in text.
|
||||
*
|
||||
* \return number of counted tokens */
|
||||
size_t ValueTokenizer::count() {
|
||||
return tokens.count();
|
||||
size_t ValueTokenizer::count()
|
||||
{
|
||||
return tokens.count();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user