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