1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

WIP native string container.

This commit is contained in:
Sandu Liviu Catalin 2021-03-28 22:36:47 +03:00
parent 8d1b6576f3
commit 9278d92603
4 changed files with 622 additions and 0 deletions

View File

@ -84,6 +84,7 @@ add_library(SqModule MODULE SqBase.hpp Main.cpp
Library/Utils.cpp Library/Utils.hpp
Library/Utils/Announce.cpp Library/Utils/Announce.hpp
Library/Utils/Map.cpp Library/Utils/Map.hpp
Library/Utils/String.cpp Library/Utils/String.hpp
Library/Utils/Vector.cpp Library/Utils/Vector.hpp
Library/ZMQ.cpp Library/ZMQ.hpp
# Misc

View File

@ -85,6 +85,7 @@ static SQInteger SqExtractIPv4(HSQUIRRELVM vm)
// ------------------------------------------------------------------------------------------------
extern void Register_Map(HSQUIRRELVM vm, Table & ns);
extern void Register_Vector(HSQUIRRELVM vm, Table & ns);
extern void Register_Native_String(HSQUIRRELVM vm, Table & ns);
extern void Register_ServerAnnouncer(HSQUIRRELVM vm, Table & ns);
// ================================================================================================
@ -94,6 +95,7 @@ void Register_Utils(HSQUIRRELVM vm)
Register_Map(vm, ns);
Register_Vector(vm, ns);
Register_Native_String(vm, ns);
Register_ServerAnnouncer(vm, ns);
ns.SquirrelFunc(_SC("ExtractIPv4"), &SqExtractIPv4);

View File

@ -0,0 +1,64 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Utils/String.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMOD_DECL_TYPENAME(Typename, _SC("SqString"))
// ================================================================================================
void Register_Native_String(HSQUIRRELVM vm, Table & ns)
{
RootTable(vm).Bind(Typename::Str,
Class< SqString >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< StackStrF & >()
// Meta-methods
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
// Properties
.Prop(_SC("Front"), &SqString::Front)
.Prop(_SC("Back"), &SqString::Back)
.Prop(_SC("Empty"), &SqString::Empty)
.Prop(_SC("Size"), &SqString::Size, &SqString::Resize)
.Prop(_SC("Capacity"), &SqString::Capacity, &SqString::Reserve)
.Prop(_SC("Sorted"), &SqString::IsSorted)
// Member Methods
.Func(_SC("Get"), &SqString::Get)
.Func(_SC("Set"), &SqString::Set)
.Func(_SC("Resize"), &SqString::ResizeEx)
.Func(_SC("Reserve"), &SqString::Reserve)
.Func(_SC("Compact"), &SqString::Compact)
.Func(_SC("Clear"), &SqString::Clear)
.Func(_SC("Push"), &SqString::Push)
.Func(_SC("Append"), &SqString::Append)
.Func(_SC("Extend"), &SqString::Extend)
.Func(_SC("Pop"), &SqString::Pop)
.Func(_SC("EraseAt"), &SqString::EraseAt)
.Func(_SC("EraseFrom"), &SqString::EraseFrom)
.Func(_SC("EraseValue"), &SqString::EraseValue)
.Func(_SC("InsertAt"), &SqString::InsertAt)
.Func(_SC("Insert"), &SqString::Insert)
.Func(_SC("Locate"), &SqString::Locate)
.Func(_SC("LocateFrom"), &SqString::LocateFrom)
.Func(_SC("Find"), &SqString::Find)
.Func(_SC("FindFrom"), &SqString::FindFrom)
.Func(_SC("Count"), &SqString::Count)
.Func(_SC("Equal"), &SqString::Equal)
.Func(_SC("Slice"), &SqString::Slice)
.Func(_SC("Each"), &SqString::Each)
.Func(_SC("EachRange"), &SqString::EachRange)
.Func(_SC("While"), &SqString::While)
.Func(_SC("WhileRange"), &SqString::WhileRange)
.Func(_SC("Reverse"), &SqString::Reverse)
.Func(_SC("Generate"), &SqString::Generate)
.Func(_SC("GenerateSome"), &SqString::GenerateSome)
.Func(_SC("GenerateFrom"), &SqString::GenerateFrom)
.Func(_SC("GenerateBetween"), &SqString::GenerateBetween)
.Func(_SC("Sort"), &SqString::Sort)
.Func(_SC("Shuffle"), &SqString::Shuffle)
);
}
} // Namespace:: SqMod

