1 /*
2  * Copyright (C) 2016 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 STRING_HELPER_H_
18 
19 #define STRING_HELPER_H_
20 
21 #include <string>
22 #include <android-base/macros.h>
23 #include <vector>
24 
25 namespace android {
26 
27 struct StringHelper {
28 
29     enum Case {
30         // return original string
31         kNoCase = 0,
32         // use camelCase
33         kCamelCase = 1,
34         // use PascalCase
35         kPascalCase = 2,
36         // use UP_SNAKE_CASE
37         kUpperSnakeCase = 3,
38         // use low_snake_case
39         kLowerSnakeCase = 4
40     };
41 
42     // methods for a single word, like device
43     // UPPERCASE
44     static std::string Uppercase(const std::string &in);
45     // lowercase
46     static std::string Lowercase(const std::string &in);
47     // Capitalize
48     static std::string Capitalize(const std::string &in);
49 
50     // methods for a multi-word identifier, like framebuffer_device
51     static std::string ToCamelCase(const std::string &in);
52     static std::string ToPascalCase(const std::string &in);
53     static std::string ToUpperSnakeCase(const std::string &in);
54     static std::string ToLowerSnakeCase(const std::string &in);
55     static std::string ToCase(Case c, const std::string &in);
56 
57     static bool EndsWith(const std::string &in, const std::string &suffix);
58     static bool StartsWith(const std::string &in, const std::string &prefix);
59 
60     /* removes suffix once from in if in ends with suffix */
61     static std::string RTrim(const std::string &in, const std::string &suffix);
62 
63     /* removes prefix once from in if in starts with prefix */
64     static std::string LTrim(const std::string &in, const std::string &prefix);
65 
66     /* removes suffix repeatedly from in if in ends with suffix */
67     static std::string RTrimAll(const std::string &in, const std::string &suffix);
68 
69     /* removes prefix repeatedly from in if in starts with prefix */
70     static std::string LTrimAll(const std::string &in, const std::string &prefix);
71 
72 
73     static void SplitString(
74         const std::string &s,
75         char c,
76         std::vector<std::string> *components);
77 
78     static std::string JoinStrings(
79         const std::vector<std::string> &components,
80         const std::string &separator);
81 
82 private:
StringHelperStringHelper83     StringHelper() {}
84 
85     static void Tokenize(const std::string &in,
86         std::vector<std::string> *vec);
87 
88     DISALLOW_COPY_AND_ASSIGN(StringHelper);
89 };
90 
91 }  // namespace android
92 
93 #endif  // STRING_HELPER_H_
94 
95