基于select模型的windows网络库libet(六)Slice

Slice.h

#pragma once
#include <string>
#include <vector>

//Slice作为Buffer的扩展,只维护Buffer的指针,不具有自己的内存
//所以要确保Slice在Buffer的生命周期内使用
class Slice 
{
public:
	Slice() : pb_("") { pe_ = pb_; }
	Slice(const char* b, const char* e) :pb_(b), pe_(e) {}
	Slice(const char* d, size_t n) : pb_(d), pe_(d + n) { }
	Slice(const std::string& s) : pb_(s.data()), pe_(s.data() + s.size()) { }
	Slice(const char* s) : pb_(s), pe_(s + strlen(s)) { }

	const char* data() const { return pb_; }
	const char* begin() const { return pb_; }
	const char* end() const { return pe_; }
	char front() { return *pb_; }
	char back() { return pe_[-1]; }
	size_t size() const { return pe_ - pb_; }
	void resize(size_t sz) { pe_ = pb_ + sz; }
	inline bool empty() const { return pe_ == pb_; }
	void clear() { pe_ = pb_ = ""; }

	//return the eated data
	Slice eatWord();
	Slice eatLine();
	Slice eat(int sz) { Slice s(pb_, sz); pb_ += sz; return s; }
	Slice sub(int boff, int eoff = 0) const { Slice s(*this); s.pb_ += boff; s.pe_ += eoff; return s; }
	Slice& trimSpace();

	inline char operator[](size_t n) const { return pb_[n]; }

	std::string toString() const { return std::string(pb_, pe_); }
	// Three-way comparison.  Returns value:
	int compare(const Slice& b) const;

	// Return true if "x" is a prefix of "*this"
	bool starts_with(const Slice& x) const {
		return (size() >= x.size() && memcmp(pb_, x.pb_, x.size()) == 0);
	}

	bool end_with(const Slice& x) const {
		return (size() >= x.size() && memcmp(pe_ - x.size(), x.pb_, x.size()) == 0);
	}
	operator std::string() const { return std::string(pb_, pe_); }
	std::vector<Slice> split(char ch) const;
private:
	const char* pb_;
	const char* pe_;
};

inline Slice Slice::eatWord() {
	const char* b = pb_;
	while (b < pe_ && isspace(*b)) {
		b++;
	}
	const char* e = b;
	while (e < pe_ && !isspace(*e)) {
		e++;
	}
	pb_ = e;
	return Slice(b, e - b);
}

inline Slice Slice::eatLine() {
	const char* p = pb_;
	while (pb_ < pe_ && *pb_ != '\n' && *pb_ != '\r') {
		pb_++;
	}
	return Slice(p, pb_ - p);
}

inline Slice& Slice::trimSpace() {
	while (pb_ < pe_ && isspace(*pb_)) pb_++;
	while (pb_ < pe_ && isspace(pe_[-1])) pe_--;
	return *this;
}

inline bool operator < (const Slice& x, const Slice& y) {
	return x.compare(y) < 0;
}

inline bool operator==(const Slice& x, const Slice& y) {
	return ((x.size() == y.size()) &&
		(memcmp(x.data(), y.data(), x.size()) == 0));
}

inline bool operator!=(const Slice& x, const Slice& y) {
	return !(x == y);
}

inline int Slice::compare(const Slice& b) const {
	size_t sz = size(), bsz = b.size();
	const int min_len = (sz < bsz) ? sz : bsz;
	int r = memcmp(pb_, b.pb_, min_len);
	if (r == 0) {
		if (sz < bsz) r = -1;
		else if (sz > bsz) r = +1;
	}
	return r;
}

inline std::vector<Slice> Slice::split(char ch) const {
	std::vector<Slice> r;
	const char* pb = pb_;
	for (const char* p = pb_; p < pe_; p++) {
		if (*p == ch) {
			r.push_back(Slice(pb, p));
			pb = p + 1;
		}
	}
	if (pe_ != pb_)
		r.push_back(Slice(pb, pe_));
	return r;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值