#pragma once12#include <string>3#include <string_view>4#include <vector>56#include "Common/Common.h"7#include "Common/Data/Collections/CharQueue.h"89class Path;1011// Acts as a queue. Intended to be as fast as possible for most uses.12// Does not do synchronization, must use external mutexes.13class Buffer {14public:15Buffer() = default;16Buffer(Buffer &&) = default;1718static Buffer Void() {19Buffer buf;20buf.void_ = true;21return buf;22}2324// Write max [length] bytes to the returned pointer.25// Any other operation on this Buffer invalidates the pointer.26char *Append(size_t length);2728// These work pretty much like you'd expect.29void Append(std::string_view str);30void Append(const Buffer &other);3132// Various types. Useful for varz etc. Appends a string representation of the33// value, rather than a binary representation.34void AppendValue(int value);3536// Parsing Helpers3738// Use for easy line skipping. If no CRLF within the buffer, returns -1.39// If parsing HTML headers, this indicates that you should probably buffer up40// more data.41int OffsetToAfterNextCRLF();4243// Takers4445void Take(size_t length, std::string *dest);46void Take(size_t length, char *dest);47void TakeAll(std::string *dest) { Take(size(), dest); }48// On failure, return value < 0 and *dest is unchanged.49// Strips off the actual CRLF from the result.50int TakeLineCRLF(std::string *dest);5152// Skippers53void Skip(size_t length);54// Returns -1 on failure (no CRLF within sight).55// Otherwise returns the length of the line skipped, not including CRLF. Can be 0.56int SkipLineCRLF();5758// Utility functions.59void Printf(const char *fmt, ...);6061// Dumps the entire buffer to the string, but keeps it around.62// Only to be used for debugging, since it might not be fast at all.63void PeekAll(std::string *dest);6465// Simple I/O.6667// Writes the entire buffer to the file descriptor. Also resets the68// size to zero, if the clear flag is true. On failure, data remains in buffer and nothing is69// written.70bool FlushToFile(const Path &filename, bool clear);7172// Utilities. Try to avoid checking for size.73size_t size() const { return data_.size(); }74bool empty() const { return size() == 0; }75void clear() { data_.clear(); }76bool IsVoid() const { return void_; }7778protected:79// Custom queue implementation.80CharQueue data_;81bool void_ = false;8283private:84DISALLOW_COPY_AND_ASSIGN(Buffer);85};868788