1 // Copyright (c) 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef SOURCE_UTIL_STRING_UTILS_H_
16 #define SOURCE_UTIL_STRING_UTILS_H_
17
18 #include <assert.h>
19
20 #include <cstdint>
21 #include <cstring>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25
26 namespace spvtools {
27 namespace utils {
28
29 // Converts arithmetic value |val| to its default string representation.
30 template <class T>
ToString(T val)31 std::string ToString(T val) {
32 static_assert(
33 std::is_arithmetic<T>::value,
34 "spvtools::utils::ToString is restricted to only arithmetic values");
35 std::stringstream os;
36 os << val;
37 return os.str();
38 }
39
40 // Converts cardinal number to ordinal number string.
41 std::string CardinalToOrdinal(size_t cardinal);
42
43 // Splits the string |flag|, of the form '--pass_name[=pass_args]' into two
44 // strings "pass_name" and "pass_args". If |flag| has no arguments, the second
45 // string will be empty.
46 std::pair<std::string, std::string> SplitFlagArgs(const std::string& flag);
47
48 // Encodes a string as a sequence of words, using the SPIR-V encoding, appending
49 // to an existing vector.
50 template <class VectorType = std::vector<uint32_t>>
AppendToVector(const std::string & input,VectorType * result)51 inline void AppendToVector(const std::string& input, VectorType* result) {
52 static_assert(std::is_same<uint32_t, typename VectorType::value_type>::value);
53 uint32_t word = 0;
54 size_t num_bytes = input.size();
55 // SPIR-V strings are null-terminated. The byte_index == num_bytes
56 // case is used to push the terminating null byte.
57 for (size_t byte_index = 0; byte_index <= num_bytes; byte_index++) {
58 const auto new_byte =
59 (byte_index < num_bytes ? uint8_t(input[byte_index]) : uint8_t(0));
60 word |= (new_byte << (8 * (byte_index % sizeof(uint32_t))));
61 if (3 == (byte_index % sizeof(uint32_t))) {
62 result->push_back(word);
63 word = 0;
64 }
65 }
66 // Emit a trailing partial word.
67 if ((num_bytes + 1) % sizeof(uint32_t)) {
68 result->push_back(word);
69 }
70 }
71
72 // Encodes a string as a sequence of words, using the SPIR-V encoding.
73 template <class VectorType = std::vector<uint32_t>>
MakeVector(const std::string & input)74 inline VectorType MakeVector(const std::string& input) {
75 static_assert(std::is_same<uint32_t, typename VectorType::value_type>::value);
76 VectorType result;
77 AppendToVector(input, &result);
78 return result;
79 }
80
81 // Decode a string from a sequence of words between first and last, using the
82 // SPIR-V encoding. Assert that a terminating 0-byte was found (unless
83 // assert_found_terminating_null is passed as false).
84 template <class InputIt>
85 inline std::string MakeString(InputIt first, InputIt last,
86 bool assert_found_terminating_null = true) {
87 std::string result;
88 constexpr size_t kCharsPerWord = sizeof(*first);
89 static_assert(kCharsPerWord == 4, "expect 4-byte word");
90
91 for (InputIt pos = first; pos != last; ++pos) {
92 uint32_t word = *pos;
93 for (size_t byte_index = 0; byte_index < kCharsPerWord; byte_index++) {
94 uint32_t extracted_word = (word >> (8 * byte_index)) & 0xFF;
95 char c = static_cast<char>(extracted_word);
96 if (c == 0) {
97 return result;
98 }
99 result += c;
100 }
101 }
102 assert(!assert_found_terminating_null &&
103 "Did not find terminating null for the string.");
104 (void)assert_found_terminating_null; /* No unused parameters in release
105 builds. */
106 return result;
107 }
108
109 // Decode a string from a sequence of words in a vector, using the SPIR-V
110 // encoding.
111 template <class VectorType>
112 inline std::string MakeString(const VectorType& words,
113 bool assert_found_terminating_null = true) {
114 return MakeString(words.cbegin(), words.cend(),
115 assert_found_terminating_null);
116 }
117
118 // Decode a string from array words, consuming up to count words, using the
119 // SPIR-V encoding.
120 inline std::string MakeString(const uint32_t* words, size_t num_words,
121 bool assert_found_terminating_null = true) {
122 return MakeString(words, words + num_words, assert_found_terminating_null);
123 }
124
125 // Check if str starts with prefix (only included since C++20)
starts_with(const std::string & str,const char * prefix)126 inline bool starts_with(const std::string& str, const char* prefix) {
127 return 0 == str.compare(0, std::strlen(prefix), prefix);
128 }
129
130 } // namespace utils
131 } // namespace spvtools
132
133 #endif // SOURCE_UTIL_STRING_UTILS_H_
134