game-makergmlgame-maker-language

Deleting tags from a string


I'm trying to remake the script below so that it does not draw text without the tags [a=...] and [/a], but just delete them from the string.

I made this script but it removes everything inside the tags, removes that between the tag and between the two [a=...] ... [/a] tags.

var xx, yy, str, st, et, ct, l, h, stl, pc, dx, dy, dp, p, c, ds, i;

str = argument0;
st = '[c=';
et = ']';
ct = '[/c]';

l = string_length(str);
h = string_height(' ');
stl = string_length(st);
dp = 1;

result = "";

for (p = 1; p <= l; p += 1) {
    c = string_char_at(str, p);
    if (c == chr(10) || p == l || (c == '#' && string_char_at(str, p - 1) != '\')) {
        result = (string_copy(str, dp, p - dp + 1));
        dp = p + 1;
    } else if (c == string_char_at(st, 1) || c == string_char_at(ct, 1)) {
        if (p + string_length(st) <= l && string_copy(str, p, string_length(st)) == st) {
            ds = string_copy(str, dp, p - dp);
            string_insert(ds, result, dp);
            i = string_copy(str, p + stl, string_pos(et, string_delete(str, 1, p + stl)));
            p += string_length(i + et + st) - 1;
            dp = p + 1;
        } else if (p + string_length(st) <= l && string_copy(str, p, string_length(ct)) == ct) {
            ds = string_copy(str, dp, p - dp);
            string_insert(ds, result, dp);
            p += string_length(ct) - 1;
            dp = p + 1;
        }
    }
}
return (result);

My script should remove tags from string without removing the string inside the tags.

code:

hello, this is [a=1] alpha tag [/a]!

result:

hello, this is alpha tag !


Solution

  • It is possible to do away with a bunch of string_pos and string_copy/string_delete

    var s = argument0;
    var st = '[a=',  stl = string_length(st) - 1;
    var et = ']',    etl = string_length(et) - 1;
    for (var p = string_pos(st, s); p != 0; p = string_pos(st, s)) {
        // suppose "ab[a=1]cd[/a]ef"
        var s0 = string_copy(s, 1, p - 1); // "ab"
        var s1 = string_delete(s, 1, p + stl); // "1]cd[/a]ef"
        p = string_pos(et, s1);
        s = s0 + string_delete(s1, 1, p + etl); // "cd[/a]ef"
    }
    s = string_replace_all(s, '[/a]', '');
    return s;