| 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 |
|
| |
| 5 |
|
| #include "util/logging.h" |
| 6 |
|
| |
| 7 |
|
| #include <errno.h> |
| 8 |
|
| #include <stdarg.h> |
| 9 |
|
| #include <stdio.h> |
| 10 |
|
| #include <stdlib.h> |
| 11 |
|
| #include "leveldb/env.h" |
| 12 |
|
| #include "leveldb/slice.h" |
| 13 |
|
| |
| 14 |
|
| namespace leveldb { |
| 15 |
|
| |
| 16 |
0 |
| void AppendNumberTo(std::string* str, uint64_t num) { |
| 17 |
|
| char buf[30]; |
| 18 |
0 |
| snprintf(buf, sizeof(buf), "%llu", (unsigned long long) num); |
| 19 |
0 |
▶ | str->append(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| str->append(buf); |
0 |
0 |
0 |
|
| 20 |
0 |
| } |
| 21 |
|
| |
| 22 |
0 |
| void AppendEscapedStringTo(std::string* str, const Slice& value) { |
| 23 |
0 |
▶ | for (size_t i = 0; i < value.size(); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < value.size(); i++) { |
0 |
0 |
0 |
|
| 24 |
0 |
| char c = value[i]; |
| 25 |
0 |
▶ | if (c >= ' ' && c <= '~') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= ' ' && c <= '~') { |
0 |
0 |
0 |
| if (c >= ' ' && c <= '~') { |
0 |
0 |
0 |
|
| 26 |
0 |
| str->push_back(c); |
| 27 |
|
| } else { |
| 28 |
|
| char buf[10]; |
| 29 |
0 |
| snprintf(buf, sizeof(buf), "\\x%02x", |
| 30 |
0 |
| static_cast<unsigned int>(c) & 0xff); |
| 31 |
0 |
▶ | str->append(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| str->append(buf); |
0 |
0 |
0 |
|
| 32 |
|
| } |
| 33 |
|
| } |
| 34 |
0 |
| } |
| 35 |
|
| |
| 36 |
0 |
| std::string NumberToString(uint64_t num) { |
| 37 |
0 |
| std::string r; |
| 38 |
0 |
▶ | AppendNumberTo(&r, num); |
Branch: 1 branch
| Branch | True | False | Total |
| AppendNumberTo(&r, num); |
0 |
0 |
0 |
|
| 39 |
0 |
| return r; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
0 |
| std::string EscapeString(const Slice& value) { |
| 43 |
0 |
| std::string r; |
| 44 |
0 |
▶ | AppendEscapedStringTo(&r, value); |
Branch: 1 branch
| Branch | True | False | Total |
| AppendEscapedStringTo(&r, value); |
0 |
0 |
0 |
|
| 45 |
0 |
| return r; |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
0 |
| bool ConsumeChar(Slice* in, char c) { |
| 49 |
0 |
▶ | if (!in->empty() && (*in)[0] == c) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!in->empty() && (*in)[0] == c) { |
0 |
0 |
0 |
| if (!in->empty() && (*in)[0] == c) { |
0 |
0 |
0 |
| if (!in->empty() && (*in)[0] == c) { |
0 |
0 |
0 |
|
| 50 |
0 |
| in->remove_prefix(1); |
| 51 |
0 |
| return true; |
| 52 |
|
| } else { |
| 53 |
0 |
| return false; |
| 54 |
|
| } |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
27 |
| bool ConsumeDecimalNumber(Slice* in, uint64_t* val) { |
| 58 |
27 |
| uint64_t v = 0; |
| 59 |
27 |
| int digits = 0; |
| 60 |
117 |
▶ | while (!in->empty()) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!in->empty()) { |
108 |
9 |
117 |
|
| 61 |
108 |
| char c = (*in)[0]; |
| 62 |
108 |
▶ | if (c >= '0' && c <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') { |
90 |
18 |
108 |
| if (c >= '0' && c <= '9') { |
90 |
0 |
90 |
|
| 63 |
90 |
| ++digits; |
| 64 |
90 |
| const int delta = (c - '0'); |
| 65 |
|
| static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0); |
| 66 |
90 |
▶ | if (v > kMaxUint64/10 || |
Branch: 2 branches
| Branch | True | False | Total |
| if (v > kMaxUint64/10 || |
90 |
0 |
90 |
| if (v > kMaxUint64/10 || |
0 |
90 |
90 |
|
| 67 |
0 |
▶ | (v == kMaxUint64/10 && delta > kMaxUint64%10)) { |
Branch: 1 branch
| Branch | True | False | Total |
| (v == kMaxUint64/10 && delta > kMaxUint64%10)) { |
0 |
0 |
0 |
|
| 68 |
|
| // Overflow |
| 69 |
0 |
| return false; |
| 70 |
|
| } |
| 71 |
90 |
| v = (v * 10) + delta; |
| 72 |
90 |
| in->remove_prefix(1); |
| 73 |
|
| } else { |
| 74 |
|
| break; |
| 75 |
|
| } |
| 76 |
|
| } |
| 77 |
27 |
| *val = v; |
| 78 |
27 |
| return (digits > 0); |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| } |