2011-03-18 22:37:00 +00: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-02 11:01:00 -07:00
|
|
|
|
2018-04-13 16:17:51 -07:00
|
|
|
#include <limits>
|
2019-05-02 11:01:00 -07:00
|
|
|
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/slice.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
void AppendNumberTo(std::string* str, uint64_t num) {
|
|
|
|
char buf[30];
|
2019-05-02 11:01:00 -07:00
|
|
|
snprintf(buf, sizeof(buf), "%llu", (unsigned long long)num);
|
2011-03-18 22:37:00 +00:00
|
|
|
str->append(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendEscapedStringTo(std::string* str, const Slice& value) {
|
2011-04-20 22:48:11 +00:00
|
|
|
for (size_t i = 0; i < value.size(); i++) {
|
2011-03-18 22:37:00 +00: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-13 15:17:46 -07: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-28 10:17:03 -07: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-13 15:17:46 -07:00
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
const uint8_t* end = start + in->size();
|
|
|
|
const uint8_t* current = start;
|
2018-04-13 15:17:46 -07:00
|
|
|
for (; current != end; ++current) {
|
2019-05-28 10:17:03 -07:00
|
|
|
const uint8_t ch = *current;
|
2019-05-02 11:01:00 -07:00
|
|
|
if (ch < '0' || ch > '9') break;
|
2018-04-13 15:17:46 -07: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-18 22:37:00 +00:00
|
|
|
}
|
2018-04-13 15:17:46 -07:00
|
|
|
|
|
|
|
value = (value * 10) + (ch - '0');
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2018-04-13 15:17:46 -07:00
|
|
|
|
|
|
|
*val = value;
|
|
|
|
const size_t digits_consumed = current - start;
|
|
|
|
in->remove_prefix(digits_consumed);
|
|
|
|
return digits_consumed != 0;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|