Skip to content

Commit

Permalink
Add skip empty strings option to HString::split()
Browse files Browse the repository at this point in the history
  • Loading branch information
robbr48 committed Oct 22, 2024
1 parent 8cd1ef6 commit 15f5a55
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
10 changes: 5 additions & 5 deletions HopsanCore/include/Components/DcpComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ namespace hopsan {
mOutputs.clear();
mParameters.clear();

HVector<HString> splitVariables = mVariables.split(';');
HVector<HString> splitInputs = splitVariables[0].split(',');
HVector<HString> splitOutputs = splitVariables[1].split(',');
HVector<HString> splitParameters = splitVariables[2].split(',');
HVector<HString> splitVariables = mVariables.split(';', HString::SkipEmptyParts);
HVector<HString> splitInputs = splitVariables[0].split(',', HString::SkipEmptyParts);
HVector<HString> splitOutputs = splitVariables[1].split(',', HString::SkipEmptyParts);
HVector<HString> 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
Expand All @@ -107,7 +107,7 @@ namespace hopsan {
if(splitParameters.size() == 1 && splitParameters[0] == "") {
splitParameters.clear();
}
HVector<HString> splitValueRefs = mValueRefs.split(',');
HVector<HString> 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");
Expand Down
4 changes: 3 additions & 1 deletion HopsanCore/include/HString.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class HOPSANCORE_DLLAPI HString
size_t mSize;

public:
enum SplitBehaviorEnumT { KeepEmptyParts, SkipEmptyParts };

static const size_t npos;

HString();
Expand Down Expand Up @@ -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<HString> split(const char delim) const;
HVector<HString> 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;
Expand Down
28 changes: 15 additions & 13 deletions HopsanCore/src/HString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,19 +523,21 @@ HString HString::substr(const size_t pos, const size_t len) const
return sub;
}

HVector<HString> HString::split(const char delim) const
{
HVector<HString> 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> HString::split(const char delim, SplitBehaviorEnumT behaviour) const
{
HVector<HString> 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;
}
6 changes: 3 additions & 3 deletions hopsandcp/src/dcpmaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<hopsan::HString> valueRefVec = inputVrs.split(',');
valueRefVec.append(outputVrs.split(','));
valueRefVec.append(parVrs.split(','));
hopsan::HVector<hopsan::HString> 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
Expand Down

0 comments on commit 15f5a55

Please sign in to comment.