Exemple #1
0
        //向input字符串中插入to_insert串,位置为start
        internal unsafe static gstring internal_insert(string input, string to_insert, int start)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (to_insert == null)
            {
                throw new ArgumentNullException("to_insert");
            }

            if (start < 0 || start >= input.Length)
            {
                throw new ArgumentOutOfRangeException("start=" + start + " Length=" + input.Length);
            }

            if (to_insert.Length == 0)
            {
                return(get(input));
            }

            int     new_len = input.Length + to_insert.Length;
            gstring result  = get(new_len);

            internal_insert(result, input, to_insert, start);
            return(result);
        }
Exemple #2
0
        //获取特定长度gstring
        internal static gstring get(int length)
        {
            if (g_current_block == null)
            {
                throw new InvalidOperationException("GString 操作必须在一个gstring_block块中。");
            }

            if (length <= 0)
            {
                throw new InvalidOperationException("错误参数 length: " + length);
            }

            gstring         result;
            Stack <gstring> stack;

            //从缓存中取Stack
            if (!g_cache.TryGetValue(length, out stack))
            {
                stack = new Stack <gstring>(INITIAL_STACK_CAPACITY);
                for (int i = 0; i < INITIAL_STACK_CAPACITY; i++)
                {
                    stack.Push(new gstring(length));
                }
                g_cache[length] = stack;
                result          = stack.Pop();
            }
            else
            {
                if (stack.Count == 0)
                {
#if DBG
                    if (Log != null)
                    {
                        Log("Stack=0 Allocating new gstring Length=" + length);
                    }
#endif
                    result = new gstring(length);
                }
                else
                {
                    result = stack.Pop();
#if DBG
                    if (log != null)
                    {
                        log("Popped Length=" + length + " Stack=" + stack.Count);
                    }
#endif
                }
            }

            result._disposed = false;

            g_current_block.push(result);//gstring推入块所在栈

            return(result);
        }
Exemple #3
0
        internal static gstring get(int length)
        {
            if (g_current_block == null)
            {
                throw new InvalidOperationException("Getting gstrings must be done in a gstring_block. Make sure you do a using(gstring.block())");
            }

            if (length <= 0)
            {
                throw new InvalidOperationException("Invalid length: " + length);
            }

            gstring         result;
            Stack <gstring> stack;

            if (!g_cache.TryGetValue(length, out stack))
            {
                stack = new Stack <gstring>(INITIAL_STACK_CAPACITY);
                for (int i = 0; i < INITIAL_STACK_CAPACITY; i++)
                {
                    stack.Push(new gstring(length));
                }
                g_cache[length] = stack;
                result          = stack.Pop();
            }
            else
            {
                if (stack.Count == 0)
                {
                    if (Log != null)
                    {
                        Log("Stack=0 Allocating new gstring Length=" + length);
                    }
                    result = new gstring(length);
                }
                else
                {
                    result = stack.Pop();
#if DBG
                    if (log != null)
                    {
                        log("Popped Length=" + length + " Stack=" + stack.Count);
                    }
#endif
                }
            }

            result._disposed = false;

            g_current_block.push(result);

            return(result);
        }
Exemple #4
0
        //字符串拼接
        internal unsafe static gstring internal_concat(string s1, string s2)
        {
            int     total_length = s1.Length + s2.Length;
            gstring result       = get(total_length);

            fixed(char *ptr_result = result._value)
            {
                fixed(char *ptr_s1 = s1)
                {
                    fixed(char *ptr_s2 = s2)
                    {
                        memcpy(dst: ptr_result, src: ptr_s1, length: s1.Length, src_offset: 0);
                        memcpy(dst: ptr_result, src: ptr_s2, length: s2.Length, src_offset: s1.Length);
                    }
                }
            }

            return(result);
        }
Exemple #5
0
        //向字符串value中自start位置插入count长度的to_insertChar
        internal unsafe static gstring internal_insert(string value, char to_insert, int start, int count)
        {
            // "HelloWorld" (to_insert=x, start=5, count=3) -> "HelloxxxWorld"

            if (start < 0 || start >= value.Length)
            {
                throw new ArgumentOutOfRangeException("start=" + start + " Length=" + value.Length);
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count=" + count);
            }

            if (count == 0)
            {
                return(get(value));
            }

            int     new_len = value.Length + count;
            gstring result  = get(new_len);

            fixed(char *ptr_value = value)
            {
                fixed(char *ptr_result = result._value)
                {
                    for (int i = 0, j = 0; i < new_len; i++)
                    {
                        if (i >= start && i < start + count)
                        {
                            ptr_result[i] = to_insert;
                        }
                        else
                        {
                            ptr_result[i] = ptr_value[j++];
                        }
                    }
                }
            }

            return(result);
        }
