Commit bfc76fba authored by captainwong's avatar captainwong

utf8

parent 3d92e869
#pragma once
#pragma once
namespace jlib
{
......
#pragma once
#pragma once
#ifdef __GNUG__
......
#pragma once
#pragma once
namespace jlib
{
......
#pragma once
#pragma once
#include "config.h"
#include <inttypes.h>
......
#pragma once
#pragma once
#include "config.h"
#include "../3rdparty/date/include/date/date.h"
......
#pragma once
#pragma once
#include "config.h"
#include "logstream.h"
......
#pragma once
#pragma once
#include "config.h"
#include "noncopyable.h"
......
#pragma once
#pragma once
namespace jlib
{
......
#pragma once
#pragma once
#include "noncopyable.h"
#include <mutex>
......
......@@ -47,12 +47,10 @@ namespace jlib
class StringArg // copyable
{
public:
StringArg(const char *str)
: str_(str)
StringArg(const char *str) : str_(str)
{}
StringArg(const std::string &str)
: str_(str.c_str())
StringArg(const std::string &str) : str_(str.c_str())
{}
const char *c_str() const { return str_; }
......@@ -72,25 +70,19 @@ public:
// We provide non-explicit singleton constructors so users can pass
// in a "const char*" or a "string" wherever a "StringPiece" is
// expected.
StringPiece()
: ptr_(nullptr), length_(0)
StringPiece() : ptr_(nullptr), length_(0)
{}
StringPiece(const char *str)
: ptr_(str), length_(static_cast<int>(strlen(ptr_)))
StringPiece(const char *str) : ptr_(str), length_(static_cast<int>(strlen(ptr_)))
{}
StringPiece(const unsigned char *str)
: ptr_(reinterpret_cast<const char *>(str)),
length_(static_cast<int>(strlen(ptr_)))
StringPiece(const unsigned char *str) : ptr_(reinterpret_cast<const char *>(str)), length_(static_cast<int>(strlen(ptr_)))
{}
StringPiece(const std::string &str)
: ptr_(str.data()), length_(static_cast<int>(str.size()))
StringPiece(const std::string &str) : ptr_(str.data()), length_(static_cast<int>(str.size()))
{}
StringPiece(const char *offset, int len)
: ptr_(offset), length_(len)
StringPiece(const char *offset, int len) : ptr_(offset), length_(len)
{}
// data() may return a pointer to a buffer with embedded NULs, and the
......@@ -100,54 +92,19 @@ public:
// this. Or better yet, change your routine so it does not rely on NUL
// termination.
const char *data() const { return ptr_; }
int size() const { return length_; }
bool empty() const { return length_ == 0; }
const char *begin() const { return ptr_; }
const char *end() const { return ptr_ + length_; }
void clear() {
ptr_ = NULL;
length_ = 0;
}
void set(const char *buffer, int len) {
ptr_ = buffer;
length_ = len;
}
void set(const char *str) {
ptr_ = str;
length_ = static_cast<int>(strlen(str));
}
void set(const void *buffer, int len) {
ptr_ = reinterpret_cast<const char *>(buffer);
length_ = len;
}
void clear() { ptr_ = NULL; length_ = 0; }
void set(const char *buffer, int len) { ptr_ = buffer; length_ = len; }
void set(const char *str) { ptr_ = str; length_ = static_cast<int>(strlen(str)); }
void set(const void *buffer, int len) { ptr_ = reinterpret_cast<const char *>(buffer); length_ = len; }
char operator[](int i) const { return ptr_[i]; }
void remove_prefix(int n) {
ptr_ += n;
length_ -= n;
}
void remove_suffix(int n) {
length_ -= n;
}
bool operator==(const StringPiece &x) const {
return ((length_ == x.length_) &&
(memcmp(ptr_, x.ptr_, length_) == 0));
}
bool operator!=(const StringPiece &x) const {
return !(*this == x);
}
void remove_prefix(int n) { ptr_ += n; length_ -= n; }
void remove_suffix(int n) { length_ -= n; }
bool operator==(const StringPiece &x) const { return ((length_ == x.length_) && (memcmp(ptr_, x.ptr_, length_) == 0)); }
bool operator!=(const StringPiece &x) const { return !(*this == x); }
#define STRINGPIECE_BINARY_PREDICATE(cmp, auxcmp) \
bool operator cmp(const StringPiece &x) const \
......@@ -173,13 +130,8 @@ public:
return r;
}
std::string as_string() const {
return std::string(data(), size());
}
void CopyToString(std::string *target) const {
target->assign(ptr_, length_);
}
std::string as_string() const { return std::string(data(), size()); }
void CopyToString(std::string *target) const { target->assign(ptr_, length_); }
// Does "this" start with "x"
bool starts_with(const StringPiece &x) const {
......
#pragma once
#pragma once
#pragma once
// for cross-platform
......
#pragma once
#pragma once
#include "config.h"
//
......
#pragma once
#pragma once
// getter & setter
......@@ -44,7 +44,7 @@
} \
}
#define DECLARE_GETTER_SETTER_STRING(val) \
#define DECLARE_GETTER_SETTER_CSTRING(val) \
DECLARE_GETTER(CString, val); \
DECLARE_SETTER(CString, val);
......
......@@ -2,12 +2,10 @@
#include <string>
#include <cmath>
#include <iomanip>
#include <sstream>
namespace jlib {
enum PositionalNotation {
enum class PositionalNotation {
//! Used for memory size or Microsoft disk space
Binary,
//! Usually used by Disk Space
......@@ -17,18 +15,15 @@ enum PositionalNotation {
/**
* @brief Format byte count to human readable string
* @note bytes must less than 1DB(DoggaByte)
* @note http://programming.guide/java/formatting-byte-size-to-human-readable-format.html
*/
static std::string human_readable_byte_count(uintmax_t bytes, size_t precision = 1, PositionalNotation po = PositionalNotation::Binary)
static inline std::string human_readable_byte_count(uintmax_t bytes, PositionalNotation po = PositionalNotation::Binary)
{
// http://programming.guide/java/formatting-byte-size-to-human-readable-format.html
auto unit = po == PositionalNotation::Binary ? 1024 : 1000;
if (bytes < unit) { return std::to_string(bytes) + "B"; }
auto exp = static_cast<size_t>(std::log(bytes) / std::log(unit));
auto pre = std::string("KMGTPEZYBND").at(exp - 1) + std::string(po == PositionalNotation::Binary ? "i" : "");
auto var = bytes / std::pow(unit, exp);
//std::stringstream ss;
//ss << std::fixed << std::setprecision(1) << var << pre << "B";
char buf[64] = { 0 };
snprintf(buf, sizeof(buf), "%.1lf%sB", var, pre.data());
return buf;
......
#pragma once
#pragma once
#include "../base/config.h"
#include <string>
......
......@@ -33,6 +33,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "base", "base", "{608A105E-4
..\jlib\base\noncopyable.h = ..\jlib\base\noncopyable.h
..\jlib\base\singleton.h = ..\jlib\base\singleton.h
..\jlib\base\stringpiece.h = ..\jlib\base\stringpiece.h
..\jlib\base\thread.h = ..\jlib\base\thread.h
..\jlib\base\time.h = ..\jlib\base\time.h
..\jlib\base\timestamp.h = ..\jlib\base\timestamp.h
..\jlib\base\timezone.h = ..\jlib\base\timezone.h
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment