public void CallingNextAtEndOfLineThrowsException()
        {
            string input = @"First line, second statement, third statement";

            using (var s = new TextScanner(input)
                           .UseDelimiter(@",\s*"))
            {
                Assert.That(s.Next(), Is.EqualTo("First line"));
                Assert.That(s.NextLine(), Is.EqualTo(", second statement, third statement"));
                Assert.Catch <InvalidOperationException>(() => s.Next());
            }
        }
        public void CanTranslateIndividualTokens()
        {
            double sum = 0;

            using (var s = new TextScanner(new StreamReader("usnumbers.txt")))
            {
                s.UseCulture(new CultureInfo("en-US"));

                int count = 0;

                while (s.HasNext())
                {
                    if (s.HasNextDouble())
                    {
                        sum += s.NextDouble();
                    }
                    else
                    {
                        s.Next();
                    }

                    Assert.That(count++ < 100);
                }
            }

            Assert.That(sum, Is.EqualTo(1032778.74159));
        }
Exemple #3
0
        private static void Main(string[] args)
        {
            TextScanner s   = null;
            double      sum = 0;

            try
            {
                s = new TextScanner(new StreamReader("usnumbers.txt"));
                s.UseCulture(new CultureInfo("en-US"));

                while (s.HasNext())
                {
                    if (s.HasNextDouble())
                    {
                        sum += s.NextDouble();
                    }
                    else
                    {
                        s.Next();
                    }
                }
            }
            finally
            {
                if (s != null)
                {
                    s.Close();
                }
            }

            Console.WriteLine(sum);
        }
