From acea3db3783cb5ac19cc7eee2da93de12729974a Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 16 Jun 2019 02:20:18 +0300 Subject: [PATCH] Move line retrieval into the script wrapper. Might be used elsewhere. --- source/Base/ScriptSrc.cpp | 21 +++++++++++++++++++++ source/Base/ScriptSrc.hpp | 4 ++++ source/Core.cpp | 8 ++------ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/source/Base/ScriptSrc.cpp b/source/Base/ScriptSrc.cpp index 1e55edf5..4c6997b1 100644 --- a/source/Base/ScriptSrc.cpp +++ b/source/Base/ScriptSrc.cpp @@ -174,4 +174,25 @@ ScriptSrc::ScriptSrc(HSQUIRRELVM vm, String && path, bool delay, bool info) } } +// ------------------------------------------------------------------------------------------------ +String ScriptSrc::FetchLine(size_t line, bool trim) const +{ + // Do we have such line? + if (line > mLine.size()) + { + return String(); // Nope! + } + // Grab it's range in the file + Line::const_reference l = mLine.at(line); + // Grab the code from that line + String code = mData.substr(l.first, l.second - l.first); + // Trim whitespace from the beginning of the code code + if (trim) + { + code.erase(0, code.find_first_not_of(" \t\n\r\f\v")); + } + // Return the resulting string + return code; +} + } // Namespace:: SqMod diff --git a/source/Base/ScriptSrc.hpp b/source/Base/ScriptSrc.hpp index 5b45529b..5e50e717 100644 --- a/source/Base/ScriptSrc.hpp +++ b/source/Base/ScriptSrc.hpp @@ -71,6 +71,10 @@ public: */ ScriptSrc & operator = (ScriptSrc && o) = default; + /* -------------------------------------------------------------------------------------------- + * Fetches a line from the code. Can also triim whitespace at the beginning. + */ + String FetchLine(size_t line, bool trim = true) const; }; diff --git a/source/Core.cpp b/source/Core.cpp index f1aac5b9..f5d63d90 100644 --- a/source/Core.cpp +++ b/source/Core.cpp @@ -903,12 +903,8 @@ bool Core::CompilerErrorHandlerEx(CSStr desc, CSStr src, SQInteger line, SQInteg { return false; // No such line! } - // Grab the line we're looking for - ScriptSrc::Line::iterator itr = script->mLine.begin() + line; - // Grab the code from that line - String code = script->mData.substr(itr->first, itr->second - itr->first); - // Trim whitespace from the beginning of the code code - code.erase(0, code.find_first_not_of(" \t\n\r\f\v")); + // Grab the associated line of code + String code = script->FetchLine(line, true); // Display the error message with the code included LogFtl("Message: %s\n[\n=>Location: %s\n=>Line: %" PRINT_SZ_FMT "\n=>Column: %" PRINT_INT_FMT "\n=>Code: %s\n]", desc, src, ++line, column, code.c_str()); // We displayed the information