axelar-cgp-sui

Module 0xa5::utils

use 0x1::ascii;
use 0x1::vector;

Constants

const ALPHABET_LENGTH: u8 = 26;

const LOWERCASE_START: u8 = 97;

const NUMBERS_LENGTH: u8 = 10;

const NUMBERS_START: u8 = 48;

const SPACE: u8 = 32;

const UNDERSCORE: u8 = 95;

const UPPERCASE_START: u8 = 65;

Function is_lowercase

entry fun is_lowercase(c: u8): bool
Implementation
entry fun is_lowercase(c: u8): bool {
    c >= LOWERCASE_START && c < LOWERCASE_START + ALPHABET_LENGTH
}

Function is_uppercase

entry fun is_uppercase(c: u8): bool
Implementation
entry fun is_uppercase(c: u8): bool {
    c >= UPPERCASE_START && c < UPPERCASE_START + ALPHABET_LENGTH
}

Function is_number

entry fun is_number(c: u8): bool
Implementation
entry fun is_number(c: u8): bool {
    c >= NUMBERS_START && c < NUMBERS_START + NUMBERS_LENGTH
}

Function module_from_symbol

public(friend) fun module_from_symbol(symbol: &ascii::String): ascii::String
Implementation
public(package) fun module_from_symbol(symbol: &ascii::String): ascii::String {
    let symbolBytes = ascii::as_bytes(symbol);
    let mut moduleName = vector[];

    let (mut i, length) = (0, vector::length(symbolBytes));
    while (is_number(*vector::borrow(symbolBytes, i))) {
        i = i + 1;
    };
    while (i < length) {
        let b = *vector::borrow(symbolBytes, i);
        if (is_lowercase(b) || is_number(b)) {
            moduleName.push_back(b);
        } else if (is_uppercase(b)) {
            moduleName.push_back(b - UPPERCASE_START + LOWERCASE_START);
        } else if (b == UNDERSCORE || b == SPACE) {
            moduleName.push_back(UNDERSCORE);
        };

        i = i + 1;
    };
    ascii::string(moduleName)
}

Function decode_metadata

public(friend) fun decode_metadata(metadata: vector<u8>): (u32, vector<u8>)
Implementation
public(package) fun decode_metadata(
    mut metadata: vector<u8>,
): (u32, vector<u8>) {
    if (metadata.length() < 4) {
        (0, vector[])
    } else {
        let mut i = 0;
        let mut version: u32 = 0;
        while (i < 4) {
            version =
                (version << (8 as u8) as u32) + (metadata.remove<u8>(0) as u32);
            i = i + 1;
        };

        (version, metadata)
    }
}