1 // Copyright (C) 2019 The Android Open Source Project
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 "utils/string_utils.h"
16 
17 #include <llvm/ADT/Optional.h>
18 
19 #include <algorithm>
20 #include <cctype>
21 #include <cstdlib>
22 #include <string>
23 #include <utility>
24 
25 
26 namespace header_checker {
27 namespace utils {
28 
29 
Trim(std::string_view s)30 std::string_view Trim(std::string_view s) {
31   std::string::size_type start = s.find_first_not_of(" \t\r\n");
32   if (start == std::string::npos) {
33     return "";
34   }
35   std::string::size_type end = s.find_last_not_of(" \t\r\n");
36   if (end == std::string::npos) {
37     return s.substr(start);
38   }
39   return s.substr(start, end - start + 1);
40 }
41 
42 
StartsWith(std::string_view s,std::string_view prefix)43 bool StartsWith(std::string_view s, std::string_view prefix) {
44   return s.size() >= prefix.size() && s.compare(0, prefix.size(), prefix) == 0;
45 }
46 
47 
EndsWith(std::string_view s,std::string_view suffix)48 bool EndsWith(std::string_view s, std::string_view suffix) {
49   return (s.size() >= suffix.size() &&
50           s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0);
51 }
52 
53 
Split(std::string_view s,std::string_view delim_chars)54 std::vector<std::string_view> Split(std::string_view s,
55                                     std::string_view delim_chars) {
56   std::vector<std::string_view> res;
57   std::string::size_type pos = 0;
58   while (true) {
59     pos = s.find_first_not_of(delim_chars, pos);
60     if (pos == std::string::npos) {
61       break;
62     }
63 
64     std::string::size_type end = s.find_first_of(delim_chars, pos + 1);
65     if (end == std::string::npos) {
66       res.push_back(s.substr(pos));
67       break;
68     }
69 
70     res.push_back(s.substr(pos, end - pos));
71     pos = end + 1;
72   }
73   return res;
74 }
75 
76 
ParseInt(const std::string & s)77 llvm::Optional<int> ParseInt(const std::string &s) {
78   const char *start = s.c_str();
79   if (*start == '\0') {
80     return {};
81   }
82 
83   char *endptr = nullptr;
84   long int res = ::strtol(start, &endptr, 10);
85   if (*endptr != '\0') {
86     return {};
87   }
88 
89   return static_cast<int>(res);
90 }
91 
92 
ParseBool(const std::string & s)93 bool ParseBool(const std::string &s) {
94   std::string value(s);
95   std::transform(value.begin(), value.end(), value.begin(), std::tolower);
96   return (value == "true" || value == "on" || value == "1");
97 }
98 
99 
IsGlobPattern(std::string_view s)100 bool IsGlobPattern(std::string_view s) {
101   return s.find_first_of("*?[") != std::string::npos;
102 }
103 
104 
105 }  // namespace utils
106 }  // namespace header_checker
107