From cee712105850ac3385cd0091a923438167433f9f Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Sat, 8 Apr 2023 01:22:00 +0200 Subject: Move solution and projects to src --- src/Spv.Generator/LiteralString.cs | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Spv.Generator/LiteralString.cs (limited to 'src/Spv.Generator/LiteralString.cs') diff --git a/src/Spv.Generator/LiteralString.cs b/src/Spv.Generator/LiteralString.cs new file mode 100644 index 00000000..629ff7bf --- /dev/null +++ b/src/Spv.Generator/LiteralString.cs @@ -0,0 +1,54 @@ +using System; +using System.IO; +using System.Text; + +namespace Spv.Generator +{ + public class LiteralString : Operand, IEquatable + { + public OperandType Type => OperandType.String; + + private string _value; + + public LiteralString(string value) + { + _value = value; + } + + public ushort WordCount => (ushort)(_value.Length / 4 + 1); + + public void WriteOperand(BinaryWriter writer) + { + writer.Write(_value.AsSpan()); + + // String must be padded to the word size (which is 4 bytes). + int paddingSize = 4 - (Encoding.ASCII.GetByteCount(_value) % 4); + + Span padding = stackalloc byte[paddingSize]; + + writer.Write(padding); + } + + public override bool Equals(object obj) + { + return obj is LiteralString literalString && Equals(literalString); + } + + public bool Equals(LiteralString cmpObj) + { + return Type == cmpObj.Type && _value.Equals(cmpObj._value); + } + + public override int GetHashCode() + { + return DeterministicHashCode.Combine(Type, DeterministicHashCode.GetHashCode(_value)); + } + + public bool Equals(Operand obj) + { + return obj is LiteralString literalString && Equals(literalString); + } + + public override string ToString() => _value; + } +} -- cgit v1.2.3