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