Internals: Add VString::startsWith predicate function

This commit is contained in:
Geza Lore 2021-07-20 14:18:35 +01:00
parent a9c4a96c0f
commit d9c893af11
4 changed files with 16 additions and 12 deletions

View File

@ -120,14 +120,12 @@ V3OptionParser::ActionIfs* V3OptionParser::find(const char* optp) {
if (it != m_pimpl->m_options.end()) return it->second.get();
for (auto&& act : m_pimpl->m_options) {
if (act.second->isOnOffAllowed()) { // Find starts with "-no"
const char* const nop = std::strncmp(optp, "-no", 3) ? nullptr : (optp + 3);
const char* const nop = VString::startsWith(optp, "-no") ? (optp + 3) : nullptr;
if (nop && (act.first == nop || act.first == (string{"-"} + nop))) {
return act.second.get();
}
} else if (act.second->isPartialMatchAllowed()) {
if (!std::strncmp(optp, act.first.c_str(), act.first.length())) {
return act.second.get();
}
if (VString::startsWith(optp, act.first)) return act.second.get();
}
}
return nullptr;
@ -148,7 +146,7 @@ V3OptionParser::ActionIfs& V3OptionParser::add(const std::string& opt, ARG arg)
bool V3OptionParser::hasPrefixNo(const char* strp) {
UASSERT(strp[0] == '-', strp << " does not start with '-'");
if (strp[1] == '-') ++strp;
return std::strncmp(strp, "-no", 3) == 0;
return VString::startsWith(strp, "-no");
}
int V3OptionParser::parse(int idx, int argc, char* argv[]) {

View File

@ -395,7 +395,7 @@ string V3PreProcImp::commentCleanup(const string& text) {
bool V3PreProcImp::commentTokenMatch(string& cmdr, const char* strg) {
int len = strlen(strg);
if (0 == strncmp(cmdr.c_str(), strg, len) && (cmdr[len] == '\0' || isspace(cmdr[len]))) {
if (VString::startsWith(cmdr, strg) && (cmdr[len] == '\0' || isspace(cmdr[len]))) {
if (isspace(cmdr[len])) len++;
cmdr = cmdr.substr(len);
return true;
@ -423,27 +423,27 @@ void V3PreProcImp::comment(const string& text) {
bool synth = false;
bool vlcomment = false;
if ((cp[0] == 'v' || cp[0] == 'V') && 0 == (strncmp(cp + 1, "erilator", 8))) {
if ((cp[0] == 'v' || cp[0] == 'V') && VString::startsWith(cp + 1, "erilator")) {
cp += strlen("verilator");
if (*cp == '_') {
fileline()->v3error("Extra underscore in meta-comment;"
" use /*verilator {...}*/ not /*verilator_{...}*/");
}
vlcomment = true;
} else if (0 == (strncmp(cp, "synopsys", strlen("synopsys")))) {
} else if (VString::startsWith(cp, "synopsys")) {
cp += strlen("synopsys");
synth = true;
if (*cp == '_') {
fileline()->v3error("Extra underscore in meta-comment;"
" use /*synopsys {...}*/ not /*synopsys_{...}*/");
}
} else if (0 == (strncmp(cp, "cadence", strlen("cadence")))) {
} else if (VString::startsWith(cp, "cadence")) {
cp += strlen("cadence");
synth = true;
} else if (0 == (strncmp(cp, "pragma", strlen("pragma")))) {
} else if (VString::startsWith(cp, "pragma")) {
cp += strlen("pragma");
synth = true;
} else if (0 == (strncmp(cp, "ambit synthesis", strlen("ambit synthesis")))) {
} else if (VString::startsWith(cp, "ambit synthesis")) {
cp += strlen("ambit synthesis");
synth = true;
} else {
@ -1544,7 +1544,7 @@ int V3PreProcImp::getFinalToken(string& buf) {
// Track `line
const char* bufp = buf.c_str();
while (*bufp == '\n') bufp++;
if ((tok == VP_TEXT || tok == VP_LINE) && 0 == strncmp(bufp, "`line ", 6)) {
if ((tok == VP_TEXT || tok == VP_LINE) && VString::startsWith(bufp, "`line ")) {
int enter;
m_finFilelinep->lineDirective(bufp, enter /*ref*/);
} else {

View File

@ -174,6 +174,10 @@ string VString::replaceWord(const string& str, const string& from, const string&
return result;
}
bool VString::startsWith(const string& str, const string& prefix) {
return str.rfind(prefix, 0) == 0; // Faster than .find(_) == 0
}
//######################################################################
// VHashSha256

View File

@ -113,6 +113,8 @@ public:
// to be a consecutive sequence of the characters [a-zA-Z0-9_]. Sub-words are not replaced.
// e.g.: replaceWords("one apple bad_apple", "apple", "banana") -> "one banana bad_apple"
static string replaceWord(const string& str, const string& from, const string& to);
// Predicate to check if 'str' starts with 'prefix'
static bool startsWith(const string& str, const string& prefix);
};
//######################################################################