View File

@ -0,0 +1,555 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Core/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <string>
#include <random>
#include <iterator>
#include <algorithm>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Wrapper around a std::string.
*/
struct SqString
{
/* --------------------------------------------------------------------------------------------
* String instance.
*/
String mS{};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
SqString() = default;
/* --------------------------------------------------------------------------------------------
* Construct with initial capacity. No element is created.
*/
explicit SqString(SQInteger n)
: mS()
{
mS.reserve(ClampL< SQInteger, size_t >(n));
}
/* --------------------------------------------------------------------------------------------
* Construct with initial string.
*/
explicit SqString(StackStrF & s)
: mS(s.mPtr, s.GetSize())
{
}
/* --------------------------------------------------------------------------------------------
* Construct with initial size. Filled with copies of specified element.
*/
SqString(SQInteger n, String::value_type v)
: mS()
{
mS.resize(ClampL< SQInteger, size_t >(n), v);
}
/* --------------------------------------------------------------------------------------------
* Copy constructor from reference.
*/
explicit SqString(const String & s) // NOLINT(modernize-pass-by-value)
: mS(s)
{
}
/* --------------------------------------------------------------------------------------------
* Move constructor from reference.
*/
explicit SqString(String && s) noexcept
: mS(std::move(s))
{
}
/* --------------------------------------------------------------------------------------------
* Construct with character range.
*/
SqString(String::iterator f, String::iterator l)
: mS(f, l)
{
}
/* --------------------------------------------------------------------------------------------
* Construct with character range.
*/
SqString(String::const_iterator f, String::const_iterator l)
: mS(f, l)
{
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
SqString(const SqString & s) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
SqString(SqString &&) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Destroys the Statement.
*/
~SqString() = default;
/* --------------------------------------------------------------------------------------------
* Assignment operator.
*/
SqString & operator = (const SqString &) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment.
*/
SqString & operator = (SqString &&) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Make sure an index is within range and return the container.
*/
SQMOD_NODISCARD String & ValidIdx(SQInteger i)
{
if (static_cast< size_t >(i) >= mS.size())
{
STHROWF("Invalid string container index({})", i);
}
return mS;
}
/* --------------------------------------------------------------------------------------------
* Make sure an index is within range and return the container.
*/
SQMOD_NODISCARD const String & ValidIdx(SQInteger i) const
{
if (static_cast< size_t >(i) >= mS.size())
{
STHROWF("Invalid string container index({})", i);
}
return mS;
}
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and is populated, then return it.
*/
SQMOD_NODISCARD String & ValidPop()
{
if (mS.empty())
{
STHROWF("String container is empty");
}
return mS;
}
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and is populated, then return it.
*/
SQMOD_NODISCARD const String & ValidPop() const
{
if (mS.empty())
{
STHROWF("String container is empty");
}
return mS;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a value from the container.
*/
SQMOD_NODISCARD SQInteger Get(SQInteger i) const
{
return ValidIdx(i)[ClampL< SQInteger, size_t >(i)];
}
/* --------------------------------------------------------------------------------------------
* Modify a value from the container.
*/
void Set(SQInteger i, String::value_type v)
{
ValidIdx(i)[i] = v;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the first element in the container.
*/
SQMOD_NODISCARD SQInteger Front() const
{
return ValidPop().front();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the last element in the container.
*/
SQMOD_NODISCARD SQInteger Back() const
{
return ValidPop().back();
}
/* --------------------------------------------------------------------------------------------
* Check if the container has no elements.
*/
SQMOD_NODISCARD bool Empty() const
{
return mS.empty();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the number of elements in the container.
*/
SQMOD_NODISCARD SQInteger Size() const
{
return static_cast< SQInteger >(mS.size());
}
/* --------------------------------------------------------------------------------------------
* Retrieve the number of elements that the container has currently allocated space for.
*/
SQMOD_NODISCARD SQInteger Capacity() const
{
return static_cast< SQInteger >(mS.capacity());
}
/* --------------------------------------------------------------------------------------------
* Resize the container to contain a specific amount of elements.
*/
void Resize(SQInteger n)
{
mS.resize(ClampL< SQInteger, size_t >(n));
}
/* --------------------------------------------------------------------------------------------
* Resize the container to contain a specific amount of elements.
*/
SqString & ResizeEx(SQInteger n, String::value_type v)
{
mS.resize(ClampL< SQInteger, size_t >(n), v);
return *this;
}
/* --------------------------------------------------------------------------------------------
* Increase the capacity of the container to a value that's greater or equal to the one specified.
*/
SqString & Reserve(SQInteger n)
{
mS.reserve(ClampL< SQInteger, size_t >(n));
return *this;
}
/* --------------------------------------------------------------------------------------------
* Request the removal of unused capacity.
*/
void Compact()
{
mS.shrink_to_fit();
}
/* --------------------------------------------------------------------------------------------
* Erase all elements from the container.
*/
void Clear()
{
mS.clear();
}
/* --------------------------------------------------------------------------------------------
* Push a value at the back of the container.
*/
void Push(String::value_type v)
{
mS.push_back(v);
}
/* --------------------------------------------------------------------------------------------
* Push copies of a value at the back of the container.
*/
void Append(SQInteger n, String::value_type v)
{
mS.append(static_cast< size_t >(n), v);
}
/* --------------------------------------------------------------------------------------------
* Extends the Container by appending all the items in the given container.
*/
void Extend(StackStrF & s)
{
mS.append(s.mPtr, s.GetSize());
}
/* --------------------------------------------------------------------------------------------
* Pop the last element in the container.
*/
void Pop()
{
ValidPop().pop_back();
}
/* --------------------------------------------------------------------------------------------
* Erase the element at a certain position.
*/
void EraseAt(SQInteger i)
{
mS.erase(ValidIdx(i).begin() + static_cast< size_t >(i));
}
/* --------------------------------------------------------------------------------------------
* Erase a certain amount of elements starting from a specific position.
*/
void EraseFrom(SQInteger i, SQInteger n)
{
mS.erase(ValidIdx(i).begin() + static_cast< size_t >(i),
ValidIdx(i + n).begin() + static_cast< size_t >(i + n));
}
/* --------------------------------------------------------------------------------------------
* Erase all occurrences of value from the container.
*/
void EraseValue(String::value_type v)
{
mS.erase(std::remove(mS.begin(), mS.end(), v), mS.end());
}
/* --------------------------------------------------------------------------------------------
* Insert a specific value starting from a certain position.
*/
void InsertAt(SQInteger i, String::value_type v)
{
mS.insert(ValidIdx(i).begin() + static_cast< size_t >(i), v);
}
/* --------------------------------------------------------------------------------------------
* Insert a certain amount of copies of a value starting from a specific position.
*/
void Insert(SQInteger i, SQInteger n, String::value_type v)
{
mS.insert(ValidIdx(i).begin() + static_cast< size_t >(i), ClampL< SQInteger, size_t >(n), v);
}
/* --------------------------------------------------------------------------------------------
* Locate the position of a value.
*/
SQMOD_NODISCARD SQInteger Locate(String::value_type v) const
{
return static_cast< SQInteger >(mS.find(v));
}
/* --------------------------------------------------------------------------------------------
* Locate the position of a value starting from an offset.
*/
SQMOD_NODISCARD SQInteger LocateFrom(SQInteger p, String::value_type v) const
{
return static_cast< SQInteger >(mS.find(v, static_cast< size_t >(p)));
}
/* --------------------------------------------------------------------------------------------
* Find the position of a sub-string.
*/
SQMOD_NODISCARD SQInteger Find(StackStrF & s) const
{
return static_cast< SQInteger >(mS.find(s.mPtr));
}
/* --------------------------------------------------------------------------------------------
* Find the position of a sub-string starting from an offset.
*/
SQMOD_NODISCARD SQInteger FindFrom(SQInteger p, StackStrF & s) const
{
return static_cast< SQInteger >(mS.find(s.mPtr, static_cast< size_t >(p), s.GetSize()));
}
/* --------------------------------------------------------------------------------------------
* Count the occurrences of a value in the container.
*/
SQMOD_NODISCARD SQInteger Count(String::value_type v) const
{
return static_cast< SQInteger >(std::count(mS.begin(), mS.end(), v));
}
/* --------------------------------------------------------------------------------------------
* See if values are the same as another container.
*/
SQMOD_NODISCARD bool Equal(SqString & o) const { return mS == o.mS; }
/* --------------------------------------------------------------------------------------------
* Retrieve a portion of this container.
*/
SQMOD_NODISCARD LightObj Slice(SQInteger p, SQInteger n) const
{
return LightObj(SqTypeIdentity< SqString >{}, SqVM(),
ValidIdx(p).begin() + static_cast< size_t >(p),
ValidIdx(p + n).begin() + static_cast< size_t >(p + n));
}
/* --------------------------------------------------------------------------------------------
* Iterate all values through a functor.
*/
void Each(Function & fn) const
{
for (const auto & e : mS)
{
fn.Execute(e);
}
}
/* --------------------------------------------------------------------------------------------
* Iterate values in range through a functor.
*/
void EachRange(SQInteger p, SQInteger n, Function & fn) const
{
std::for_each(ValidIdx(p).begin() + static_cast< size_t >(p),
ValidIdx(p + n).begin() + static_cast< size_t >(p + n),
[&](String::const_reference e) {
fn.Execute(e);
});
}
/* --------------------------------------------------------------------------------------------
* Iterate all values through a functor until stopped (i.e false is returned).
*/
void While(Function & fn) const
{
for (const auto & e : mS)
{
auto ret = fn.Eval(e);
// (null || true) == continue & false == break
if (!ret.IsNull() || !ret.template Cast< bool >())
{
break;
}
}
}
/* --------------------------------------------------------------------------------------------
* Iterate values in range through a functor until stopped (i.e false is returned).
*/
void WhileRange(SQInteger p, SQInteger n, Function & fn) const
{
auto itr = ValidIdx(p).begin() + static_cast< size_t >(p);
auto end = ValidIdx(p + n).begin() + static_cast< size_t >(p + n);
for (; itr != end; ++itr)
{
auto ret = fn.Eval(*itr);
// (null || true) == continue & false == break
if (!ret.IsNull() || !ret.template Cast< bool >())
{
break;
}
}
}
/* --------------------------------------------------------------------------------------------
* Reverse the order of elements in the container.
*/
SqString & Reverse()
{
std::reverse(mS.begin(), mS.end());
return *this;
}
/* --------------------------------------------------------------------------------------------
* Generate new elements at the back of the container.
*/
SqString & Generate(LightObj & ctx, Function & fn)
{
for (;;)
{
auto ret = fn.Eval(ctx);
// null == break
if (ret.IsNull())
{
break;
}
// Extract the value from object
mS.push_back(ret.Cast< String::value_type >());
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Generate new elements at the back of the container.
*/
SqString & GenerateSome(SQInteger n, LightObj & ctx, Function & fn)
{
while (n--)
{
auto ret = fn.Eval(ctx);
// Extract the value from object
mS.push_back(ret.Cast< String::value_type >());
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Generate new elements at specified position.
*/
SqString & GenerateFrom(SQInteger p, SQInteger n, LightObj & ctx, Function & fn)
{
if (static_cast< size_t >(p) >= mS.size())
{
STHROWF("Invalid container index ({} >= {})", p, mS.size());
}
for (auto i = static_cast< size_t >(p); n--; ++i)
{
auto ret = fn.Eval(ctx);
// Extract the value from object and insert it
mS.insert(mS.begin() + i, ret.Cast< String::value_type >());
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Generate new elements at specified position.
*/
SqString & GenerateBetween(SQInteger p, SQInteger n, LightObj & ctx, Function & fn)
{
if (static_cast< size_t >(p) >= mS.size())
{
STHROWF("Invalid container index ({} >= {})", p, mS.size());
}
else if (static_cast< size_t >(p + n) >= mS.size())
{
STHROWF("Invalid container index ({} >= {})", p + n, mS.size());
}
for (n = (p + n); p <= n; ++p)
{
auto ret = fn.Eval(ctx);
// Extract the value from object and assign it
mS.at(static_cast< size_t >(p)) = ret.Cast< String::value_type >();
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Sort the elements from the container.
*/
SqString & Sort()
{
std::sort(mS.begin(), mS.end());
return *this;
}
/* --------------------------------------------------------------------------------------------
* Check if the elements from the container are sorted.
*/
SQMOD_NODISCARD bool IsSorted()
{
return std::is_sorted(mS.begin(), mS.end());
}
/* --------------------------------------------------------------------------------------------
* Shuffle the elements from the container.
*/
SqString & Shuffle()
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(mS.begin(), mS.end(), g);
return *this;
}
};
} // Namespace:: SqMod