예제 #1
0
 public int CompareTo(AsciiString value)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     return(AsciiString.Compare(this, value));
 }
예제 #2
0
 public bool Contains(AsciiString value)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     return(this.IndexOf(value) >= 0);
 }
예제 #3
0
 public bool EndsWith(AsciiString value, bool ignoreCase)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     return(AsciiString.Compare(this, this.data.Length - value.data.Length, value, 0, value.Length, ignoreCase) == 0);
 }
예제 #4
0
        public bool Equals(AsciiString value)
        {
            //    return GetHashCode() == value.GetHashCode();
            //  if (value == null) throw new ArgumentNullException("value");
            // return equals1(data, value.data);
            //   return StructuralComparisons.StructuralEqualityComparer.Equals(data, value.data);

            //    return data.SequenceEqual(value.data);
            // return data.SequenceEqual<byte[]>(value.data, new BigEndianByteArrayEqualityComparer());
            return(BigEndianByteArrayEqualityComparer.Instance.Equals(data, value.data));
            //return CompareTo(value) == 0;
        }
예제 #5
0
        public static AsciiString Join(AsciiString seperator, IEnumerable <AsciiString> values)
        {
            if (seperator == null)
            {
                throw new ArgumentNullException("seperator");
            }
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            int totalBytes = 0;
            int offset     = 0;

            foreach (AsciiString asciiString in values)
            {
                if (asciiString == null)
                {
                    continue;
                }
                totalBytes += asciiString.data.Length;
                totalBytes += seperator.data.Length;
            }

            if (totalBytes > 0)
            {
                totalBytes -= seperator.data.Length;
            }

            byte[] data = new byte[totalBytes];

            foreach (AsciiString asciiString in values)
            {
                if (asciiString == null)
                {
                    continue;
                }

                Buffer.BlockCopy(asciiString.data, 0, data, offset, asciiString.data.Length);
                offset += asciiString.data.Length;

                if (offset < totalBytes)
                {
                    Buffer.BlockCopy(seperator.data, 0, data, offset, seperator.data.Length);
                    offset += seperator.data.Length;
                }
            }

            return(new AsciiString(data));
        }
예제 #6
0
        public int IndexOf(AsciiString value, int startIndex, int count, bool ignoreCase)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (startIndex < 0 || startIndex > this.data.Length)
            {
                throw new ArgumentOutOfRangeException("startIndex");
            }
            if (count < 0 || startIndex + count > this.data.Length || count < value.data.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            int charactersFound = 0;

            for (int i = startIndex; i < startIndex + count; i++)
            {
                if (i + (value.data.Length - charactersFound) > this.data.Length)
                {
                    return(-1);
                }

                byte byteA = this.data[i];
                byte byteB = value.data[charactersFound];

                if (ignoreCase)
                {
                    byteA = AsciiChar.ToLower(byteA);
                    byteB = AsciiChar.ToLower(byteB);
                }

                if (byteA == byteB)
                {
                    charactersFound++;
                }
                else
                {
                    charactersFound = 0;
                }

                if (charactersFound == value.data.Length)
                {
                    return(i - charactersFound + 1);
                }
            }

            return(-1);
        }
예제 #7
0
        public AsciiString ToUpper()
        {
            AsciiString s = AsciiString.Copy(this);

            for (int i = 0; i < s.data.Length; i++)
            {
                byte b = s.data[i];
                if (AsciiChar.IsLower(b))
                {
                    s.data[i] = AsciiChar.ToUpper(b);
                }
            }

            return(s);
        }
예제 #8
0
        public AsciiString Replace(AsciiString oldString, AsciiString newString)
        {
            if (oldString == null)
            {
                throw new ArgumentNullException("oldString");
            }
            if (newString == null)
            {
                throw new ArgumentNullException("newString");
            }

            List <int> indexes = new List <int>();
            int        index   = 0;

            do
            {
                index = this.IndexOf(oldString, index, false);

                if (index >= 0)
                {
                    indexes.Add(index);
                    index += oldString.data.Length;
                }
            }while (index >= 0 && index + oldString.Length < this.data.Length);

            if (indexes.Count == 0)
            {
                return(this.Clone());
            }

            byte[] data = new byte[this.data.Length - (oldString.data.Length * indexes.Count) + (newString.data.Length * indexes.Count)];

            int oldIndex = 0;
            int newIndex = 0;

            foreach (int stringIndex in indexes)
            {
                Buffer.BlockCopy(this.data, oldIndex, data, newIndex, stringIndex - oldIndex);
                newIndex += stringIndex - oldIndex;
                oldIndex  = stringIndex + oldString.data.Length;
                Buffer.BlockCopy(newString.data, 0, data, newIndex, newString.data.Length);
                newIndex += newString.data.Length;
            }

            Buffer.BlockCopy(this.data, oldIndex, data, newIndex, this.data.Length - oldIndex);

            return(new AsciiString(data));
        }
