logging.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <cstdarg>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <limits>
  9. #include "leveldb/env.h"
  10. #include "leveldb/slice.h"
  11. namespace leveldb {
  12. void AppendNumberTo(std::string* str, uint64_t num) {
  13. char buf[30];
  14. std::snprintf(buf, sizeof(buf), "%llu", static_cast<unsigned long long>(num));
  15. str->append(buf);
  16. }
  17. void AppendEscapedStringTo(std::string* str, const Slice& value) {
  18. for (size_t i = 0; i < value.size(); i++) {
  19. char c = value[i];
  20. if (c >= ' ' && c <= '~') {
  21. str->push_back(c);
  22. } else {
  23. char buf[10];
  24. std::snprintf(buf, sizeof(buf), "\\x%02x",
  25. static_cast<unsigned int>(c) & 0xff);
  26. str->append(buf);
  27. }
  28. }
  29. }
  30. std::string NumberToString(uint64_t num) {
  31. std::string r;
  32. AppendNumberTo(&r, num);
  33. return r;
  34. }
  35. std::string EscapeString(const Slice& value) {
  36. std::string r;
  37. AppendEscapedStringTo(&r, value);
  38. return r;
  39. }
  40. bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
  41. // Constants that will be optimized away.
  42. constexpr const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
  43. constexpr const char kLastDigitOfMaxUint64 =
  44. '0' + static_cast<char>(kMaxUint64 % 10);
  45. uint64_t value = 0;
  46. // reinterpret_cast-ing from char* to uint8_t* to avoid signedness.
  47. const uint8_t* start = reinterpret_cast<const uint8_t*>(in->data());
  48. const uint8_t* end = start + in->size();
  49. const uint8_t* current = start;
  50. for (; current != end; ++current) {
  51. const uint8_t ch = *current;
  52. if (ch < '0' || ch > '9') break;
  53. // Overflow check.
  54. // kMaxUint64 / 10 is also constant and will be optimized away.
  55. if (value > kMaxUint64 / 10 ||
  56. (value == kMaxUint64 / 10 && ch > kLastDigitOfMaxUint64)) {
  57. return false;
  58. }
  59. value = (value * 10) + (ch - '0');
  60. }
  61. *val = value;
  62. const size_t digits_consumed = current - start;
  63. in->remove_prefix(digits_consumed);
  64. return digits_consumed != 0;
  65. }
  66. } // namespace leveldb