diff --git a/HopsanCore/include/Components/DcpComponent.hpp b/HopsanCore/include/Components/DcpComponent.hpp index f4305badc2..1142b596b9 100644 --- a/HopsanCore/include/Components/DcpComponent.hpp +++ b/HopsanCore/include/Components/DcpComponent.hpp @@ -91,10 +91,10 @@ namespace hopsan { mOutputs.clear(); mParameters.clear(); - HVector splitVariables = mVariables.split(';'); - HVector splitInputs = splitVariables[0].split(','); - HVector splitOutputs = splitVariables[1].split(','); - HVector splitParameters = splitVariables[2].split(','); + HVector splitVariables = mVariables.split(';', HString::SkipEmptyParts); + HVector splitInputs = splitVariables[0].split(',', HString::SkipEmptyParts); + HVector splitOutputs = splitVariables[1].split(',', HString::SkipEmptyParts); + HVector splitParameters = splitVariables[2].split(',', HString::SkipEmptyParts); //The split function will return a single-element vector with an //empty string if delimiter was not found, clear vector in this case @@ -107,7 +107,7 @@ namespace hopsan { if(splitParameters.size() == 1 && splitParameters[0] == "") { splitParameters.clear(); } - HVector splitValueRefs = mValueRefs.split(','); + HVector splitValueRefs = mValueRefs.split(',', HString::SkipEmptyParts); if(splitValueRefs.size() != splitInputs.size()+splitOutputs.size()+splitParameters.size()) { addErrorMessage("Number of value references does not equal number of variables"); diff --git a/HopsanCore/include/HString.h b/HopsanCore/include/HString.h index 0797115a58..8fcdd78d74 100644 --- a/HopsanCore/include/HString.h +++ b/HopsanCore/include/HString.h @@ -39,6 +39,8 @@ class HOPSANCORE_DLLAPI HString size_t mSize; public: + enum SplitBehaviorEnumT { KeepEmptyParts, SkipEmptyParts }; + static const size_t npos; HString(); @@ -74,7 +76,7 @@ class HOPSANCORE_DLLAPI HString bool toBool(bool *isOK) const; HString substr(const size_t pos, const size_t len=npos) const; - HVector split(const char delim) const; + HVector split(const char delim, SplitBehaviorEnumT behaviour = KeepEmptyParts) const; size_t find_first_of(const char c, size_t pos = 0) const; size_t rfind(const char c, size_t pos = npos) const; diff --git a/HopsanCore/src/HString.cpp b/HopsanCore/src/HString.cpp index f33a0b9b18..ec1d556bf0 100644 --- a/HopsanCore/src/HString.cpp +++ b/HopsanCore/src/HString.cpp @@ -523,19 +523,21 @@ HString HString::substr(const size_t pos, const size_t len) const return sub; } -HVector HString::split(const char delim) const -{ - HVector parts; - size_t b = 0; - while (true && mSize > 0) - { - size_t e = find(delim, b); - parts.append(substr(b,e-b)); - if (e == npos) +HVector HString::split(const char delim, SplitBehaviorEnumT behaviour) const +{ + HVector parts; + size_t b = 0; + while (true) { - break; + size_t e = find(delim, b); + if(behaviour == KeepEmptyParts || !substr(b,e-b).empty()) { + parts.append(substr(b,e-b)); + } + if (e == npos) + { + break; + } + b=e+1; } - b=e+1; - } - return parts; + return parts; } diff --git a/hopsandcp/src/dcpmaster.cpp b/hopsandcp/src/dcpmaster.cpp index 0449c979f1..45d96ddbda 100644 --- a/hopsandcp/src/dcpmaster.cpp +++ b/hopsandcp/src/dcpmaster.cpp @@ -331,9 +331,9 @@ void getDataFromProtocolFile(const hopsan::HString &rFilePath, hopsan::HString & rVariables = inputs+";"+outputs+";"+pars; //Value references is a plain comma-separated string - hopsan::HVector valueRefVec = inputVrs.split(','); - valueRefVec.append(outputVrs.split(',')); - valueRefVec.append(parVrs.split(',')); + hopsan::HVector valueRefVec = inputVrs.split(',', hopsan::HString::SkipEmptyParts); + valueRefVec.append(outputVrs.split(',', hopsan::HString::SkipEmptyParts)); + valueRefVec.append(parVrs.split(',', hopsan::HString::SkipEmptyParts)); rValueReferences = join(valueRefVec, ','); //Value references string could end with a comma if one