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