public void Words() { #region Words MyStringReader r = new MyStringReader(" (skip leading,\r\n\tand trailing\vwhitespace) "); string[] words = r.Words().ToArray(); Assert.IsTrue(r.WasDisposed); Assert.AreEqual(5, words.Length); Assert.AreEqual("(skip", words [0]); Assert.AreEqual("leading,", words [1]); Assert.AreEqual("and", words [2]); Assert.AreEqual("trailing", words [3]); Assert.AreEqual("whitespace)", words [4]); r = new MyStringReader("notext"); words = r.Words(TextReaderRocksOptions.None).ToArray(); Assert.IsFalse(r.WasDisposed); Assert.AreEqual(1, words.Length); Assert.AreEqual("notext", words [0]); r = new MyStringReader("1 2 3 4"); Assert.AreEqual("1", r.Words().First()); Assert.AreEqual("2", r.Words().First()); Assert.AreEqual("3", r.Words().First()); Assert.AreEqual("4", r.Words().First()); #endregion }
private object GetKeywordFromString(MyStringReader jsonString, char ch) { switch (ch) { case 't': if (CheckArray(jsonString.ReadChars(3), TRUE_ARRAY)) { return(true); } goto default; case 'f': if (CheckArray(jsonString.ReadChars(4), FALSE_ARRAY)) { return(false); } goto default; case 'n': if (CheckArray(jsonString.ReadChars(3), NULL_ARRAY)) { return(null); } goto default; default: throw new Exception("Invalid JSON document."); } }
public void SetState(string state) { var sr = new MyStringReader(state); SourceUri = new Uri(sr.ReadLine()); DestFilePath = sr.ReadLine(); sr.ReadLine(); // skip Downloaded, which should be calculated from Ranges Length = long.Parse(sr.ReadLine()); string line; while (!string.IsNullOrEmpty(line = sr.ReadLine())) { var splits = line.Split(seperator, 4); var range = new Range { Offset = long.Parse(splits[0]), Current = long.Parse(splits[1]) }; range.Length = long.Parse(splits[2]) - range.Offset + 1; Ranges.Add(range); Downloaded += range.Current; } if (Downloaded == Length) { State = DownloadState.Success; } }
private object GetNumberFromString(MyStringReader jsonString, char ch) { StringBuilder sb = new StringBuilder(); sb.Append(ch); bool isFloatingPoint = false; while (true) { ch = jsonString.ReadChar(); if (!((ch >= '0' && ch <= '9') || ch == '.' || (isFloatingPoint && (ch == 'e' || ch == 'E' || ch == '-' || ch == '+')))) { jsonString.Position -= 1; break; } sb.Append(ch); if (ch == '.' && !isFloatingPoint) { isFloatingPoint = true; } else if (ch == '.' && isFloatingPoint) { throw new Exception("Invalid JSON document."); } } if (!isFloatingPoint) { int temp; if (int.TryParse(sb.ToString(), out temp)) { return(temp); } else { throw new Exception("Invalid JSON document."); } } else { double temp; if (double.TryParse(sb.ToString(), out temp)) { return(temp); } else { throw new Exception("Invalid JSON document."); } } }
private string GetStringFromString(MyStringReader jsonString) { char ch; StringBuilder sb = new StringBuilder(); bool backslashMode = false; while (true) { ch = jsonString.ReadChar(); if (backslashMode) { switch (ch) { case 'n': sb [sb.Length - 1] = '\n'; break; case 'r': sb [sb.Length - 1] = '\r'; break; case 't': sb [sb.Length - 1] = '\t'; break; case '\\': sb [sb.Length - 1] = '\\'; break; case '"': sb [sb.Length - 1] = '"'; break; case '/': sb [sb.Length - 1] = '/'; break; case 'b': sb [sb.Length - 1] = '\b'; break; case 'f': sb [sb.Length - 1] = '\f'; break; case 'u': sb.Remove(sb.Length - 1, 1); sb.Append(ToNumberFromHexa(jsonString.ReadChars(2))); sb.Append(ToNumberFromHexa(jsonString.ReadChars(2))); break; default: throw new Exception(); } backslashMode = false; } else if (ch == '"') { break; } else { sb.Append(ch); if (ch == '\\') { backslashMode = true; } } } return(sb.ToString()); }
public void Tokens() { #region Tokens var r = new MyStringReader("(append 3.5 \"hello, world!\")"); var words = r.Tokens( (p, c) => char.IsLetterOrDigit(c) || c == '.', (p, c) => !char.IsWhiteSpace(c)) .ToArray(); Assert.IsTrue(r.WasDisposed); Assert.IsTrue( new[] { "(", "append", "3.5", "\"", "hello", ",", "world", "!\")" } .SequenceEqual(words)); r = new MyStringReader("Hello, world!"); Assert.AreEqual(false, r.Tokens(TextReaderRocksOptions.None, (p, c) => false).Any()); Assert.IsFalse(r.WasDisposed); #endregion }
public void Lines() { MyStringReader r = new MyStringReader ("hello\nout\rthere\r\nin\nTV\nland!"); string[] lines = r.Lines ().ToArray (); Assert.IsTrue (r.WasDisposed); Assert.AreEqual (6, lines.Length); Assert.AreEqual ("hello", lines [0]); Assert.AreEqual ("out", lines [1]); Assert.AreEqual ("there", lines [2]); Assert.AreEqual ("in", lines [3]); Assert.AreEqual ("TV", lines [4]); Assert.AreEqual ("land!", lines [5]); r = new MyStringReader ("\nhello\n\nworld!"); lines = r.Lines (TextReaderCodaOptions.None).ToArray (); Assert.IsFalse (r.WasDisposed); Assert.AreEqual (4, lines.Length); Assert.AreEqual ("", lines [0]); Assert.AreEqual ("hello", lines [1]); Assert.AreEqual ("", lines [2]); Assert.AreEqual ("world!", lines [3]); }
public void Lines() { MyStringReader r = new MyStringReader("hello\nout\rthere\r\nin\nTV\nland!"); string[] lines = r.Lines().ToArray(); Assert.IsTrue(r.WasDisposed); Assert.AreEqual(6, lines.Length); Assert.AreEqual("hello", lines [0]); Assert.AreEqual("out", lines [1]); Assert.AreEqual("there", lines [2]); Assert.AreEqual("in", lines [3]); Assert.AreEqual("TV", lines [4]); Assert.AreEqual("land!", lines [5]); r = new MyStringReader("\nhello\n\nworld!"); lines = r.Lines(TextReaderRocksOptions.None).ToArray(); Assert.IsFalse(r.WasDisposed); Assert.AreEqual(4, lines.Length); Assert.AreEqual("", lines [0]); Assert.AreEqual("hello", lines [1]); Assert.AreEqual("", lines [2]); Assert.AreEqual("world!", lines [3]); }
private JsonContainer ParseString(MyStringReader jsonString) { try { ParseState parseMode = ParseState.None; ParseState parseState = ParseState.None; Queue <object> tokenStack = new Queue <object> (); while (true) { char rc = '\0'; do { rc = jsonString.ReadChar(); } while (rc == ' ' || rc == ' '|| rc == ' ' || rc == '\n' || rc == '\r'); if (rc == ':') { if (!(parseMode == ParseState.Object && parseState == ParseState.Key)) { throw new Exception("Invalid JSON document."); } parseState = ParseState.Value; } else if (rc == ',') { if (parseState != ParseState.Value) { throw new Exception("Invalid JSON document."); } parseState = (parseMode == ParseState.Object) ? ParseState.Key : ParseState.Value; } else if (rc == '"') { tokenStack.Enqueue(GetStringFromString(jsonString)); } else if (rc == 't' || rc == 'f' || rc == 'n') { tokenStack.Enqueue(GetKeywordFromString(jsonString, rc)); } else if ((rc >= '0' && rc <= '9') || rc == '-' || rc == '+') { tokenStack.Enqueue(GetNumberFromString(jsonString, rc)); } else if (rc == '{' || rc == '[') { if (parseState == ParseState.Value) { jsonString.Position -= 1; tokenStack.Enqueue(ParseString(jsonString)); } else { switch (rc) { case '{': parseMode = ParseState.Object; parseState = ParseState.Key; break; case '[': parseMode = ParseState.Array; parseState = ParseState.Value; break; } } } else if ((rc == '}' && parseMode == ParseState.Object) || (rc == ']' && parseMode == ParseState.Array)) { break; } else { throw new ArgumentException("Invalid JSON document."); } } return(Build(parseMode, tokenStack)); } catch { throw new ArgumentException("Invalid JSON document."); } }
public void Tokens() { #region Tokens var r = new MyStringReader ("(append 3.5 \"hello, world!\")"); var words = r.Tokens ( (p, c) => char.IsLetterOrDigit (c) || c == '.', (p, c) => !char.IsWhiteSpace (c)) .ToArray (); Assert.IsTrue (r.WasDisposed); Assert.IsTrue ( new[]{"(", "append", "3.5", "\"", "hello", ",", "world", "!\")"} .SequenceEqual (words)); r = new MyStringReader ("Hello, world!"); Assert.AreEqual (false, r.Tokens (TextReaderCodaOptions.None, (p, c) => false).Any ()); Assert.IsFalse (r.WasDisposed); #endregion }
public void Words() { #region Words MyStringReader r = new MyStringReader (" (skip leading,\r\n\tand trailing\vwhitespace) "); string[] words = r.Words ().ToArray (); Assert.IsTrue (r.WasDisposed); Assert.AreEqual (5, words.Length); Assert.AreEqual ("(skip", words [0]); Assert.AreEqual ("leading,", words [1]); Assert.AreEqual ("and", words [2]); Assert.AreEqual ("trailing", words [3]); Assert.AreEqual ("whitespace)", words [4]); r = new MyStringReader ("notext"); words = r.Words (TextReaderCodaOptions.None).ToArray (); Assert.IsFalse (r.WasDisposed); Assert.AreEqual (1, words.Length); Assert.AreEqual ("notext", words [0]); r = new MyStringReader ("1 2 3 4"); Assert.AreEqual ("1", r.Words ().First ()); Assert.AreEqual ("2", r.Words ().First ()); Assert.AreEqual ("3", r.Words ().First ()); Assert.AreEqual ("4", r.Words ().First ()); #endregion }