Sha256: fed61e5dcffb7fbc5be93ba0f262b59d8bd63e394429cfee23ff612dda86cf0d

Contents?: true

Size: 1.51 KB

Versions: 368

Compression:

Stored size: 1.51 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;

public static class VariableLengthQuantity
{
    private const uint SevenBitsMask = 0x7fu;
    private const uint EightBitMask = 0x80u;

    public static uint[] ToBytes(uint[] numbers) => numbers.SelectMany(ToBytesSingle).ToArray();

    private static IEnumerable<uint> ToBytesSingle(uint number)
    {
        if (number == 0)
        {
            return new[] { 0u };
        }

        var bytes = new List<uint>(4);

        while (number > 0)
        {
            var tmp = number & SevenBitsMask;
            number >>= 7;

            if (bytes.Any())
            {
                tmp |= EightBitMask;
            }

            bytes.Add(tmp);
        }

        bytes.Reverse();
        return bytes;
    }

    public static uint[] FromBytes(uint[] bytes)
    {
        var numbers = new List<uint>();
        var tmp = 0u;

        for (var index = 0; index < bytes.Length; index++)
        {
            if ((tmp & 0xfe000000u) > 0)
            {
                throw new InvalidOperationException("Overflow exception.");
            }

            var b = bytes[index];
            tmp = (tmp << 7) | (b & 0x7fu);

            if ((b & 0x80) == 0)
            {
                numbers.Add(tmp);
                tmp = 0;
            }
            else if (index == bytes.Length - 1)
            {
                throw new InvalidOperationException("Incomplete byte sequence.");
            }
        }
        
        return numbers.ToArray();
    }
}

Version data entries

368 entries across 368 versions & 1 rubygems

Version Path
trackler-2.2.1.70 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.69 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.68 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.67 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.66 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.65 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.64 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.63 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.62 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.61 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.60 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.59 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.58 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.57 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.56 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.55 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.54 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.53 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.52 tracks/csharp/exercises/variable-length-quantity/Example.cs
trackler-2.2.1.51 tracks/csharp/exercises/variable-length-quantity/Example.cs