Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/1 Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
34 lines
589 B
C#
34 lines
589 B
C#
using CanonSharp.Common.Abstractions;
|
|
|
|
namespace CanonSharp.Common.Reader;
|
|
|
|
public class StringReader(string source) : ISourceReader
|
|
{
|
|
private int _pos;
|
|
|
|
public char Read()
|
|
{
|
|
if (_pos >= source.Length)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
char result = source[_pos];
|
|
_pos += 1;
|
|
return result;
|
|
|
|
}
|
|
|
|
public bool TryPeek(out char c)
|
|
{
|
|
if (_pos < source.Length)
|
|
{
|
|
c = source[_pos];
|
|
return true;
|
|
}
|
|
|
|
c = char.MinValue;
|
|
return false;
|
|
}
|
|
}
|