Exemple #1
0
        public BString Replace(BString Old, BString New, int StartAt)
        {
            if (StartAt >= this.Length)
            {
                throw new Exception("Starting postition cannot be greater than Length");
            }

            if (Old.Length > this.Length)
            {
                return(new BString(this._elements));
            }

            BStringBuilder builder = new BStringBuilder();

            bool found = false;

            for (int i = StartAt; i < this.Length; i++)
            {
                found = (i >= this.Length - Old.Length ? false : BString.Equals(this, i, Old, 0, Old.Length));
                if (!found)
                {
                    builder.Append(this[i]);
                }
                else
                {
                    builder.Append(New);
                    i += Old.Length - 1;
                }
            }

            return(builder.ToBString());
        }
Exemple #2
0
        // Concatenations //
        public static BString Concat(BString A, BString B)
        {
            BStringBuilder builder = new BStringBuilder();

            builder.Append(A);
            builder.Append(B);
            return(builder.ToBString());
        }
Exemple #3
0
        public static BString Concat(IEnumerable <BString> Elements)
        {
            BStringBuilder builder = new BStringBuilder();

            foreach (BString u in Elements)
            {
                builder.Append(u);
            }
            return(builder.ToBString());
        }
Exemple #4
0
        public BString[] Split(byte[] Delim, byte Escape, bool KeepDelims)
        {
            if (Delim.Contains(Escape))
            {
                throw new Exception("The deliminators cannot contain the escape token");
            }

            List <BString> TempArray = new List <BString>();
            BStringBuilder sb        = new BStringBuilder();
            bool           InEscape  = false;

            // Go through each char in string //
            foreach (byte b in this._elements)
            {
                // turn on escaping //
                if (b == Escape)
                {
                    InEscape = (!InEscape);
                }

                // Slipt //
                if (!InEscape)
                {
                    // We found a deliminator //
                    if (Delim.Contains(b))
                    {
                        BString s = sb.ToBString();

                        // Check the size of the current cache and add the string, which would happend if we had 'A,B,,C,D' //
                        if (s.Length == 0)
                        {
                            TempArray.Add(null);
                        }
                        else
                        {
                            TempArray.Add(s);
                        }

                        // Check to see if we need to keep our delims //
                        if (KeepDelims)
                        {
                            TempArray.Add(new BString(b));
                        }

                        sb = new BStringBuilder();
                    }
                    else if (b != Escape)
                    {
                        sb.Append(b);
                    }
                }// end the string building phase //
                else if (b != Escape)
                {
                    sb.Append(b);
                }
            }

            if (InEscape)
            {
                throw new ArgumentOutOfRangeException("Unclosed escape sequence");
            }

            // Now do clean up //
            BString t = sb.ToBString();

            // The string has some AWValue //
            if (t.Length != 0)
            {
                // Check that we didn't end on a delim AWValue, but if we did and we want delims, then keep it //
                if (!(t.Length == 1 && Delim.Contains(t[0])) || KeepDelims)
                {
                    TempArray.Add(sb.ToBString());
                }
            }
            // Check if we end on a delim, such as A,B,C,D, where ',' is a delim; we want our array to be {A , B , C , D , null}
            else if (Delim.Contains(this._elements.Last()))
            {
                TempArray.Add(null);
            }
            return(TempArray.ToArray());
        }