blob: 277367b6944b20105e0ec4302a2fea6a306899d9 [file] [log] [blame]
Steven Morelandaf440142016-09-07 10:09:11 -07001/*
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
25namespace android {
26
27struct StringHelper {
28
Yifan Hong42009032016-09-29 09:38:15 -070029 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
Steven Moreland868aff82016-09-12 16:20:56 +000048 static std::string Capitalize(const std::string &in);
Yifan Hong42009032016-09-29 09:38:15 -070049
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);
Steven Moreland95b46232016-09-20 14:46:55 -070056
Steven Morelandf9a6ef32016-09-27 14:20:56 -070057 static bool EndsWith(const std::string &in, const std::string &suffix);
Steven Moreland95b46232016-09-20 14:46:55 -070058 static bool StartsWith(const std::string &in, const std::string &prefix);
59
Yifan Hong42009032016-09-29 09:38:15 -070060 /* removes suffix once from in if in ends with suffix */
Steven Morelandf9a6ef32016-09-27 14:20:56 -070061 static std::string RTrim(const std::string &in, const std::string &suffix);
62
Yifan Hong42009032016-09-29 09:38:15 -070063 /* removes prefix once from in if in starts with prefix */
Steven Morelandf9a6ef32016-09-27 14:20:56 -070064 static std::string LTrim(const std::string &in, const std::string &prefix);
Steven Morelandaf440142016-09-07 10:09:11 -070065
Yifan Hong42009032016-09-29 09:38:15 -070066 /* 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
Steven Morelandaf440142016-09-07 10:09:11 -070073 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
82private:
83 StringHelper() {}
84
Yifan Hong42009032016-09-29 09:38:15 -070085 static void Tokenize(const std::string &in,
86 std::vector<std::string> *vec);
87
Steven Morelandaf440142016-09-07 10:09:11 -070088 DISALLOW_COPY_AND_ASSIGN(StringHelper);
89};
90
91} // namespace android
92
93#endif // STRING_HELPER_H_
94