2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-09-30 02:56:11 +02:00
|
|
|
#include "Library/String.hpp"
|
2016-02-20 23:25:00 +01:00
|
|
|
#include "Base/Shared.hpp"
|
|
|
|
#include "Base/Buffer.hpp"
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <sqstdstring.h>
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <algorithm>
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
CSStr LeftStr(CSStr t, SQChar f, Uint32 w)
|
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
|
|
|
return _SC("");
|
|
|
|
// Allocate a buffer with the requested width
|
|
|
|
Buffer b(w);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!t || *t == 0)
|
|
|
|
// Insert only the fill character
|
|
|
|
memset(b.Data(), f, w);
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
|
|
|
const Uint32 n = strlen(t);
|
|
|
|
// Insert only the fill character first
|
|
|
|
memset(b.Data(), f, w);
|
|
|
|
// Overwrite with the specified string
|
2016-02-20 23:25:00 +01:00
|
|
|
strncpy(b.Data(), t, n);
|
|
|
|
}
|
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(w) = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the resulted string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
|
|
|
CSStr LeftStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
|
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
|
|
|
return _SC("");
|
|
|
|
// Is the specified offset within width range?
|
|
|
|
else if (o > w)
|
|
|
|
SqThrowF("Offset is out of bounds");
|
|
|
|
// Allocate a buffer with the requested width
|
|
|
|
Buffer b(w);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!t || *t == 0)
|
|
|
|
// Insert only the fill character
|
|
|
|
memset(b.Data(), f, w);
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Clculate the string length
|
|
|
|
const Uint32 n = strlen(t);
|
|
|
|
// Insert the fill character first
|
|
|
|
memset(b.Data(), f, w);
|
|
|
|
// Overwrite with the specified string
|
|
|
|
strncpy(b.Data() + o, t, w - n);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(w) = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
return b.Get();
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr RightStr(CSStr t, SQChar f, Uint32 w)
|
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
|
|
|
return _SC("");
|
|
|
|
// Allocate a buffer with the requested width
|
|
|
|
Buffer b(w);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!t || *t == 0)
|
|
|
|
// Insert only the fill character
|
|
|
|
memset(b.Data(), f, w);
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
|
|
|
const Uint32 n = strlen(t);
|
|
|
|
// Insert the fill character first
|
|
|
|
memset(b.Data(), f, w);
|
|
|
|
// Overwrite with the specified string
|
|
|
|
if (n >= w)
|
|
|
|
strncpy(b.Data(), t, w);
|
|
|
|
else
|
|
|
|
strncpy(b.Data() + (w - n), t, n);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(w) = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the resulted string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
|
|
|
CSStr RightStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
|
|
|
|
{
|
|
|
|
// Is the specified width valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!w)
|
|
|
|
return _SC("");
|
|
|
|
// Is the specified offset within width range?
|
|
|
|
else if (o > w)
|
|
|
|
SqThrowF("Offset is out of bounds");
|
|
|
|
// Allocate a buffer with the requested width
|
|
|
|
Buffer b(w);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!t || *t == 0)
|
|
|
|
// Insert only the fill character
|
|
|
|
memset(b.Data(), f, w);
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
|
|
|
const Uint32 n = strlen(t);
|
|
|
|
// Insert the fill character first
|
|
|
|
memset(b.Data(), f, w);
|
|
|
|
// Overwrite with the specified string
|
|
|
|
if (n >= w || (n + o) >= w)
|
|
|
|
strncpy(b.Data(), t, w - o);
|
|
|
|
else
|
|
|
|
strncpy(b.Data() + ((w - n) - o), t, n);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(w) = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the resulted string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr CenterStr(CSStr t, SQChar f, Uint32 w)
|
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)
|
|
|
|
return _SC("");
|
|
|
|
// Allocate a buffer with the requested width
|
|
|
|
Buffer b(w);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is the specified string valid?
|
2016-03-10 04:57:13 +01:00
|
|
|
if (!t || *t == 0)
|
|
|
|
// Insert only the fill character
|
|
|
|
memset(b.Data(), f, w);
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
|
|
|
const Uint32 n = strlen(t);
|
|
|
|
// Insert only the fill character first
|
|
|
|
memset(b.Data(), f, w);
|
|
|
|
// Overwrite with the specified string
|
|
|
|
strncpy(b.Data() + ((w/2) - (n/2)), t, n);
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(w) = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the resulted string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr StrJustAlphaNum(CSStr str)
|
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-03-10 04:57:13 +01:00
|
|
|
if(!str || *str == 0)
|
2016-02-20 23:25:00 +01:00
|
|
|
return _SC("");
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
|
|
|
Uint32 size = strlen(str);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Obtain a temporary buffer
|
|
|
|
Buffer b(size);
|
|
|
|
// Resulted string size
|
|
|
|
Uint32 n = 0;
|
|
|
|
// Currently processed character
|
|
|
|
SQChar c = 0;
|
|
|
|
// Process characters
|
|
|
|
while ((c = *(str++)) != 0)
|
|
|
|
{
|
|
|
|
// Is this an alpha-numeric character?
|
|
|
|
if (isalnum(c) != 0)
|
|
|
|
// Save it and move to the next one
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n++) = c;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n) = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr StrToLowercase(CSStr str)
|
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-03-10 04:57:13 +01:00
|
|
|
if(!str || *str == 0)
|
2016-02-20 23:25:00 +01:00
|
|
|
return _SC("");
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
|
|
|
Uint32 size = strlen(str);
|
2016-02-20 23:25:00 +01:00
|
|
|
// Obtain a temporary buffer
|
|
|
|
Buffer b(size);
|
|
|
|
// Resulted string size
|
|
|
|
Uint32 n = 0;
|
|
|
|
// Currently processed character
|
|
|
|
SQChar c = 0;
|
|
|
|
// Process characters
|
|
|
|
while ((c = *(str++)) != 0)
|
|
|
|
// Convert it and move to the next one
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n++) = tolower(c);
|
2016-02-20 23:25:00 +01:00
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n) = 0;
|
2016-02-20 23:25:00 +01:00
|
|
|
// Return the string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
2016-02-21 09:30:47 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr StrToUppercase(CSStr str)
|
|
|
|
{
|
|
|
|
// See if we actually have something to search for
|
2016-03-10 04:57:13 +01:00
|
|
|
if(!str || *str == 0)
|
2016-02-21 09:30:47 +01:00
|
|
|
return _SC("");
|
2016-03-10 04:57:13 +01:00
|
|
|
// Calculate the string length
|
|
|
|
Uint32 size = strlen(str);
|
2016-02-21 09:30:47 +01:00
|
|
|
// Obtain a temporary buffer
|
|
|
|
Buffer b(size);
|
|
|
|
// Resulted string size
|
|
|
|
Uint32 n = 0;
|
|
|
|
// Currently processed character
|
|
|
|
SQChar c = 0;
|
|
|
|
// Process characters
|
|
|
|
while ((c = *(str++)) != 0)
|
|
|
|
// Convert it and move to the next one
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n++) = toupper(c);
|
2016-02-21 09:30:47 +01:00
|
|
|
// End the resulted string
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n) = 0;
|
2016-02-21 09:30:47 +01:00
|
|
|
// Return the string
|
|
|
|
return b.Get< SQChar >();
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static CSStr FromArray(Array & arr)
|
|
|
|
{
|
2016-02-21 09:30:47 +01:00
|
|
|
// Determine array size
|
2016-03-10 04:57:13 +01:00
|
|
|
const Int32 length = static_cast< Int32 >(arr.Length());
|
2016-02-21 09:30:47 +01:00
|
|
|
// Obtain a temporary buffer
|
|
|
|
Buffer b(length * sizeof(Int32));
|
|
|
|
// Get array elements as integers
|
|
|
|
arr.GetArray< Int32 >(b.Get< Int32 >(), length);
|
|
|
|
// Overwrite integers with characters
|
|
|
|
for (Int32 n = 0; n < length; ++n)
|
2016-03-10 04:57:13 +01:00
|
|
|
b.At(n) = static_cast< SQChar >(b.At< Int32 >(n));
|
2016-02-21 09:30:47 +01:00
|
|
|
// Terminate the resulted string
|
2016-03-10 04:57:13 +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)
|
|
|
|
{
|
|
|
|
CStr msg = NULL;
|
|
|
|
SQInteger length = 0;
|
2016-03-10 04:57:13 +01:00
|
|
|
// Attempt to run the specified format and save the result
|
|
|
|
const SQRESULT res = sqstd_format(vm, 2, &length, &msg);
|
|
|
|
// Validate the result for errors and propagate them to the VM
|
|
|
|
if(SQ_FAILED(res))
|
|
|
|
return res;
|
|
|
|
// Send the resulted string to console as a user message
|
2016-02-20 23:25:00 +01:00
|
|
|
LogUsr("%s", msg);
|
2016-03-10 04:57:13 +01:00
|
|
|
// This function doesn't return anything
|
2016-02-20 23:25:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ================================================================================================
|
|
|
|
void Register_String(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
Table strns(vm);
|
|
|
|
|
|
|
|
strns.Func(_SC("FromArray"), &FromArray)
|
|
|
|
.Overload< CSStr (*)(CSStr, SQChar, Uint32) >(_SC("Left"), &LeftStr)
|
|
|
|
.Overload< CSStr (*)(CSStr, SQChar, Uint32, Uint32) >(_SC("Left"), &LeftStr)
|
|
|
|
.Overload< CSStr (*)(CSStr, SQChar, Uint32) >(_SC("Right"), &RightStr)
|
|
|
|
.Overload< CSStr (*)(CSStr, SQChar, Uint32, Uint32) >(_SC("Right"), &RightStr)
|
2016-02-21 09:30:47 +01:00
|
|
|
.Func(_SC("Center"), &CenterStr)
|
|
|
|
.Func(_SC("JustAlphaNum"), &StrJustAlphaNum)
|
|
|
|
.Func(_SC("Lowercase"), &StrToLowercase)
|
|
|
|
.Func(_SC("Uppercase"), &StrToUppercase);
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
RootTable(vm).Bind(_SC("SqStr"), strns);
|
|
|
|
RootTable(vm).SquirrelFunc(_SC("printf"), &StdPrintF);
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 04:36:03 +01:00
|
|
|
} // Namespace:: SqMod
|