示例#1
0
        public static string Concat(StringView v0, StringView v1, StringView v2, StringView v3)
        {
            int nonEmpty = (v0.Length != 0 ? 1 : 0) + (v1.Length != 0 ? 2 : 0) + (v2.Length != 0 ? 4 : 0) + (v3.Length != 0 ? 8 : 0);

            switch (nonEmpty)
            {
            case 1: return(v0.ToString());

            case 2: return(v1.ToString());

            case 4: return(v2.ToString());

            case 8: return(v3.ToString());
            }
            var ret = FastAllocateString(v0.Length + v1.Length + v2.Length + v3.Length);

            fixed(char *p = ret, p0 = v0.Original, p1 = v1.Original, p2 = v2.Original, p3 = v3.Original)
            {
                wstrcpy(p + 0, p0 + v0.Offset, v0.Length);
                wstrcpy(p + v0.Length, p1 + v1.Offset, v1.Length);
                wstrcpy(p + v0.Length + v1.Length, p2 + v2.Offset, v2.Length);
                wstrcpy(p + v0.Length + v1.Length + v2.Length, p3 + v3.Offset, v3.Length);
            }

            return(ret);
        }
示例#2
0
        public unsafe StringView ToLower()
        {
            var result = new StringView(Original, Offset, Length);
            var value  = ToString();

            fixed(char *ptr_this = value)
            {
                fixed(char *ptr_result = result.ToString())
                {
                    for (int i = 0; i < value.Length; i++)
                    {
                        var ch = ptr_this[i];
                        if (char.IsUpper(ch))
                        {
                            ptr_result[i] = char.ToLower(ch);
                        }
                        else
                        {
                            ptr_result[i] = ptr_this[i];
                        }
                    }
                }
            }

            return(result);
        }
示例#3
0
 public bool Compare(StringView v)
 {
     if (v.Length != this.Length)
     {
         return(false);
     }
     for (int i = Offset; i < this.Length; ++i)
     {
         if (this[i] != v[i])
         {
             return(false);
         }
     }
     return(true);
 }
示例#4
0
        public StringView[] Split(params char[] split)
        {
            int length = this.Length;

            int[] posArray   = new int[length];
            int   splitCount = MakeSplitIndexArray(split, posArray);

            StringView[] ret   = new StringView[splitCount + 1];
            int          count = 0;
            int          index = 0;

            for (int i = 0; i < splitCount; ++i)
            {
                ret[count++] = this.Substring(index, posArray[i] - index);
                index        = posArray[i] + 1;
            }
            if (index != length)
            {
                ret[count++] = this.Substring(index, length - index);
            }
            return(ret);
        }
示例#5
0
 public bool Equals(StringView v)
 {
     return(this.Equals(v.Original, v.Offset, v.Length));
 }
示例#6
0
        private unsafe StringView Replace(string old_value, string new_value)
        {
            string value = Offset != 0 || Length != Original.Length ? ToString() : Original;

            if (old_value == null)
            {
                throw new ArgumentNullException("old_value");
            }
            if (new_value == null)
            {
                throw new ArgumentNullException("new_value");
            }
            int idx = IndexOf(old_value);

            if (idx == -1)
            {
                return(value);
            }
            if (g_finds == null)
            {
                g_finds = new List <int>();
            }
            g_finds.Clear();
            g_finds.Add(idx);

            while (idx + old_value.Length < value.Length)
            {
                idx = IndexOf(old_value, idx + old_value.Length);
                if (idx == -1)
                {
                    break;
                }
                g_finds.Add(idx);
            }
            // calc the right new total length
            int new_len;
            int dif = old_value.Length - new_value.Length;

            if (dif > 0)
            {
                new_len = value.Length - (g_finds.Count * dif);
            }
            else
            {
                new_len = value.Length + (g_finds.Count * -dif);
            }
            StringView result = new StringView(value, 0, new_len);

            fixed(char *ptr_this = value)
            {
                fixed(char *ptr_result = result.ToString())
                {
                    for (int i = 0, x = 0, j = 0; i < new_len;)
                    {
                        if (x == g_finds.Count || g_finds[x] != j)
                        {
                            ptr_result[i++] = ptr_this[j++];
                        }
                        else
                        {
                            for (int n = 0; n < new_value.Length; n++)
                            {
                                ptr_result[i + n] = new_value[n];
                            }
                            x++;
                            i += new_value.Length;
                            j += old_value.Length;
                        }
                    }
                }
            }

            return(result);
        }
示例#7
0
 public static string Concat(StringView v0)
 {
     return(v0.ToString());
 }