示例#1
0
        public void TrimStartSubstring(string input, char trimChar, string expected)
        {
            var substring = new Substring(input);

            substring = Substring.TrimStart(substring, trimChar);

            Assert.Equal(expected, substring.ToString());
        }
示例#2
0
        public void SubstringConstructor()
        {
            AString astr = new AString();
            AString res  = new AString();

            constructorTest("a", res, false); UT_EQ("a", res);
            constructorTest(" a", res, false); UT_EQ(" a", res);
            constructorTest("a ", res, false); UT_EQ("a ", res);
            constructorTest("a b", res, false); UT_EQ("a b", res);
            constructorTest(" a b", res, false); UT_EQ(" a b", res);
            constructorTest("a b ", res, false); UT_EQ("a b ", res);

            constructorTest("a", res, true); UT_EQ("a", res);
            constructorTest(" a", res, true); UT_EQ("a", res);
            constructorTest("a ", res, true); UT_EQ("a", res);
            constructorTest("a b", res, true); UT_EQ("a b", res);
            constructorTest(" a b", res, true); UT_EQ("a b", res);
            constructorTest("a b ", res, true); UT_EQ("a b", res);

            // changing whitespaces
            {
                {
                    astr.Clear()._("xy xz abc xy");
                    Substring subs = new Substring();
                    subs.Set(astr).Trim("xy ".ToCharArray());
                    subs.CopyTo(res);
                    UT_EQ("z abc", res);
                }

                {
                    Substring subs = new Substring("xy xz abc xy");
                    subs.TrimStart("xy ".ToCharArray());
                    subs.TrimEnd("xy ".ToCharArray());
                    subs.CopyTo(res);
                    UT_EQ("z abc", res);
                }
            }

            // test other constructors
            {
                astr.Clear()._(" astring ");
                UT_TRUE((new Substring()).IsEmpty());
                UT_TRUE((new Substring()).IsNull());
                UT_EQ("astring", (new Substring(astr)).Trim().ToString());
                UT_EQ("str", (new Substring(astr, 2, 3)).ToString());
                UT_EQ("", (new Substring(astr, 20, 3)).ToString());
                UT_TRUE((new Substring(astr, 20, 3)).IsEmpty());
                UT_FALSE((new Substring(astr, 20, 3)).IsNull());


                Substring s2 = new Substring(astr); s2.Trim();
                UT_EQ("astring", new Substring(s2.ToString()).ToString());
                UT_EQ("str", (new Substring((new Substring(astr, 2, 3)))).ToString());
            }
        }
        bool  Get(Object category, Object name, AString target)
        {
            // assemble option name as CATEGORY_NAME
            target.Clear()._(category);
            if (target.IsNotEmpty())
            {
                target._('_');
            }
            target._(name);


            int       optionLength = target.Length();
            Substring actVar       = new Substring();

            for (int i = 0; i < args.Length; i++)
            {
                // remove whitespaces (if somebody would work with quotation marks...)
                actVar.Set(args[i]).Trim();

                // request '-' and allow a second '-'
                if (!actVar.Consume('-'))
                {
                    continue;
                }
                actVar.Consume('-');

                if (target.CompareTo(args[i], Case.Ignore, actVar.Start, optionLength, 0, optionLength) == 0)
                {
                    //again, lets trim before searching the = sign (really almost unnecessary)
                    actVar.Start += optionLength;
                    if (actVar.Consume('=', Whitespaces.Trim))
                    {
                        actVar.TrimStart();
                        actVar.TrimEnd();
                        target.Clear();
                        target._NC(args[i], actVar.Start, actVar.Length());
                        return(true);
                    }
                }
            }

            return(false);
        }
        /** ****************************************************************************************
         * 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);
        }