/** ****************************************************************************************
         * 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);
        }
Exemple #2
0
        // #############################################################################################
        // Internals
        // #############################################################################################

        /** ****************************************************************************************
         * Internal, recursive helper of #Find.
         *
         * @param       domainPath  Path to search.
         * @param       sensitivity Denotes if domain name search is treated case sensitive or not.
         * @param       maxCreate   The maximum number of sub domains that are created if not
         *                          found at the end of the path.
         * @param[out]  wasCreated  Output parameter that is set \c true if domain was not found
         *                          and hence created.
         * @return The domain found or created.
         ******************************************************************************************/
        protected Domain findRecursive(Substring domainPath, Case sensitivity,
                                       int maxCreate, ref bool wasCreated)
        {
            //--- get act sub-name and rest of path
            domainPath.Consume(PathSeparator);
            int endSubName = domainPath.IndexOf(PathSeparator);

            ALIB.ASSERT_ERROR(endSubName != 0, "Internal Error");

            // find end of actual domain name and save rest
            Substring restOfDomainPath = tSubstring2;

            restOfDomainPath.SetNull();
            if (endSubName > 0)
            {
                domainPath.Split(endSubName, restOfDomainPath, 1);
            }

            // search sub-domain
            Domain subDomain = null;

            // "."
            if (domainPath.Equals("."))
            {
                subDomain = this;
            }

            // ".."
            else if (domainPath.Equals(".."))
            {
                subDomain = Parent != null ? Parent : this;
            }


            // search in sub-domain
            else
            {
                int  i;
                bool fixedOnce = false;
                for (;;)
                {
                    for (i = 0; i < SubDomains.Count; i++)
                    {
                        int comparison = SubDomains[i].Name.CompareTo(domainPath, sensitivity);
                        if (comparison >= 0)
                        {
                            if (comparison == 0)
                            {
                                subDomain = SubDomains[i];
                            }
                            break;
                        }
                    }

                    // domain found?
                    if (subDomain != null)
                    {
                        break;
                    }

                    // try and fix name
                    if (!fixedOnce)
                    {
                        fixedOnce = true;

                        bool illegalCharacterFound = false;
                        for (int cp = 0; cp < domainPath.Length(); ++cp)
                        {
                            char c = domainPath.CharAt(cp);
                            if (c < '-' || c > 'z' ||
                                c == '<' || c == '>' ||
                                c == '[' || c == ']' ||
                                c == '=' || c == '?' || c == ';' || c == ':' ||
                                c == '\\' || c == '\'' || c == '.' || c == ','
                                )
                            {
                                illegalCharacterFound = true;
                                domainPath.Buf[domainPath.Start + cp] = '#';
                            }
                        }

                        if (illegalCharacterFound)
                        {
                            continue;
                        }
                    }

                    // create
                    if (maxCreate == 0)
                    {
                        return(null);
                    }
                    wasCreated = true;
                    SubDomains.Insert(i, subDomain = new Domain(this, new AString(domainPath)));
                    maxCreate--;
                    if (maxCreate == 0)
                    {
                        return(subDomain);
                    }

                    break;
                }
            }
            // recursion?
            if (restOfDomainPath.IsNotEmpty())
            {
                domainPath.Set(restOfDomainPath);
                return(subDomain.findRecursive(domainPath, sensitivity, maxCreate, ref wasCreated));
            }

            // that's it
            return(subDomain);
        }
        // #############################################################################################
        //  Interface
        // #############################################################################################

        /** ****************************************************************************************
         *  Sets the tokenizer to the new source and delim.
         *
         * @param  str        The character array to use as the source for the tokens.
         * @param  delim      The delimiter that separates the tokens. Can be changed with every
         *                    next token.
         ******************************************************************************************/
        public void Set(String str, char delim)
        {
            this.delim = delim;
            Rest.Set(str);
            Actual.SetNull();
        }
Exemple #4
0
        public void CompareTo()
        {
            Substring tss = new Substring();

            // null string comparison

            String nullString = null;

            UT_TRUE(tss.CompareTo(nullString) == 0);
            UT_TRUE(tss.Equals(nullString));

            tss.Set("");
            UT_TRUE(tss.CompareTo(nullString) != 0);
            UT_TRUE(!tss.Equals(nullString));

            tss.SetNull();
            UT_TRUE(tss.CompareTo(nullString) == 0);
            UT_TRUE(tss.Equals(nullString));

            String t = "abcde";

            tss = new Substring("01" + t + "234", 2, t.Length);

            int result, resSys;

            result = tss.CompareTo(t);                                     UT_EQ(t.CompareTo(t), result);
            result = tss.CompareTo(t + "x");                               UT_EQ(t.CompareTo(t + "x"), result);
            result = tss.CompareTo(t.Substring(0, t.Length - 1));             UT_EQ(t.CompareTo(t.Substring(0, t.Length - 1)), result);
            result = tss.CompareTo("pad" + t, Case.Sensitive, 3);   UT_EQ(0, result);
            result = tss.CompareTo("pad" + t, Case.Sensitive, 3, 2);   UT_EQ(1, result);


            // greater/smaller strings
            {
                String greater  = "x";
                String greater2 = "abcdef";
                String smaller  = "aaa";
                String smaller2 = "abcd";
                result = tss.CompareTo(greater);  resSys = t.CompareTo(greater); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(greater2);  resSys = t.CompareTo(greater2); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(smaller);  resSys = t.CompareTo(smaller); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(smaller2);  resSys = t.CompareTo(smaller2); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                String ut = t.ToUpper();
                UT_FALSE(0 == tss.CompareTo(ut));
                UT_FALSE(0 == tss.CompareTo(ut, Case.Sensitive));
                UT_TRUE(0 == tss.CompareTo(ut, Case.Ignore));
            }

            {
                AString greater  = new AString("x");
                AString greater2 = new AString("abcdef");
                AString smaller  = new AString("aaa");
                AString smaller2 = new AString("abcd");
                result = tss.CompareTo(greater);  resSys = t.CompareTo(greater.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(greater2);  resSys = t.CompareTo(greater2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(smaller);  resSys = t.CompareTo(smaller.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(smaller2);  resSys = t.CompareTo(smaller2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                AString ut = new AString(t.ToUpper());
                UT_FALSE(0 == tss.CompareTo(ut));
                UT_FALSE(0 == tss.CompareTo(ut, Case.Sensitive));
                UT_TRUE(0 == tss.CompareTo(ut, Case.Ignore));
            }
            {
                Substring greater  = new Substring("123x", 3);
                Substring greater2 = new Substring("123abcdef", 3);
                Substring smaller  = new Substring("123aaa", 3);
                Substring smaller2 = new Substring("123abcd", 3);
                result = tss.CompareTo(greater);  resSys = t.CompareTo(greater.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(greater2);  resSys = t.CompareTo(greater2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(smaller);  resSys = t.CompareTo(smaller.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                result = tss.CompareTo(smaller2);  resSys = t.CompareTo(smaller2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0));
                Substring ut = new Substring(t.ToUpper());
                UT_FALSE(0 == tss.CompareTo(ut));
                UT_FALSE(0 == tss.CompareTo(ut, Case.Sensitive));
                UT_TRUE(0 == tss.CompareTo(ut, Case.Ignore));
            }
        }