Exemple #6
0
        //剪切start起count长度的子串
        public unsafe gstring Substring(int start, int count)
        {
            if (start < 0 || start >= Length)
            {
                throw new ArgumentOutOfRangeException("start");
            }

            if (count > Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            gstring result = get(count);

            fixed(char *src = this._value)
            fixed(char *dst = result._value)
            memcpy(dst, src + start, count);

            return(result);
        }
Exemple #7
0
        //移除string中自start起始count长度子串
        internal unsafe static gstring internal_remove(string input, int start, int count)
        {
            if (start < 0 || start >= input.Length)
            {
                throw new ArgumentOutOfRangeException("start=" + start + " Length=" + input.Length);
            }

            if (count < 0 || start + count > input.Length)
            {
                throw new ArgumentOutOfRangeException("count=" + count + " start+count=" + (start + count) + " Length=" + input.Length);
            }

            if (count == 0)
            {
                return(input);
            }

            gstring result = get(input.Length - count);

            internal_remove(result, input, start, count);
            return(result);
        }
Exemple #8
0
        //子字符替换
        public unsafe gstring Replace(char old_value, char new_value)
        {
            gstring result = get(Length);

            fixed(char *ptr_this = this._value)
            {
                fixed(char *ptr_result = result._value)
                {
                    for (int i = 0; i < Length; i++)
                    {
                        if (ptr_this[i] == old_value)
                        {
                            ptr_result[i] = new_value;
                        }
                        else
                        {
                            ptr_result[i] = ptr_this[i];
                        }
                    }
                }
            }

            return(result);
        }
Exemple #9
0
        //获取格式化字符串
        internal unsafe static gstring internal_format(string input, int num_args)
        {
            // "{0} {1}", "Hello", "World" ->
            // "xxxxxxxxxxx"
            // "Helloxxxxxx"
            // "Hello xxxxx"
            // "Hello World"

            // "Player={0} Id={1}", "Jon", 10 ->
            // "xxxxxxxxxxxxxxxx"
            // "Player=xxxxxxxxx"
            // "Player=Jonxxxxxx"
            // "Player=Jon Id=xx"
            // "Player=Jon Id=10"

            if (input == null)
            {
                throw new ArgumentNullException("value");
            }
            //新字符串长度
            int new_len = input.Length - 3 * num_args;

            for (int i = 0; i < num_args; i++)
            {
                gstring arg = g_format_args[i];
                new_len += arg.Length;
            }

            gstring result    = get(new_len);
            string  res_value = result._value;

            int brace_idx = -3;

            for (int i = 0, j = 0, x = 0; x < num_args; x++)
            {
                string arg = g_format_args[x]._value;
                brace_idx = internal_index_of(input, '{', brace_idx + 3);
                if (brace_idx == -1)
                {
                    throw new InvalidOperationException("没有发现大括号{ for argument " + arg);
                }
                if (brace_idx + 2 >= input.Length || input[brace_idx + 2] != '}')
                {
                    throw new InvalidOperationException("没有发现大括号} for argument " + arg);

                    fixed(char *ptr_input = input)
                    {
                        fixed(char *ptr_result = res_value)
                        {
                            for (int k = 0; i < new_len;)
                            {
                                if (j < brace_idx)
                                {
                                    ptr_result[i++] = ptr_input[j++];
                                }
                                else
                                {
                                    ptr_result[i++] = arg[k++];
                                    if (k == arg.Length)
                                    {
                                        j += 3;
                                        break;
                                    }
                                }
                            }
                        }
                    }
            }

            return(result);
        }
Exemple #10
0
 public static gstring Concat(gstring s0, gstring s1)
 {
     return s0 + s1;
 }
Exemple #11
0
        internal static gstring get(int length)
        {
            if (g_current_block == null)
                throw new InvalidOperationException("Getting gstrings must be done in a gstring_block. Make sure you do a using(gstring.block())");

            if (length <= 0)
                throw new InvalidOperationException("Invalid length: " + length);

            gstring result;
            Stack<gstring> stack;
            if (!g_cache.TryGetValue(length, out stack))
            {
                stack = new Stack<gstring>(INITIAL_STACK_CAPACITY);
                for (int i = 0; i < INITIAL_STACK_CAPACITY; i++)
                    stack.Push(new gstring(length));
                g_cache[length] = stack;
                result = stack.Pop();
            }
            else
            {
                if (stack.Count == 0)
                {
                    if (Log != null)
                        Log("Stack=0 Allocating new gstring Length=" + length);
                    result = new gstring(length);
                }
                else
                {
                    result = stack.Pop();
            #if DBG
                    if (log != null)
                        log("Popped Length=" + length + " Stack=" + stack.Count);
            #endif
                }
            }

            result._disposed = false;

            g_current_block.push(result);

            return result;
        }
Exemple #12
0
 public static gstring Concat(gstring s0, gstring s1, gstring s2, gstring s3)
 {
     return s0 + s1 + s2 + s3;
 }
Exemple #13
0
        public static gstring Format(string input, gstring arg0, gstring arg1, gstring arg2)
        {
            if (arg0 == null) throw new ArgumentNullException("arg0");
            if (arg1 == null) throw new ArgumentNullException("arg1");
            if (arg2 == null) throw new ArgumentNullException("arg2");

            g_format_args[0] = arg0;
            g_format_args[1] = arg1;
            g_format_args[2] = arg2;
            return internal_format(input, 3);
        }
Exemple #14
0
        public static gstring Format(string input, gstring arg0, gstring arg1, gstring arg2, gstring arg3, gstring arg4, gstring arg5)
        {
            if (arg0 == null) throw new ArgumentNullException("arg0");
            if (arg1 == null) throw new ArgumentNullException("arg1");
            if (arg2 == null) throw new ArgumentNullException("arg2");
            if (arg3 == null) throw new ArgumentNullException("arg3");
            if (arg4 == null) throw new ArgumentNullException("arg4");
            if (arg5 == null) throw new ArgumentNullException("arg5");

            g_format_args[0] = arg0;
            g_format_args[1] = arg1;
            g_format_args[2] = arg2;
            g_format_args[3] = arg3;
            g_format_args[4] = arg4;
            g_format_args[5] = arg5;
            return internal_format(input, 6);
        }
Exemple #15
0
        public static gstring Format(string input, gstring arg0, gstring arg1, gstring arg2, gstring arg3, gstring arg4, gstring arg5, gstring arg6, gstring arg7, gstring arg8)
        {
            if (arg0 == null) throw new ArgumentNullException("arg0");
            if (arg1 == null) throw new ArgumentNullException("arg1");
            if (arg2 == null) throw new ArgumentNullException("arg2");
            if (arg3 == null) throw new ArgumentNullException("arg3");
            if (arg4 == null) throw new ArgumentNullException("arg4");
            if (arg5 == null) throw new ArgumentNullException("arg5");
            if (arg6 == null) throw new ArgumentNullException("arg6");
            if (arg7 == null) throw new ArgumentNullException("arg7");
            if (arg8 == null) throw new ArgumentNullException("arg8");

            g_format_args[0] = arg0;
            g_format_args[1] = arg1;
            g_format_args[2] = arg2;
            g_format_args[3] = arg3;
            g_format_args[4] = arg4;
            g_format_args[5] = arg5;
            g_format_args[6] = arg6;
            g_format_args[7] = arg7;
            g_format_args[8] = arg8;
            return internal_format(input, 9);
        }
Exemple #16
0
 public static gstring Concat(gstring s0, gstring s1, gstring s2, gstring s3, gstring s4, gstring s5, gstring s6, gstring s7, gstring s8, gstring s9)
 {
     return s0 + s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
 }
Exemple #17
0
 public static gstring Concat(gstring s0, gstring s1, gstring s2, gstring s3, gstring s4, gstring s5, gstring s6, gstring s7)
 {
     return s0 + s1 + s2 + s3 + s4 + s5 + s6 + s7;
 }
Exemple #18
0
 public static gstring Concat(gstring s0, gstring s1, gstring s2, gstring s3, gstring s4, gstring s5)
 {
     return s0 + s1 + s2 + s3 + s4 + s5;
 }
Exemple #19
0
        //字符串replace,原字符串,需替换子串,替换的新子串
        internal unsafe static gstring internal_replace(string value, string old_value, string new_value)
        {
            // "Hello, World. There World" | World->Jon =
            // "000000000000000000000" (len = orig - 2 * (world-jon) = orig - 4
            // "Hello, 00000000000000"
            // "Hello, Jon00000000000"
            // "Hello, Jon. There 000"
            // "Hello, Jon. There Jon"

            // "Hello, World. There World" | World->Alexander =
            // "000000000000000000000000000000000" (len = orig + 2 * (alexander-world) = orig + 8
            // "Hello, 00000000000000000000000000"
            // "Hello, Alexander00000000000000000"
            // "Hello, Alexander. There 000000000"
            // "Hello, Alexander. There Alexander"

            if (old_value == null)
            {
                throw new ArgumentNullException("old_value");
            }

            if (new_value == null)
            {
                throw new ArgumentNullException("new_value");
            }

            int idx = internal_index_of(value, old_value);

            if (idx == -1)
            {
                return(value);
            }

            g_finds.Clear();
            g_finds.Add(idx);

            // 记录所有需要替换的idx点
            while (idx + old_value.Length < value.Length)
            {
                idx = internal_index_of(value, 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);
            }

            gstring result = get(new_len);

            fixed(char *ptr_this = value)
            {
                fixed(char *ptr_result = result._value)
                {
                    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);
        }
Exemple #20
0
 internal void push(gstring str)
 {
     stack.Push(str);
 }
Exemple #21
0
        public static gstring Format(string input, gstring arg0)
        {
            if (arg0 == null) throw new ArgumentNullException("arg0");

            g_format_args[0] = arg0;
            return internal_format(input, 1);
        }
Exemple #22
0
 public static gstring Concat(gstring s0, gstring s1, gstring s2)
 {
     return s0 + s1 + s2;
 }
Exemple #23
0
 internal void push(gstring str)
 {
     stack.Push(str);
 }
Exemple #24
0
 public gstring Concat(gstring value)
 {
     return internal_concat(this, value);
 }