2012-08-01 22:49:15 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# A C++ like string class (http://en.cppreference.com/w/cpp/string/basic_string)
|
|
|
|
# ------------------------------------------------------------------------------
|
2020-06-08 13:07:28 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#
|
|
|
|
# NOTE! This copyright does *not* cover user models that use these Nasal
|
|
|
|
# services by normal function calls - this is merely considered normal use
|
|
|
|
# of the code, and does *not* fall under the heading of "derived work."
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2013 by Thomas Geymayer
|
|
|
|
|
2020-07-01 12:46:47 +00:00
|
|
|
#load only once (via /Nasal/std.nas) not via C++ module loader
|
|
|
|
if (ishash(globals["std"]) and ishash(std["String"]))
|
|
|
|
return;
|
2012-08-01 22:49:15 +00:00
|
|
|
|
2020-07-01 12:46:47 +00:00
|
|
|
var String = {
|
2012-08-01 22:49:15 +00:00
|
|
|
# public:
|
|
|
|
new: func(str)
|
|
|
|
{
|
2020-07-01 12:46:47 +00:00
|
|
|
return { parents: [String], _str: str };
|
2012-08-01 22:49:15 +00:00
|
|
|
},
|
2013-01-22 16:47:51 +00:00
|
|
|
# compare(s)
|
|
|
|
# compare(pos, n, s)
|
|
|
|
#
|
|
|
|
# @param s String to compare to
|
|
|
|
# @param pos Position of first character used to compare
|
|
|
|
# @param n Number of characters to compare
|
|
|
|
compare: func
|
|
|
|
{
|
|
|
|
var s = "";
|
|
|
|
var pos = 0;
|
|
|
|
var n = -1;
|
|
|
|
|
|
|
|
var num = size(arg);
|
|
|
|
if( num == 1 )
|
|
|
|
s = arg[0];
|
|
|
|
else if( num == 3 )
|
|
|
|
{
|
|
|
|
pos = arg[0];
|
|
|
|
n = arg[1];
|
|
|
|
s = arg[2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
die("std::string::compare: Invalid args");
|
|
|
|
|
|
|
|
if( n < 0 )
|
|
|
|
n = me.size();
|
|
|
|
else if( n > me.size() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( n != size(s) )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for(var i = pos; i < n; i += 1)
|
|
|
|
if( me._str[i] != s[i] )
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
},
|
2020-07-01 12:46:47 +00:00
|
|
|
|
|
|
|
# returns index (zero based) of first occurrence of s
|
|
|
|
# searching from pos
|
2012-08-01 22:49:15 +00:00
|
|
|
find_first_of: func(s, pos = 0)
|
|
|
|
{
|
|
|
|
return me._find(pos, size(me._str), s, 1);
|
|
|
|
},
|
|
|
|
find: func(s, pos = 0)
|
|
|
|
{
|
|
|
|
return me.find_first_of(s, pos);
|
|
|
|
},
|
|
|
|
find_first_not_of: func(s, pos = 0)
|
|
|
|
{
|
|
|
|
return me._find(pos, size(me._str), s, 0);
|
|
|
|
},
|
|
|
|
substr: func(pos, len = nil)
|
|
|
|
{
|
|
|
|
return substr(me._str, pos, len);
|
|
|
|
},
|
2013-01-22 16:47:51 +00:00
|
|
|
starts_with: func(s)
|
|
|
|
{
|
|
|
|
return me.compare(0, size(s), s);
|
|
|
|
},
|
2012-08-01 22:49:15 +00:00
|
|
|
size: func()
|
|
|
|
{
|
|
|
|
return size(me._str);
|
|
|
|
},
|
|
|
|
# private:
|
|
|
|
_eq: func(pos, s)
|
|
|
|
{
|
|
|
|
for(var i = 0; i < size(s); i += 1)
|
|
|
|
if( me._str[pos] == s[i] )
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
_find: func(first, last, s, eq)
|
|
|
|
{
|
2013-01-31 19:20:36 +00:00
|
|
|
if( first < 0 or last < 0 )
|
|
|
|
return -1;
|
|
|
|
|
2012-08-01 22:49:15 +00:00
|
|
|
var sign = first <= last ? 1 : -1;
|
|
|
|
for(var i = first; sign * i < last; i += sign)
|
|
|
|
if( me._eq(i, s) == eq )
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-01 12:46:47 +00:00
|
|
|
# for backward compatibility
|
|
|
|
var string = {parents: [String]};
|
|
|
|
string.new = func {
|
|
|
|
logprint(LOG_ALERT, "Deprecated use of std.string, please use std.String instead.");
|
|
|
|
return String.new(arg[0]);
|
|
|
|
}
|
|
|
|
|
2012-08-01 22:49:15 +00:00
|
|
|
# converts a string to an unsigned integer
|
|
|
|
var stoul = func(str, base = 10)
|
|
|
|
{
|
|
|
|
var val = 0;
|
|
|
|
for(var pos = 0; pos < size(str); pos += 1)
|
|
|
|
{
|
|
|
|
var c = str[pos];
|
|
|
|
|
2020-07-01 12:46:47 +00:00
|
|
|
if( globals.string.isdigit(c) )
|
2012-08-01 22:49:15 +00:00
|
|
|
var digval = c - `0`;
|
2020-07-01 12:46:47 +00:00
|
|
|
else if( globals.string.isalpha(c) )
|
|
|
|
var digval = globals.string.toupper(c) - `A` + 10;
|
2012-08-01 22:49:15 +00:00
|
|
|
else
|
|
|
|
break;
|
2013-01-22 16:47:51 +00:00
|
|
|
|
2012-08-01 22:49:15 +00:00
|
|
|
if( digval >= base )
|
|
|
|
break;
|
2013-01-22 16:47:51 +00:00
|
|
|
|
2012-08-01 22:49:15 +00:00
|
|
|
val = val * base + digval;
|
|
|
|
}
|
2013-01-22 16:47:51 +00:00
|
|
|
|
2012-08-01 22:49:15 +00:00
|
|
|
return val;
|
|
|
|
};
|