string_utils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef SOURCE_UTIL_STRING_UTILS_H_
  15. #define SOURCE_UTIL_STRING_UTILS_H_
  16. #include <assert.h>
  17. #include <cstdint>
  18. #include <cstring>
  19. #include <sstream>
  20. #include <string>
  21. #include <vector>
  22. namespace spvtools {
  23. namespace utils {
  24. // Converts arithmetic value |val| to its default string representation.
  25. template <class T>
  26. std::string ToString(T val) {
  27. static_assert(
  28. std::is_arithmetic<T>::value,
  29. "spvtools::utils::ToString is restricted to only arithmetic values");
  30. std::stringstream os;
  31. os << val;
  32. return os.str();
  33. }
  34. // Converts cardinal number to ordinal number string.
  35. std::string CardinalToOrdinal(size_t cardinal);
  36. // Splits the string |flag|, of the form '--pass_name[=pass_args]' into two
  37. // strings "pass_name" and "pass_args". If |flag| has no arguments, the second
  38. // string will be empty.
  39. std::pair<std::string, std::string> SplitFlagArgs(const std::string& flag);
  40. // Encodes a string as a sequence of words, using the SPIR-V encoding, appending
  41. // to an existing vector.
  42. template <class VectorType = std::vector<uint32_t>>
  43. inline void AppendToVector(const std::string& input, VectorType* result) {
  44. static_assert(std::is_same<uint32_t, typename VectorType::value_type>::value);
  45. uint32_t word = 0;
  46. size_t num_bytes = input.size();
  47. // SPIR-V strings are null-terminated. The byte_index == num_bytes
  48. // case is used to push the terminating null byte.
  49. for (size_t byte_index = 0; byte_index <= num_bytes; byte_index++) {
  50. const auto new_byte =
  51. (byte_index < num_bytes ? uint8_t(input[byte_index]) : uint8_t(0));
  52. word |= (new_byte << (8 * (byte_index % sizeof(uint32_t))));
  53. if (3 == (byte_index % sizeof(uint32_t))) {
  54. result->push_back(word);
  55. word = 0;
  56. }
  57. }
  58. // Emit a trailing partial word.
  59. if ((num_bytes + 1) % sizeof(uint32_t)) {
  60. result->push_back(word);
  61. }
  62. }
  63. // Encodes a string as a sequence of words, using the SPIR-V encoding.
  64. template <class VectorType = std::vector<uint32_t>>
  65. inline VectorType MakeVector(const std::string& input) {
  66. static_assert(std::is_same<uint32_t, typename VectorType::value_type>::value);
  67. VectorType result;
  68. AppendToVector(input, &result);
  69. return result;
  70. }
  71. // Decode a string from a sequence of words between first and last, using the
  72. // SPIR-V encoding. Assert that a terminating 0-byte was found (unless
  73. // assert_found_terminating_null is passed as false).
  74. template <class InputIt>
  75. inline std::string MakeString(InputIt first, InputIt last,
  76. bool assert_found_terminating_null = true) {
  77. std::string result;
  78. constexpr size_t kCharsPerWord = sizeof(*first);
  79. static_assert(kCharsPerWord == 4, "expect 4-byte word");
  80. for (InputIt pos = first; pos != last; ++pos) {
  81. uint32_t word = *pos;
  82. for (size_t byte_index = 0; byte_index < kCharsPerWord; byte_index++) {
  83. uint32_t extracted_word = (word >> (8 * byte_index)) & 0xFF;
  84. char c = static_cast<char>(extracted_word);
  85. if (c == 0) {
  86. return result;
  87. }
  88. result += c;
  89. }
  90. }
  91. assert(!assert_found_terminating_null &&
  92. "Did not find terminating null for the string.");
  93. (void)assert_found_terminating_null; /* No unused parameters in release
  94. builds. */
  95. return result;
  96. }
  97. // Decode a string from a sequence of words in a vector, using the SPIR-V
  98. // encoding.
  99. template <class VectorType>
  100. inline std::string MakeString(const VectorType& words,
  101. bool assert_found_terminating_null = true) {
  102. return MakeString(words.cbegin(), words.cend(),
  103. assert_found_terminating_null);
  104. }
  105. // Decode a string from array words, consuming up to count words, using the
  106. // SPIR-V encoding.
  107. inline std::string MakeString(const uint32_t* words, size_t num_words,
  108. bool assert_found_terminating_null = true) {
  109. return MakeString(words, words + num_words, assert_found_terminating_null);
  110. }
  111. // Check if str starts with prefix (only included since C++20)
  112. inline bool starts_with(const std::string& str, const char* prefix) {
  113. return 0 == str.compare(0, std::strlen(prefix), prefix);
  114. }
  115. } // namespace utils
  116. } // namespace spvtools
  117. #endif // SOURCE_UTIL_STRING_UTILS_H_