Ryujinx/Ryujinx.HLE/Debugger/StringStream.cs

109 lines
2.6 KiB
C#
Raw Normal View History

2022-02-09 21:29:53 +00:00
using System.Diagnostics;
using System.Globalization;
2022-02-08 19:50:26 +00:00
namespace Ryujinx.HLE.Debugger
{
class StringStream
{
private readonly string Data;
private int Position;
public StringStream(string s)
{
Data = s;
}
public char ReadChar()
{
return Data[Position++];
}
public string ReadUntil(char needle)
{
int needlePos = Data.IndexOf(needle, Position);
if (needlePos == -1)
{
needlePos = Data.Length;
}
string result = Data.Substring(Position, needlePos - Position);
Position = needlePos + 1;
return result;
}
public string ReadLength(int len)
{
string result = Data.Substring(Position, len);
Position += len;
return result;
}
public string ReadRemaining()
{
string result = Data.Substring(Position);
Position = Data.Length;
return result;
}
public ulong ReadRemainingAsHex()
{
return ulong.Parse(ReadRemaining(), NumberStyles.HexNumber);
}
public ulong ReadUntilAsHex(char needle)
{
return ulong.Parse(ReadUntil(needle), NumberStyles.HexNumber);
}
public ulong ReadLengthAsHex(int len)
{
return ulong.Parse(ReadLength(len), NumberStyles.HexNumber);
}
2022-02-09 21:29:53 +00:00
public ulong ReadLengthAsLEHex(int len)
{
Debug.Assert(len % 2 == 0);
ulong result = 0;
int pos = 0;
while (pos < len)
{
result += ReadLengthAsHex(2) << (4 * pos);
pos += 2;
}
return result;
}
2022-02-13 13:59:58 +00:00
2022-02-13 14:36:47 +00:00
public ulong? ReadRemainingAsThreadUid()
2022-02-13 13:59:58 +00:00
{
string s = ReadRemaining();
2022-02-13 14:36:47 +00:00
return s == "-1" ? null : ulong.Parse(s, NumberStyles.HexNumber);
2022-02-13 13:59:58 +00:00
}
2022-02-09 21:29:53 +00:00
public bool ConsumePrefix(string prefix)
{
if (Data.Substring(Position).StartsWith(prefix))
{
Position += prefix.Length;
return true;
}
return false;
}
public bool ConsumeRemaining(string match)
{
if (Data.Substring(Position) == match)
{
Position += match.Length;
return true;
}
return false;
}
2022-02-08 19:50:26 +00:00
public bool IsEmpty()
{
return Position >= Data.Length;
}
}
2022-02-13 13:59:58 +00:00
}