1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-19 19:12:05 +00:00
Ryujinx/src/Spv.Generator/LiteralString.cs
TSRBerry 2989c163a8
editorconfig: Set default encoding to UTF-8 (#5793)
* editorconfig: Add default charset

* Change file encoding from UTF-8-BOM to UTF-8
2023-12-04 14:17:13 +01:00

54 lines
1.4 KiB
C#

using System;
using System.IO;
using System.Text;
namespace Spv.Generator
{
public class LiteralString : IOperand, IEquatable<LiteralString>
{
public OperandType Type => OperandType.String;
private readonly 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(IOperand obj)
{
return obj is LiteralString literalString && Equals(literalString);
}
public override string ToString() => _value;
}
}