diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-03-02 00:34:15 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-03-02 00:34:15 +0100 |
commit | 596fdab71b8bd116e20e33647d68f1d7a567696e (patch) | |
tree | cba0b3a79d9608265d9171942324935931d75421 /src/core/common/Utils.cpp | |
parent | 8197dc488926e8645efb47e60d0988a6a65fc15f (diff) |
Wrote isUserDefinedToken function which checks whether a token is a valid user defined token and added unit tests
Diffstat (limited to 'src/core/common/Utils.cpp')
-rw-r--r-- | src/core/common/Utils.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/core/common/Utils.cpp b/src/core/common/Utils.cpp index 85d2c28..219b437 100644 --- a/src/core/common/Utils.cpp +++ b/src/core/common/Utils.cpp @@ -118,5 +118,29 @@ bool Utils::endsWith(const std::string &s, const std::string &suffix) return suffix.size() <= s.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix; } + +bool Utils::isUserDefinedToken(const std::string &token) +{ + // Make sure the token meets is neither empty, nor starts or ends with an + // alphanumeric character + const size_t len = token.size(); + if (len == 0 || isAlphanumeric(token[0]) || isAlphanumeric(token[len - 1])) { + return false; + } + + // Make sure the token is not any special OSML token + if (token == "\\" || token == "%" || token == "%{" || token == "}%" || + token == "{!" || token == "<\\" || token == "\\>") { + return false; + } + + // Make sure the token contains other characters but { and } + for (char c: token) { + if (c != '{' && c != '}') { + return true; + } + } + return false; +} } |