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 <cstring>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24
25 #include "source/util/string_utils.h"
26
27 namespace spvtools {
28 namespace utils {
29
30 // Converts arithmetic value |val| to its default string representation.
31 template <class T>
ToString(T val)32 std::string ToString(T val) {
33 static_assert(
34 std::is_arithmetic<T>::value,
35 "spvtools::utils::ToString is restricted to only arithmetic values");
36 std::stringstream os;
37 os << val;
38 return os.str();
39 }
40
41 // Converts cardinal number to ordinal number string.
42 std::string CardinalToOrdinal(size_t cardinal);
43
44 // Splits the string |flag|, of the form '--pass_name[=pass_args]' into two
45 // strings "pass_name" and "pass_args". If |flag| has no arguments, the second
46 // string will be empty.
47 std::pair<std::string, std::string> SplitFlagArgs(const std::string& flag);
48
49 // Encodes a string as a sequence of words, using the SPIR-V encoding, appending
50 // to an existing vector.
AppendToVector(const std::string & input,std::vector<uint32_t> * result)51 inline void AppendToVector(const std::string& input,
52 std::vector<uint32_t>* result) {
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.
MakeVector(const std::string & input)73 inline std::vector<uint32_t> MakeVector(const std::string& input) {
74 std::vector<uint32_t> result;
75 AppendToVector(input, &result);
76 return result;
77 }
78
79 // Decode a string from a sequence of words between first and last, using the
80 // SPIR-V encoding. Assert that a terminating 0-byte was found (unless
81 // assert_found_terminating_null is passed as false).
82 template <class InputIt>
83 inline std::string MakeString(InputIt first, InputIt last,
84 bool assert_found_terminating_null = true) {
85 std::string result;
86 constexpr size_t kCharsPerWord = sizeof(*first);
87 static_assert(kCharsPerWord == 4, "expect 4-byte word");
88
89 for (InputIt pos = first; pos != last; ++pos) {
90 uint32_t word = *pos;
91 for (size_t byte_index = 0; byte_index < kCharsPerWord; byte_index++) {
92 uint32_t extracted_word = (word >> (8 * byte_index)) & 0xFF;
93 char c = static_cast<char>(extracted_word);
94 if (c == 0) {
95 return result;
96 }
97 result += c;
98 }
99 }
100 assert(!assert_found_terminating_null &&
101 "Did not find terminating null for the string.");
102 (void)assert_found_terminating_null; /* No unused parameters in release
103 builds. */
104 return result;
105 }
106
107 // Decode a string from a sequence of words in a vector, using the SPIR-V
108 // encoding.
109 template <class VectorType>
110 inline std::string MakeString(const VectorType& words,
111 bool assert_found_terminating_null = true) {
112 return MakeString(words.cbegin(), words.cend(),
113 assert_found_terminating_null);
114 }
115
116 // Decode a string from array words, consuming up to count words, using the
117 // SPIR-V encoding.
118 inline std::string MakeString(const uint32_t* words, size_t num_words,
119 bool assert_found_terminating_null = true) {
120 return MakeString(words, words + num_words, assert_found_terminating_null);
121 }
122
123 // Check if str starts with prefix (only included since C++20)
starts_with(const std::string & str,const char * prefix)124 inline bool starts_with(const std::string& str, const char* prefix) {
125 return 0 == str.compare(0, std::strlen(prefix), prefix);
126 }
127
128 } // namespace utils
129 } // namespace spvtools
130
131 #endif // SOURCE_UTIL_STRING_UTILS_H_
132