json_scalar.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "parser.h"
  2. namespace sunrise::core::settings::parser {
  3. namespace {
  4. /** JSON code points below 0x20 are unescaped control characters. */
  5. constexpr unsigned char kFirstPrintableCodePoint = 0x20;
  6. /** JSON allows only these one-byte escape codes after a backslash. */
  7. constexpr std::string_view kSimpleEscapeCodes = "\"\\/bfnrt";
  8. /** Lowercase u starts the 4-digit JSON Unicode escape. */
  9. constexpr char kUnicodeEscapeCode = 'u';
  10. /** A JSON Unicode escape has exactly 4 hex digits. */
  11. constexpr std::size_t kUnicodeEscapeDigitCount = 4;
  12. /**
  13. * Tests one byte for a hex digit, as used by JSON Unicode escapes.
  14. * @return True for an ASCII decimal or hex digit.
  15. */
  16. [[nodiscard]] constexpr bool is_hex_digit(char value) noexcept {
  17. return (value >= '0' && value <= '9') || (value >= 'A' && value <= 'F')
  18. || (value >= 'a' && value <= 'f');
  19. }
  20. } // namespace
  21. /** Reads one JSON string and returns the borrowed encoded bytes. */
  22. bool Parser::string(std::string_view& output) noexcept {
  23. whitespace();
  24. if (position_ >= input_.size() || input_[position_] != '"') {
  25. return false;
  26. }
  27. const std::size_t start = ++position_;
  28. while (position_ < input_.size()) {
  29. const char value = input_[position_++];
  30. if (value == '"') {
  31. output = input_.substr(start, position_ - start - 1);
  32. return true;
  33. }
  34. if (static_cast<unsigned char>(value) < kFirstPrintableCodePoint) {
  35. return false;
  36. }
  37. if (value == '\\') {
  38. if (position_ >= input_.size()) {
  39. return false;
  40. }
  41. const char escape = input_[position_++];
  42. if (escape != kUnicodeEscapeCode) {
  43. if (kSimpleEscapeCodes.find(escape) == std::string_view::npos) {
  44. return false;
  45. }
  46. continue;
  47. }
  48. // A Unicode escape cannot take digits from the closing quote or the next token.
  49. if (input_.size() - position_ < kUnicodeEscapeDigitCount) {
  50. return false;
  51. }
  52. for (std::size_t index = 0; index < kUnicodeEscapeDigitCount; ++index) {
  53. if (!is_hex_digit(input_[position_ + index])) {
  54. return false;
  55. }
  56. }
  57. position_ += kUnicodeEscapeDigitCount;
  58. }
  59. }
  60. return false;
  61. }
  62. /** Reads a JSON true or false literal. */
  63. bool Parser::boolean(bool& output) noexcept {
  64. if (literal("true")) {
  65. output = true;
  66. return true;
  67. }
  68. if (literal("false")) {
  69. output = false;
  70. return true;
  71. }
  72. return false;
  73. }
  74. /** Reads one exact JSON literal after leading whitespace. */
  75. bool Parser::literal(std::string_view value) noexcept {
  76. whitespace();
  77. if (input_.substr(position_, value.size()) != value) {
  78. return false;
  79. }
  80. position_ += value.size();
  81. return true;
  82. }
  83. /** Converts a JSON level token to the logging enum. */
  84. bool Parser::level_value(std::string_view name, log::Level& output) noexcept {
  85. if (name == "error") {
  86. output = log::Level::error;
  87. } else if (name == "warn") {
  88. output = log::Level::warn;
  89. } else if (name == "info") {
  90. output = log::Level::info;
  91. } else if (name == "debug") {
  92. output = log::Level::debug;
  93. } else if (name == "off") {
  94. output = log::Level::off;
  95. } else {
  96. return false;
  97. }
  98. return true;
  99. }
  100. /** Converts a JSON channel token to its settings-array index. */
  101. std::size_t Parser::channel_index(std::string_view name) noexcept {
  102. if (name == "core") {
  103. return static_cast<std::size_t>(log::Channel::core);
  104. }
  105. if (name == "client") {
  106. return static_cast<std::size_t>(log::Channel::client);
  107. }
  108. if (name == "state") {
  109. return static_cast<std::size_t>(log::Channel::state);
  110. }
  111. if (name == "server") {
  112. return static_cast<std::size_t>(log::Channel::server);
  113. }
  114. if (name == "middleware") {
  115. return static_cast<std::size_t>(log::Channel::middleware);
  116. }
  117. return static_cast<std::size_t>(log::Channel::count);
  118. }
  119. } // namespace sunrise::core::settings::parser