From d9c893af11601efaee82cb8f18b2dc3d0097b238 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Tue, 20 Jul 2021 14:18:35 +0100 Subject: [PATCH] Internals: Add VString::startsWith predicate function --- src/V3OptionParser.cpp | 8 +++----- src/V3PreProc.cpp | 14 +++++++------- src/V3String.cpp | 4 ++++ src/V3String.h | 2 ++ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/V3OptionParser.cpp b/src/V3OptionParser.cpp index c6eec718c..f1db000af 100644 --- a/src/V3OptionParser.cpp +++ b/src/V3OptionParser.cpp @@ -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[]) { diff --git a/src/V3PreProc.cpp b/src/V3PreProc.cpp index 3bf62d303..02dbeeaa4 100644 --- a/src/V3PreProc.cpp +++ b/src/V3PreProc.cpp @@ -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 { diff --git a/src/V3String.cpp b/src/V3String.cpp index c4f054575..caf5a8283 100644 --- a/src/V3String.cpp +++ b/src/V3String.cpp @@ -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 diff --git a/src/V3String.h b/src/V3String.h index f3ed2f651..e028561f3 100644 --- a/src/V3String.h +++ b/src/V3String.h @@ -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); }; //######################################################################