mirror of
https://github.com/UOX3DevTeam/UOX3
synced 2026-08-13 12:27:04 -04:00
Added new Makefile that handles compiling UOX3 and Spidermonkey on both Linux and MacOS (punt)
Added VS solution (SpiderMonkey.sln) and VC++ project files for compiling SpiderMonkey on Windows (Xuri)
Updated SpiderMonkey from v1.6.0 to v1.7.0
Added optimization flag -O2 for release build in CMakeLists.txt in project root (Xuri)
Added StringUtility file with general common string manipulation functions (punt)
Updated RandomNum function to use a "seedless" random number generator from C++11 instead of the old C rand() function. (punt)
Overall code cleanup to remove/replace platform-dependent code (punt)
Replaced potentially unsafe usage of C string stuff like sprintf, vsprintf, vsnprintf, strncat, strcpy and strlen throughout the code with calls to a format() function containing only a single, safe use of vsnprintf, ensuring there's a single place of failure if there's anything to fix and and making it easy to potentially replace this with std::format from C++20 when the time comes. (punt)
Replaced usage of char in many places with std::string (punt)
Started process of replacing UString usage with functions provided through StringUtility instead (punt)
Replaced platform specific fileIO handling in Windows/Linux with cross-platform C++17 standard std::filesystem (punt)
Replaced platform specific time handling by using cross-platform chrono library (punt)
Removed UOX namespace to reduce complexity (punt)
Elimitated template code approach to singletons for more modern c++ constructs, removing dependices in the process (punt)
Removed ODBC support; it hasn't been touched since the initial implementation in 2008, and there is a lack of someone to maintain the code. (punt)
Removed some platform specific files like uoxlinux.h, which is no longer required (punt)
Removed legacy crash protection code, and removed support for cluox (punt)
Removed legacy "support" for Borland compiler (punt)
Removed legacy VC++ 6 Workspace/Project files, VS2005 Solution/Project files, as these are no longer supported (Xuri)
Removed legacy BUILD folder with outdated compilation instructions (Xuri)
Removed old Changelog file (merged into Changelog.txt) (Xuri)
Fixed a pointer bug for items in multis on world load (punt)
Fixed a dictionary related issue that caused segmentation faults on Linux/MacOS (punt)
Fixed an issue with callbacks to JS scripts that caused segmentation faults on Linux/MacOS (punt)
Paths in uox.ini should now load properly on all platforms regardless of whether those paths use slashes or backslashes, and whether or not they end in a slash/backslash. (punt)
Moved to c++17 style of threading, and got rid of threadsafeobject.cpp/h (punt)
Replaced parsing of UOX.INI tags with a system that's more easily maintainable (punt)
Added some new commands to js/commands/custom/misc-cmd.js (Xuri)
cont // Targeted item will be made a container, set to nondecay and movable 2
endfight // Targeted character (and character being fought) will stop fighting
getmulti // Get multiObject for targeted item
finditem // Find item at layer X
movespeed // Set movement speed of target player (0x0 Normal, 0x1 Mounted, 0x2 Slow (walk only), 0x3 Hybrid ("jog"?), 0x4 Frozen)
Added new HTML based documentation in docs folder, and removed many old legacy docs that are either incorporated into this document already or no longer relevant for the current UOX3 version (Xuri)
Co-Authored-By: Charles Kerr <punt1959@users.noreply.github.com>
305 lines
7.7 KiB
C++
305 lines
7.7 KiB
C++
//
|
|
// StringUtility.cpp
|
|
// Infinity
|
|
//
|
|
//
|
|
|
|
#include "StringUtility.hpp"
|
|
|
|
#include <regex>
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <filesystem>
|
|
|
|
//==============================================================
|
|
//++++++++++++++++++++++++++++++++++++
|
|
std::string ltrim(const std::string& s) {
|
|
return std::regex_replace(s, std::regex("^\\s+"), std::string(""));
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++
|
|
std::string rtrim(const std::string& s) {
|
|
return std::regex_replace(s, std::regex("\\s+$"), std::string(""));
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++
|
|
std::string trim(const std::string& s) {
|
|
return ltrim(rtrim(s));
|
|
}
|
|
|
|
|
|
//==============================================================
|
|
//++++++++++++++++++++++++++++++++++++
|
|
std::string stripComment(const std::string &input,const std::string &commentdelim ){
|
|
|
|
auto loc = input.find_first_of(commentdelim) ;
|
|
auto value = input.substr(0,loc);
|
|
value = trim(value) ;
|
|
return value;
|
|
}
|
|
|
|
|
|
|
|
//==============================================================
|
|
//++++++++++++++++++++++++++++++++++++
|
|
std::string str_toupper(const std::string &s) {
|
|
auto input = s ;
|
|
std::transform(input.begin(), input.end(), input.begin(),
|
|
[](unsigned char c){ return std::toupper(c); } // correct
|
|
);
|
|
return input;
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++
|
|
std::string str_tolower(const std::string &s) {
|
|
auto input = s ;
|
|
std::transform(input.begin(), input.end(), input.begin(),
|
|
[](unsigned char c){ return std::tolower(c); } // correct
|
|
);
|
|
return input;
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++++
|
|
std::tuple<std::string,std::string> separate(const std::string& input,
|
|
const std::string& separator){
|
|
auto loc = input.find(separator);
|
|
auto first = input.substr(0,loc) ;
|
|
std::string second ="";
|
|
if ((loc != std::string::npos) && ((loc+1)< input.size()) ) {
|
|
second = input.substr(loc+1) ;
|
|
}
|
|
return std::make_tuple(trim(first),trim(second));
|
|
|
|
}
|
|
//+++++++++++++++++++++++++++++++++++++
|
|
std::string format(std::size_t maxsize, const std::string fmtstring,...) {
|
|
|
|
std::va_list argptr ;
|
|
va_start(argptr, fmtstring);
|
|
auto test = format(fmtstring,argptr) ;
|
|
if (maxsize>0){
|
|
if (test.size() > maxsize) {
|
|
test = test.substr(0,maxsize);
|
|
}
|
|
|
|
}
|
|
return test ;
|
|
}
|
|
//++++++++++++++++++++++++++++++++++++++
|
|
std::string format(std::size_t maxsize,const std::string fmtstring,va_list& list){
|
|
auto temp = format(fmtstring,list);
|
|
if (maxsize>0){
|
|
if (temp.size() > maxsize) {
|
|
temp = temp.substr(0,maxsize);
|
|
}
|
|
}
|
|
return temp ;
|
|
}
|
|
//+++++++++++++++++++++++++++++++++++++
|
|
std::string format(const std::string fmtstring,...) {
|
|
|
|
std::va_list argptr ;
|
|
va_start(argptr, fmtstring);
|
|
return format(fmtstring,argptr) ;
|
|
}
|
|
//++++++++++++++++++++++++++++++++++++++
|
|
std::string format(const std::string fmtstring,va_list& list){
|
|
constexpr std::uintmax_t buffersize = 4096 ;
|
|
char msg[buffersize];
|
|
std::memset(msg, 0,buffersize);
|
|
std::vsnprintf(msg, buffersize, fmtstring.c_str(), list);
|
|
va_end(list);
|
|
return std::string(msg) ;
|
|
}
|
|
//++++++++++++++++++++++++++++++++++++++++
|
|
std::string simplify(const std::string& input){
|
|
std::regex re("\\s{2,}");
|
|
std::string fmt = " ";
|
|
|
|
auto output = regex_replace(input, re, fmt);
|
|
return output ;
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++
|
|
std::vector<std::string> sections(const std::string& value, const std::string& sep, std::string::size_type start, std::string::size_type end ) {
|
|
std::vector<std::string> rvalue ;
|
|
|
|
auto pos = value.find(sep,start) ;
|
|
while (pos < end) {
|
|
if ((pos - start) > 0) {
|
|
auto potential = trim(value.substr(start,pos - start)) ;
|
|
rvalue.push_back(potential);
|
|
start = pos+sep.size() ;
|
|
}
|
|
else if (pos == start) {
|
|
rvalue.push_back("");
|
|
start += sep.size() ;
|
|
}
|
|
pos = value.find(sep,start) ;
|
|
}
|
|
rvalue.push_back(trim(value.substr(start,pos - start))) ;
|
|
return rvalue ;
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++
|
|
std::uintmax_t sectionCount(const std::string& value, const std::string& sep, std::string::size_type start , std::string::size_type end ){
|
|
|
|
return sections(value,sep,start,end).size() ;
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++
|
|
std::string indexSection(const std::string& value, std::uintmax_t sectionindex,const std::string& sep, std::string::size_type start , std::string::size_type end ){
|
|
auto sec = sections(value,sep,start,end) ;
|
|
if (sec.size() <= sectionindex) {
|
|
return sec[sectionindex];
|
|
}
|
|
throw std::runtime_error(format("Section index %ul exceeded size %ul from sections %s",sectionindex,sec.size(),value.c_str()));
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++
|
|
std::string extractSection( const std::string& value, const std::string& sep, std::string::size_type start, std::string::size_type stop )
|
|
{
|
|
std::string data ;
|
|
int count = -1 ;
|
|
size_t startoffset = 0 ;
|
|
size_t stopoffset = 0 ;
|
|
bool match = false ;
|
|
stopoffset = value.find(sep,startoffset);
|
|
while ( startoffset != std::string::npos )
|
|
{
|
|
count++ ;
|
|
if( count == start )
|
|
{
|
|
match = true ;
|
|
// We dont return the seperator, so jump over it
|
|
size_t tempoffset = startoffset ;
|
|
while ( stopoffset != std::string::npos )
|
|
{
|
|
if( count == stop )
|
|
{
|
|
break ;
|
|
}
|
|
else
|
|
{
|
|
tempoffset = stopoffset + sep.length() ;
|
|
stopoffset = value.find(sep,tempoffset) ;
|
|
count++ ;
|
|
}
|
|
}
|
|
break ;
|
|
|
|
}
|
|
else
|
|
{
|
|
if( stopoffset != std::string::npos )
|
|
startoffset = stopoffset+ sep.length();
|
|
else
|
|
startoffset = static_cast<size_t>(std::string::npos) ;
|
|
stopoffset = value.find(sep,startoffset) ;
|
|
|
|
}
|
|
}
|
|
if( match )
|
|
{
|
|
size_t length = stopoffset - startoffset ;
|
|
if( stopoffset == std::string::npos ){
|
|
length = value.length() - startoffset ;
|
|
}
|
|
data = value.substr(startoffset,length) ;
|
|
}
|
|
return data ;
|
|
}
|
|
|
|
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++
|
|
std::vector<std::string> breakSize(std::size_t maxsize, const std::string& input){
|
|
auto working = input ;
|
|
std::vector<std::string> total ;
|
|
while (working.size() > maxsize) {
|
|
auto portion = working.substr(0,maxsize);
|
|
working.replace(0, maxsize, "");
|
|
total.push_back(portion);
|
|
|
|
}
|
|
if (!working.empty()){
|
|
total.push_back(working);
|
|
}
|
|
return total ;
|
|
}
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++
|
|
std::string fixDirectory(const std::string& base){
|
|
std::string value = "\\" ;
|
|
std::string::size_type index = 0 ;
|
|
auto data = trim(base) ;
|
|
while ( (index = data.find( value, index ) ) != std::string::npos )
|
|
{
|
|
data = data.replace(index,1,"/") ;
|
|
}
|
|
if( data.find_last_of("/") != data.size()-1 )
|
|
{
|
|
data = data+"/" ;
|
|
}
|
|
|
|
return data ;
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++
|
|
std::string replaceSlash( std::string& data )
|
|
{
|
|
std::string value = "\\" ;
|
|
std::string::size_type index = 0 ;
|
|
|
|
while ( (index = data.find( value, index ) ) != std::string::npos )
|
|
{
|
|
data = data.replace(index,1,"/") ;
|
|
}
|
|
return data ;
|
|
}
|
|
|
|
std::string str_number(char n, int base){
|
|
std::stringstream conversion ;
|
|
auto value = static_cast<std::int16_t>(n);
|
|
|
|
switch (base) {
|
|
case 10:
|
|
conversion << std::dec << value ;
|
|
break;
|
|
case 16:
|
|
conversion <<std::hex << value <<std::dec;
|
|
break;
|
|
case 8:
|
|
conversion << std::oct << value <<std::dec;
|
|
break;
|
|
default:
|
|
conversion << std::dec << value ;
|
|
break;
|
|
}
|
|
|
|
return conversion.str() ;
|
|
}
|
|
std::string str_number(unsigned char n, int base){
|
|
std::stringstream conversion ;
|
|
auto value = static_cast<std::uint16_t>(n);
|
|
|
|
switch (base) {
|
|
case 10:
|
|
conversion << std::dec << value ;
|
|
break;
|
|
case 16:
|
|
conversion <<std::hex << value <<std::dec;
|
|
break;
|
|
case 8:
|
|
conversion << std::oct << value <<std::dec;
|
|
break;
|
|
default:
|
|
conversion << std::dec << value ;
|
|
break;
|
|
}
|
|
|
|
return conversion.str() ;
|
|
}
|