Skip to content

Commit

Permalink
static array
Browse files Browse the repository at this point in the history
  • Loading branch information
xhliu committed Nov 14, 2020
1 parent e50c9b8 commit 4ef8304
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 44 deletions.
23 changes: 23 additions & 0 deletions docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ where the most significant bit indicates the sign (``0`` for positive, ``1`` for

Converts a number ``num`` into a byte array of certain size ``size``, including the sign bit. It fails if the number cannot be accommodated.

* ``len()``
Returns the length.

.. code-block:: solidity
int a = len(b'ffee11'); // a == 3
* **Slicing Opeartor** - ``b[start:end]`` returns subarray of ``b`` from index ``start`` (inclusive) to ``end`` (exclusive).
``start`` is ``0`` if omitted, ``end`` is length of array if omitted.

.. code-block:: solidity
bytes b = b'0011223344556677';
// b[3:6] == b'334455'
// b[:4] == b'00112233'
// b[5:] = b'556677'
* **Concatenation**

.. code-block:: solidity
bytes b = b'00112233' + b'334455' // b == b'00112233334455'
* ``reverseBytes20(bytes b)`` ``reverseBytes32(bytes b)``

Returns reversed bytes of ``b``, which is of 20/32 bytes. They are often useful when converting a number between little-endian and big-endian.
Expand Down
62 changes: 18 additions & 44 deletions docs/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,66 +54,40 @@ Basic Types
int a2 = -4242424242424242;
int a3 = 55066263022277343669578718895168534326250603453777594175500187360389116729240;
int a4 = 0xFF8C;
* **byte** - a single byte, whose literals are in single-quoted hexadecimal format.
* **bytes** - a variable length array of bytes, whose literals are in quoted hexadecimal format prefixed by ``b``.

.. code-block:: solidity
byte a1 = 'FF';
bytes b0 = ['ff', 'ee', '12', '34'];
bytes b1 = b'ffee1234'; // b0 and b1 are equivalent
bytes b2 = b'414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00';
bytes b3 = b'1122' + b'eeff'; // b3 is b'1122eeff'
Array Types
-----------
An array is a list of values of the same type.
* **Array Literals** - a comma-separated list of expressions, enclosed in square brackets
An array is a fixed-size list of values of the same basic type.

.. code-block:: solidity
byte[] a = ['ff', 'ee', '11', '22'];
bool[] b = [false, false && true || false, true || (1 > 2)];
int[] c = [72, -4 - 1 - 40, 833 * (99 + 9901) + 8888];
int[] empty = [];
* **len()** - returns the length of an array.
* **Array Literals** - a comma-separated list of expressions, enclosed in square brackets.
Array size must be an integer constant greater than zero.

.. code-block:: solidity
int a = len([1, 4, 2]); // a == 3
* **Index Operator** - index starting from 0. Out of bound access will immediately fail contract execution.
bool[3] b = [false, false && true || false, true || (1 > 2)];
int[3] c = [72, -4 - 1 - 40, 833 * (99 + 9901) + 8888];
bytes[3] a = [b'ffee', b'11', b'22'];
* **Index Operator** - index starting from 0. Out of bound access is undefined.

.. code-block:: solidity
int[] a = [1, 4, 2];
int[3] a = [1, 4, 2];
int d = a[2];
a[1] = -4;
int idx = 2;
d = a[idx]; // allowed
a[idx] = 2; // disallowed as only const int index is allowed when writing to an array
* **Slicing Opeartor** - ``b[start:end]`` returns subarray of ``b`` from index ``start`` (inclusive) to ``end`` (exclusive).
``start`` is ``0`` if omitted, ``end`` is length of array if omitted.

.. code-block:: solidity
// see "bytes" type below
bytes b = b'0011223344556677';
// b[3:6] == b'334455'
// b[:4] == b'00112233'
// b[5:] = b'556677'
* **Concatenation**

.. code-block:: solidity
int s = [3, 2] + [1, 4]; // s = [3, 2, 1, 4]
``bytes`` Type
--------------
``byte[]`` is used so often that it demands its own synonym ``bytes``.
It is a variable length array of bytes, whose literals are in quoted hexadecimal format prefixed by ``b``.

.. code-block:: solidity
bytes b0 = ['ff', 'ee', '12', '34'];
bytes b1 = b'ffee1234'; // b0 and b1 are equivalent
bytes b2 = b'414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff00';
bytes b3 = b'1122' + b'eeff'; // b3 is b'1122eeff'
Type Inference
--------------
Expand Down

0 comments on commit 4ef8304

Please sign in to comment.