diff options
| author | TSR Berry <20988865+TSRBerry@users.noreply.github.com> | 2023-04-08 01:22:00 +0200 |
|---|---|---|
| committer | Mary <thog@protonmail.com> | 2023-04-27 23:51:14 +0200 |
| commit | cee712105850ac3385cd0091a923438167433f9f (patch) | |
| tree | 4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Spv.Generator/LiteralString.cs | |
| parent | cd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff) | |
Move solution and projects to src
Diffstat (limited to 'src/Spv.Generator/LiteralString.cs')
| -rw-r--r-- | src/Spv.Generator/LiteralString.cs | 54 |
1 files changed, 54 insertions, 0 deletions
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<LiteralString> + { + 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<byte> 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; + } +} |
