Exemplo n.º 1
0
        /** ****************************************************************************************
         * Returns the next token, which is afterwards also available through field #Actual.
         * If no further token  was available, the returned
         * \ref cs::aworx::lib::strings::Substring "Substring" will be 'nulled'
         * (see \ref cs::aworx::lib::strings::Substring::IsNull "Substring.IsNull").
         * To prevent this, the availability of a next token should be
         * checked using method #HasNext().
         *
         * For clarification, see the explanation and sample code in this classes documentation.
         *
         *  @param trimming  Determines if the token is trimmed in respect to the white space
         *                   characters defined in field #Whitespaces.
         *                   Defaults to \c Whitespaces.Trim.
         *  @param newDelim  The delimiter separates the tokens. Defaults to 0, which keeps the
         *                   current delimiter intact.
         *                   However, it a new delimiter can be provided for every next token.
         * @return true if a next token was available, false if not.
         ******************************************************************************************/
        public Substring    Next(Whitespaces trimming = enums.Whitespaces.Trim, char newDelim = '\0')
        {
            if (Rest.IsNull())
            {
                Actual.SetNull();
                return(Actual);
            }

            // change of delim?
            if (newDelim != '\0')
            {
                delim = newDelim;
            }

            // set buf, start and find end
            Actual.Buf   = Rest.Buf;
            Actual.Start = Rest.Start;

            int nextDelimiter = Rest.IndexOf(delim);

            if (nextDelimiter >= 0)
            {
                Rest.Start += nextDelimiter + 1;
                Actual.End  = Rest.Start - 2;
            }
            else
            {
                Actual.End = Rest.End;
                Rest.SetNull();
            }


            // trim
            if (trimming == enums.Whitespaces.Trim)
            {
                Actual.TrimStart(Whitespaces);
                Actual.TrimEnd(Whitespaces);
            }

            return(Actual);
        }
