public void ReadTest() { ValueStringReader sc = new ValueStringReader("Hello"); Assert.IsTrue(sc.HasNext); Assert.AreEqual('H', sc.Read()); Assert.AreEqual('e', sc.Read()); Assert.AreEqual('l', sc.Read()); Assert.AreEqual('l', sc.Read()); Assert.AreEqual('o', sc.Read()); Assert.IsFalse(sc.HasNext); }
public void NextTest() { ValueStringReader sc = new ValueStringReader("Hello"); Assert.AreEqual('H', sc.Next); sc.Read(); Assert.AreEqual('e', sc.Next); sc.Read(); Assert.AreEqual('l', sc.Next); sc.Read(); Assert.AreEqual('l', sc.Next); sc.Read(); Assert.AreEqual('o', sc.Next); sc.Read(); Assert.IsNull(sc.Next); }
public void PrevTest() { ValueStringReader sc = new ValueStringReader("Hello"); Assert.IsNull(sc.Prev); sc.Read(); Assert.IsNull(sc.Prev); sc.Read(); Assert.AreEqual('H', sc.Prev); sc.Read(); Assert.AreEqual('e', sc.Prev); sc.Read(); Assert.AreEqual('l', sc.Prev); sc.Read(); Assert.AreEqual('l', sc.Prev); sc.Read(); Assert.AreEqual('l', sc.Prev); Assert.AreEqual('o', sc.Current); Assert.IsFalse(sc.HasNext); }
/// <summary> /// Gets an array with all the tokens of the given expression. /// </summary> /// <param name="expression">The expression to split.</param> /// <returns>An array with the tokens of the expression.</returns> public static string[] ToStringArray(string expression) { if (string.IsNullOrWhiteSpace(expression)) { return(Array.Empty <string>()); } List <string> tokens = new List <string>(); ValueStringReader scanner = new ValueStringReader(expression); char curChar; while (scanner.HasNext) { curChar = scanner.Read(); if (char.IsLetter(curChar)) { StringBuilder sb = StringBuilderCache.Acquire(); do { sb.Append(curChar); char?nextChar = scanner.Next; if (nextChar != null && char.IsLetterOrDigit(nextChar.Value)) { curChar = scanner.Read(); } else { break; } }while (true); tokens.Add(StringBuilderCache.ToStringAndRelease(ref sb !)); } else if (char.IsDigit(curChar)) { StringBuilder sb = StringBuilderCache.Acquire(); bool hasDecimalPoint = false; do { if (curChar == '.' && !hasDecimalPoint) { hasDecimalPoint = true; } sb.Append(curChar); char?nextChar = scanner.Next; if (nextChar != null && (char.IsDigit(nextChar.Value) || (nextChar == '.' && !hasDecimalPoint))) { curChar = scanner.Read(); } else { break; } }while (true); tokens.Add(StringBuilderCache.ToStringAndRelease(ref sb !)); } else { if (char.IsWhiteSpace(curChar)) { continue; } tokens.Add(curChar.ToString()); } } return(tokens.ToArray()); }