Exemplo n.º 1
0
        public object this[Slice /*!*/ slice] {
            get {
                lock (this) {
                    List <byte> res = _bytes.Slice(slice);
                    if (res == null)
                    {
                        return(new ByteArray());
                    }

                    return(new ByteArray(res));
                }
            }
            set {
                if (slice == null)
                {
                    throw PythonOps.TypeError("bytearray indices must be integer or slice, not None");
                }

                // get a list of the bytes we're going to assign into the slice.  We accept:
                //      integers, longs, etc... - fill in an array of 0 bytes
                //      list of bytes, indexables, etc...

                IList <byte> list = value as IList <byte>;
                if (list == null)
                {
                    int?iVal = null;
                    if (value is int)
                    {
                        iVal = (int)value;
                    }
                    else if (value is Extensible <int> )
                    {
                        iVal = ((Extensible <int>)value).Value;
                    }
                    else if (value is BigInteger)
                    {
                        int intval;
                        if (((BigInteger)value).AsInt32(out intval))
                        {
                            iVal = intval;
                        }
                    }

                    if (iVal != null)
                    {
                        List <byte> newlist = new List <byte>();
                        newlist.Capacity = iVal.Value;
                        for (int i = 0; i < iVal; i++)
                        {
                            newlist.Add(0);
                        }
                        list = newlist;
                    }
                    else
                    {
                        IEnumerator ie = PythonOps.GetEnumerator(value);
                        list = new List <byte>();
                        while (ie.MoveNext())
                        {
                            list.Add(GetByte(ie.Current));
                        }
                    }
                }

                lock (this) {
                    if (slice.step != null)
                    {
                        // try to assign back to self: make a copy first
                        if (this == list)
                        {
                            value = CopyThis();
                        }
                        else if (list.Count == 0)
                        {
                            DeleteItem(slice);
                            return;
                        }

                        IList <byte> castedVal = GetBytes(value);

                        int start, stop, step;
                        slice.indices(_bytes.Count, out start, out stop, out step);

                        int n = (step > 0 ? (stop - start + step - 1) : (stop - start + step + 1)) / step;

                        // we don't use slice.Assign* helpers here because bytearray has different assignment semantics.

                        if (list.Count < n)
                        {
                            throw PythonOps.ValueError("too few items in the enumerator. need {0} have {1}", n, castedVal.Count);
                        }

                        for (int i = 0, index = start; i < castedVal.Count; i++, index += step)
                        {
                            if (i >= n)
                            {
                                if (index == _bytes.Count)
                                {
                                    _bytes.Add(castedVal[i]);
                                }
                                else
                                {
                                    _bytes.Insert(index, castedVal[i]);
                                }
                            }
                            else
                            {
                                _bytes[index] = castedVal[i];
                            }
                        }
                    }
                    else
                    {
                        int start, stop, step;
                        slice.indices(_bytes.Count, out start, out stop, out step);

                        SliceNoStep(start, stop, list);
                    }
                }
            }
        }