Skip to content

Commit

Permalink
common: introduce COOLProtocol::getNonNegTokenInteger()
Browse files Browse the repository at this point in the history
To simplify code that wants to extract a non-negative number from a
token value. See
<CollaboraOnline#3788 (review)>.

Signed-off-by: Miklos Vajna <[email protected]>
Change-Id: I389dfa1a2838e501fc308dbfd7927b142a361922
  • Loading branch information
vmiklos authored and Ashod committed Dec 17, 2021
1 parent 1e92cc5 commit 15e08d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 7 additions & 0 deletions common/Protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ namespace COOLProtocol
return false;
}

/// Extracts a name and value from token. Returns true if value is a non-negative integer.
template <std::size_t N>
inline bool getNonNegTokenInteger(const std::string& token, const char (&name)[N], int& value)
{
return getTokenInteger(token, name, value) && value >= 0;
}

inline bool getTokenString(const StringVector& tokens,
const std::string& name,
std::string& value)
Expand Down
12 changes: 4 additions & 8 deletions wsd/TileCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,10 @@ std::pair<int, Util::Rectangle> TileCache::parseInvalidateMsg(const std::string&
int height = 0;
if (tokens.size() == 6 &&
getTokenInteger(tokens[1], "part", part) &&
getTokenInteger(tokens[2], "x", x) &&
x >= 0 &&
getTokenInteger(tokens[3], "y", y) &&
y >= 0 &&
getTokenInteger(tokens[4], "width", width) &&
width >= 0 &&
getTokenInteger(tokens[5], "height", height) &&
height >= 0)
getNonNegTokenInteger(tokens[2], "x", x) &&
getNonNegTokenInteger(tokens[3], "y", y) &&
getNonNegTokenInteger(tokens[4], "width", width) &&
getNonNegTokenInteger(tokens[5], "height", height))
{
return std::pair<int, Util::Rectangle>(part, Util::Rectangle(x, y, width, height));
}
Expand Down

0 comments on commit 15e08d3

Please sign in to comment.