2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-09-30 02:56:11 +02:00
|
|
|
#include "Library/String.hpp"
|
2021-01-30 07:51:39 +01:00
|
|
|
#include "Core/Utility.hpp"
|
|
|
|
#include "Core/Buffer.hpp"
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <algorithm>
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static LightObj SqLeftStr(SQChar f, uint32_t w, StackStrF & s)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2020-04-30 20:03:15 +02:00
|
|
|
return LightObj(_SC(""), 0); // Default to an empty string!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Allocate a buffer with the requested width
|
2016-03-21 21:37:58 +01:00
|
|
|
Buffer b(w + 1); // + null terminator
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-11-17 16:05:12 +01:00
|
|
|
if (!s.mLen)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::memset(b.Data(), f, w); // Insert only the fill character
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Insert only the fill character first
|
2016-03-22 01:07:34 +01:00
|
|
|
std::memset(b.Data(), f, w);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Overwrite with the specified string
|
2016-11-17 16:05:12 +01:00
|
|
|
std::strncpy(b.Data(), s.mPtr, s.mLen);
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), w);
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static LightObj SqLeftOffsetStr(SQChar f, uint32_t w, uint32_t o, StackStrF & s)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2020-04-30 20:03:15 +02:00
|
|
|
return LightObj(_SC(""), 0); // Default to an empty string!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Is the specified offset within width range?
|
|
|
|
else if (o > w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("Offset is out of bounds");
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Allocate a buffer with the requested width
|
2016-03-21 21:37:58 +01:00
|
|
|
Buffer b(w + 1); // + null terminator
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-11-17 16:05:12 +01:00
|
|
|
if (!s.mLen)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::memset(b.Data(), f, w); // Insert only the fill character
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-25 00:41:30 +01:00
|
|
|
// Calculate the string length
|
2021-01-30 07:51:39 +01:00
|
|
|
const uint32_t n = ConvTo< uint32_t >::From(s.mLen);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Insert the fill character first
|
2016-03-22 01:07:34 +01:00
|
|
|
std::memset(b.Data(), f, w);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Overwrite with the specified string
|
2016-03-25 00:41:30 +01:00
|
|
|
if (n > (w - o))
|
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::strncpy(b.Data() + o, s.mPtr, n);
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::memcpy(b.Data() + o, s.mPtr, n * sizeof(SQChar));
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), w);
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static LightObj SqRightStr(SQChar f, uint32_t w, StackStrF & s)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2020-04-30 20:03:15 +02:00
|
|
|
return LightObj(_SC(""), 0); // Default to an empty string!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Allocate a buffer with the requested width
|
2016-03-21 21:37:58 +01:00
|
|
|
Buffer b(w + 1); // + null terminator
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-11-17 16:05:12 +01:00
|
|
|
if (!s.mLen)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::memset(b.Data(), f, w); // Insert only the fill character
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
2021-01-30 07:51:39 +01:00
|
|
|
const uint32_t n = ConvTo< uint32_t >::From(s.mLen);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Insert the fill character first
|
2016-03-22 01:07:34 +01:00
|
|
|
std::memset(b.Data(), f, w);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Overwrite with the specified string
|
|
|
|
if (n >= w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::strncpy(b.Data(), s.mPtr, w);
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
else
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::strncpy(b.Data() + (w - n), s.mPtr, n);
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), w);
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:41:30 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static LightObj SqRightOffsetStr(SQChar f, uint32_t w, uint32_t o, StackStrF & s)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2020-04-30 20:03:15 +02:00
|
|
|
return LightObj(_SC(""), 0); // Default to an empty string!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Is the specified offset within width range?
|
|
|
|
else if (o > w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("Offset is out of bounds");
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Allocate a buffer with the requested width
|
2016-03-21 21:37:58 +01:00
|
|
|
Buffer b(w + 1); // + null terminator
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-11-17 16:05:12 +01:00
|
|
|
if (!s.mLen)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::memset(b.Data(), f, w); // Insert only the fill character
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
2021-01-30 07:51:39 +01:00
|
|
|
const uint32_t n = ConvTo< uint32_t >::From(s.mLen);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Insert the fill character first
|
2016-03-22 01:07:34 +01:00
|
|
|
std::memset(b.Data(), f, w);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Overwrite with the specified string
|
|
|
|
if (n >= w || (n + o) >= w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::strncpy(b.Data(), s.mPtr, w - o);
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
else
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::strncpy(b.Data() + ((w - n) - o), s.mPtr, n);
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), w);
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static LightObj SqCenterStr(SQChar f, uint32_t w, StackStrF & s)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2020-04-30 20:03:15 +02:00
|
|
|
return LightObj(_SC(""), 0); // Default to an empty string!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Allocate a buffer with the requested width
|
2016-03-21 21:37:58 +01:00
|
|
|
Buffer b(w + 1); // + null terminator
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-11-17 16:05:12 +01:00
|
|
|
if (!s.mLen)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
std::memset(b.Data(), f, w); // Insert only the fill character
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-04-03 22:23:01 +02:00
|
|
|
// Calculate the insert position
|
2021-01-30 07:51:39 +01:00
|
|
|
const int32_t p = ((w/2) - (s.mLen/2));
|
2016-03-10 04:57:13 +01:00
|
|
|
// Insert only the fill character first
|
2016-03-22 01:07:34 +01:00
|
|
|
std::memset(b.Data(), f, w);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Overwrite with the specified string
|
2016-11-17 16:05:12 +01:00
|
|
|
std::strncpy(b.Data() + (p < 0 ? 0 : p), s.mPtr, s.mLen);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), w);
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:41:30 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static Buffer StrJustAlphaNumImpl(const SQChar * str, uint32_t len)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-11-17 16:05:12 +01:00
|
|
|
if(!len)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-03-25 00:50:05 +01:00
|
|
|
return Buffer(); // Default to an empty buffer!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
// Obtain a temporary buffer
|
2016-11-17 16:05:12 +01:00
|
|
|
Buffer b(len + 1); // + null terminator
|
2016-02-20 23:25:00 +01:00
|
|
|
// Resulted string size
|
2021-01-30 07:51:39 +01:00
|
|
|
uint32_t n = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Currently processed character
|
2021-01-30 07:51:39 +01:00
|
|
|
SQChar c;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Process characters
|
2016-03-21 21:37:58 +01:00
|
|
|
while ((c = *(str++)) != '\0')
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
// Is this an alpha-numeric character?
|
2016-03-22 01:07:34 +01:00
|
|
|
if (std::isalnum(c) != 0)
|
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
// Save it and move to the next one
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n++) = c;
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// End the resulted string
|
2016-03-22 01:07:34 +01:00
|
|
|
b.At(n) = '\0';
|
2016-03-25 00:50:05 +01:00
|
|
|
// Move the cursor to the end
|
|
|
|
b.Move(n);
|
|
|
|
// Return ownership of the buffer
|
2021-01-27 06:27:48 +01:00
|
|
|
return b;
|
2016-03-25 00:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
Buffer StrJustAlphaNumB(const SQChar * str)
|
2016-03-25 00:50:05 +01:00
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
|
|
|
if(!str || *str == '\0')
|
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
return Buffer(); // Default to an empty string!
|
2016-03-25 00:50:05 +01:00
|
|
|
}
|
|
|
|
// Attempt to convert and return the result
|
2016-11-17 16:05:12 +01:00
|
|
|
return StrJustAlphaNumImpl(str, std::strlen(str));
|
2016-03-25 00:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * StrJustAlphaNum(const SQChar * str)
|
2016-03-25 00:50:05 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
// See if we actually have something to search for
|
|
|
|
if(!str || *str == '\0')
|
2016-03-25 00:50:05 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
return _SC(""); // Default to an empty string!
|
2016-03-25 00:50:05 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Attempt to convert and return the result
|
|
|
|
return StrJustAlphaNumImpl(str, std::strlen(str)).Get< SQChar >();
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-02-17 16:23:59 +01:00
|
|
|
static LightObj SqJustAlphaNum(StackStrF & s)
|
2016-11-17 16:05:12 +01:00
|
|
|
{
|
|
|
|
const Buffer b(StrJustAlphaNumImpl(s.mPtr, s.mLen));
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), b.Position());
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static Buffer StrToLowercaseImpl(const SQChar * str, uint32_t len)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-11-17 16:05:12 +01:00
|
|
|
if(!len)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-03-25 00:41:30 +01:00
|
|
|
return Buffer(); // Default to an empty buffer!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
// Obtain a temporary buffer
|
2016-11-17 16:05:12 +01:00
|
|
|
Buffer b(len + 1); // + null terminator
|
2016-02-20 23:25:00 +01:00
|
|
|
// Resulted string size
|
2021-01-30 07:51:39 +01:00
|
|
|
uint32_t n = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Process characters
|
2016-03-25 00:44:55 +01:00
|
|
|
while (*str != '\0')
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
// Convert it and move to the next one
|
2016-03-25 00:44:55 +01:00
|
|
|
b.At(n++) = std::tolower(*(str++));
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
// End the resulted string
|
2016-03-22 01:07:34 +01:00
|
|
|
b.At(n) = '\0';
|
2016-03-25 00:41:30 +01:00
|
|
|
// Move the cursor to the end
|
|
|
|
b.Move(n);
|
|
|
|
// Return ownership of the buffer
|
2021-01-27 06:27:48 +01:00
|
|
|
return b;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
2016-02-21 09:30:47 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
Buffer StrToLowercaseB(const SQChar * str)
|
2016-02-21 09:30:47 +01:00
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-03-22 01:07:34 +01:00
|
|
|
if(!str || *str == '\0')
|
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
return Buffer(); // Default to an empty string!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-25 00:41:30 +01:00
|
|
|
// Attempt to convert and return the result
|
2016-11-17 16:05:12 +01:00
|
|
|
return StrToLowercaseImpl(str, std::strlen(str));
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * StrToLowercase(const SQChar * str)
|
2016-03-25 00:41:30 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
// See if we actually have something to search for
|
|
|
|
if(!str || *str == '\0')
|
2016-03-25 00:41:30 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
return _SC(""); // Default to an empty string!
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Attempt to convert and return the result
|
|
|
|
return StrToLowercaseImpl(str, std::strlen(str)).Get< SQChar >();
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-02-17 16:23:59 +01:00
|
|
|
static LightObj SqToLowercase(StackStrF & s)
|
2016-11-17 16:05:12 +01:00
|
|
|
{
|
|
|
|
const Buffer b(StrToLowercaseImpl(s.mPtr, s.mLen));
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), b.Position());
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static Buffer StrToUppercaseImpl(const SQChar * str, uint32_t len)
|
2016-03-25 00:41:30 +01:00
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-11-17 16:05:12 +01:00
|
|
|
if(!len)
|
2016-03-25 00:41:30 +01:00
|
|
|
{
|
|
|
|
return Buffer(); // Default to an empty buffer!
|
|
|
|
}
|
2016-02-21 09:30:47 +01:00
|
|
|
// Obtain a temporary buffer
|
2016-11-17 16:05:12 +01:00
|
|
|
Buffer b(len + 1); // + null terminator
|
2016-02-21 09:30:47 +01:00
|
|
|
// Resulted string size
|
2021-01-30 07:51:39 +01:00
|
|
|
uint32_t n = 0;
|
2016-02-21 09:30:47 +01:00
|
|
|
// Process characters
|
2016-03-25 00:41:30 +01:00
|
|
|
while (*str != '\0')
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-02-21 09:30:47 +01:00
|
|
|
// Convert it and move to the next one
|
2016-03-25 00:41:30 +01:00
|
|
|
b.At(n++) = std::toupper(*(str++));
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-21 09:30:47 +01:00
|
|
|
// End the resulted string
|
2016-03-22 01:07:34 +01:00
|
|
|
b.At(n) = '\0';
|
2016-03-25 00:41:30 +01:00
|
|
|
// Move the cursor to the end
|
|
|
|
b.Move(n);
|
|
|
|
// Return ownership of the buffer
|
2021-01-27 06:27:48 +01:00
|
|
|
return b;
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
Buffer StrToUppercaseB(const SQChar * str)
|
2016-03-25 00:41:30 +01:00
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
|
|
|
if(!str || *str == '\0')
|
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
return Buffer(); // Default to an empty string!
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
// Attempt to convert and return the result
|
2016-11-17 16:05:12 +01:00
|
|
|
return StrToUppercaseImpl(str, std::strlen(str));
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * StrToUppercase(const SQChar * str)
|
2016-03-25 00:41:30 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
// See if we actually have something to search for
|
|
|
|
if(!str || *str == '\0')
|
2016-03-25 00:41:30 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
return _SC(""); // Default to an empty string!
|
2016-03-25 00:41:30 +01:00
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Attempt to convert and return the result
|
|
|
|
return StrToUppercaseImpl(str, std::strlen(str)).Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-02-17 16:23:59 +01:00
|
|
|
static LightObj SqToUppercase(StackStrF & s)
|
2016-11-17 16:05:12 +01:00
|
|
|
{
|
|
|
|
const Buffer b(StrToUppercaseImpl(s.mPtr, s.mLen));
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// Push the string onto the stack
|
|
|
|
sq_pushstring(s.mVM, b.Data(), b.Position());
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 23:25:52 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-09-15 02:03:06 +02:00
|
|
|
* Checks if all the characters in the specified string are of the specified class or not.
|
2016-03-24 23:25:52 +01:00
|
|
|
*/
|
2019-02-17 16:23:59 +01:00
|
|
|
template < int (*Fn)(int) > static bool SqAllChars(StackStrF & s)
|
2016-03-24 23:25:52 +01:00
|
|
|
{
|
2016-09-15 02:03:06 +02:00
|
|
|
// See if we actually have something to search for
|
2016-11-17 16:05:12 +01:00
|
|
|
if (!s.mLen)
|
2016-09-15 02:03:06 +02:00
|
|
|
{
|
|
|
|
// Since there are no other different character then
|
|
|
|
// we count this as all characters being of this type
|
2016-11-17 16:05:12 +01:00
|
|
|
return true;
|
2016-09-15 02:03:06 +02:00
|
|
|
}
|
|
|
|
// Start iterating characters and find the first that doesn't match
|
2021-01-30 07:51:39 +01:00
|
|
|
for (const SQChar * itr = s.mPtr; *itr != '\0'; ++itr)
|
2016-09-15 02:03:06 +02:00
|
|
|
{
|
|
|
|
// Call the predicate with the current character
|
2016-11-17 16:05:12 +01:00
|
|
|
if (Fn(*itr) == 0)
|
2016-09-15 02:03:06 +02:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
return false; // This character did not pass the test
|
2016-09-15 02:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// All characters passed the test
|
2016-11-17 16:05:12 +01:00
|
|
|
return true;
|
2016-09-15 02:03:06 +02:00
|
|
|
}
|
2016-03-24 23:25:52 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-09-15 02:03:06 +02:00
|
|
|
* Find the position of the first character that matches the specified class.
|
2016-03-24 23:25:52 +01:00
|
|
|
*/
|
2019-02-17 16:23:59 +01:00
|
|
|
template < int (*Fn)(int), bool Neg > static LightObj SqFirstChar(StackStrF & s)
|
2016-03-24 23:25:52 +01:00
|
|
|
{
|
2016-09-15 02:03:06 +02:00
|
|
|
// See if we actually have something to search for
|
2016-11-17 16:05:12 +01:00
|
|
|
if (s.mLen)
|
2016-03-24 23:25:52 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
// Start iterating characters and find the first that doesn't match
|
2021-01-30 07:51:39 +01:00
|
|
|
for (const SQChar * itr = s.mPtr; *itr != '\0'; ++itr)
|
2016-03-24 23:25:52 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
// Call the predicate with the current character
|
|
|
|
if ((Fn(*itr) == 0) == Neg)
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// This character passed our test, push it's position
|
|
|
|
sq_pushinteger(s.mVM, itr - s.mPtr);
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
|
|
|
}
|
2016-03-24 23:25:52 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Since there are no other different character then
|
|
|
|
// we count this as all characters being of this type
|
|
|
|
return LightObj();
|
2016-09-15 02:03:06 +02:00
|
|
|
}
|
2016-03-24 23:25:52 +01:00
|
|
|
|
2016-09-15 02:03:06 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Find the position of the last character that matches the specified class.
|
|
|
|
*/
|
2019-02-17 16:23:59 +01:00
|
|
|
template < int (*Fn)(int), bool Neg > static LightObj SqLastChar(StackStrF & s)
|
2016-09-15 02:03:06 +02:00
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-11-17 16:05:12 +01:00
|
|
|
if (s.mLen)
|
2016-09-15 02:03:06 +02:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
// Start iterating characters and find the first that doesn't match
|
2021-01-30 07:51:39 +01:00
|
|
|
for (const SQChar * itr = (s.mPtr + s.mLen) - 1; itr >= s.mPtr; --itr)
|
2016-03-24 23:25:52 +01:00
|
|
|
{
|
2016-11-17 16:05:12 +01:00
|
|
|
// Call the predicate with the current character
|
|
|
|
if ((Fn(*itr) == 0) == Neg)
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg(s.mVM);
|
|
|
|
// This character passed our test, push it's position
|
|
|
|
sq_pushinteger(s.mVM, itr - s.mPtr);
|
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return std::move(Var< LightObj >(s.mVM, -1).value);
|
|
|
|
}
|
2016-03-24 23:25:52 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 16:05:12 +01:00
|
|
|
// Since there are no other different character then
|
|
|
|
// we count this as all characters being of this type
|
|
|
|
return LightObj();
|
2016-09-15 02:03:06 +02:00
|
|
|
}
|
2016-03-24 23:25:52 +01:00
|
|
|
|
2016-09-15 02:04:05 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Split the string into chunks wherever a character matches or not the specified class.
|
|
|
|
*/
|
|
|
|
static SQInteger SplitWhereCharImpl(HSQUIRRELVM vm, int(*fn)(int), bool neg)
|
|
|
|
{
|
|
|
|
static const SQInteger top = sq_gettop(vm);
|
|
|
|
// Is there a value to process?
|
|
|
|
if (top <= 1)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Missing string or value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to retrieve the value from the stack as a string
|
2018-07-29 23:58:27 +02:00
|
|
|
StackStrF val(vm, 2);
|
2016-09-15 02:04:05 +02:00
|
|
|
// Have we failed to retrieve the string?
|
2018-07-29 23:58:27 +02:00
|
|
|
if (SQ_FAILED(val.Proc(true)))
|
2016-09-15 02:04:05 +02:00
|
|
|
{
|
|
|
|
return val.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new array on the stack
|
|
|
|
sq_newarray(vm, 0);
|
|
|
|
|
|
|
|
// See if we actually have something to search for
|
|
|
|
if (!(val.mPtr) || *(val.mPtr) == '\0')
|
|
|
|
{
|
|
|
|
return 1; // Return an empty array
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember the position of the last found match
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * last = val.mPtr;
|
2016-09-15 02:04:05 +02:00
|
|
|
|
|
|
|
// Start iterating characters and slice where a match is found
|
2021-01-30 07:51:39 +01:00
|
|
|
for (const SQChar * itr = val.mPtr; *itr != '\0'; ++itr)
|
2016-09-15 02:04:05 +02:00
|
|
|
{
|
|
|
|
// Call the predicate with the current character
|
|
|
|
if ((fn(*itr) == 0) == neg)
|
|
|
|
{
|
|
|
|
// Are there any characters before this match?
|
2017-05-05 19:39:25 +02:00
|
|
|
if ((itr - last) > 0 && (*last != '\0'))
|
2016-09-15 02:04:05 +02:00
|
|
|
{
|
|
|
|
// Push this chunk of string on the stack
|
|
|
|
sq_pushstring(vm, last, itr - last);
|
|
|
|
// Insert this element into the array on the stack
|
|
|
|
const SQRESULT r = sq_arrayappend(vm, -2);
|
|
|
|
// Did we fail to append the element?
|
|
|
|
if (SQ_FAILED(r))
|
|
|
|
{
|
|
|
|
return r; // We're done here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Push this character as an integer on the stack
|
|
|
|
sq_pushinteger(vm, *itr);
|
|
|
|
// Insert this element into the array on the stack
|
|
|
|
const SQRESULT r = sq_arrayappend(vm, -2);
|
|
|
|
// Did we fail to append the element?
|
|
|
|
if (SQ_FAILED(r))
|
|
|
|
{
|
|
|
|
return r; // We're done here
|
|
|
|
}
|
|
|
|
// Update the position of the last found match
|
2016-09-15 02:42:26 +02:00
|
|
|
last = (itr + 1);
|
2016-09-15 02:04:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the remaining chunk, if any
|
2016-09-15 02:42:26 +02:00
|
|
|
if (*last != '\0')
|
2016-09-15 02:04:05 +02:00
|
|
|
{
|
|
|
|
// Push this chunk of string on the stack
|
|
|
|
sq_pushstring(vm, last, -1);
|
|
|
|
// Insert this element into the array on the stack
|
|
|
|
const SQRESULT r = sq_arrayappend(vm, -2);
|
|
|
|
// Did we fail to append the element?
|
|
|
|
if (SQ_FAILED(r))
|
|
|
|
{
|
|
|
|
return r; // We're done here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a value to return on the stack
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Simple forwarder to minimize templated functions with large body and reduce executable size.
|
|
|
|
*/
|
|
|
|
template < int (*Fn)(int), bool Neg > static SQInteger SplitWhereCharProxy(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
return SplitWhereCharImpl(vm, Fn, Neg);
|
|
|
|
}
|
|
|
|
|
2016-09-15 02:03:06 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Checks if the specified character is of the specified class.
|
|
|
|
*/
|
|
|
|
template < int (*Fn)(int) > static bool IsCharOfType(int c)
|
|
|
|
{
|
|
|
|
return (Fn(c) != 0);
|
|
|
|
}
|
2016-03-24 23:25:52 +01:00
|
|
|
|
2016-03-22 01:07:34 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static bool OnlyDelimiter(const SQChar * str, SQChar chr)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
|
|
|
while (*str != '\0')
|
|
|
|
{
|
|
|
|
// Is this different from the delimiter?
|
|
|
|
if (*(str++) != chr)
|
|
|
|
{
|
|
|
|
// Another character was found
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No other character was found
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-09 13:12:51 +02:00
|
|
|
static SQInteger SqStrExplode(HSQUIRRELVM vm)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
const int32_t top = sq_gettop(vm);
|
2016-07-09 13:12:51 +02:00
|
|
|
// Was the delimiter character specified?
|
|
|
|
if (top <= 1)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, _SC("Missing delimiter character"));
|
|
|
|
}
|
|
|
|
// Was the boolean empty specified?
|
|
|
|
else if (top <= 2)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, _SC("Missing boolean empty"));
|
|
|
|
}
|
|
|
|
// Was the string value specified?
|
|
|
|
else if (top <= 3)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, _SC("Missing string value"));
|
|
|
|
}
|
|
|
|
// Attempt to generate the string value
|
2018-07-29 23:58:27 +02:00
|
|
|
StackStrF val(vm, 4);
|
2016-07-09 13:12:51 +02:00
|
|
|
// Have we failed to retrieve the string?
|
2018-07-29 23:58:27 +02:00
|
|
|
if (SQ_FAILED(val.Proc(true)))
|
2016-07-09 13:12:51 +02:00
|
|
|
{
|
|
|
|
return val.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// The delimiter character and boolean empty
|
2021-01-30 07:51:39 +01:00
|
|
|
SQChar delim;
|
|
|
|
bool empty;
|
2016-07-09 13:12:51 +02:00
|
|
|
// Attempt to retrieve the remaining arguments from the stack
|
|
|
|
try
|
|
|
|
{
|
|
|
|
delim = Var< SQChar >(vm, 2).value;
|
|
|
|
empty = Var< bool >(vm, 3).value;
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
catch (const std::exception & e)
|
2016-07-09 13:12:51 +02:00
|
|
|
{
|
2016-07-09 13:18:09 +02:00
|
|
|
return sq_throwerror(vm, e.what());
|
2016-07-09 13:12:51 +02:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, _SC("Unable to retrieve arguments"));
|
|
|
|
}
|
|
|
|
// Grab the string value to a shorter name
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * str = val.mPtr;
|
2016-07-09 13:12:51 +02:00
|
|
|
// Create an empty array on the stack
|
|
|
|
sq_newarray(vm, 0);
|
2016-03-22 01:07:34 +01:00
|
|
|
// See if we actually have something to explode
|
|
|
|
if(!str || *str == '\0')
|
|
|
|
{
|
2016-07-09 13:12:51 +02:00
|
|
|
// Specify that we have an argument on the stack
|
|
|
|
return 1;
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
|
|
|
// Don't modify the specified string pointer
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * itr = str, * last = str;
|
2016-03-22 01:07:34 +01:00
|
|
|
// The number of delimiter occurrences
|
2021-01-30 07:51:39 +01:00
|
|
|
uint32_t num = 0;
|
2016-03-22 01:07:34 +01:00
|
|
|
// Pre-count how many delimiters of this type exist
|
|
|
|
while (*itr != '\0')
|
|
|
|
{
|
|
|
|
// Is this our delimiter?
|
2016-07-09 13:12:51 +02:00
|
|
|
if (*(itr++) == delim)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
|
|
|
// Are we allowed to include empty elements?
|
|
|
|
if (empty || (itr - last) > 1)
|
|
|
|
{
|
|
|
|
// Increase the count
|
|
|
|
++num;
|
|
|
|
}
|
|
|
|
// Update the last delimiter position
|
|
|
|
last = itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Were there no delimiters found and can we include empty elements?
|
2016-07-09 13:12:51 +02:00
|
|
|
if (num == 0 && !empty && (str[1] == '\0' || OnlyDelimiter(str, delim)))
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-07-09 13:12:51 +02:00
|
|
|
// Specify that we have an argument on the stack
|
|
|
|
return 1;
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
|
|
|
// Have we found any delimiters?
|
|
|
|
else if (num == 0)
|
|
|
|
{
|
|
|
|
// Test against strings with only delimiters
|
2016-07-09 13:12:51 +02:00
|
|
|
if (str[1] == '\0' || OnlyDelimiter(str, delim))
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-07-09 13:12:51 +02:00
|
|
|
sq_pushstring(vm, _SC(""), 0); // Add an empty string
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-09 13:12:51 +02:00
|
|
|
sq_pushstring(vm, val.mPtr, val.mLen); // Add the whole string
|
|
|
|
}
|
|
|
|
// Append the string on the stack to the array
|
|
|
|
const SQRESULT r = sq_arrayappend(vm, -2);
|
|
|
|
// Check the result
|
|
|
|
if (SQ_FAILED(r))
|
|
|
|
{
|
|
|
|
return r; // Propagate the error
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-07-09 13:12:51 +02:00
|
|
|
// Specify that we have an argument on the stack
|
|
|
|
return 1;
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
|
|
|
// Is there anything after the last delimiter?
|
2016-07-09 13:12:51 +02:00
|
|
|
if (itr != last && *last != delim)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
|
|
|
++num; // Add it to the counter
|
|
|
|
}
|
2016-07-09 13:12:51 +02:00
|
|
|
SQRESULT r = SQ_OK;
|
2016-03-22 01:07:34 +01:00
|
|
|
// Pre-allocate an array with the number of found delimiters
|
2016-07-09 13:12:51 +02:00
|
|
|
r = sq_arrayresize(vm, -1, num);
|
|
|
|
// Check the result
|
|
|
|
if (SQ_FAILED(r))
|
|
|
|
{
|
|
|
|
return r; // Propagate the error
|
|
|
|
}
|
2016-03-22 01:07:34 +01:00
|
|
|
// Don't modify the specified string pointer
|
|
|
|
itr = str, last = str;
|
|
|
|
// Reset the counter and use it as the element index
|
|
|
|
num = 0;
|
|
|
|
// Process the string again, this time slicing the actual elements
|
|
|
|
while (*itr != '\0')
|
|
|
|
{
|
|
|
|
// Is this our delimiter?
|
2016-07-09 13:12:51 +02:00
|
|
|
if (*itr++ == delim)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
|
|
|
// Are we allowed to include empty elements?
|
|
|
|
if (empty || (itr - last) > 1)
|
|
|
|
{
|
|
|
|
// Push the element index on the stack and advance to the next one
|
2016-07-09 13:12:51 +02:00
|
|
|
sq_pushinteger(vm, num++);
|
2016-03-22 01:07:34 +01:00
|
|
|
// Push the string portion on the stack
|
2016-07-09 13:12:51 +02:00
|
|
|
sq_pushstring(vm, last, itr - last - 1);
|
2016-03-22 01:07:34 +01:00
|
|
|
// Assign the string onto the
|
2016-07-09 13:12:51 +02:00
|
|
|
r = sq_set(vm, -3);
|
|
|
|
// Check the result
|
|
|
|
if (SQ_FAILED(r))
|
|
|
|
{
|
|
|
|
return r; // Propagate the error
|
|
|
|
}
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
|
|
|
// Update the last delimiter position
|
|
|
|
last = itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Is there anything after the last delimiter?
|
2016-07-09 13:12:51 +02:00
|
|
|
if (itr != last && *last != delim)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
|
|
|
// Push the element index on the stack
|
2016-07-09 13:12:51 +02:00
|
|
|
sq_pushinteger(vm, num);
|
2016-03-22 01:07:34 +01:00
|
|
|
// Add the remaining string as an element
|
2016-07-09 13:12:51 +02:00
|
|
|
sq_pushstring(vm, last, itr - last);
|
2016-03-22 01:07:34 +01:00
|
|
|
// Assign the string onto the
|
2016-07-09 13:12:51 +02:00
|
|
|
r = sq_set(vm, -3);
|
|
|
|
// Check the result
|
|
|
|
if (SQ_FAILED(r))
|
|
|
|
{
|
|
|
|
return r; // Propagate the error
|
|
|
|
}
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-07-09 13:12:51 +02:00
|
|
|
// Specify that we have an argument on the stack
|
|
|
|
return 1;
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static const SQChar * StrImplode(SQChar chr, Array & arr)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
|
|
|
// Determine array size
|
2021-01-30 07:51:39 +01:00
|
|
|
const auto length = static_cast< int32_t >(arr.Length());
|
2016-03-22 01:07:34 +01:00
|
|
|
// Is there anything to implode?
|
|
|
|
if (length <= 0)
|
|
|
|
{
|
|
|
|
return _SC(""); // Default to an empty string
|
|
|
|
}
|
|
|
|
// Obtain a temporary buffer
|
|
|
|
Buffer b(length * 32);
|
|
|
|
// Process array elements
|
|
|
|
for (SQInteger i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
// Retrieve the element value as string
|
|
|
|
SharedPtr< String > str = arr.GetValue< String >(i);
|
|
|
|
// Was there any value retrieved?
|
|
|
|
if (!!str)
|
|
|
|
{
|
|
|
|
// Append the value to the buffer
|
|
|
|
b.AppendS(str->data(), str->size());
|
|
|
|
}
|
|
|
|
// Append the delimiter
|
|
|
|
b.Push(chr);
|
|
|
|
}
|
|
|
|
// Move the cursor back one element
|
|
|
|
b.Retreat(1);
|
|
|
|
// Set that as the null character
|
|
|
|
b.Cursor() = '\0';
|
2016-02-21 09:30:47 +01:00
|
|
|
// Return the string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
static const SQChar * FromArray(Array & arr)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-02-21 09:30:47 +01:00
|
|
|
// Determine array size
|
2021-01-30 07:51:39 +01:00
|
|
|
const int32_t length = ConvTo< int32_t >::From(arr.Length());
|
2016-02-21 09:30:47 +01:00
|
|
|
// Obtain a temporary buffer
|
2021-01-30 07:51:39 +01:00
|
|
|
Buffer b(length * sizeof(int32_t));
|
2016-02-21 09:30:47 +01:00
|
|
|
// Get array elements as integers
|
2021-01-30 07:51:39 +01:00
|
|
|
arr.GetArray< int32_t >(b.Get< int32_t >(), length);
|
2016-02-21 09:30:47 +01:00
|
|
|
// Overwrite integers with characters
|
2021-01-30 07:51:39 +01:00
|
|
|
for (int32_t n = 0; n < length; ++n)
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
b.At(n) = ConvTo< SQChar >::From(b.At< int32_t >(n * sizeof(int32_t)));
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-02-21 09:30:47 +01:00
|
|
|
// Terminate the resulted string
|
2016-03-22 01:07:34 +01:00
|
|
|
b.At(length) = '\0';
|
2016-02-21 09:30:47 +01:00
|
|
|
// Return the string
|
2016-02-20 23:25:00 +01:00
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static SQInteger StdPrintF(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-03-25 01:05:46 +01:00
|
|
|
// Attempt to retrieve the value from the stack as a string
|
2018-07-29 23:58:27 +02:00
|
|
|
StackStrF val(vm, 2);
|
2016-03-25 01:05:46 +01:00
|
|
|
// Have we failed to retrieve the string?
|
2018-07-29 23:58:27 +02:00
|
|
|
if (SQ_FAILED(val.Proc(true)))
|
2016-03-22 01:07:34 +01:00
|
|
|
{
|
2016-03-25 01:05:46 +01:00
|
|
|
return val.mRes; // Propagate the error!
|
2016-03-22 01:07:34 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Send the resulted string to console as a user message
|
2016-03-25 01:05:46 +01:00
|
|
|
LogUsr("%s", val.mPtr);
|
2016-03-10 04:57:13 +01:00
|
|
|
// This function doesn't return anything
|
2016-02-20 23:25:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-14 03:33:09 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static String StrCharacterSwap(SQInteger a, SQInteger b, StackStrF & val)
|
|
|
|
{
|
|
|
|
// Have we failed to retrieve the string?
|
|
|
|
if (SQ_FAILED(val.Proc(true)))
|
|
|
|
{
|
|
|
|
STHROWLASTF("Invalid string");
|
|
|
|
}
|
|
|
|
// Is the string empty?
|
|
|
|
else if (!val.mLen)
|
|
|
|
{
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
// Turn it into a string that we can edit
|
|
|
|
String str(val.mPtr, val.mLen);
|
2021-01-30 07:51:39 +01:00
|
|
|
// Replace all occurrences of the specified character
|
2019-08-14 03:33:09 +02:00
|
|
|
std::replace(str.begin(), str.end(),
|
|
|
|
static_cast< String::value_type >(a), static_cast< String::value_type >(b));
|
|
|
|
// Return the new string
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:25:05 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static SQInteger SqStrToI(SQInteger base, StackStrF & s)
|
|
|
|
{
|
|
|
|
#ifdef _SQ64
|
|
|
|
return std::stoll(s.mPtr, nullptr, ConvTo< int >::From((base)));
|
|
|
|
#else
|
|
|
|
return std::stoi(s.mPtr, nullptr, ConvTo< int >::From((base)));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static SQFloat SqStrToF(StackStrF & s)
|
|
|
|
{
|
|
|
|
#ifdef SQUSEDOUBLE
|
|
|
|
return std::strtod(s.mPtr, nullptr);
|
|
|
|
#else
|
|
|
|
return std::strtof(s.mPtr, nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_String(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
Table strns(vm);
|
|
|
|
|
|
|
|
strns.Func(_SC("FromArray"), &FromArray)
|
2016-07-09 13:12:51 +02:00
|
|
|
.SquirrelFunc(_SC("Explode"), &SqStrExplode)
|
2016-03-24 23:25:52 +01:00
|
|
|
.Func(_SC("Implode"), &StrImplode)
|
2016-11-17 16:05:12 +01:00
|
|
|
.FmtFunc(_SC("Center"), &SqCenterStr)
|
|
|
|
.FmtFunc(_SC("Left"), &SqLeftStr)
|
|
|
|
.FmtFunc(_SC("Right"), &SqRightStr)
|
|
|
|
.FmtFunc(_SC("LeftEx"), &SqLeftOffsetStr)
|
|
|
|
.FmtFunc(_SC("RightEx"), &SqRightOffsetStr)
|
|
|
|
.FmtFunc(_SC("ToLower"), &SqToLowercase)
|
|
|
|
.FmtFunc(_SC("ToUpper"), &SqToUppercase)
|
2019-08-14 03:33:09 +02:00
|
|
|
.FmtFunc(_SC("CharSwap"), &StrCharacterSwap)
|
2016-11-17 16:05:12 +01:00
|
|
|
.FmtFunc(_SC("Lowercase"), &SqToLowercase)
|
|
|
|
.FmtFunc(_SC("Uppercase"), &SqToUppercase)
|
|
|
|
.FmtFunc(_SC("JustAlnum"), &SqJustAlphaNum)
|
2020-05-08 17:25:05 +02:00
|
|
|
.FmtFunc(_SC("ToInt"), &SqStrToI)
|
|
|
|
.FmtFunc(_SC("ToFloat"), &SqStrToF)
|
2016-11-17 16:05:12 +01:00
|
|
|
.FmtFunc(_SC("AreAllSpace"), &SqAllChars< std::isspace >)
|
|
|
|
.FmtFunc(_SC("AreAllPrint"), &SqAllChars< std::isprint >)
|
|
|
|
.FmtFunc(_SC("AreAllCntrl"), &SqAllChars< std::iscntrl >)
|
|
|
|
.FmtFunc(_SC("AreAllUpper"), &SqAllChars< std::isupper >)
|
|
|
|
.FmtFunc(_SC("AreAllLower"), &SqAllChars< std::islower >)
|
|
|
|
.FmtFunc(_SC("AreAllAlpha"), &SqAllChars< std::isalpha >)
|
|
|
|
.FmtFunc(_SC("AreAllDigit"), &SqAllChars< std::isdigit >)
|
|
|
|
.FmtFunc(_SC("AreAllPunct"), &SqAllChars< std::ispunct >)
|
|
|
|
.FmtFunc(_SC("AreAllXdigit"), &SqAllChars< std::isxdigit >)
|
|
|
|
.FmtFunc(_SC("AreAllAlnum"), &SqAllChars< std::isalnum >)
|
|
|
|
.FmtFunc(_SC("AreAllGraph"), &SqAllChars< std::isgraph >)
|
|
|
|
.FmtFunc(_SC("AreAllBlank"), &SqAllChars< std::isblank >)
|
|
|
|
.FmtFunc(_SC("FirstSpace"), &SqFirstChar< std::isspace, false >)
|
|
|
|
.FmtFunc(_SC("FirstPrint"), &SqFirstChar< std::isprint, false >)
|
|
|
|
.FmtFunc(_SC("FirstCntrl"), &SqFirstChar< std::iscntrl, false >)
|
|
|
|
.FmtFunc(_SC("FirstUpper"), &SqFirstChar< std::isupper, false >)
|
|
|
|
.FmtFunc(_SC("FirstLower"), &SqFirstChar< std::islower, false >)
|
|
|
|
.FmtFunc(_SC("FirstAlpha"), &SqFirstChar< std::isalpha, false >)
|
|
|
|
.FmtFunc(_SC("FirstDigit"), &SqFirstChar< std::isdigit, false >)
|
|
|
|
.FmtFunc(_SC("FirstPunct"), &SqFirstChar< std::ispunct, false >)
|
|
|
|
.FmtFunc(_SC("FirstXdigit"), &SqFirstChar< std::isxdigit, false >)
|
|
|
|
.FmtFunc(_SC("FirstAlnum"), &SqFirstChar< std::isalnum, false >)
|
|
|
|
.FmtFunc(_SC("FirstGraph"), &SqFirstChar< std::isgraph, false >)
|
|
|
|
.FmtFunc(_SC("FirstBlank"), &SqFirstChar< std::isblank, false >)
|
|
|
|
.FmtFunc(_SC("FirstNotSpace"), &SqFirstChar< std::isspace, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotPrint"), &SqFirstChar< std::isprint, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotCntrl"), &SqFirstChar< std::iscntrl, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotUpper"), &SqFirstChar< std::isupper, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotLower"), &SqFirstChar< std::islower, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotAlpha"), &SqFirstChar< std::isalpha, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotDigit"), &SqFirstChar< std::isdigit, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotPunct"), &SqFirstChar< std::ispunct, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotXdigit"), &SqFirstChar< std::isxdigit, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotAlnum"), &SqFirstChar< std::isalnum, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotGraph"), &SqFirstChar< std::isgraph, true >)
|
|
|
|
.FmtFunc(_SC("FirstNotBlank"), &SqFirstChar< std::isblank, true >)
|
|
|
|
.FmtFunc(_SC("LastSpace"), &SqLastChar< std::isspace, false >)
|
|
|
|
.FmtFunc(_SC("LastPrint"), &SqLastChar< std::isprint, false >)
|
|
|
|
.FmtFunc(_SC("LastCntrl"), &SqLastChar< std::iscntrl, false >)
|
|
|
|
.FmtFunc(_SC("LastUpper"), &SqLastChar< std::isupper, false >)
|
|
|
|
.FmtFunc(_SC("LastLower"), &SqLastChar< std::islower, false >)
|
|
|
|
.FmtFunc(_SC("LastAlpha"), &SqLastChar< std::isalpha, false >)
|
|
|
|
.FmtFunc(_SC("LastDigit"), &SqLastChar< std::isdigit, false >)
|
|
|
|
.FmtFunc(_SC("LastPunct"), &SqLastChar< std::ispunct, false >)
|
|
|
|
.FmtFunc(_SC("LastXdigit"), &SqLastChar< std::isxdigit, false >)
|
|
|
|
.FmtFunc(_SC("LastAlnum"), &SqLastChar< std::isalnum, false >)
|
|
|
|
.FmtFunc(_SC("LastGraph"), &SqLastChar< std::isgraph, false >)
|
|
|
|
.FmtFunc(_SC("LastBlank"), &SqLastChar< std::isblank, false >)
|
|
|
|
.FmtFunc(_SC("LastNotSpace"), &SqLastChar< std::isspace, true >)
|
|
|
|
.FmtFunc(_SC("LastNotPrint"), &SqLastChar< std::isprint, true >)
|
|
|
|
.FmtFunc(_SC("LastNotCntrl"), &SqLastChar< std::iscntrl, true >)
|
|
|
|
.FmtFunc(_SC("LastNotUpper"), &SqLastChar< std::isupper, true >)
|
|
|
|
.FmtFunc(_SC("LastNotLower"), &SqLastChar< std::islower, true >)
|
|
|
|
.FmtFunc(_SC("LastNotAlpha"), &SqLastChar< std::isalpha, true >)
|
|
|
|
.FmtFunc(_SC("LastNotDigit"), &SqLastChar< std::isdigit, true >)
|
|
|
|
.FmtFunc(_SC("LastNotPunct"), &SqLastChar< std::ispunct, true >)
|
|
|
|
.FmtFunc(_SC("LastNotXdigit"), &SqLastChar< std::isxdigit, true >)
|
|
|
|
.FmtFunc(_SC("LastNotAlnum"), &SqLastChar< std::isalnum, true >)
|
|
|
|
.FmtFunc(_SC("LastNotGraph"), &SqLastChar< std::isgraph, true >)
|
|
|
|
.FmtFunc(_SC("LastNotBlank"), &SqLastChar< std::isblank, true >)
|
2016-09-15 02:04:05 +02:00
|
|
|
.SquirrelFunc(_SC("SplitWhereSpace"), &SplitWhereCharProxy< std::isspace, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWherePrint"), &SplitWhereCharProxy< std::isprint, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereCntrl"), &SplitWhereCharProxy< std::iscntrl, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereUpper"), &SplitWhereCharProxy< std::isupper, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereLower"), &SplitWhereCharProxy< std::islower, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereAlpha"), &SplitWhereCharProxy< std::isalpha, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereDigit"), &SplitWhereCharProxy< std::isdigit, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWherePunct"), &SplitWhereCharProxy< std::ispunct, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereXdigit"), &SplitWhereCharProxy< std::isxdigit, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereAlnum"), &SplitWhereCharProxy< std::isalnum, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereGraph"), &SplitWhereCharProxy< std::isgraph, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereBlank"), &SplitWhereCharProxy< std::isblank, false >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotSpace"), &SplitWhereCharProxy< std::isspace, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotPrint"), &SplitWhereCharProxy< std::isprint, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotCntrl"), &SplitWhereCharProxy< std::iscntrl, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotUpper"), &SplitWhereCharProxy< std::isupper, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotLower"), &SplitWhereCharProxy< std::islower, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotAlpha"), &SplitWhereCharProxy< std::isalpha, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotDigit"), &SplitWhereCharProxy< std::isdigit, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotPunct"), &SplitWhereCharProxy< std::ispunct, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotXdigit"), &SplitWhereCharProxy< std::isxdigit, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotAlnum"), &SplitWhereCharProxy< std::isalnum, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotGraph"), &SplitWhereCharProxy< std::isgraph, true >)
|
|
|
|
.SquirrelFunc(_SC("SplitWhereNotBlank"), &SplitWhereCharProxy< std::isblank, true >);
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
RootTable(vm).Bind(_SC("SqStr"), strns);
|
|
|
|
RootTable(vm).SquirrelFunc(_SC("printf"), &StdPrintF);
|
2016-04-03 20:09:49 +02:00
|
|
|
|
|
|
|
RootTable(vm)
|
2016-09-15 02:03:06 +02:00
|
|
|
.Func(_SC("IsSpace"), &IsCharOfType< std::isspace >)
|
|
|
|
.Func(_SC("IsPrint"), &IsCharOfType< std::isprint >)
|
|
|
|
.Func(_SC("IsCntrl"), &IsCharOfType< std::iscntrl >)
|
|
|
|
.Func(_SC("IsUpper"), &IsCharOfType< std::isupper >)
|
|
|
|
.Func(_SC("IsLower"), &IsCharOfType< std::islower >)
|
|
|
|
.Func(_SC("IsAlpha"), &IsCharOfType< std::isalpha >)
|
|
|
|
.Func(_SC("IsDigit"), &IsCharOfType< std::isdigit >)
|
|
|
|
.Func(_SC("IsPunct"), &IsCharOfType< std::ispunct >)
|
|
|
|
.Func(_SC("IsXdigit"), &IsCharOfType< std::isxdigit >)
|
|
|
|
.Func(_SC("IsAlnum"), &IsCharOfType< std::isalnum >)
|
|
|
|
.Func(_SC("IsGraph"), &IsCharOfType< std::isgraph >)
|
|
|
|
.Func(_SC("IsBlank"), &IsCharOfType< std::isblank >)
|
2016-04-03 20:10:17 +02:00
|
|
|
.Func(_SC("ToLower"), &tolower)
|
|
|
|
.Func(_SC("ToUpper"), &toupper);
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 04:36:03 +01:00
|
|
|
} // Namespace:: SqMod
|