Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 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 |
Steven Moreland | 868aff8 | 2016-09-12 16:20:56 +0000 | [diff] [blame] | 48 | static std::string Capitalize(const std::string &in); |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 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); |
Steven Moreland | 95b4623 | 2016-09-20 14:46:55 -0700 | [diff] [blame] | 56 | |
Steven Moreland | f9a6ef3 | 2016-09-27 14:20:56 -0700 | [diff] [blame] | 57 | static bool EndsWith(const std::string &in, const std::string &suffix); |
Steven Moreland | 95b4623 | 2016-09-20 14:46:55 -0700 | [diff] [blame] | 58 | static bool StartsWith(const std::string &in, const std::string &prefix); |
| 59 | |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 60 | /* removes suffix once from in if in ends with suffix */ |
Steven Moreland | f9a6ef3 | 2016-09-27 14:20:56 -0700 | [diff] [blame] | 61 | static std::string RTrim(const std::string &in, const std::string &suffix); |
| 62 | |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 63 | /* removes prefix once from in if in starts with prefix */ |
Steven Moreland | f9a6ef3 | 2016-09-27 14:20:56 -0700 | [diff] [blame] | 64 | static std::string LTrim(const std::string &in, const std::string &prefix); |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 65 | |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 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 | |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 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: |
| 83 | StringHelper() {} |
| 84 | |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 85 | static void Tokenize(const std::string &in, |
| 86 | std::vector<std::string> *vec); |
| 87 | |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 88 | DISALLOW_COPY_AND_ASSIGN(StringHelper); |
| 89 | }; |
| 90 | |
| 91 | } // namespace android |
| 92 | |
| 93 | #endif // STRING_HELPER_H_ |
| 94 | |