Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 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 | #include "string_utils.h" |
| 11 | |
| 12 | #include <fstream> |
| 13 | #include <sstream> |
| 14 | |
Jamie Madill | 8b9b792 | 2016-05-19 13:13:37 -0400 | [diff] [blame^] | 15 | #include "common/platform.h" |
| 16 | |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 17 | namespace angle |
| 18 | { |
| 19 | |
Corentin Wallez | 47ac69c | 2015-11-24 11:15:57 -0500 | [diff] [blame] | 20 | const char kWhitespaceASCII[] = " \f\n\r\t\v"; |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 21 | |
Corentin Wallez | 47ac69c | 2015-11-24 11:15:57 -0500 | [diff] [blame] | 22 | std::vector<std::string> SplitString(const std::string &input, |
| 23 | const std::string &delimiters, |
| 24 | WhitespaceHandling whitespace, |
| 25 | SplitResult resultType) |
| 26 | { |
| 27 | std::vector<std::string> result; |
| 28 | if (input.empty()) |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 29 | { |
Corentin Wallez | 47ac69c | 2015-11-24 11:15:57 -0500 | [diff] [blame] | 30 | return result; |
| 31 | } |
| 32 | |
| 33 | std::string::size_type start = 0; |
| 34 | while (start != std::string::npos) |
| 35 | { |
| 36 | auto end = input.find_first_of(delimiters, start); |
| 37 | |
| 38 | std::string piece; |
| 39 | if (end == std::string::npos) |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 40 | { |
Corentin Wallez | 47ac69c | 2015-11-24 11:15:57 -0500 | [diff] [blame] | 41 | piece = input.substr(start); |
| 42 | start = std::string::npos; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | piece = input.substr(start, end - start); |
| 47 | start = end + 1; |
| 48 | } |
| 49 | |
| 50 | if (whitespace == TRIM_WHITESPACE) |
| 51 | { |
| 52 | piece = TrimString(piece, kWhitespaceASCII); |
| 53 | } |
| 54 | |
| 55 | if (resultType == SPLIT_WANT_ALL || !piece.empty()) |
| 56 | { |
| 57 | result.push_back(piece); |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 58 | } |
| 59 | } |
Corentin Wallez | 47ac69c | 2015-11-24 11:15:57 -0500 | [diff] [blame] | 60 | |
| 61 | return result; |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void SplitStringAlongWhitespace(const std::string &input, |
| 65 | std::vector<std::string> *tokensOut) |
| 66 | { |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 67 | |
| 68 | std::istringstream stream(input); |
| 69 | std::string line; |
| 70 | |
| 71 | while (std::getline(stream, line)) |
| 72 | { |
| 73 | size_t prev = 0, pos; |
Corentin Wallez | 47ac69c | 2015-11-24 11:15:57 -0500 | [diff] [blame] | 74 | while ((pos = line.find_first_of(kWhitespaceASCII, prev)) != std::string::npos) |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 75 | { |
| 76 | if (pos > prev) |
| 77 | tokensOut->push_back(line.substr(prev, pos - prev)); |
| 78 | prev = pos + 1; |
| 79 | } |
| 80 | if (prev < line.length()) |
| 81 | tokensOut->push_back(line.substr(prev, std::string::npos)); |
| 82 | } |
| 83 | } |
| 84 | |
Corentin Wallez | 47ac69c | 2015-11-24 11:15:57 -0500 | [diff] [blame] | 85 | std::string TrimString(const std::string &input, const std::string &trimChars) |
| 86 | { |
| 87 | auto begin = input.find_first_not_of(trimChars); |
| 88 | if (begin == std::string::npos) |
| 89 | { |
| 90 | return ""; |
| 91 | } |
| 92 | |
| 93 | std::string::size_type end = input.find_last_not_of(trimChars); |
| 94 | if (end == std::string::npos) |
| 95 | { |
| 96 | return input.substr(begin); |
| 97 | } |
| 98 | |
| 99 | return input.substr(begin, end - begin + 1); |
| 100 | } |
| 101 | |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 102 | bool HexStringToUInt(const std::string &input, unsigned int *uintOut) |
| 103 | { |
Jamie Madill | c7be82a | 2015-06-01 14:14:04 -0400 | [diff] [blame] | 104 | unsigned int offset = 0; |
| 105 | |
| 106 | if (input.size() >= 2 && input[0] == '0' && input[1] == 'x') |
| 107 | { |
| 108 | offset = 2u; |
| 109 | } |
| 110 | |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 111 | // Simple validity check |
Jamie Madill | c7be82a | 2015-06-01 14:14:04 -0400 | [diff] [blame] | 112 | if (input.find_first_not_of("0123456789ABCDEFabcdef", offset) != std::string::npos) |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 113 | { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | std::stringstream inStream(input); |
| 118 | inStream >> std::hex >> *uintOut; |
| 119 | return !inStream.fail(); |
| 120 | } |
| 121 | |
| 122 | bool ReadFileToString(const std::string &path, std::string *stringOut) |
| 123 | { |
| 124 | std::ifstream inFile(path.c_str()); |
Jamie Madill | c7be82a | 2015-06-01 14:14:04 -0400 | [diff] [blame] | 125 | if (inFile.fail()) |
| 126 | { |
| 127 | return false; |
| 128 | } |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 129 | |
| 130 | inFile.seekg(0, std::ios::end); |
Jamie Madill | c7be82a | 2015-06-01 14:14:04 -0400 | [diff] [blame] | 131 | stringOut->reserve(static_cast<std::string::size_type>(inFile.tellg())); |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 132 | inFile.seekg(0, std::ios::beg); |
| 133 | |
Jamie Madill | c7be82a | 2015-06-01 14:14:04 -0400 | [diff] [blame] | 134 | stringOut->assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>()); |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 135 | return !inFile.fail(); |
| 136 | } |
| 137 | |
Jamie Madill | 8b9b792 | 2016-05-19 13:13:37 -0400 | [diff] [blame^] | 138 | Optional<std::vector<wchar_t>> WidenString(size_t length, const char *cString) |
| 139 | { |
| 140 | std::vector<wchar_t> wcstring(length + 1); |
| 141 | #if !defined(ANGLE_PLATFORM_WINDOWS) |
| 142 | size_t written = mbstowcs(wcstring.data(), cString, length + 1); |
| 143 | if (written == 0) |
| 144 | { |
| 145 | return Optional<std::vector<wchar_t>>::Invalid(); |
| 146 | } |
| 147 | #else |
| 148 | size_t convertedChars = 0; |
| 149 | errno_t err = mbstowcs_s(&convertedChars, wcstring.data(), length + 1, cString, _TRUNCATE); |
| 150 | if (err != 0) |
| 151 | { |
| 152 | return Optional<std::vector<wchar_t>>::Invalid(); |
| 153 | } |
| 154 | #endif |
| 155 | return Optional<std::vector<wchar_t>>(wcstring); |
Jamie Madill | 8c5aeb6 | 2015-05-21 08:17:18 -0400 | [diff] [blame] | 156 | } |
Jamie Madill | 8b9b792 | 2016-05-19 13:13:37 -0400 | [diff] [blame^] | 157 | |
| 158 | } // namespace angle |