[C++ runtime] Optimizations of Vocabulary::fromTokenNames

This commit doesn't change the behavior, it contains few small improvements:
- prevent useless copy when creating the variable tokenName
- avoid to check if tokenName is empty twice
- use std::string::empty() instead of creating an empty string to compare with
- use std::string::clear() instead of assigning an empty C string to clear a string
This commit is contained in:
irabeson 2020-06-04 22:29:27 -04:00
parent 621b933c7a
commit 3c0dbd83dd
3 changed files with 16 additions and 24 deletions

View File

@ -242,4 +242,5 @@ YYYY/MM/DD, github id, Full name, email
2020/02/21, StochasticTinkr, Daniel Pitts, github@coloraura.com
2020/03/17, XsongyangX, Song Yang, songyang1218@gmail.com
2020/04/07, deniskyashif, Denis Kyashif, denis.kyashif@gmail.com
2020/04/30, TristonianJones, Tristan Swadell, tswadell@google.com
2020/04/30, TristonianJones, Tristan Swadell, tswadell@google.com
2020/06/04, IohannRabeson, Iohann Rabeson, iotaka6@gmail.com

View File

@ -22,8 +22,7 @@ Vocabulary::Vocabulary(const std::vector<std::string> &literalNames,
// See note here on -1 part: https://github.com/antlr/antlr4/pull/1146
}
Vocabulary::~Vocabulary() {
}
Vocabulary::~Vocabulary() = default;
Vocabulary Vocabulary::fromTokenNames(const std::vector<std::string> &tokenNames) {
if (tokenNames.empty()) {
@ -34,25 +33,18 @@ Vocabulary Vocabulary::fromTokenNames(const std::vector<std::string> &tokenNames
std::vector<std::string> symbolicNames = tokenNames;
std::locale locale;
for (size_t i = 0; i < tokenNames.size(); i++) {
std::string tokenName = tokenNames[i];
if (tokenName == "") {
const std::string& tokenName = tokenNames[i];
if (tokenName.empty()) {
continue;
} else if (tokenName.front() == '\'') {
symbolicNames[i].clear();
} else if (std::isupper(tokenName.front(), locale)) {
literalNames[i].clear();
} else {
// wasn't a literal or symbolic name
literalNames[i].clear();
symbolicNames[i].clear();
}
if (!tokenName.empty()) {
char firstChar = tokenName[0];
if (firstChar == '\'') {
symbolicNames[i] = "";
continue;
} else if (std::isupper(firstChar, locale)) {
literalNames[i] = "";
continue;
}
}
// wasn't a literal or symbolic name
literalNames[i] = "";
symbolicNames[i] = "";
}
return Vocabulary(literalNames, symbolicNames, tokenNames);

View File

@ -14,9 +14,6 @@ namespace dfa {
/// interface.
class ANTLR4CPP_PUBLIC Vocabulary {
public:
Vocabulary(Vocabulary const&) = default;
virtual ~Vocabulary();
/// Gets an empty <seealso cref="Vocabulary"/> instance.
///
/// <para>
@ -25,7 +22,9 @@ namespace dfa {
/// except <seealso cref="Token#EOF"/>.</para>
static const Vocabulary EMPTY_VOCABULARY;
Vocabulary() {}
Vocabulary() = default;
Vocabulary(Vocabulary const&) = default;
virtual ~Vocabulary();
/// <summary>
/// Constructs a new instance of <seealso cref="Vocabulary"/> from the specified