diff --git a/src/V3String.cpp b/src/V3String.cpp index 6a0e54215..4ce8482e7 100644 --- a/src/V3String.cpp +++ b/src/V3String.cpp @@ -310,10 +310,11 @@ double VString::parseDouble(const string& str, bool* successp) { string VString::replaceSubstr(const string& str, const string& from, const string& to) { string result = str; - const size_t len = from.size(); - UASSERT_STATIC(len > 0, "Cannot replace empty string"); - for (size_t pos = 0; (pos = result.find(from, pos)) != string::npos; pos += len) { - result.replace(pos, len, to); + const size_t fromLen = from.size(); + const size_t toLen = to.size(); + UASSERT_STATIC(fromLen > 0, "Cannot replace empty string"); + for (size_t pos = 0; (pos = result.find(from, pos)) != string::npos; pos += toLen) { + result.replace(pos, fromLen, to); } return result; } @@ -396,6 +397,10 @@ uint64_t VString::hashMurmur(const string& str) VL_PURE { return h; } +void VString::selfTest() { + UASSERT_SELFTEST(const string&, VString::replaceSubstr("aa", "a", "ba"), "baba"); +} + //###################################################################### // VHashSha256 diff --git a/src/V3String.h b/src/V3String.h index 537dea287..a8d55ea01 100644 --- a/src/V3String.h +++ b/src/V3String.h @@ -143,6 +143,8 @@ public: static string aOrAn(const string& word) { return aOrAn(word.c_str()); } // Hash the string static uint64_t hashMurmur(const string& str) VL_PURE; + + static void selfTest(); // Test this class }; //###################################################################### diff --git a/src/Verilator.cpp b/src/Verilator.cpp index 143b7956c..fbd1648e2 100644 --- a/src/Verilator.cpp +++ b/src/Verilator.cpp @@ -692,6 +692,7 @@ static bool verilate(const string& argString) { if (v3Global.opt.debugSelfTest()) { V3Os::selfTest(); V3Number::selfTest(); + VString::selfTest(); VHashSha256::selfTest(); VSpellCheck::selfTest(); V3Graph::selfTest();