1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "android-base/strings.h"
18 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <string>
23 #include <vector>
24 
25 namespace android {
26 namespace base {
27 
28 #define CHECK_NE(a, b) \
29   if ((a) == (b)) abort();
30 
Split(const std::string & s,const std::string & delimiters)31 std::vector<std::string> Split(const std::string& s,
32                                const std::string& delimiters) {
33   CHECK_NE(delimiters.size(), 0U);
34 
35   std::vector<std::string> result;
36 
37   size_t base = 0;
38   size_t found;
39   while (true) {
40     found = s.find_first_of(delimiters, base);
41     result.push_back(s.substr(base, found - base));
42     if (found == s.npos) break;
43     base = found + 1;
44   }
45 
46   return result;
47 }
48 
Trim(const std::string & s)49 std::string Trim(const std::string& s) {
50   std::string result;
51 
52   if (s.size() == 0) {
53     return result;
54   }
55 
56   size_t start_index = 0;
57   size_t end_index = s.size() - 1;
58 
59   // Skip initial whitespace.
60   while (start_index < s.size()) {
61     if (!isspace(s[start_index])) {
62       break;
63     }
64     start_index++;
65   }
66 
67   // Skip terminating whitespace.
68   while (end_index >= start_index) {
69     if (!isspace(s[end_index])) {
70       break;
71     }
72     end_index--;
73   }
74 
75   // All spaces, no beef.
76   if (end_index < start_index) {
77     return "";
78   }
79   // Start_index is the first non-space, end_index is the last one.
80   return s.substr(start_index, end_index - start_index + 1);
81 }
82 
83 // These cases are probably the norm, so we mark them extern in the header to
84 // aid compile time and binary size.
85 template std::string Join(const std::vector<std::string>&, char);
86 template std::string Join(const std::vector<const char*>&, char);
87 template std::string Join(const std::vector<std::string>&, const std::string&);
88 template std::string Join(const std::vector<const char*>&, const std::string&);
89 
StartsWith(const std::string & s,const char * prefix)90 bool StartsWith(const std::string& s, const char* prefix) {
91   return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
92 }
93 
StartsWithIgnoreCase(const std::string & s,const char * prefix)94 bool StartsWithIgnoreCase(const std::string& s, const char* prefix) {
95   return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0;
96 }
97 
EndsWith(const std::string & s,const char * suffix,bool case_sensitive)98 static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) {
99   size_t suffix_length = strlen(suffix);
100   size_t string_length = s.size();
101   if (suffix_length > string_length) {
102     return false;
103   }
104   size_t offset = string_length - suffix_length;
105   return (case_sensitive ? strncmp : strncasecmp)(s.c_str() + offset, suffix, suffix_length) == 0;
106 }
107 
EndsWith(const std::string & s,const char * suffix)108 bool EndsWith(const std::string& s, const char* suffix) {
109   return EndsWith(s, suffix, true);
110 }
111 
EndsWithIgnoreCase(const std::string & s,const char * suffix)112 bool EndsWithIgnoreCase(const std::string& s, const char* suffix) {
113   return EndsWith(s, suffix, false);
114 }
115 
EqualsIgnoreCase(const std::string & lhs,const std::string & rhs)116 bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) {
117   return strcasecmp(lhs.c_str(), rhs.c_str()) == 0;
118 }
119 
120 }  // namespace base
121 }  // namespace android
122