A utils code

Started by 2b2ttianxiu, Jul 02, 2023, 06:37 AM

Previous topic - Next topic

2b2ttianxiu

function str_replace(str, search, replace) {
    if (str == search) return replace
    local start = 0, res = "", pos
    while ((pos = str.find(search, start)) != null) {
        res += str.slice(start, pos) + replace
        start = pos + search.len()
    }
    if (pos == null) res += str.slice(start, str.len())
    return res
}
function str_replace_lower(str, search, replace) {
    if (str.tolower() == search.tolower()) return replace
    local start = 0, res = "", pos
    while ((pos = str.tolower().find(search.tolower(), start)) != null) {
        res += str.slice(start, pos) + replace
        start = pos + search.len()
    }
    if (pos == null) res += str.slice(start, str.len())
    return res
}
function str_count(str, key) {
    local n = 0, s = 0
    while ((s = str.find(key, s)) != null) {
        n++
        s++
    }
    return n
}
function executeCommand(player, command) {
    if (command.len() == 1) return;
    local cmd = command.slice(str_startswith(command, "/") ? 1 : 0, command.find(" ",0) == null ? command.len() : command.find(" ",0)), text = command.find(" ",0) != null ? command.slice(command.find(" ",0)) : null
    return onPlayerCommand(player, cmd, text) == 1 ? true : false
}
some code. u can take them

2b2ttianxiu

sort array
function sort(arr, compareTo = null) {
    function String_compareTo(a, b) {
        for (local i = 0; i < ((a + "").len() <= (b + "").len() ? (a + "").len() : (b + "").len()); i++) {
            if ((a + "")[i] != (b + "")[i]) {
                return (a + "")[i] - (b + "")[i] < 0 ? -1 : 1
            }
        }
        return 0
    }
    function Number_compareTo(a, b) {
        return a - b < 0 ? 1 : -1
    }
    local number = true
    foreach (v in arr) if (!IsNum(v)) number = false
    if (compareTo == null) compareTo = number ? Number_compareTo : String_compareTo
    for (local i = 0; i < arr.len() - 1; i++) {
        for (local j = 0; j < arr.len() - i - 1; j++) {
            if (compareTo(arr[j], arr[j + 1]) > 0){
                local temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    return arr;
}
function sort_in_table(arr, key_, compareTo = null) {
    function String_compareTo(a, b, key) {
        for (local i = 0; i < ((a[key] + "").len() <= (b[key] + "").len() ? (a[key] + "").len() : (b[key] + "").len()); i++) {
            if ((a[key] + "")[i] != (b[key]+ "")[i]) {
                return (a[key] + "")[i] - (b[key] + "")[i] < 0 ? -1 : 1
            }
        }
        return 0
    }
    function Number_compareTo(a, b, key) {
        return a[key] - b[key] < 0.0 ? 1 : -1
    }
    local keys = []
    if (type(key_) != "array") {
        keys.append(key_)
    } else keys = key_
    foreach (key in keys) {
        local number = true
        foreach (v in arr) if (!IsNum(v[key])) number = false
        if (compareTo == null) compareTo = (number ? Number_compareTo : String_compareTo)
        for (local i = 0; i < arr.len() - 1; i++) {
            for (local j = 0; j < arr.len() - i - 1; j++) {
            if (compareTo(arr[j], arr[j + 1], key) > 0){
                local temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
            }
        }
    }
    return arr;
}
function str_complie(str1, str2) {
    local charbuf1 = 0, charbuf2 = 0, cur = 0, t = 0
    while (cur < str1.len() && cur < str2.len()) {
        t = str[1] ^ str[2]
        if (t != 0) return t < 0 ? -1 : 1
    }
    return 0
}

PSL

Thanks for sharing some useful code, but
This is my code
    local arr=[1,6,2,78,123,1,23,2,5];
    local arr2=sort(arr);
    for(local i=0;i<arr2.len();i++) print(arr2[i]);

In the sort function, an error occurred on this line:
foreach (v in arr) if (!IsNum(v)) number = false
error is: AN ERROR HAS OCCURED [parameter 1 has an invalid type 'integer' ; expected: 'string']

2b2ttianxiu

Quote from: PSL on Jul 02, 2023, 10:53 AMThanks for sharing some useful code, but
This is my code
    local arr=[1,6,2,78,123,1,23,2,5];
    local arr2=sort(arr);
    for(local i=0;i<arr2.len();i++) print(arr2[i]);

In the sort function, an error occurred on this line:
foreach (v in arr) if (!IsNum(v)) number = false
error is: AN ERROR HAS OCCURED [parameter 1 has an invalid type 'integer' ; expected: 'string']
old code, outdated, new utils code will be release