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