diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2012-08-15 20:02:54 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2012-08-15 20:02:54 +0000 | 
| commit | 56d91b49b13fe55c918afbda19f6165b5fbff87a (patch) | |
| tree | 9abb1a658a297776086f4e0dfa6ca533de02104e /lib/Basic/VersionTuple.cpp | |
| parent | 41e20f564abdb05101d6b2b29c59459a966c22cc (diff) | |
Notes
Diffstat (limited to 'lib/Basic/VersionTuple.cpp')
| -rw-r--r-- | lib/Basic/VersionTuple.cpp | 52 | 
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/Basic/VersionTuple.cpp b/lib/Basic/VersionTuple.cpp index 77aad39cbf2c..4f479d00d6cc 100644 --- a/lib/Basic/VersionTuple.cpp +++ b/lib/Basic/VersionTuple.cpp @@ -34,3 +34,55 @@ raw_ostream& clang::operator<<(raw_ostream &Out,      Out << '.' << *Subminor;    return Out;  } + +static bool parseInt(StringRef &input, unsigned &value) { +  assert(value == 0); +  if (input.empty()) return true; + +  char next = input[0]; +  input = input.substr(1); +  if (next < '0' || next > '9') return true; +  value = (unsigned) (next - '0'); + +  while (!input.empty()) { +    next = input[0]; +    if (next < '0' || next > '9') return false; +    input = input.substr(1); +    value = value * 10 + (unsigned) (next - '0'); +  } + +  return false; +} + +bool VersionTuple::tryParse(StringRef input) { +  unsigned major = 0, minor = 0, micro = 0; + +  // Parse the major version, [0-9]+ +  if (parseInt(input, major)) return true; + +  if (input.empty()) { +    *this = VersionTuple(major); +    return false; +  } + +  // If we're not done, parse the minor version, \.[0-9]+ +  if (input[0] != '.') return true; +  input = input.substr(1); +  if (parseInt(input, minor)) return true; + +  if (input.empty()) { +    *this = VersionTuple(major, minor); +    return false; +  } + +  // If we're not done, parse the micro version, \.[0-9]+ +  if (input[0] != '.') return true; +  input = input.substr(1); +  if (parseInt(input, micro)) return true; + +  // If we have characters left over, it's an error. +  if (!input.empty()) return true; + +  *this = VersionTuple(major, minor, micro); +  return false; +}  | 
