Pchp.Core.PhpString.this C# (CSharp) Method

this() public method

Gets or sets character at given index according to PHP semantics.
public this ( int index ) : char
index int Character index.
return char
        public char this[int index]
        {
            get
            {
                var chunks = _chunks;
                if (chunks != null)
                {
                    if (chunks.GetType() == typeof(object[]))
                    {
                        var arr = (object[])chunks;
                        foreach (var ch in arr)
                        {
                            int ch_length = ChunkLength(ch);
                            if (index < ch_length)
                            {
                                return GetCharInChunk(ch, index);
                            }
                            index -= ch_length;
                        }
                    }
                    else
                    {
                        return GetCharInChunk(chunks, index);
                    }
                }

                throw new ArgumentOutOfRangeException();
            }
            set
            {
                if (index >= this.Length)
                {
                    if (index == this.Length)
                    {
                        this.Append(value.ToString());
                    }
                    else
                    {
                        this.Append(new string('\0', index - this.Length) + value.ToString());
                    }
                }
                else if (index >= 0)
                {
                    // TODO: EnsureWritable

                    var chunks = _chunks;
                    if (chunks != null)
                    {
                        if (chunks.GetType() == typeof(object[]))
                        {
                            var arr = (object[])chunks;
                            for (int i = 0; i < arr.Length; i++)
                            {
                                int ch_length = ChunkLength(arr[i]);
                                if (index < ch_length)
                                {
                                    SetCharInChunk(ref arr[i], index, value);
                                }
                                index -= ch_length;
                            }
                        }
                        else
                        {
                            SetCharInChunk(ref _chunks, index, value);
                        }
                    }
                }
                else
                {
                    // index < 0, ignored
                }
            }
        }