From e83dd4d2415f153dc9b06ed8ec0bf4555bcd2aa5 Mon Sep 17 00:00:00 2001 From: Christopher Pascoe Date: Mon, 28 Dec 2015 17:53:10 -0500 Subject: [PATCH] Use lightweight tests when we only care if the buffer is empty or full. --- cores/esp8266/cbuf.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cores/esp8266/cbuf.h b/cores/esp8266/cbuf.h index 02b612c5c..4b5157e1a 100644 --- a/cores/esp8266/cbuf.h +++ b/cores/esp8266/cbuf.h @@ -45,18 +45,22 @@ class cbuf { return _begin - _end - 1; } - bool empty() const { + inline bool empty() const { return _begin == _end; } + inline bool full() const { + return wrap_if_bufend(_end + 1) == _begin; + } + int peek() { - if(_end == _begin) return -1; + if(empty()) return -1; return static_cast(*_begin); } int read() { - if(getSize() == 0) return -1; + if(empty()) return -1; char result = *_begin; _begin = wrap_if_bufend(_begin + 1); @@ -80,7 +84,7 @@ class cbuf { } size_t write(char c) { - if(room() == 0) return 0; + if(full()) return 0; *_end = c; _end = wrap_if_bufend(_end + 1); @@ -109,7 +113,7 @@ class cbuf { } private: - inline char* wrap_if_bufend(char* ptr) { + inline char* wrap_if_bufend(char* ptr) const { return (ptr == _bufend) ? _buf : ptr; }