Jump to content

Converting Arrays (and more) Into String and Vice-Versa.


the DtTvB
 Share

Recommended Posts

This is not confirmed to be 100% working, but it works in most cases.

If you like/hate this script, if you have some way to make this better, or if you found a bug, please comment!

This function turn arrays like this:

[0] = hello

[1] = this

[2] = is

[3][0] = a

[3][1] = test

... Into a string like this:

[39=4-$5=hello$4=this$2=is[13=2-$1=a$4=test

And it can convert the string back to array.

; This function gets the value of $x and return it into this format:
;    [length]:[data]
func _serializeDataFormat($x);
    $y = string(($x));
    return stringLen($y) & '=' & ($y);
endFunc;

; This function turns a variable into an string.
func _serialize($x);
    ; Array? List list through each element.
    if (isArray($x)) then;
        local $r;
        local $b = ubound($x);
        local $i;
        $r = $b & '-'
        for $i = 0 to $b - 1;
            ; Recursively call the serialize function to collect more data.
            $r &= _serialize($x[$i]);
        next;
        ; Return it.
        return '[' & stringLen($r) & '=' & $r;
    ; Other types?
    elseIf (isBinaryString($x)) then;
        return '#' & _serializeDataFormat($x);
    elseIf (isBool($x)) then;
        if ($x) then;
            return '?1=t';
        else
            return '?1=f';
        endIf;
    elseIf (isHWnd($x)) then;
        return '*' & _serializeDataFormat($x);
    elseIf (isNumber($x)) then;
        return '&' & _serializeDataFormat($x);
    elseIf (isString($x)) then;
        return '$' & _serializeDataFormat($x);
    endIf;
endFunc;

; This function parses boolean data.
func _unserializeBool($v);
    if ($v == 't') then
        return true;
    else;
        return false;
    endIf;
endFunc;

; This function changes string back to data.
func _unserialize($x, $ia = 0);
    ; If this function was called recursively, $ia is set to 1.
    ; Don't set $ia outside this function!
    if ($ia) then
        ; Create a temp array.
        local $bnd = stringLeft($x, stringInStr($x, '-') - 1);
        local $rar[number($bnd)];
        $x = stringMid($x, stringLen($bnd) + 2);
    endIf;
    local $typ;
    local $len;
    local $dst;
    local $i = 0;
    do;
        ; Loop, loop, loop. until $x is empty.
        ; Get the data type.
        $typ = stringMid($x, 1, 1);
        $x = stringMid($x, 2);
        ; Get the length of the data.
        $len = stringLeft($x, stringInStr($x, '=') - 1);
        ; Get the data.
        $dst = stringMid($x, stringLen($len) + 2, $len);
        $x = stringMid($x, stringLen($len) + 2 + $len);
        ; Not array?
        if ($typ = '#') then;
            if ($ia) then;
                $rar[$i] = binaryString($dst);
                $i += 1;
            else;
                return binaryString($dst);
            endIf;
        elseIf ($typ = '?') then;
            if ($ia) then;
                $rar[$i] = _unserializeBool($dst);
                $i += 1;
            else;
                return _unserializeBool($dst);
            endIf;
        elseIf ($typ = '*') then;
            if ($ia) then;
                $rar[$i] = hWnd($dst);
                $i += 1;
            else;
                return hWnd($dst);
            endIf;
        elseIf ($typ = '&') then;
            if ($ia) then;
                $rar[$i] = number($dst);
                $i += 1;
            else;
                return number($dst);
            endIf;
        elseIf ($typ = '$') then;
            if ($ia) then;
                $rar[$i] = string($dst);
                $i += 1;
            else;
                return string($dst);
            endIf;
        ; Array?
        elseIf ($typ = '[') then;
            if ($ia) then;
                $rar[$i] = _unserialize($dst, 1);
                $i += 1;
            else;
                return _unserialize($dst, 1);
            endIf;
        ; Unrecognized?
        else
            if ($ia) then;
                $rar[$i] = 'Error';
                return $rar;
                $i += 1;
            else;
                return 'Error';
            endIf;
            exitLoop;
        endIf;
    until not($ia and stringLen($x) > 0)
    ; Well, return it!
    if ($ia) then;
        return $rar;
    endIf;
endFunc;
Edited by the DtTvB
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...