Skip to content

Commit

Permalink
Fixed whitespace parsing issue with metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
InsertCreativityHere committed Oct 27, 2020
1 parent 879a16c commit dcb6790
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
71 changes: 70 additions & 1 deletion cpp/src/Slice/SliceUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,75 @@ Slice::splitScopedName(const string& scoped)
return ids;
}

string Slice::splitMetadata(const string& s, const string& delimiter, vector<string>& result)
{
size_t startPos = 0;
size_t endPos;
size_t delimPos;
size_t errorPos;

while(startPos != string::npos)
{
// Skip any leading whitespace.
startPos = s.find_first_not_of(" \t", startPos);

// If the string has nothing but whitespace left.
if (startPos == string::npos)
{
result.push_back("");
break;
}

// Check if the string is quoted.
if (s[startPos] == '"')
{
// Skip the opening quotation mark.
endPos = startPos++;

// Find the closing quotation mark, not counting escaped quotation marks.
do
{
endPos = s.find('"', endPos + 1);
// If no closing quotation mark was found.
if (endPos == string::npos)
{
return "unbalanced quotation marks";
}
} while(s[endPos - 1] == '\\');

// Get the position of the next delimiter.
delimPos = s.find(delimiter, endPos + 1);
// Ensure there is only whitespace between the closing quotation mark and the next delimiter.
errorPos = s.find_first_not_of(" \t", endPos + 1);
if (errorPos != delimPos)
{
return "illegal syntax: `" + s.substr(errorPos, (delimPos - errorPos)) +
"', only whitespace can appear between quotation marks and delimiters";
}
}
else
{
// Get the position of the next delimiter.
delimPos = s.find(delimiter, startPos);
// Skip any trailing whitespace.
endPos = s.find_last_not_of(" \t", delimPos - 1) + 1;

// Check for errorneous quotation marks in the string.
errorPos = s.find('"', startPos);
if (errorPos < endPos)
{
return "illegal syntax: `" + s.substr(errorPos, (delimPos - errorPos)) +
"', only whitespace can appear between quotation marks and delimiters";
}
}

result.push_back(s.substr(startPos, (endPos - startPos)));
// If a delimiter was found, skip past it. If a delimiter wasn't found, this sets startPos to npos.
startPos = delimPos + (delimPos == string::npos ? 0 : delimiter.length());
}
return "";
}

bool
Slice::checkIdentifier(const string& id)
{
Expand Down Expand Up @@ -773,7 +842,7 @@ bool opCompress(const OperationPtr& op, bool params)
return false;
}
vector<string> directions;
splitString(compress, ",", directions);
splitMetadata(compress, ",", directions);
return find(directions.begin(), directions.end(), direction) != directions.end();
}

Expand Down
4 changes: 4 additions & 0 deletions cpp/src/Slice/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ writeDependencies(const std::string&, const std::string&);
std::vector<std::string>
splitScopedName(const std::string&);

// TODO
std::string
splitMetadata(const std::string& s, const std::string& delimiter, std::vector<std::string>& result);

// Checks an identifier for illegal syntax and reports any that is present.
bool
checkIdentifier(const std::string&);
Expand Down
2 changes: 1 addition & 1 deletion csharp/test/Ice/compress/Test.ice
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface TestIntf
{
[compress(params)] void opCompressParams(int size, ByteSeq p1);
[compress(return)] ByteSeq opCompressReturn(int size);
[compress(params,return)] ByteSeq opCompressParamsAndReturn(ByteSeq p1);
[compress(params, return)] ByteSeq opCompressParamsAndReturn(ByteSeq p1);

void opWithUserException(int size);

Expand Down

0 comments on commit dcb6790

Please sign in to comment.