| 1 | // |
| 2 | // Copyright 2015 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // string_utils: |
| 7 | // String helper functions. |
| 8 | // |
| 9 | |
| 10 | #ifndef LIBANGLE_STRING_UTILS_H_ |
| 11 | #define LIBANGLE_STRING_UTILS_H_ |
| 12 | |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "common/Optional.h" |
| 17 | |
| 18 | namespace angle |
| 19 | { |
| 20 | |
| 21 | extern const char kWhitespaceASCII[]; |
| 22 | |
| 23 | enum WhitespaceHandling |
| 24 | { |
| 25 | KEEP_WHITESPACE, |
| 26 | TRIM_WHITESPACE, |
| 27 | }; |
| 28 | |
| 29 | enum SplitResult |
| 30 | { |
| 31 | SPLIT_WANT_ALL, |
| 32 | SPLIT_WANT_NONEMPTY, |
| 33 | }; |
| 34 | |
| 35 | std::vector<std::string> SplitString(const std::string &input, |
| 36 | const std::string &delimiters, |
| 37 | WhitespaceHandling whitespace, |
| 38 | SplitResult resultType); |
| 39 | |
| 40 | void SplitStringAlongWhitespace(const std::string &input, std::vector<std::string> *tokensOut); |
| 41 | |
| 42 | std::string TrimString(const std::string &input, const std::string &trimChars); |
| 43 | |
| 44 | bool HexStringToUInt(const std::string &input, unsigned int *uintOut); |
| 45 | |
| 46 | bool ReadFileToString(const std::string &path, std::string *stringOut); |
| 47 | |
| 48 | Optional<std::vector<wchar_t>> WidenString(size_t length, const char *cString); |
| 49 | |
| 50 | // Check if the string str begins with the given prefix. |
| 51 | // The comparison is case sensitive. |
| 52 | bool BeginsWith(const std::string &str, const std::string &prefix); |
| 53 | |
| 54 | // Check if the string str begins with the given prefix. |
| 55 | // Prefix may not be NULL and needs to be NULL terminated. |
| 56 | // The comparison is case sensitive. |
| 57 | bool BeginsWith(const std::string &str, const char *prefix); |
| 58 | |
| 59 | // Check if the string str begins with the given prefix. |
| 60 | // str and prefix may not be NULL and need to be NULL terminated. |
| 61 | // The comparison is case sensitive. |
| 62 | bool BeginsWith(const char *str, const char *prefix); |
| 63 | |
| 64 | // Check if the string str begins with the first prefixLength characters of the given prefix. |
| 65 | // The length of the prefix string should be greater than or equal to prefixLength. |
| 66 | // The comparison is case sensitive. |
| 67 | bool BeginsWith(const std::string &str, const std::string &prefix, const size_t prefixLength); |
| 68 | |
| 69 | // Check if the string str ends with the given suffix. |
| 70 | // Suffix may not be NUL and needs to be NULL terminated. |
| 71 | // The comparison is case sensitive. |
| 72 | bool EndsWith(const std::string &str, const char *suffix); |
| 73 | |
| 74 | // Convert to lower-case. |
| 75 | void ToLower(std::string *str); |
| 76 | |
| 77 | // Replaces the substring 'substring' in 'str' with 'replacement'. Returns true if successful. |
| 78 | bool ReplaceSubstring(std::string *str, |
| 79 | const std::string &substring, |
| 80 | const std::string &replacement); |
| 81 | |
| 82 | } // namespace angle |
| 83 | |
| 84 | #endif // LIBANGLE_STRING_UTILS_H_ |
| 85 | |