CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Buffer.h
Views: 1401
#pragma once12#include <string>3#include <vector>45#include "Common/Common.h"67class Path;89// Acts as a queue. Intended to be as fast as possible for most uses.10// Does not do synchronization, must use external mutexes.11class Buffer {12public:13Buffer();14Buffer(Buffer &&) = default;15~Buffer();1617static Buffer Void() {18Buffer buf;19buf.void_ = true;20return buf;21}2223// Write max [length] bytes to the returned pointer.24// Any other operation on this Buffer invalidates the pointer.25char *Append(size_t length);2627// These work pretty much like you'd expect.28void Append(const char *str); // str null-terminated. The null is not copied.29void Append(const std::string &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. On failure, data remains in buffer and nothing is69// written.70bool FlushToFile(const Path &filename);7172// Utilities. Try to avoid checking for size.73size_t size() const { return data_.size(); }74bool empty() const { return size() == 0; }75void clear() { data_.resize(0); }76bool IsVoid() const { return void_; }7778protected:79// TODO: Find a better internal representation, like a cord.80std::vector<char> data_;81bool void_ = false;8283private:84DISALLOW_COPY_AND_ASSIGN(Buffer);85};868788