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 #ifndef ART_OTAPREOPT_STRING_HELPERS_H_
18 #define ART_OTAPREOPT_STRING_HELPERS_H_
19 
20 #include <sstream>
21 #include <string>
22 
23 #include <android-base/macros.h>
24 
25 namespace android {
26 namespace installd {
27 
StringStartsWith(const std::string & target,const char * prefix)28 static inline bool StringStartsWith(const std::string& target,
29                                     const char* prefix) {
30     return target.compare(0, strlen(prefix), prefix) == 0;
31 }
32 
33 // Split the input according to the separator character. Doesn't honor quotation.
Split(const std::string & in,const char separator)34 static inline std::vector<std::string> Split(const std::string& in, const char separator) {
35     if (in.empty()) {
36         return std::vector<std::string>();
37     }
38 
39     std::vector<std::string> ret;
40     std::stringstream strstr(in);
41     std::string token;
42 
43     while (std::getline(strstr, token, separator)) {
44         ret.push_back(token);
45     }
46 
47     return ret;
48 }
49 
50 template <typename StringT>
Join(const std::vector<StringT> & strings,char separator)51 static inline std::string Join(const std::vector<StringT>& strings, char separator) {
52     if (strings.empty()) {
53         return "";
54     }
55 
56     std::string result(strings[0]);
57     for (size_t i = 1; i < strings.size(); ++i) {
58         result += separator;
59         result += strings[i];
60     }
61     return result;
62 }
63 
64 }  // namespace installd
65 }  // namespace android
66 
67 #endif  // ART_OTAPREOPT_STRING_HELPERS_H_
68