Move string manipulation FAQ items to type docs

Update docs/types/value-types.rst

Co-Authored-By: ChrisChinchilla <chriswhward@gmail.com>

Update docs/types/value-types.rst

Co-Authored-By: ChrisChinchilla <chriswhward@gmail.com>

Fixed formatting

Re-add example

Clarify text

Rearrange string manipulation
This commit is contained in:
Chris Ward
2019-01-22 13:57:54 +02:00
co-authored by ChrisChinchilla
parent fd3bdcb747
commit 9a8882c9fc
4 changed files with 16 additions and 46 deletions
+11 -3
View File
@@ -106,13 +106,24 @@ Array elements can be of any type, including mapping or struct. The general
restrictions for types apply, in that mappings can only be stored in the
``storage`` data location and publicly-visible functions need parameters that are :ref:`ABI types <ABI>`.
It is possible to mark arrays ``public`` and have Solidity create a :ref:`getter <visibility-and-getters>`.
The numeric index becomes a required parameter for the getter.
Accessing an array past its end causes a failing assertion. You can use the ``.push()`` method to append a new element at the end or assign to the ``.length`` :ref:`member <array-members>` to change the size (see below for caveats).
method or increase the ``.length`` :ref:`member <array-members>` to add elements.
``bytes`` and ``strings`` as Arrays
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Variables of type ``bytes`` and ``string`` are special arrays. A ``bytes`` is similar to ``byte[]``,
but it is packed tightly in calldata and memory. ``string`` is equal to ``bytes`` but does not allow
length or index access.
While Solidity does not have string manipulation functions, you can use
this implicit conversion for equivalent functionality. For example to compare
two strings ``keccak256(abi.encode(s1)) == keccak256(abi.encode(s2))``, or to
concatenate two strings already encoded with ``abi.encodePacked(s1, s2);``.
You should use ``bytes`` over ``byte[]`` because it is cheaper, since ``byte[]`` adds 31 padding bytes between the elements. As a general rule,
use ``bytes`` for arbitrary-length raw byte data and ``string`` for arbitrary-length
string (UTF-8) data. If you can limit the length to a certain number of bytes,
@@ -124,9 +135,6 @@ always use one of the value types ``bytes1`` to ``bytes32`` because they are muc
that you are accessing the low-level bytes of the UTF-8 representation,
and not the individual characters.
It is possible to mark arrays ``public`` and have Solidity create a :ref:`getter <visibility-and-getters>`.
The numeric index becomes a required parameter for the getter.
.. index:: ! array;allocating, new
Allocating Memory Arrays
+1 -1
View File
@@ -708,4 +708,4 @@ Another example that uses external function types::
}
.. note::
Lambda or inline functions are planned but not yet supported.
Lambda or inline functions are planned but not yet supported.