Quick actions

cmd+k|ctrl+k

Navigation

Languages

VLQ Parser

Snippet info

Language

Csharp

Visibility

public

Author

levi

Created

2016-12-07T14:15:38Z

Updated

2016-12-07T17:01:48Z

using System;
using System.Text;
using System.Collections.Generic;

/* TO USE - enter a comma-delimited sequence of bytes into the Stdin section below and then press RUN. */

class MainClass {
    static void Main() {
        int[] ints = CreateIntegerArray();
        {
            var tuple = ints.ParseVlq();
            Console.WriteLine("Parse unsigned: {0}, Rest of VQL: {1}", tuple.Item1, tuple.Item2);
        }
        {
            var tuple = ints.ParseSignedVlq();
            Console.WriteLine("Parse signed: {0}, Rest of VQL: {1}", tuple.Item1, tuple.Item2);
        }
        {
            var tuple = ints.ParseStupidStarboundSignedVlq();
            Console.WriteLine("Parse stupid starbound signed: {0}, Rest of VQL: {1}", tuple.Item1, tuple.Item2);
        }
    }
    
    static int[] CreateIntegerArray() {
        string[] x = Console.ReadLine().Split(',');
        List<int> list = new List<int>();
        foreach(string s in x) {
            list.Add(Convert.ToInt32(s.Trim()));
        }
        return list.ToArray();
    }
}

static class IntArrayExtensions
{
    public static Tuple<ulong, int[]> ParseVlq(this int[] ints)
    {
        ulong ret = 0;
        int[] rest = null;

        for(int index = 0; index < ints.Length; ++index)
        {
            ret = ret << 7;

            ret += (ulong)(ints[index] & 0x7F);

            if ((ints[index] & 0x80) == 0 && index + 1 < ints.Length)
            {
                rest = new int[ints.Length - index - 1];
                Array.Copy(ints, index + 1, rest, 0, rest.Length);
                break;
            }
        }

        return Tuple.Create(ret, rest);
    }

    public static Tuple<long, int[]> ParseSignedVlq(this int[] ints)
    {
        long ret = 0;
        int[] rest = null;
        int negative = 1;

        for (int index = 0; index < ints.Length; ++index)
        {
            if (index == 0)
            {
                ret = ret << 6;

                ret += (long)(ints[index] & 0x3F);

                if ((ints[index] & 0x40) == 1)
                {
                    negative = -1;
                }
            }
            else
            {
                ret = ret << 7;

                ret += (long)(ints[index] & 0x7F);
            }

            if ((ints[index] & 0x80) == 0 && index + 1 < ints.Length)
            {
                rest = new int[ints.Length - index - 1];
                Array.Copy(ints, index + 1, rest, 0, rest.Length);
                break;
            }
        }

        return Tuple.Create((ret * negative), rest);
    }
    
    public static Tuple<long, int[]> ParseStupidStarboundSignedVlq(this int[] ints) {
        var tuple = ints.ParseVlq();
        long ret = (long)(tuple.Item1 >> 1);
        if((tuple.Item1 & 1) != 0) {
            ret *= -1;
            ret += -1;
        }
        return Tuple.Create(ret, tuple.Item2);
    }
    
    public static string ToCommaList(this int[] ints) {
        StringBuilder str = new StringBuilder();
        foreach(int i in ints ?? new int[] { }) {
            if(str.Length == 0) {
                str.AppendFormat("{0}", i);
            }
            else {
                str.AppendFormat(", {0}", i);
            }
        }
        return str.ToString();
    }
}
INFO