From 20ae383c4282233bd8784275a50d2fc063601682 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 11 Mar 2016 04:23:59 +0200 Subject: [PATCH] Remove duplicate code that converts a buffer to a script string object. --- source/Base/Shared.cpp | 27 ++++++++++++++++++++++++++ source/Base/Shared.hpp | 10 ++++++++++ source/Library/SysEnv.cpp | 39 ++++++++++++++------------------------ source/Library/SysPath.cpp | 18 ++---------------- 4 files changed, 53 insertions(+), 41 deletions(-) diff --git a/source/Base/Shared.cpp b/source/Base/Shared.cpp index cd0590ae..1d5343d1 100644 --- a/source/Base/Shared.cpp +++ b/source/Base/Shared.cpp @@ -96,6 +96,33 @@ Function & NullFunction() return f; } +// ------------------------------------------------------------------------------------------------ +Object BufferToStrObj(const Buffer & b) +{ + // Obtain the initial stack size + const StackGuard sg(DefaultVM::Get()); + // Push the string onto the stack + sq_pushstring(DefaultVM::Get(), b.Data(), b.Position()); + // Obtain the object from the stack and return it + return Var< Object >(DefaultVM::Get(), -1).value; +} + +// -------------------------------------------------------------------------------------------- +Object BufferToStrObj(const Buffer & b, Uint32 size) +{ + // Perform a range check on the specified buffer + if (size > b.Capacity()) + { + SqThrowF("The specified buffer size is out of range: %u >= %u", size, b.Capacity()); + } + // Obtain the initial stack size + const StackGuard sg(DefaultVM::Get()); + // Push the string onto the stack + sq_pushstring(DefaultVM::Get(), b.Data(), size); + // Obtain the object from the stack and return it + return Var< Object >(DefaultVM::Get(), -1).value; +} + // -------------------------------------------------------------------------------------------- bool SToB(CSStr str) { diff --git a/source/Base/Shared.hpp b/source/Base/Shared.hpp index 9a06f9a5..545d84a4 100644 --- a/source/Base/Shared.hpp +++ b/source/Base/Shared.hpp @@ -239,6 +239,16 @@ Array & NullArray(); */ Function & NullFunction(); +/* ------------------------------------------------------------------------------------------------ + * Create a script string object from a buffer. +*/ +Object BufferToStrObj(const Buffer & b); + +/* ------------------------------------------------------------------------------------------------ + * Create a script string object from a portion of a buffer. +*/ +Object BufferToStrObj(const Buffer & b, Uint32 size); + /* ------------------------------------------------------------------------------------------------ * Create a script object from the specified value on the default VM. */ diff --git a/source/Library/SysEnv.cpp b/source/Library/SysEnv.cpp index 95c15415..2e2bfa22 100644 --- a/source/Library/SysEnv.cpp +++ b/source/Library/SysEnv.cpp @@ -1077,17 +1077,6 @@ Buffer SysEnv::NullDir() return std::move(b); } -// ------------------------------------------------------------------------------------------------ -static Object BufferToObj(const Buffer & b) -{ - // Obtain the initial stack size - const StackGuard sg(DefaultVM::Get()); - // Push the string onto the stack - sq_pushstring(DefaultVM::Get(), b.Data(), b.Position()); - // Obtain the object from the stack and return it - return Var< Object >(DefaultVM::Get(), -1).value; -} - // ------------------------------------------------------------------------------------------------ static bool SqEnv_Has(CCStr name) { @@ -1097,13 +1086,13 @@ static bool SqEnv_Has(CCStr name) // ------------------------------------------------------------------------------------------------ static Object SqEnv_Get(CCStr name) { - return BufferToObj(SysEnv::Get(name, nullptr)); + return BufferToStrObj(SysEnv::Get(name, nullptr)); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_GetOr(CCStr name, CCStr fallback) { - return BufferToObj(SysEnv::Get(name, fallback)); + return BufferToStrObj(SysEnv::Get(name, fallback)); } // ------------------------------------------------------------------------------------------------ @@ -1115,73 +1104,73 @@ static void SqEnv_Set(CCStr name, CCStr value) // ------------------------------------------------------------------------------------------------ static Object SqEnv_ExpandVars(CCStr str) { - return BufferToObj(SysEnv::ExpandVars(str)); + return BufferToStrObj(SysEnv::ExpandVars(str)); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_ExpandPath(CCStr path) { - return BufferToObj(SysEnv::ExpandPath(path)); + return BufferToStrObj(SysEnv::ExpandPath(path)); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_WorkingDir() { - return BufferToObj(SysEnv::WorkingDir()); + return BufferToStrObj(SysEnv::WorkingDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_HomeDir() { - return BufferToObj(SysEnv::HomeDir()); + return BufferToStrObj(SysEnv::HomeDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_ConfigHomeDir() { - return BufferToObj(SysEnv::ConfigHomeDir()); + return BufferToStrObj(SysEnv::ConfigHomeDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_DataHomeDir() { - return BufferToObj(SysEnv::DataHomeDir()); + return BufferToStrObj(SysEnv::DataHomeDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_TempHomeDir() { - return BufferToObj(SysEnv::TempHomeDir()); + return BufferToStrObj(SysEnv::TempHomeDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_CacheHomeDir() { - return BufferToObj(SysEnv::CacheHomeDir()); + return BufferToStrObj(SysEnv::CacheHomeDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_TempDir() { - return BufferToObj(SysEnv::TempDir()); + return BufferToStrObj(SysEnv::TempDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_ConfigDir() { - return BufferToObj(SysEnv::ConfigDir()); + return BufferToStrObj(SysEnv::ConfigDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_SystemDir() { - return BufferToObj(SysEnv::SystemDir()); + return BufferToStrObj(SysEnv::SystemDir()); } // ------------------------------------------------------------------------------------------------ static Object SqEnv_NullDir() { - return BufferToObj(SysEnv::NullDir()); + return BufferToStrObj(SysEnv::NullDir()); } // ================================================================================================ diff --git a/source/Library/SysPath.cpp b/source/Library/SysPath.cpp index 03c477c4..7b51d28c 100644 --- a/source/Library/SysPath.cpp +++ b/source/Library/SysPath.cpp @@ -278,14 +278,7 @@ Int32 SysPath::Cmp(const SysPath & o) const // ------------------------------------------------------------------------------------------------ Object SysPath::ToString() const { - // Transform the path components into a string - Buffer b(ToBuffer()); - // Obtain the initial stack size - const StackGuard sg(DefaultVM::Get()); - // Push the string onto the stack - sq_pushstring(DefaultVM::Get(), b.Data(), b.Position()); - // Obtain the object from the stack and return it - return Var< Object >(DefaultVM::Get(), -1).value; + return BufferToStrObj(ToBuffer()); } // ------------------------------------------------------------------------------------------------ @@ -687,14 +680,7 @@ Buffer SysPath::ToBuffer(Style style) const // ------------------------------------------------------------------------------------------------ Object SysPath::ToStr(Int32 style) const { - // Transform the path components into a string - Buffer b(ToBuffer(static_cast< Style >(style))); - // Obtain the initial stack size - const StackGuard sg(DefaultVM::Get()); - // Push the string onto the stack - sq_pushstring(DefaultVM::Get(), b.Data(), b.Position()); - // Obtain the object from the stack and return it - return Var< Object >(DefaultVM::Get(), -1).value; + return BufferToStrObj(ToBuffer(static_cast< Style >(style))); } // ------------------------------------------------------------------------------------------------