作者: 韩晨旭 10225101440 李畅 10225102463
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

84 строки
2.1 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "util/logging.h"
  5. #include <errno.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <limits>
  10. #include "leveldb/env.h"
  11. #include "leveldb/slice.h"
  12. namespace leveldb {
  13. void AppendNumberTo(std::string* str, uint64_t num) {
  14. char buf[30];
  15. snprintf(buf, sizeof(buf), "%llu", (unsigned long long)num);
  16. str->append(buf);
  17. }
  18. void AppendEscapedStringTo(std::string* str, const Slice& value) {
  19. for (size_t i = 0; i < value.size(); i++) {
  20. char c = value[i];
  21. if (c >= ' ' && c <= '~') {
  22. str->push_back(c);
  23. } else {
  24. char buf[10];
  25. snprintf(buf, sizeof(buf), "\\x%02x",
  26. static_cast<unsigned int>(c) & 0xff);
  27. str->append(buf);
  28. }
  29. }
  30. }
  31. std::string NumberToString(uint64_t num) {
  32. std::string r;
  33. AppendNumberTo(&r, num);
  34. return r;
  35. }
  36. std::string EscapeString(const Slice& value) {
  37. std::string r;
  38. AppendEscapedStringTo(&r, value);
  39. return r;
  40. }
  41. bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
  42. // Constants that will be optimized away.
  43. constexpr const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
  44. constexpr const char kLastDigitOfMaxUint64 =
  45. '0' + static_cast<char>(kMaxUint64 % 10);
  46. uint64_t value = 0;
  47. // reinterpret_cast-ing from char* to uint8_t* to avoid signedness.
  48. const uint8_t* start = reinterpret_cast<const uint8_t*>(in->data());
  49. const uint8_t* end = start + in->size();
  50. const uint8_t* current = start;
  51. for (; current != end; ++current) {
  52. const uint8_t ch = *current;
  53. if (ch < '0' || ch > '9') break;
  54. // Overflow check.
  55. // kMaxUint64 / 10 is also constant and will be optimized away.
  56. if (value > kMaxUint64 / 10 ||
  57. (value == kMaxUint64 / 10 && ch > kLastDigitOfMaxUint64)) {
  58. return false;
  59. }
  60. value = (value * 10) + (ch - '0');
  61. }
  62. *val = value;
  63. const size_t digits_consumed = current - start;
  64. in->remove_prefix(digits_consumed);
  65. return digits_consumed != 0;
  66. }
  67. } // namespace leveldb