1 // Copyright 2015 The Shaderc Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "libshaderc_util/version_profile.h"
16 
17 #include <cctype>
18 #include <sstream>
19 
20 namespace {
21 
22 const int kVersionNumberLength = 3;
23 const int kMaxProfileLength = 13;  // strlen(compatibility)
24 const int kMaxVersionProfileLength = kVersionNumberLength + kMaxProfileLength;
25 const int kMinVersionProfileLength = kVersionNumberLength;
26 
27 }  // anonymous namespace
28 
29 namespace shaderc_util {
30 
ParseVersionProfile(const std::string & version_profile,int * version,EProfile * profile)31 bool ParseVersionProfile(const std::string& version_profile, int* version,
32                          EProfile* profile) {
33   if (version_profile.size() < kMinVersionProfileLength ||
34       version_profile.size() > kMaxVersionProfileLength ||
35       !::isdigit(version_profile.front()))
36     return false;
37 
38   std::string profile_string;
39   std::istringstream(version_profile) >> *version >> profile_string;
40 
41   if (!IsKnownVersion(*version)) {
42     return false;
43   }
44   if (profile_string.empty()) {
45     *profile = ENoProfile;
46   } else if (profile_string == "core") {
47     *profile = ECoreProfile;
48   } else if (profile_string == "es") {
49     *profile = EEsProfile;
50   } else if (profile_string == "compatibility") {
51     *profile = ECompatibilityProfile;
52   } else {
53     return false;
54   }
55 
56   return true;
57 }
58 
59 }  // namespace shaderc_util
60