blob: acb0376bf9b0d3a3331ceb4dc0112b11011355d0 [file] [log] [blame]
Jamie Madill8c5aeb62015-05-21 08:17:18 -04001//
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
15namespace angle
16{
17
Corentin Wallez47ac69c2015-11-24 11:15:57 -050018const char kWhitespaceASCII[] = " \f\n\r\t\v";
Jamie Madill8c5aeb62015-05-21 08:17:18 -040019
Corentin Wallez47ac69c2015-11-24 11:15:57 -050020std::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 Madill8c5aeb62015-05-21 08:17:18 -040027 {
Corentin Wallez47ac69c2015-11-24 11:15:57 -050028 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 Madill8c5aeb62015-05-21 08:17:18 -040038 {
Corentin Wallez47ac69c2015-11-24 11:15:57 -050039 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 Madill8c5aeb62015-05-21 08:17:18 -040056 }
57 }
Corentin Wallez47ac69c2015-11-24 11:15:57 -050058
59 return result;
Jamie Madill8c5aeb62015-05-21 08:17:18 -040060}
61
62void SplitStringAlongWhitespace(const std::string &input,
63 std::vector<std::string> *tokensOut)
64{
Jamie Madill8c5aeb62015-05-21 08:17:18 -040065
66 std::istringstream stream(input);
67 std::string line;
68
69 while (std::getline(stream, line))
70 {
71 size_t prev = 0, pos;
Corentin Wallez47ac69c2015-11-24 11:15:57 -050072 while ((pos = line.find_first_of(kWhitespaceASCII, prev)) != std::string::npos)
Jamie Madill8c5aeb62015-05-21 08:17:18 -040073 {
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 Wallez47ac69c2015-11-24 11:15:57 -050083std::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 Madill8c5aeb62015-05-21 08:17:18 -0400100bool HexStringToUInt(const std::string &input, unsigned int *uintOut)
101{
Jamie Madillc7be82a2015-06-01 14:14:04 -0400102 unsigned int offset = 0;
103
104 if (input.size() >= 2 && input[0] == '0' && input[1] == 'x')
105 {
106 offset = 2u;
107 }
108
Jamie Madill8c5aeb62015-05-21 08:17:18 -0400109 // Simple validity check
Jamie Madillc7be82a2015-06-01 14:14:04 -0400110 if (input.find_first_not_of("0123456789ABCDEFabcdef", offset) != std::string::npos)
Jamie Madill8c5aeb62015-05-21 08:17:18 -0400111 {
112 return false;
113 }
114
115 std::stringstream inStream(input);
116 inStream >> std::hex >> *uintOut;
117 return !inStream.fail();
118}
119
120bool ReadFileToString(const std::string &path, std::string *stringOut)
121{
122 std::ifstream inFile(path.c_str());
Jamie Madillc7be82a2015-06-01 14:14:04 -0400123 if (inFile.fail())
124 {
125 return false;
126 }
Jamie Madill8c5aeb62015-05-21 08:17:18 -0400127
128 inFile.seekg(0, std::ios::end);
Jamie Madillc7be82a2015-06-01 14:14:04 -0400129 stringOut->reserve(static_cast<std::string::size_type>(inFile.tellg()));
Jamie Madill8c5aeb62015-05-21 08:17:18 -0400130 inFile.seekg(0, std::ios::beg);
131
Jamie Madillc7be82a2015-06-01 14:14:04 -0400132 stringOut->assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>());
Jamie Madill8c5aeb62015-05-21 08:17:18 -0400133 return !inFile.fail();
134}
135
136}