Created
July 5, 2022 19:47
-
-
Save audinue/b3e48db3c1411a47c68ab2987df78105 to your computer and use it in GitHub Desktop.
.NET idiomatic scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.IO; | |
public class Scanner | |
{ | |
TextReader Reader; | |
StringBuilder Builder; | |
public Scanner(TextReader reader) | |
{ | |
Reader = reader; | |
Builder = new StringBuilder(); | |
} | |
public bool MoveNext() | |
{ | |
Builder.Length = 0; | |
while (true) | |
{ | |
int c = Reader.Peek(); | |
if (c == -1) | |
break; | |
if (!char.IsWhiteSpace((char)c)) | |
break; | |
} | |
while (true) | |
{ | |
int c = Reader.Read(); | |
if (c == -1) | |
break; | |
if (char.IsWhiteSpace((char)c)) | |
break; | |
Builder.Append((char)c); | |
} | |
return Builder.Length > 0; | |
} | |
public string Current | |
{ | |
get { return Builder.ToString(); } | |
} | |
public Boolean CurrentBoolean | |
{ | |
get { return Boolean.Parse(Current); } | |
} | |
public char CurrentChar | |
{ | |
get { return char.Parse(Current); } | |
} | |
public byte CurrentByte | |
{ | |
get { return byte.Parse(Current); } | |
} | |
public sbyte CurrentSByte | |
{ | |
get { return sbyte.Parse(Current); } | |
} | |
public Int16 CurrentInt16 | |
{ | |
get { return Int16.Parse(Current); } | |
} | |
public UInt16 CurrentUInt16 | |
{ | |
get { return UInt16.Parse(Current); } | |
} | |
public Int32 CurrentInt32 | |
{ | |
get { return Int32.Parse(Current); } | |
} | |
public UInt32 CurrentUInt32 | |
{ | |
get { return UInt32.Parse(Current); } | |
} | |
public Int64 CurrentInt64 | |
{ | |
get { return Int64.Parse(Current); } | |
} | |
public UInt64 CurrentUInt64 | |
{ | |
get { return UInt64.Parse(Current); } | |
} | |
public Single CurrentSingle | |
{ | |
get { return Single.Parse(Current); } | |
} | |
public Double CurrentDouble | |
{ | |
get { return Double.Parse(Current); } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
class ScannerExample | |
{ | |
static void Main(string[] args) | |
{ | |
Scanner s = new Scanner(new StringReader("Hello world!")); | |
while (s.MoveNext()) | |
Console.WriteLine(s.Current); | |
Console.Read(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment