public override void EncodePacked(ref Span <byte> buffer)
 {
     foreach (var item in _val)
     {
         _itemEncoder.SetValue(item);
         _itemEncoder.EncodePacked(ref buffer);
     }
 }
示例#2
0
 public void Encode(ref AbiEncodeBuffer buffer)
 {
     ValidateArrayLength();
     foreach (var item in _val)
     {
         _itemEncoder.SetValue(item);
         _itemEncoder.Encode(ref buffer);
     }
 }
        public void Encode(ref AbiEncodeBuffer buffer)
        {
            ValidateArrayLength();

            // If we have no elements, no work needs to be done.
            if (TypeInfo.ArrayDimensionSizes.Length == 0)
            {
                return;
            }

            // Create a variable to track our position.
            int[] encodingPosition = new int[TypeInfo.ArrayDimensionSizes.Length];

            // Get value as array (convert to array if its underlying type is something else)
            IList parentArray = _val is IList arr ? arr : _val.ToArray();

            // Loop for each element to index.
            bool reachedEnd = false;

            while (!reachedEnd)
            {
                // Define the parent array to resolve for this element.
                IList innerMostArray = parentArray;

                // Increment our decoding position.
                bool incrementing = true;
                for (int x = 0; x < encodingPosition.Length; x++)
                {
                    // If this isn't the final index (inner most array index), then it's an index to another array.
                    if (x < encodingPosition.Length - 1)
                    {
                        // Get value as array (convert to array if its underlying type is something else)
                        var inner = innerMostArray[encodingPosition[x]];
                        innerMostArray = inner is IList ia ? ia : ((inner as IEnumerable).Cast <object>()).ToArray();
                    }
                    else
                    {
                        // We've resolved the element to index.
                        _itemEncoder.SetValue(innerMostArray[encodingPosition[x]]);
                        _itemEncoder.Encode(ref buffer);
                    }

                    // Increment the index for this dimension
                    if (incrementing)
                    {
                        // Increment our position.
                        encodingPosition[x]++;

                        // Determine if we need to carry a digit.
                        if (encodingPosition[x] >= TypeInfo.ArrayDimensionSizes[x])
                        {
                            // Reset the digit, we will carry over to the next.
                            encodingPosition[x] = 0;
                        }
                        else
                        {
                            incrementing = false;
                        }
                    }
                }

                // If we incremented all digits and still have increment flag set, we overflowed our last element, so we reached the end
                reachedEnd = incrementing;
            }
        }