Skip to content

Commit

Permalink
update reverseBytes() and repeat()
Browse files Browse the repository at this point in the history
  • Loading branch information
xhliu committed Apr 6, 2021
1 parent ab84aeb commit d84981b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/ctc.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _ctc-label:

=====================
Compile Time Constant
=====================
Expand All @@ -11,6 +13,8 @@ There are two cases where only compile time constants are allowed.

* loop bound
* array size
* ``size`` in ``reverseBytes(bytes b, int size)``
* ``size`` in ``repeat(T e, int size)``

.. code-block:: solidity
Expand Down
7 changes: 4 additions & 3 deletions docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ where the most significant bit indicates the sign (``0`` for positive, ``1`` for
bytes b = b'00112233' + b'334455' // b == b'00112233334455'
* ``reverseBytes20(bytes b)`` ``reverseBytes32(bytes b)``
* ``reverseBytes(bytes b, int size)``

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.
Returns reversed bytes of ``b``, which is of ``size`` bytes. Note ``size`` must be a :ref:`compile time constant<ctc-label>`.
It is often useful when converting a number between little-endian and big-endian.

.. code-block:: solidity
.. code-block:: solidity
// returns b'6cfeea2d7a1d51249f0624ee98151bfa259d095642e253d8e2dce1e79df33f79'
reverseBytes32(b'793ff39de7e1dce2d853e24256099d25fa1b1598ee24069f24511d7a2deafe6c')
16 changes: 15 additions & 1 deletion docs/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,21 @@ An array is a fixed-size list of values of the same basic type.
// array demension can be omitted when declared
int[] e = [1, 4, 2]; // e is of type int[3]
int[][] f = [[11, 12, 13], [21, 22, 23]]; // f is of type int[2][3]
* **Initialize/set an array to the same value** - Function ``T[size] repeat(T e, int size)`` returns an array with all ``size`` elements set to ``e``, where T can be any type.
Note ``size`` must be a :ref:`compile time constant<ctc-label>`.

.. code-block:: solidity
// a == [0, 0, 0]
int[3] a = repeat(0, 3);
// arr2D == [[0, 0, 0], [0, 0, 0]]
int[2][3] arr2D = repeat(0, 2);
int[4] flags = [false, true, false, true]
// set all flags to be false
flags = repeat(false, 4);
* **Index Operator** - index starting from 0. Out of bound access fails contract execution immediately.

.. code-block:: solidity
Expand Down

0 comments on commit d84981b

Please sign in to comment.