Пример #1
0
        public void CanCheckHasNextLine()
        {
            string input =
                @"First line
4453
Last line";

            using (var s = new TextScanner(input))
            {
                // verified the following with a java sample.
                Assert.That(s.HasNextLine(), Is.True);

                Assert.That(s.NextLine(), Is.EqualTo("First line"));
                Assert.That(s.ToString(), Contains.Substring("position=12"));

                Assert.That(s.HasNextLine(), Is.True);
                Assert.That(s.NextDouble(), Is.EqualTo(4453.0));
                Assert.That(s.ToString(), Contains.Substring("position=16"));

                Assert.That(s.HasNextLine(), Is.True);
                Assert.That(s.NextLine(), Is.EqualTo(""));
                Assert.That(s.ToString(), Contains.Substring("position=18"));

                Assert.That(s.HasNextLine(), Is.True);
                Assert.That(s.NextLine(), Is.EqualTo("Last line"));
                Assert.That(s.HasNextLine(), Is.False);
                Assert.Catch(() => s.NextLine());
            }
        }
Пример #2
0
        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));
        }
Пример #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);
        }
Пример #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);
        }
Пример #5
0
        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)));
            }
        }
Пример #6
0
        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"));
            }
        }