2011-03-19 06:37:00 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "util/logging.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-05-03 02:01:00 +08:00
|
|
|
|
2018-04-14 07:17:51 +08:00
|
|
|
#include <limits>
|
2019-05-03 02:01:00 +08:00
|
|
|
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/slice.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
void AppendNumberTo(std::string* str, uint64_t num) {
|
|
|
|
char buf[30];
|
2019-05-03 02:01:00 +08:00
|
|
|
snprintf(buf, sizeof(buf), "%llu", (unsigned long long)num);
|
2011-03-19 06:37:00 +08:00
|
|
|
str->append(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendEscapedStringTo(std::string* str, const Slice& value) {
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < value.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
char c = value[i];
|
|
|
|
if (c >= ' ' && c <= '~') {
|
|
|
|
str->push_back(c);
|
|
|
|
} else {
|
|
|
|
char buf[10];
|
|
|
|
snprintf(buf, sizeof(buf), "\\x%02x",
|
|
|
|
static_cast<unsigned int>(c) & 0xff);
|
|
|
|
str->append(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string NumberToString(uint64_t num) {
|
|
|
|
std::string r;
|
|
|
|
AppendNumberTo(&r, num);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EscapeString(const Slice& value) {
|
|
|
|
std::string r;
|
|
|
|
AppendEscapedStringTo(&r, value);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
|
2018-04-14 06:17:46 +08:00
|
|
|
// Constants that will be optimized away.
|
|
|
|
constexpr const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
|
|
|
|
constexpr const char kLastDigitOfMaxUint64 =
|
|
|
|
'0' + static_cast<char>(kMaxUint64 % 10);
|
|
|
|
|
|
|
|
uint64_t value = 0;
|
|
|
|
|
2019-05-29 01:17:03 +08:00
|
|
|
// reinterpret_cast-ing from char* to uint8_t* to avoid signedness.
|
|
|
|
const uint8_t* start = reinterpret_cast<const uint8_t*>(in->data());
|
2018-04-14 06:17:46 +08:00
|
|
|
|
2019-05-29 01:17:03 +08:00
|
|
|
const uint8_t* end = start + in->size();
|
|
|
|
const uint8_t* current = start;
|
2018-04-14 06:17:46 +08:00
|
|
|
for (; current != end; ++current) {
|
2019-05-29 01:17:03 +08:00
|
|
|
const uint8_t ch = *current;
|
2019-05-03 02:01:00 +08:00
|
|
|
if (ch < '0' || ch > '9') break;
|
2018-04-14 06:17:46 +08:00
|
|
|
|
|
|
|
// Overflow check.
|
|
|
|
// kMaxUint64 / 10 is also constant and will be optimized away.
|
|
|
|
if (value > kMaxUint64 / 10 ||
|
|
|
|
(value == kMaxUint64 / 10 && ch > kLastDigitOfMaxUint64)) {
|
|
|
|
return false;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2018-04-14 06:17:46 +08:00
|
|
|
|
|
|
|
value = (value * 10) + (ch - '0');
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2018-04-14 06:17:46 +08:00
|
|
|
|
|
|
|
*val = value;
|
|
|
|
const size_t digits_consumed = current - start;
|
|
|
|
in->remove_prefix(digits_consumed);
|
|
|
|
return digits_consumed != 0;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|