string_utils.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <cstring>
  18. #include <sstream>
  19. #include <string>
  20. #include <vector>
  21. #include "source/util/string_utils.h"
  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. inline void AppendToVector(const std::string& input,
  43. std::vector<uint32_t>* result) {
  44. uint32_t word = 0;
  45. size_t num_bytes = input.size();
  46. // SPIR-V strings are null-terminated. The byte_index == num_bytes
  47. // case is used to push the terminating null byte.
  48. for (size_t byte_index = 0; byte_index <= num_bytes; byte_index++) {
  49. const auto new_byte =
  50. (byte_index < num_bytes ? uint8_t(input[byte_index]) : uint8_t(0));
  51. word |= (new_byte << (8 * (byte_index % sizeof(uint32_t))));
  52. if (3 == (byte_index % sizeof(uint32_t))) {
  53. result->push_back(word);
  54. word = 0;
  55. }
  56. }
  57. // Emit a trailing partial word.
  58. if ((num_bytes + 1) % sizeof(uint32_t)) {
  59. result->push_back(word);
  60. }
  61. }
  62. // Encodes a string as a sequence of words, using the SPIR-V encoding.
  63. inline std::vector<uint32_t> MakeVector(const std::string& input) {
  64. std::vector<uint32_t> result;
  65. AppendToVector(input, &result);
  66. return result;
  67. }
  68. // Decode a string from a sequence of words between first and last, using the
  69. // SPIR-V encoding. Assert that a terminating 0-byte was found (unless
  70. // assert_found_terminating_null is passed as false).
  71. template <class InputIt>
  72. inline std::string MakeString(InputIt first, InputIt last,
  73. bool assert_found_terminating_null = true) {
  74. std::string result;
  75. constexpr size_t kCharsPerWord = sizeof(*first);
  76. static_assert(kCharsPerWord == 4, "expect 4-byte word");
  77. for (InputIt pos = first; pos != last; ++pos) {
  78. uint32_t word = *pos;
  79. for (size_t byte_index = 0; byte_index < kCharsPerWord; byte_index++) {
  80. uint32_t extracted_word = (word >> (8 * byte_index)) & 0xFF;
  81. char c = static_cast<char>(extracted_word);
  82. if (c == 0) {
  83. return result;
  84. }
  85. result += c;
  86. }
  87. }
  88. assert(!assert_found_terminating_null &&
  89. "Did not find terminating null for the string.");
  90. (void)assert_found_terminating_null; /* No unused parameters in release
  91. builds. */
  92. return result;
  93. }
  94. // Decode a string from a sequence of words in a vector, using the SPIR-V
  95. // encoding.
  96. template <class VectorType>
  97. inline std::string MakeString(const VectorType& words,
  98. bool assert_found_terminating_null = true) {
  99. return MakeString(words.cbegin(), words.cend(),
  100. assert_found_terminating_null);
  101. }
  102. // Decode a string from array words, consuming up to count words, using the
  103. // SPIR-V encoding.
  104. inline std::string MakeString(const uint32_t* words, size_t num_words,
  105. bool assert_found_terminating_null = true) {
  106. return MakeString(words, words + num_words, assert_found_terminating_null);
  107. }
  108. // Check if str starts with prefix (only included since C++20)
  109. inline bool starts_with(const std::string& str, const char* prefix) {
  110. return 0 == str.compare(0, std::strlen(prefix), prefix);
  111. }
  112. } // namespace utils
  113. } // namespace spvtools
  114. #endif // SOURCE_UTIL_STRING_UTILS_H_