예제 #9
0
        public static bool IsNullOrWhitespace(AsciiString value)
        {
            if (value == null || value.data.Length == 0)
            {
                return(true);
            }

            foreach (byte b in value.data)
            {
                if (!AsciiChar.IsWhitespace(b))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #10
0
        public AsciiString[] Split(IEnumerable <AsciiChar> seperators, int count, StringSplitOptions options)
        {
            List <AsciiString> parts = new List <AsciiString>();

            int startIndex = 0;

            for (int dataIndex = 0; dataIndex < this.data.Length; dataIndex++)
            {
                bool found = false;

                foreach (AsciiChar seperator in seperators)
                {
                    if (this.data[dataIndex] == seperator)
                    {
                        found = true;
                    }
                }

                if (found)
                {
                    AsciiString part = this.Substring(startIndex, dataIndex - startIndex);
                    if (part.data.Length > 0 || options == StringSplitOptions.None)
                    {
                        parts.Add(part);
                    }

                    startIndex = dataIndex + 1;

                    if (parts.Count + 1 == count)
                    {
                        break;
                    }
                }
            }

            AsciiString remainingPart = this.Substring(startIndex);

            if (remainingPart.data.Length > 0 || options == StringSplitOptions.None)
            {
                parts.Add(remainingPart);
            }

            return(parts.ToArray());
        }
예제 #11
0
        private static int SafeCompare(AsciiString strA, int indexA, AsciiString strB, int indexB, int length, bool ignoreCase)
        {
            for (int i = 0; i < length; i++)
            {
                int iA = i + indexA;
                int iB = i + indexB;

                if (iA == strA.data.Length && iB == strB.data.Length)
                {
                    return(0);
                }
                if (iA == strA.data.Length)
                {
                    return(-1);
                }
                if (iB == strB.data.Length)
                {
                    return(1);
                }

                byte byteA = strA.data[iA];
                byte byteB = strB.data[iB];

                if (ignoreCase)
                {
                    byteA = AsciiChar.ToLower(byteA);
                    byteB = AsciiChar.ToLower(byteB);
                }

                if (byteA < byteB)
                {
                    return(-1);
                }
                if (byteB < byteA)
                {
                    return(1);
                }
            }

            return(0);
        }
예제 #12
0
        public AsciiString Insert(AsciiString value, int index)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (index < 0 || index > this.data.Length)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            int totalBytes = this.data.Length + value.data.Length;

            byte[] data = new byte[totalBytes];

            Buffer.BlockCopy(this.data, 0, data, 0, index);
            Buffer.BlockCopy(value.data, 0, data, index, value.data.Length);
            Buffer.BlockCopy(this.data, index, data, index + value.data.Length, this.data.Length - index);

            return(new AsciiString(data));
        }
예제 #13
0
        private static int FastSafeCompare(AsciiString strA, AsciiString strB, int length)
        {
            for (var i = 0; i < length; i++)
            {
                var iIsEqualLengthA = (i == strA.data.Length);
                var iIsEqualLengthB = (i == strB.data.Length);

                if (iIsEqualLengthA && iIsEqualLengthB)
                {
                    return(0);
                }

                if (iIsEqualLengthA)
                {
                    return(-1);
                }

                if (iIsEqualLengthB)
                {
                    return(1);
                }

                var byteA = strA.data[i];
                var byteB = strB.data[i];


                if (byteA < byteB)
                {
                    return(-1);
                }
                if (byteB < byteA)
                {
                    return(1);
                }
            }

            return(0);
        }
예제 #14
0
 public int LastIndexOf(AsciiString value, int startIndex)
 {
     return(this.LastIndexOf(value, startIndex, this.Length - startIndex, false));
 }
예제 #15
0
 public int LastIndexOf(AsciiString value, bool ignoreCase)
 {
     return(this.LastIndexOf(value, 0, this.Length, ignoreCase));
 }
예제 #16
0
 public int LastIndexOf(AsciiString value)
 {
     return(this.LastIndexOf(value, 0, this.Length, false));
 }
예제 #17
0
 public static int Compare(AsciiString strA, AsciiString strB)
 {
     return(Compare(strA, strB, false));
 }
예제 #18
0
 public static bool IsNullOrEmpty(AsciiString value)
 {
     return(value == null || value.data.Length == 0);
 }
예제 #19
0
 int IComparable <AsciiString> .CompareTo(AsciiString other)
 {
     return(this.CompareTo(other));
 }
예제 #20
0
 public bool EndsWith(AsciiString value)
 {
     return(this.EndsWith(value, false));
 }
예제 #21
0
 public bool StartsWith(AsciiString value)
 {
     return(this.StartsWith(value, false));
 }
예제 #22
0
 public static AsciiString Copy(AsciiString value)
 {
     byte[] data = new byte[value.data.Length];
     Buffer.BlockCopy(value.data, 0, data, 0, value.data.Length);
     return(new AsciiString(data));
 }
예제 #23
0
 public static AsciiString operator +(AsciiString strA, AsciiString strB)
 {
     return(AsciiString.Concat(strA, strB));
 }
예제 #24
0
 public int LastIndexOf(AsciiString value, int startIndex, bool ignoreCase)
 {
     return(this.LastIndexOf(value, startIndex, this.Length - startIndex, ignoreCase));
 }
예제 #25
0
 static AsciiString()
 {
     Empty = new AsciiString(new byte[] { });
 }
예제 #26
0
 public int LastIndexOf(AsciiString value, int startIndex, int count)
 {
     return(this.LastIndexOf(value, startIndex, count, false));
 }
예제 #27
0
 public static bool operator !=(AsciiString strA, AsciiString strB)
 {
     return(AsciiString.Compare(strA, strB) != 0);
 }
예제 #28
0
 public static int Compare(AsciiString strA, int indexA, AsciiString strB, int indexB, int length)
 {
     return(Compare(strA, indexA, strB, indexB, length, false));
 }
예제 #29
0
 public static AsciiString Join(AsciiString seperator, params AsciiString[] values)
 {
     return(Join(seperator, (IEnumerable <AsciiString>)values));
 }