string_utils.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <sstream>
  18. #include <string>
  19. #include <vector>
  20. #include "source/util/string_utils.h"
  21. namespace spvtools {
  22. namespace utils {
  23. // Converts arithmetic value |val| to its default string representation.
  24. template <class T>
  25. std::string ToString(T val) {
  26. static_assert(
  27. std::is_arithmetic<T>::value,
  28. "spvtools::utils::ToString is restricted to only arithmetic values");
  29. std::stringstream os;
  30. os << val;
  31. return os.str();
  32. }
  33. // Converts cardinal number to ordinal number string.
  34. std::string CardinalToOrdinal(size_t cardinal);
  35. // Splits the string |flag|, of the form '--pass_name[=pass_args]' into two
  36. // strings "pass_name" and "pass_args". If |flag| has no arguments, the second
  37. // string will be empty.
  38. std::pair<std::string, std::string> SplitFlagArgs(const std::string& flag);
  39. // Encodes a string as a sequence of words, using the SPIR-V encoding.
  40. inline std::vector<uint32_t> MakeVector(std::string input) {
  41. std::vector<uint32_t> result;
  42. uint32_t word = 0;
  43. size_t num_bytes = input.size();
  44. // SPIR-V strings are null-terminated. The byte_index == num_bytes
  45. // case is used to push the terminating null byte.
  46. for (size_t byte_index = 0; byte_index <= num_bytes; byte_index++) {
  47. const auto new_byte =
  48. (byte_index < num_bytes ? uint8_t(input[byte_index]) : uint8_t(0));
  49. word |= (new_byte << (8 * (byte_index % sizeof(uint32_t))));
  50. if (3 == (byte_index % sizeof(uint32_t))) {
  51. result.push_back(word);
  52. word = 0;
  53. }
  54. }
  55. // Emit a trailing partial word.
  56. if ((num_bytes + 1) % sizeof(uint32_t)) {
  57. result.push_back(word);
  58. }
  59. return result;
  60. }
  61. // Decode a string from a sequence of words, using the SPIR-V encoding.
  62. template <class VectorType>
  63. inline std::string MakeString(const VectorType& words) {
  64. std::string result;
  65. for (uint32_t word : words) {
  66. for (int byte_index = 0; byte_index < 4; byte_index++) {
  67. uint32_t extracted_word = (word >> (8 * byte_index)) & 0xFF;
  68. char c = static_cast<char>(extracted_word);
  69. if (c == 0) {
  70. return result;
  71. }
  72. result += c;
  73. }
  74. }
  75. assert(false && "Did not find terminating null for the string.");
  76. return result;
  77. } // namespace utils
  78. } // namespace utils
  79. } // namespace spvtools
  80. #endif // SOURCE_UTIL_STRING_UTILS_H_