From c3d9dbc7522c2ba685074d9ab5672e3e023e5f5d Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 19 Dec 2015 01:43:37 -0800 Subject: [PATCH] Try to use std::streambuf instead of std::istream. --- Code.hpp | 20 ++++---------------- Complete.cpp | 4 ++-- Console.cpp | 8 ++++---- Driver.cpp | 2 +- Driver.hpp | 4 ++-- Execute.cpp | 4 ++-- Library.cpp | 2 +- Scanner.lpp.in | 12 +++--------- 8 files changed, 19 insertions(+), 37 deletions(-) diff --git a/Code.hpp b/Code.hpp index f6b3312..abb1805 100644 --- a/Code.hpp +++ b/Code.hpp @@ -27,26 +27,14 @@ #include "String.hpp" class CYStream : - public std::istream + public std::streambuf { - private: - class CYBuffer : - public std::streambuf - { - public: - CYBuffer(const char *start, const char *end) { - setg(const_cast(start), const_cast(start), const_cast(end)); - } - } buffer_; - public: - CYStream(const char *start, const char *end) : - std::istream(&buffer_), - buffer_(start, end) - { + CYStream(const char *start, const char *end) { + setg(const_cast(start), const_cast(start), const_cast(end)); } }; -CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream); +CYUTF8String CYPoolCode(CYPool &pool, std::streambuf &stream); #endif//CODE_HPP diff --git a/Complete.cpp b/Complete.cpp index 6ce6671..d822d99 100644 --- a/Complete.cpp +++ b/Complete.cpp @@ -28,7 +28,7 @@ static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) { std::stringstream stream; stream << '(' << code << ')'; - CYDriver driver(pool, stream); + CYDriver driver(pool, *stream.rdbuf()); if (driver.Parse() || !driver.errors_.empty()) return NULL; @@ -51,7 +51,7 @@ static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) { _visible char **CYComplete(const char *word, const std::string &line, CYUTF8String (*run)(CYPool &pool, const std::string &)) { CYLocalPool pool; - std::istringstream stream(line); + std::stringbuf stream(line); CYDriver driver(pool, stream); driver.auto_ = true; diff --git a/Console.cpp b/Console.cpp index 76ad553..e01dd07 100644 --- a/Console.cpp +++ b/Console.cpp @@ -420,7 +420,7 @@ static int CYConsoleKeyReturn(int count, int key) { else { std::string command(rl_line_buffer, rl_end); command += '\n'; - std::istringstream stream(command); + std::stringbuf stream(command); size_t last(std::string::npos); for (size_t i(0); i != std::string::npos; i = command.find('\n', i + 1)) @@ -680,7 +680,7 @@ static void Console(CYOptions &options) { if (bypass) code = command; else try { - std::istringstream stream(command); + std::stringbuf stream(command); CYPool pool; CYDriver driver(pool, stream); @@ -1066,7 +1066,7 @@ int Main(int argc, char * const argv[], char const * const envp[]) { stream = new std::istringstream(buffer.str()); CYPool pool; - CYDriver driver(pool, *stream, script); + CYDriver driver(pool, *stream->rdbuf(), script); Setup(driver); uint64_t begin(CYGetTime()); @@ -1092,7 +1092,7 @@ int Main(int argc, char * const argv[], char const * const envp[]) { } CYPool pool; - CYDriver driver(pool, *stream, script); + CYDriver driver(pool, *stream->rdbuf(), script); Setup(driver); bool failed(driver.Parse()); diff --git a/Driver.cpp b/Driver.cpp index 788afee..031625c 100644 --- a/Driver.cpp +++ b/Driver.cpp @@ -24,7 +24,7 @@ bool CYParser(CYPool &pool, bool debug); -CYDriver::CYDriver(CYPool &pool, std::istream &data, const std::string &filename) : +CYDriver::CYDriver(CYPool &pool, std::streambuf &data, const std::string &filename) : pool_(pool), newline_(false), last_(false), diff --git a/Driver.hpp b/Driver.hpp index bbce9fd..dce9213 100644 --- a/Driver.hpp +++ b/Driver.hpp @@ -64,7 +64,7 @@ class _visible CYDriver { bool newline_; bool last_; - std::istream &data_; + std::streambuf &data_; int debug_; bool strict_; @@ -120,7 +120,7 @@ class _visible CYDriver { void ScannerDestroy(); public: - CYDriver(CYPool &pool, std::istream &data, const std::string &filename = ""); + CYDriver(CYPool &pool, std::streambuf &data, const std::string &filename = ""); ~CYDriver(); bool Parse(CYMark mark = CYMarkScript); diff --git a/Execute.cpp b/Execute.cpp index b450076..9862755 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -369,7 +369,7 @@ _visible void CYGarbageCollect(JSContextRef context) { static JSValueRef Cycript_compile_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { CYPool pool; CYUTF8String before(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0]))); - std::stringstream value(std::string(before.data, before.size)); + std::stringbuf value(std::string(before.data, before.size)); CYUTF8String after(CYPoolCode(pool, value)); return CYCastJSValue(context, CYJSString(after)); } CYCatch_(NULL, "SyntaxError") } @@ -1871,7 +1871,7 @@ static JSValueRef require(JSContextRef context, JSObjectRef object, JSObjectRef std::stringstream wrap; wrap << "(function (exports, require, module) { " << code << "\n});"; - code = CYPoolCode(pool, wrap); + code = CYPoolCode(pool, *wrap.rdbuf()); JSValueRef value(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0)); JSObjectRef function(CYCastJSObject(context, value)); diff --git a/Library.cpp b/Library.cpp index 7203225..aa85e81 100644 --- a/Library.cpp +++ b/Library.cpp @@ -224,7 +224,7 @@ _visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &nee return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0; } -CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) { +CYUTF8String CYPoolCode(CYPool &pool, std::streambuf &stream) { CYLocalPool local; CYDriver driver(local, stream); _assert(!driver.Parse()); diff --git a/Scanner.lpp.in b/Scanner.lpp.in index 6d79127..235dc80 100644 --- a/Scanner.lpp.in +++ b/Scanner.lpp.in @@ -184,15 +184,9 @@ static unsigned U(char *&local, const char *text, yy_size_t &i) { I(type, Type(P.strmemdup(yyextra->buffer_.data(), yyextra->buffer_.size()), yyextra->buffer_.size()), value, highlight); \ } while (false) -#define YY_INPUT(data, value, size) { \ - if (yyextra->data_.eof()) \ - value = YY_NULL; \ - else { \ - yyextra->data_.read(data, size); \ - size_t copy(yyextra->data_.gcount()); \ - value = copy == 0 ? YY_NULL : copy; \ - } \ -} +#define YY_INPUT(data, value, size) do { \ + value = yyextra->data_.sgetn(data, size) ?: YY_NULL; \ +} while (false) %} -- 2.45.2