From e05d50215be337da22cbcb41233ce48a084acee6 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Wed, 18 Dec 2019 04:27:10 -0500 Subject: [PATCH] implement WriteVariableLengthInt32 function --- .../UniversalEditor.Core/IO/Writer.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs b/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs index 2c6291fc..13e2f5e0 100644 --- a/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs +++ b/CSharp/Libraries/UniversalEditor.Core/IO/Writer.cs @@ -242,6 +242,31 @@ namespace UniversalEditor.IO WriteInt16(values[i]); } } + + /// + /// Writes a variable-length unsigned integer to the current stream and advances the stream position by the number of bytes written. + /// + /// The value to write. + /// a representing the number of bytes written to the stream for the given + /// This code is taken from the answer on StackOverflow https://stackoverflow.com/q/3564685 + public int WriteVariableLengthInt32(int value) + { + // thx stackoverflow :) https://stackoverflow.com/q/3564685 + int count = 0; + bool first = true; + while (first || value > 0) + { + first = false; + byte lower7bits = (byte)(value & 0x7f); + value >>= 7; + if (value > 0) + lower7bits |= 128; + WriteByte(lower7bits); + count++; + } + return count; + } + /// /// Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes. ///