Exemple #4
0
        private static void Main(string[] args)
        {
            TextScanner s = null;
            double sum = 0;

            try
            {
                s = new TextScanner(new StreamReader("usnumbers.txt"));
                s.UseCulture(new CultureInfo("en-US"));

                while (s.HasNext())
                {
                    if (s.HasNextDouble())
                    {
                        sum += s.NextDouble();
                    }
                    else
                    {
                        s.Next();
                    }
                }
            }
            finally
            {
                if (s != null)
                {
                    s.Close();
                }
            }

            Console.WriteLine(sum);
        }
        public void CanTranslateAllTokenTypes()
        {
            string input = "text 9999999999999999999999999999 -32768 2,147,483,647 -9,223,372,036,854,775,808 129 NaN -3.402823e38 true 65,535 4,294,967,295 18,446,744,073,709,551,615 -128";

            using (var s = new TextScanner(input)
                           .UseCulture(new CultureInfo("en-US")))
            {
                Assert.That(s.HasNext(), Is.True);
                Assert.That(s.Next(), Is.EqualTo("text"));

                Assert.That(s.HasNextDecimal(), Is.True);
                Assert.That(s.NextDecimal(), Is.EqualTo(9999999999999999999999999999m));

                Assert.That(s.HasNextInt16(), Is.True);
                Assert.That(s.NextInt16(), Is.EqualTo((short)-32768));

                Assert.That(s.HasNextInt32(), Is.True);
                Assert.That(s.NextInt32(), Is.EqualTo(2147483647));

                Assert.That(s.HasNextInt64(), Is.True);
                Assert.That(s.NextInt64(), Is.EqualTo(-9223372036854775808L));

                Assert.That(s.HasNextByte(), Is.True);
                Assert.That(s.NextByte(), Is.EqualTo((byte)129));

                Assert.That(s.HasNextDouble(), Is.True);
                Assert.That(s.NextDouble(), Is.EqualTo(double.NaN));

                Assert.That(s.HasNextSingle(), Is.True);
                Assert.That(s.NextSingle(), Is.EqualTo(-3.402823e38f));

                Assert.That(s.HasNextBoolean(), Is.True);
                Assert.That(s.NextBoolean(), Is.EqualTo(true));

                Assert.That(s.HasNextUInt16(), Is.True);
                Assert.That(s.NextUInt16(), Is.EqualTo((ushort)65535));

                Assert.That(s.HasNextUInt32(), Is.True);
                Assert.That(s.NextUInt32(), Is.EqualTo((uint)4294967295U));

                Assert.That(s.HasNextUInt64(), Is.True);
                Assert.That(s.NextUInt64(), Is.EqualTo((ulong)18446744073709551615U));

                Assert.That(s.HasNextSByte(), Is.True);
                Assert.That(s.NextSByte(), Is.EqualTo((sbyte)-128));
            }

            using (var s = new TextScanner("5/1/2008 8:30:52 AM").UseDelimiter(",\\s*"))
            {
                Assert.That(s.HasNextDateTime(), Is.True);
                Assert.That(s.NextDateTime(), Is.EqualTo(new DateTime(2008, 5, 1, 8, 30, 52)));
            }
        }
        public void CanSkipLines()
        {
            string input =
                @"First Line, second statement,
Second Line, fourth statement
";

            using (var s = new TextScanner(input)
                           .UseDelimiter(@",\s*"))
            {
                Assert.That(s.Next(), Is.EqualTo("First Line"));
                Assert.That(s.NextLine(), Is.EqualTo(", second statement,"));
                Assert.That(s.Next(), Is.EqualTo("Second Line"));

                Assert.That(s.FindInLine(@"^[a-z, ]+$"), Is.EqualTo(", fourth statement"));

                Assert.That(s.HasNextLine(), Is.False);

                // there are no more line endings, so the following should throw
                Assert.Catch <InvalidOperationException>(() => s.NextLine());
            }
        }
        public void CanSkipLineAfterTokenCheck()
        {
            string input =
                @"First Line, second statement,
Second Line, fourth statement
";

            using (var s = new TextScanner(input)
                           .UseDelimiter(@",\s*"))
            {
                // verified the following with a java sample.
                Assert.That(s.HasNextUInt32(), Is.False);
                Assert.That(s.Next(), Is.EqualTo("First Line"));
                Assert.That(s.HasNextUInt32(), Is.False);
                Assert.That(s.NextLine(), Is.EqualTo(", second statement,"));
                Assert.That(s.Next(), Is.EqualTo("Second Line"));

                Assert.That(s.NextLine(), Is.EqualTo(", fourth statement"));

                // there are no more line endings, so the following should throw
                Assert.Catch <InvalidOperationException>(() => s.NextLine());
            }
        }
        public void MatchIsFilledOnNext()
        {
            string input = "4234; a string; +3,234.32";

            using (var s = new TextScanner(input)
                           .UseCulture(new CultureInfo("en-US"))
                           .UseDelimiter(";\\s*"))
            {
                Assert.That(s.NextInt64(), Is.EqualTo(4234));
                Assert.That(s.Match.Value, Is.EqualTo("4234"));

                Assert.That(s.Next(), Is.EqualTo("a string"));
                Assert.That(s.Match.Value, Is.EqualTo("a string"));

                Assert.That(s.NextDouble(), Is.EqualTo(3234.32));
                Assert.That(s.Match.Value, Is.EqualTo("+3,234.32"));
            }
        }
        public void CanFindInLine()
        {
            var expectedStrings = new[] { "1", "2", "red", "blue" };

            string input = "1 fish 2 fish red fish blue fish";

            using (var s = new TextScanner(input))
            {
                var matchingText = s.FindInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");

                // skip the overall match and get the captured group values
                var groups = from @group in s.Match.Groups.Cast <Group>().Skip(1)
                             select @group.Value;

                // verified the following with a java sample.
                Assert.That(groups, Is.EquivalentTo(expectedStrings));
                Assert.That(matchingText, Is.EqualTo("1 fish 2 fish red fish blue"));
                Assert.That(s.Next(), Is.EqualTo("fish"));
            }
        }
Exemple #10
0
        private static void Main(string[] args)
        {
            TextScanner s = null;

            try
            {
                s = new TextScanner(new StreamReader("xanadu.txt"));

                while (s.HasNext())
                {
                    Console.WriteLine(s.Next());
                }
            }
            finally
            {
                if (s != null)
                {
                    s.Close();
                }
            }
        }
Exemple #11
0
        private static void Main(string[] args)
        {
            TextScanner s = null;

            try
            {
                s = new TextScanner(new StreamReader("xanadu.txt"));

                while (s.HasNext())
                {
                    Console.WriteLine(s.Next());
                }
            }
            finally
            {
                if (s != null)
                {
                    s.Close();
                }
            }
        }