summaryrefslogtreecommitdiff
path: root/npc/functions/string.txt
blob: fba5e776dca72a616482b1c776bc0e392f250ddd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Evol Script
// Author: Gumi

// safe string manipulation functions
// ** does not require PCRE


// str(<int>)
//     returns whatever is passed, converted to string

function	script	str	{
    return "" + getarg(0);
}



// startswith("<string>", "<search>")
//     returns true if <string> begins with <search>

function	script	startswith	{
    return substr(getarg(0), 0, getstrlen(getarg(1)) - 1) == getarg(1);
}



// endswith("<string>", "<search>")
//     returns true if <string> ends with <search>

function	script	endswith	{
    .@t = getstrlen(getarg(0)); // total length
    .@n = getstrlen(getarg(1)); // substring length
    return substr(getarg(0), .@t - .@n, .@t - 1) == getarg(1);
}



// capitalize("<string>")
//     returns <string> with its first letter capitalized

function	script	capitalize	{
    return setchar(getarg(0), strtoupper(charat(getarg(0), 0)), 0);
}



// titlecase("<string>" {, "<delimiter>" {, <camel>}})
//     returns <string> with the first letter of each word capitalized
//     if <camel> is true, the string is joined in a camelCase fashion

function	script	titlecase	{
    .@delimiter$ = getarg(1, " ");
    .@c = getarg(2, 0);
    explode(.@words$, getarg(0), .@delimiter$);

    for (.@i = (.@c ? 1 : 0); .@i < 255; ++.@i)
    {
        if (.@words$[.@i] == "")
        {
            break;
        }

        .@words$[.@i] = setchar(.@words$[.@i], strtoupper(charat(.@words$[.@i], 0)), 0);
    }

    return implode(.@words$, (.@c ? "" : .@delimiter$));
}



// camelcase("<string" {, "<delimiter>"})

function	script	camelcase	{
    return titlecase(getarg(0), getarg(1, " "), true);
}



// zfill("<string>" {, <width> {, "<padding>"}})
//     returns <string> padded to the left with <padding> up to width

function	script	zfill	{
    .@str$ = getarg(0);
    .@width = getarg(1, 8);
    .@padding$ = getarg(2, "0");

    for (.@s = getstrlen(.@str$); .@s < .@width; ++.@s)
    {
        .@str$ = .@padding$ + .@str$;
    }

    return .@str$;
}



// format_number(<integer> {, "<separator>"})
//     formats a number properly

function	script	format_number	{
    .@number$ = str(getarg(0));
    .@len = getstrlen(.@number$);
    .@separator$ = getarg(1, ",");

    if (getargcount() < 2 && playerattached()) {
        // get from user language
        switch (Lang) {
        case 1: .@separator$ = " "; break; // French
        case 4: .@separator$ = "."; break; // Brazilian
        default: .@separator$ = ","; // English (default)
        }
    }

    for (.@i = .@len - 3; .@i > 0; .@i -= 3) {
        .@number$ = insertchar(.@number$, .@separator$, .@i);
    }

    return .@number$;
}



// strip("<string>")
//     removes spaces at the start and end

function	script	strip	{
    .@s$ = getarg(0);
    if (.@s$ == "") {
        return "";
    }
    .@start = 0;
    .@end = getstrlen(.@s$) - 1;
    for (.@i = .@start; .@i < .@end; .@i++)
    {
        if (charat(.@s$, .@i) != " ") {
            break;
        } else {
            .@start++;
        }
    }
    for (.@i = .@end; .@i >= .@start; .@i--)
    {
        if (charat(.@s$, .@i) != " ") {
            break;
        } else {
            .@end--;
        }
    }
    //debugmes "STRIP.DEBUG MODE ENABLED BY JESUSALVA. PASSING SUBSTRING PARAMS";
    //debugmes "String \""+.@s$+"\" from "+str(.@start)+" to "+str(.@end);
    return substr(.@s$, .@start, .@end);
}



// reverse("<string>")
//     returns <string> reversed

function	script	reverse	{
    .@str$ = getarg(0);
    .@len = getstrlen(.@str$);

    for (.@i = 0; .@i < (.@len / 2); ++.@i) {
        .@tmp$ = charat(.@str$, .@i);
        .@str$ = setchar(.@str$, charat(.@str$, (.@len - 1 - .@i)), .@i); // a <= b
        .@str$ = setchar(.@str$, .@tmp$, (.@len - 1 - .@i)); // b <= a
    }

    return .@str$;
}



// repeat("<string>", <multiplier>)
//     repeats <string> many times and returns it

function	script	repeat	{
    .@mul = getarg(1);

    for (.@i = 0; .@i < .@mul; ++.@i) {
        .@str$ += getarg(0);
    }

    return .@str$;
}



// shuffle("<string>")
//     returns <string> shuffled

function	script	shuffle	{
    .@str$ = getarg(0);

    for (.@len = getstrlen(.@str$); .@len > 0; --.@len) {
        .@rnd = rand(.@len);
        .@out$ += charat(.@str$, .@rnd);
        .@str$ = delchar(.@str$, .@rnd);
    }

    return .@out$;
}