作者: 谢瑞阳 10225101483 徐翔宇 10225101535
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8 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 "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. snprintf(buf, sizeof(buf), "%llu", (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. 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 ConsumeChar(Slice* in, char c) {
  41. if (!in->empty() && (*in)[0] == c) {
  42. in->remove_prefix(1);
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. }
  48. bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
  49. uint64_t v = 0;
  50. int digits = 0;
  51. while (!in->empty()) {
  52. char c = (*in)[0];
  53. if (c >= '0' && c <= '9') {
  54. ++digits;
  55. const int delta = (c - '0');
  56. static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0);
  57. if (v > kMaxUint64/10 ||
  58. (v == kMaxUint64/10 && delta > kMaxUint64%10)) {
  59. // Overflow
  60. return false;
  61. }
  62. v = (v * 10) + delta;
  63. in->remove_prefix(1);
  64. } else {
  65. break;
  66. }
  67. }
  68. *val = v;
  69. return (digits > 0);
  70. }
  71. } // namespace leveldb