Debian Codegraph @

Nasal/std/string.nas

raw
1 # ------------------------------------------------------------------------------
2 # A C++ like string class (http://en.cppreference.com/w/cpp/string/basic_string)
3 # ------------------------------------------------------------------------------
4 # SPDX-License-Identifier: GPL-2.0-or-later
5 #
6 # NOTE! This copyright does *not* cover user models that use these Nasal
7 # services by normal function calls - this is merely considered normal use
8 # of the code, and does *not* fall under the heading of "derived work."
9 #
10 # Copyright (C) 2012-2013 by Thomas Geymayer
11
12 #load only once (via /Nasal/std.nas) not via C++ module loader
13 if (ishash(globals["std"]) and ishash(std["String"]))
14 return;
15
16 var String = {
17 # public:
18 new: func(str)
19 {
20 return { parents: [String], _str: str };
21 },
22 # compare(s)
23 # compare(pos, n, s)
24 #
25 # @param s String to compare to
26 # @param pos Position of first character used to compare
27 # @param n Number of characters to compare
28 compare: func
29 {
30 var s = "";
31 var pos = 0;
32 var n = -1;
33
34 var num = size(arg);
35 if( num == 1 )
36 s = arg[0];
37 else if( num == 3 )
38 {
39 pos = arg[0];
40 n = arg[1];
41 s = arg[2];
42 }
43 else
44 die("std::string::compare: Invalid args");
45
46 if( n < 0 )
47 n = me.size();
48 else if( n > me.size() )
49 return 0;
50
51 if( n != size(s) )
52 return 0;
53
54 for(var i = pos; i < n; i += 1)
55 if( me._str[i] != s[i] )
56 return 0;
57 return 1;
58 },
59
60 # returns index (zero based) of first occurrence of s
61 # searching from pos
62 find_first_of: func(s, pos = 0)
63 {
64 return me._find(pos, size(me._str), s, 1);
65 },
66 find: func(s, pos = 0)
67 {
68 return me.find_first_of(s, pos);
69 },
70 find_first_not_of: func(s, pos = 0)
71 {
72 return me._find(pos, size(me._str), s, 0);
73 },
74 substr: func(pos, len = nil)
75 {
76 return substr(me._str, pos, len);
77 },
78 starts_with: func(s)
79 {
80 return me.compare(0, size(s), s);
81 },
82 size: func()
83 {
84 return size(me._str);
85 },
86 # private:
87 _eq: func(pos, s)
88 {
89 for(var i = 0; i < size(s); i += 1)
90 if( me._str[pos] == s[i] )
91 return 1;
92 return 0;
93 },
94 _find: func(first, last, s, eq)
95 {
96 if( first < 0 or last < 0 )
97 return -1;
98
99 var sign = first <= last ? 1 : -1;
100 for(var i = first; sign * i < last; i += sign)
101 if( me._eq(i, s) == eq )
102 return i;
103 return -1;
104 }
105 };
106
107 # for backward compatibility
108 var string = {parents: [String]};
109 string.new = func {
110 logprint(LOG_ALERT, "Deprecated use of std.string, please use std.String instead.");
111 return String.new(arg[0]);
112 }
113
114 # converts a string to an unsigned integer
115 var stoul = func(str, base = 10)
116 {
117 var val = 0;
118 for(var pos = 0; pos < size(str); pos += 1)
119 {
120 var c = str[pos];
121
122 if( globals.string.isdigit(c) )
123 var digval = c - `0`;
124 else if( globals.string.isalpha(c) )
125 var digval = globals.string.toupper(c) - `A` + 10;
126 else
127 break;
128
129 if( digval >= base )
130 break;
131
132 val = val * base + digval;
133 }
134
135 return val;
136 };