Exemplo n.º 2
0
        public void Consume()
        {
            // null substring
            {
                Substring s = new Substring();
                Substring r = new Substring("oldval");
                UT_EQ('\0', s.Consume( ));
                UT_EQ(0, s.Consume(0, r)); UT_TRUE(r.IsNull());
                UT_EQ(0, s.Consume(5, r)); UT_TRUE(r.IsNull());
                UT_EQ(false, s.Consume('a'));
                UT_EQ(false, s.Consume("word"));
                UT_EQ('\0', s.ConsumeFromEnd( ));
                UT_EQ(0, s.ConsumeFromEnd(0));
                UT_EQ(0, s.ConsumeFromEnd(5));
                UT_EQ(false, s.ConsumeFromEnd('a'));
                UT_EQ(false, s.ConsumeFromEnd("word"));
            }

            // empty substring
            {
                Substring s = new Substring("aaaaaaaaaaaa");
                Substring r = new Substring("oldval");
                s.Start = 5;
                s.End   = 4;
                UT_EQ('\0', s.Consume( ));
                UT_EQ(0, s.Consume(0, r)); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty());
                UT_EQ(0, s.Consume(5, r)); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty());
                UT_EQ(false, s.Consume('a'));
                UT_EQ(false, s.Consume("word"));
                UT_EQ('\0', s.ConsumeFromEnd( ));
                UT_EQ(0, s.ConsumeFromEnd(0));
                UT_EQ(0, s.ConsumeFromEnd(5));
                UT_EQ(false, s.ConsumeFromEnd('a'));
                UT_EQ(false, s.ConsumeFromEnd("word"));
            }

            // substring of length 1
            {
                Substring s = new Substring("aaaaaaaaaaaa");
                Substring r = new Substring("oldval");

                s.Start = s.End = 5; UT_EQ('a', s.Consume( )); UT_EQ(0, s.Length());
                s.Start = s.End = 5; UT_EQ(1, s.Consume(0)); UT_EQ(1, s.Length());
                s.Start = s.End = 5; UT_EQ(0, s.Consume(1, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("a"));
                s.Start = s.End = 5; UT_EQ(0, s.Consume(5, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("a"));
                s.Start = s.End = 5; UT_EQ(true, s.Consume('a')); UT_EQ(0, s.Length());
                s.Start = s.End = 5; UT_EQ(false, s.Consume('b')); UT_EQ(1, s.Length());
                s.Start = s.End = 5; UT_EQ(false, s.Consume("word")); UT_EQ(1, s.Length());
                s.Start = s.End = 5; UT_EQ('a', s.ConsumeFromEnd( )); UT_EQ(0, s.Length());
                s.Start = s.End = 5; UT_EQ(1, s.ConsumeFromEnd(0)); UT_EQ(1, s.Length());
                s.Start = s.End = 5; UT_EQ(0, s.ConsumeFromEnd(1)); UT_EQ(0, s.Length());
                s.Start = s.End = 5; UT_EQ(0, s.ConsumeFromEnd(5)); UT_EQ(0, s.Length());
                s.Start = s.End = 5; UT_EQ(true, s.ConsumeFromEnd('a')); UT_EQ(0, s.Length());
                s.Start = s.End = 5; UT_EQ(false, s.ConsumeFromEnd('b')); UT_EQ(1, s.Length());
                s.Start = s.End = 5; UT_EQ(false, s.ConsumeFromEnd("word")); UT_EQ(1, s.Length());
            }

            // substring of length 2
            {
                Substring s = new Substring("12ab3456");
                Substring r = new Substring("oldval");

                s.Start = 2; s.End = 3; UT_EQ('a', s.Consume( )); UT_EQ(1, s.Length());
                UT_EQ('b', s.Consume( )); UT_EQ(0, s.Length());
                s.Start = 2; s.End = 3; UT_EQ('b', s.ConsumeFromEnd( )); UT_EQ(1, s.Length());
                UT_EQ('a', s.ConsumeFromEnd( )); UT_EQ(0, s.Length());

                s.Start = 2; s.End = 3; UT_EQ(2, s.Consume(0, r)); UT_EQ(2, s.Length()); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty());
                s.Start = 2; s.End = 3; UT_EQ(1, s.Consume(1, r)); UT_EQ(1, s.Length()); UT_TRUE(r.Equals("a"));
                s.Start = 2; s.End = 3; UT_EQ(0, s.Consume(2, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab"));
                s.Start = 2; s.End = 3; UT_EQ(0, s.Consume(3, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab"));
                s.Start = 2; s.End = 3; UT_EQ(2, s.ConsumeFromEnd(0, r)); UT_EQ(2, s.Length()); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty());
                s.Start = 2; s.End = 3; UT_EQ(1, s.ConsumeFromEnd(1, r)); UT_EQ(1, s.Length()); UT_TRUE(r.Equals("b"));
                s.Start = 2; s.End = 3; UT_EQ(0, s.ConsumeFromEnd(2, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab"));
                s.Start = 2; s.End = 3; UT_EQ(0, s.ConsumeFromEnd(3, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab"));

                s.Start = 2; s.End = 3; UT_EQ(false, s.Consume('b')); UT_EQ(2, s.Length());
                UT_EQ(true, s.Consume('a')); UT_EQ(1, s.Length());
                UT_EQ(true, s.Consume('b')); UT_EQ(0, s.Length());
                UT_EQ(false, s.Consume('a')); UT_EQ(0, s.Length());
                UT_EQ(false, s.Consume('b')); UT_EQ(0, s.Length());
                s.Start = 2; s.End = 3; UT_EQ(false, s.ConsumeFromEnd('a')); UT_EQ(2, s.Length());
                UT_EQ(true, s.ConsumeFromEnd('b')); UT_EQ(1, s.Length());
                UT_EQ(true, s.ConsumeFromEnd('a')); UT_EQ(0, s.Length());
                UT_EQ(false, s.ConsumeFromEnd('b')); UT_EQ(0, s.Length());
                UT_EQ(false, s.ConsumeFromEnd('a')); UT_EQ(0, s.Length());

                s.Start = 2; s.End = 3; UT_EQ(false, s.Consume("word")); UT_EQ(2, s.Length());
                s.Start = 2; s.End = 3; UT_EQ(false, s.Consume("AB")); UT_EQ(2, s.Length());
                s.Start = 2; s.End = 3; UT_EQ(true, s.Consume("ab")); UT_EQ(0, s.Length());
                s.Start = 2; s.End = 3; UT_EQ(false, s.ConsumeFromEnd("word")); UT_EQ(2, s.Length());
                s.Start = 2; s.End = 3; UT_EQ(false, s.ConsumeFromEnd("AB")); UT_EQ(2, s.Length());
                s.Start = 2; s.End = 3; UT_EQ(true, s.ConsumeFromEnd("ab")); UT_EQ(0, s.Length());
            }

            // 3 words
            {
                Substring s = new Substring("word1 word2 word3");

                UT_EQ('w', s.Consume( ));
                UT_EQ('o', s.Consume( ));
                UT_EQ('r', s.Consume( ));
                UT_EQ('d', s.Consume( ));
                UT_EQ('1', s.Consume( ));

                UT_EQ(false, s.Consume('w'));
                UT_EQ(true, s.Consume('w', Whitespaces.Trim));
                UT_EQ(true, s.Consume('o', Whitespaces.Trim));
                UT_EQ(false, s.Consume('o', Whitespaces.Trim));
                UT_EQ(true, s.Consume('r', Whitespaces.Trim));
                UT_EQ(false, s.Consume("D2", Whitespaces.Trim));
                UT_EQ(false, s.Consume("D2"));
                UT_EQ(true, s.Consume("d2"));

                UT_EQ(2, s.Consume(4));
                UT_EQ("d3", s.ToString());

                s = new Substring("word1 word2 word3");

                UT_EQ('3', s.ConsumeFromEnd( ));
                UT_EQ('d', s.ConsumeFromEnd( ));
                UT_EQ('r', s.ConsumeFromEnd( ));
                UT_EQ('o', s.ConsumeFromEnd( ));
                UT_EQ('w', s.ConsumeFromEnd( ));

                UT_EQ(false, s.ConsumeFromEnd('2'));
                UT_EQ(true, s.ConsumeFromEnd('2', Whitespaces.Trim));
                UT_EQ(true, s.ConsumeFromEnd('d', Whitespaces.Trim));
                UT_EQ(false, s.ConsumeFromEnd('d', Whitespaces.Trim));
                UT_EQ(true, s.ConsumeFromEnd('r', Whitespaces.Trim));
                UT_EQ(false, s.ConsumeFromEnd("WO", Whitespaces.Trim));
                UT_EQ(false, s.ConsumeFromEnd("WO"));
                UT_EQ(true, s.ConsumeFromEnd("wo"));

                UT_EQ(2, s.ConsumeFromEnd(4));
                UT_EQ("wo", s.ToString());
            }

            // consume AString, Substring
            {
                Substring s        = new Substring("word1 word2 word3 word4");
                Substring sConsume = new Substring("1234word12", 4, 4);
                AString   aConsume = new AString("word");

                UT_EQ(true, s.Consume(sConsume));
                UT_EQ(false, s.Consume(sConsume));
                UT_EQ('1', s.Consume( ));
                UT_EQ(false, s.Consume(sConsume));
                UT_EQ(true, s.Consume(sConsume, Whitespaces.Trim));
                UT_EQ('2', s.Consume( ));
                UT_EQ(' ', s.Consume( ));

                UT_EQ(true, s.Consume(aConsume));
                UT_EQ(false, s.Consume(aConsume));
                UT_EQ('3', s.Consume( ));
                UT_EQ(false, s.Consume(aConsume));
                UT_EQ(true, s.Consume(aConsume, Whitespaces.Trim));

                s.Set("1word  2word 3word  4word");

                UT_EQ(true, s.ConsumeFromEnd(sConsume));
                UT_EQ(false, s.ConsumeFromEnd(sConsume));
                UT_EQ('4', s.ConsumeFromEnd( ));
                UT_EQ(false, s.ConsumeFromEnd(sConsume));
                UT_EQ(true, s.ConsumeFromEnd(sConsume, Whitespaces.Trim));
                UT_EQ('3', s.ConsumeFromEnd( ));
                UT_EQ(' ', s.ConsumeFromEnd( ));

                UT_EQ(true, s.ConsumeFromEnd(aConsume));
                UT_EQ(false, s.ConsumeFromEnd(aConsume));
                UT_EQ('2', s.ConsumeFromEnd( ));
                UT_EQ(false, s.ConsumeFromEnd(aConsume));
                UT_EQ(true, s.ConsumeFromEnd(aConsume, Whitespaces.Trim));
            }
        }