From 7a8ebab596be06e25a182dc8e60589a7dfd355da Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Thu, 27 Mar 2025 14:59:33 -0400 Subject: [PATCH 1/3] fix(docs): clarify strings --- gitlab-pages/docs/data-types/strings.md | 71 +++++++++---------------- 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/gitlab-pages/docs/data-types/strings.md b/gitlab-pages/docs/data-types/strings.md index ab8606f254..a359640e80 100644 --- a/gitlab-pages/docs/data-types/strings.md +++ b/gitlab-pages/docs/data-types/strings.md @@ -4,8 +4,8 @@ title: Strings import Syntax from '@theme/Syntax'; -Strings are of the predefined type `string`. Literal strings are set -between double quotes. +Strings are of the predefined type `string`. +Literal strings are set between double quotes. @@ -13,9 +13,6 @@ between double quotes. let a : string = "Hello Alice" ``` -Note: See the predefined -[module String](../reference/string-reference/?lang=cameligo) - @@ -24,14 +21,22 @@ Note: See the predefined const a :string = "Hello Alice"; ``` -Note: See predefined [namespace String](../reference/string-reference/?lang=jsligo) + + + + +For reference, see the predefined [module String](../reference/string-reference/?lang=cameligo). -### Casting + + +For reference, see the predefined [namespace String](../reference/string-reference/?lang=jsligo). + + Strings can be used in contexts where a boolean is expected: an empty -string is then interpreted as `false`, and `true` otherwise. +string is interpreted as `false` and a non-empty string is interpreted as `true`. @@ -63,9 +68,6 @@ let greeting = "Hello" let full_greeting = greeting ^ " " ^ name ``` -Note: See the predefined -[module String](../reference/string-reference/?lang=cameligo) - @@ -79,14 +81,11 @@ const greeting = "Hello"; const full_greeting = greeting + " " + name; ``` -Note: See predefined [namespace String](../reference/string-reference/?lang=jsligo) - ## Sizing -The length of a string can be obtain by calling the predefined -functions `String.length` or `String.size`: +To get the length of a string, use the function `String.length` or `String.size`: @@ -94,9 +93,6 @@ functions `String.length` or `String.size`: let length : nat = String.size "Alice" // length = 5n ``` -Note: See the predefined -[module String](../reference/string-reference/?lang=cameligo) - @@ -105,15 +101,14 @@ Note: See the predefined const length : nat = String.size("Alice"); // length == 5n ``` -Note: See predefined [namespace String](../reference/string-reference/?lang=jsligo) - ## Slicing -Substrings can be extracted using the predefined function -`String.sub`. The first character has index 0 and the interval of -indices for the substring has inclusive bounds. +You can extract a substring from a string with the `String.sub` function. +It accepts a nat for the index of the start of the substring and a nat for the number of characters. +Both numbers are inclusive. +The first character of a string has the index 0. @@ -122,57 +117,39 @@ let name = "Alice" let slice = String.sub 0n 1n name // slice = "A" ``` -Note: See the predefined -[module String](../reference/string-reference/?lang=cameligo) - -The offset and length of the slice are natural number: - ```jsligo group=slicing const name = "Alice"; const slice = String.sub (0n, 1n, name); // slice == "A" ``` -Note: See predefined [namespace String](../reference/string-reference/?lang=jsligo) - -## Verbatim +## Verbatim strings -Strings can contain control characters, like `\n`. Sometimes we need -that each character in a string is interpreted on its own, for example -`\n` as two characters instead of a newline character. In that case, -either we escape the backslash character, or we use verbatim -strings. Those have the same type `string` as normal (that is, -interpreted) strings. +Strings can contain control characters, like `\n`. +To interpret each character on its own (such as treating `\n` as two characters), you can either escape the backslash character or use _verbatim strings_. +Verbatim strings have the same type as ordinary strings (that is, interpreted strings). -Verbatim strings are given between the delimiters `{|` and `|}`, -instead of double quotes: +Verbatim strings are given between the delimiters `{|` and `|}` instead of double quotes: ```cameligo group=verbatim let s : string = {|\n|} // String made of two characters ``` -Note: See the predefined -[module String](../reference/string-reference/?lang=cameligo) - -Verbatim strings are given between backquotes (a.k.a. backticks), -instead of double quotes: +Verbatim strings are given between backquotes (a.k.a. backticks), instead of double quotes: ```jsligo group=verbatim const s : string = `\n` // String made of two characters ``` -Note: See predefined [namespace String](../reference/string-reference/?lang=jsligo) - - From bd0c71d4dd377bf351824fefb3af3eb38a49d1e0 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Thu, 27 Mar 2025 15:12:05 -0400 Subject: [PATCH 2/3] fix(docs): review bytes topic --- gitlab-pages/docs/data-types/bytes.md | 83 ++++++++++++--------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/gitlab-pages/docs/data-types/bytes.md b/gitlab-pages/docs/data-types/bytes.md index e07f419963..06430d3b67 100644 --- a/gitlab-pages/docs/data-types/bytes.md +++ b/gitlab-pages/docs/data-types/bytes.md @@ -35,16 +35,13 @@ const zero_too = 0x00; -Clearly, this means that literal bytes are always comprised of an even -number of hexadecimal digits (because one hexadecimal digit requires -up to four bits in binary, and eight are needed to make up a byte). +This means that literal bytes are always comprised of an even number of hexadecimal digits, because one hexadecimal digit requires up to four bits in binary, and eight are needed to make up a byte. ## From numbers to bytes and back -Some other numerals can be converted to bytes by means of calling the -predefined function `bytes`, which is overloaded. The reverse -conversion is done by the predefined functions `int` and `nat`. For -instance, here how to create bytes from natural numbers and integers: +You can convert some other numerals to bytes by calling the predefined function `bytes`. +To convert ints or nats to bytes, use the predefined functions `int` and `nat`. +For instance, here how to create bytes from natural numbers and integers: @@ -72,21 +69,16 @@ const i: int = int(0x7B); // i == 123 -> Note: See -> [Two's complement](https://en.wikipedia.org/wiki/Two's_complement). +Note: See [Two's complement](https://en.wikipedia.org/wiki/Two's_complement). ## From strings -A string literal can be converted to bytes in two ways: +You can convert a string literal to bytes in two ways: - 1. by interpreting the [ASCII](https://en.wikipedia.org/wiki/ASCII) -code of each character (which spans over two hexadecimal digits) as -one byte; - 2. by interpreting directly each character as one hexadecimal digit. +- By interpreting the [ASCII](https://en.wikipedia.org/wiki/ASCII) code of each character (which spans over two hexadecimal digits) as one byte +- By interpreting directly each character as one hexadecimal digit - -In the former case, the syntax is somewhat odd -- as opposed to simply -calling the function `bytes`: +To interpret the ASCII code, use this syntax: @@ -104,7 +96,7 @@ const from_ascii: bytes = bytes`foo`; // Not a call -The latter case is implemented as a type cast: +To interpret each character directly, use a type cast: @@ -113,12 +105,13 @@ The latter case is implemented as a type cast: let raw : bytes = ("666f6f" : bytes) ``` -> Note that both the `[%bytes ...]` and `(... : bytes)` syntaxes apply -> only to *string literals*, not general expressions of type -> `string`. In other words, the contents of the strings must be -> available in-place at compile-time. (This actually reveals that -> `("666f6f" : bytes)` is not really a cast, as casts are -> non-operations.) +:::note + +Both cases apply only to string literals, not variables or other expressions of type `string`. +In other words, the contents of the strings must be available in-place at compile time. +(This reveals that `("666f6f" : bytes)` is not really a cast, as casts are non-operations.) + +::: @@ -129,11 +122,13 @@ let raw : bytes = ("666f6f" : bytes) const raw: bytes = ("666f6f" as bytes); ``` -> Note that both syntaxes apply respectively only to *verbatim* string -> literals and general strings, not general expressions of type -> `string`. In other words, the contents of the strings must be -> available at compile-time. (This actually reveals that `("666f6f" as -> bytes)` is not really a cast, as casts are non-operations.) +:::note + +Both cases apply only to string literals, not variables or other expressions of type `string`. +In other words, the contents of the strings must be available in-place at compile time. +(This reveals that `("666f6f" as bytes)` is not really a cast, as casts are non-operations.) + +::: @@ -161,8 +156,7 @@ const three: bytes = Bytes.concats([0x70, 0xAA, 0xFF]); ## Sizing -In order to obtain the length of a sequence of bytes, use the -predefined function `Bytes.length` like so: +To obtain the length of a sequence of bytes, use the predefined function `Bytes.length` like so: @@ -182,10 +176,10 @@ const len: nat = Bytes.length(0x0AFF); // len == 2n ## Slicing -Bytes can be extracted using the predefined function `Bytes.sub`. The -first parameter is the start index and the second is the number of -bytes of the slice we want. Keep in mind that the first byte in a -sequence has index `0n`. +You can extract a subset from bytes with the `Bytes.sub` function. +It accepts a nat for the index of the start of the subset and a nat for the number of bytes in the subset. +Both numbers are inclusive. +The first byte has the index 0. @@ -253,18 +247,17 @@ const shift_right: bytes = 0x0006 >> 1n; // 0x0003 ## Packing and unpacking -As Michelson provides the instructions `PACK` and `UNPACK` for data -serialisation, so does LIGO with `Bytes.pack` and `Bytes.unpack`. The -former serialises Michelson data structures into a binary format, and -the latter reverses that transformation. Unpacking may fail, so the -return type of `Byte.unpack` is an option that needs to be annotated. +LIGO provides the functions `Bytes.pack` and `Bytes.unpack` to serialize and deserialize data into a binary format. +These functions correspond to the Michelson instructions `PACK` and `UNPACK`. +Unpacking may fail, so the return type of `Byte.unpack` is an option that needs a type annotation. + +:::note -> Note: `PACK` and `UNPACK` are Michelson instructions that are -> intended to be used by people that really know what they are -> doing. There are several risks and failure cases, such as unpacking -> a lambda from an untrusted source or casting the result to the wrong -> type. Be careful. +Be careful packing and unpacking data. +These functions are intended for use by developers who are familiar with data serialization. +There are several risks and failure cases, such as unpacking a lambda from an untrusted source or casting the result to the wrong type. +::: From 68311f8951c88db86a288f810b2820285afc9451 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Thu, 27 Mar 2025 15:13:30 -0400 Subject: [PATCH 3/3] fix(docs): remove duplicate strings-bytes topic --- .../src/strings-bytes/a.jsligo | 3 - .../language-basics/src/strings-bytes/a.mligo | 3 - .../src/strings-bytes/b.jsligo | 2 - .../language-basics/src/strings-bytes/b.mligo | 2 - .../src/strings-bytes/c.jsligo | 2 - .../language-basics/src/strings-bytes/c.mligo | 2 - .../src/strings-bytes/d.jsligo | 3 - .../language-basics/src/strings-bytes/d.mligo | 3 - .../src/strings-bytes/e.jsligo | 2 - .../language-basics/src/strings-bytes/e.mligo | 2 - .../src/strings-bytes/f.jsligo | 2 - .../language-basics/src/strings-bytes/f.mligo | 2 - .../src/strings-bytes/g.jsligo | 14 - .../language-basics/src/strings-bytes/g.mligo | 14 - .../src/strings-bytes/h.jsligo | 10 - .../language-basics/src/strings-bytes/h.mligo | 10 - .../src/strings-bytes/ungrouped.jsligo | 3 - .../src/strings-bytes/ungrouped.mligo | 2 - .../docs/language-basics/strings-bytes.md | 320 ------------------ 19 files changed, 401 deletions(-) delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/a.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/a.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/b.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/b.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/c.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/c.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/d.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/d.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/e.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/e.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/f.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/f.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/g.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/g.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/h.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/h.mligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.jsligo delete mode 100644 gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.mligo delete mode 100644 gitlab-pages/docs/language-basics/strings-bytes.md diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/a.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/a.jsligo deleted file mode 100644 index e18f48d075..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/a.jsligo +++ /dev/null @@ -1,3 +0,0 @@ -const name = "Alice"; -const greeting = "Hello"; -const full_greeting = greeting + " " + name; \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/a.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/a.mligo deleted file mode 100644 index f2407cf5c8..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/a.mligo +++ /dev/null @@ -1,3 +0,0 @@ -let name : string = "Alice" -let greeting : string = "Hello" -let full_greeting : string = greeting ^ " " ^ name \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/b.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/b.jsligo deleted file mode 100644 index fbef99f207..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/b.jsligo +++ /dev/null @@ -1,2 +0,0 @@ -const name = "Alice"; -const slice = String.sub (0n, 1n, name); // slice == "A" \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/b.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/b.mligo deleted file mode 100644 index 9f2c87682c..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/b.mligo +++ /dev/null @@ -1,2 +0,0 @@ -let name : string = "Alice" -let slice : string = String.sub 0n 1n name (* slice = "A" *) \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/c.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/c.jsligo deleted file mode 100644 index 38ab59bc49..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/c.jsligo +++ /dev/null @@ -1,2 +0,0 @@ -const name = "Alice"; -const length = String.length(name); // length == 5 \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/c.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/c.mligo deleted file mode 100644 index 3d7abfdf9d..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/c.mligo +++ /dev/null @@ -1,2 +0,0 @@ -let name : string = "Alice" -let length : nat = String.length name // length = 5 \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/d.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/d.jsligo deleted file mode 100644 index 7ca49cfb6a..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/d.jsligo +++ /dev/null @@ -1,3 +0,0 @@ -const white = 0xffff; -const black = 0x0000; -const pixels = Bytes.concat(white, black); // 0xffff0000 \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/d.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/d.mligo deleted file mode 100644 index 617ff4bc15..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/d.mligo +++ /dev/null @@ -1,3 +0,0 @@ -let white : bytes = 0xffff -let black : bytes = 0x0000 -let pixels : bytes = Bytes.concat white black (* 0xffff0000 *) \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/e.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/e.jsligo deleted file mode 100644 index 86f9b8b13d..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/e.jsligo +++ /dev/null @@ -1,2 +0,0 @@ -const b = 0x12345678; -const slice = Bytes.sub (1n, 2n, b); // 0x3456 \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/e.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/e.mligo deleted file mode 100644 index b9a3cf1402..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/e.mligo +++ /dev/null @@ -1,2 +0,0 @@ -let b : bytes = 0x12345678 -let slice : bytes = Bytes.sub 1n 2n b (* 0x3456 *) \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/f.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/f.jsligo deleted file mode 100644 index 7cf74df3bf..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/f.jsligo +++ /dev/null @@ -1,2 +0,0 @@ -const b = 0x123456; -const length = Bytes.length(b); // length = 3 \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/f.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/f.mligo deleted file mode 100644 index bd991bed45..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/f.mligo +++ /dev/null @@ -1,2 +0,0 @@ -let b : bytes = 0x123456 -let length : nat = Bytes.length b (* length = 3 *) \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/g.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/g.jsligo deleted file mode 100644 index 06a07a5413..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/g.jsligo +++ /dev/null @@ -1,14 +0,0 @@ -/* Bitwise and */ -const b_and = 0x0005 & 0x0106; // 0x0004 - -/* Bitwise or */ -const b_or = 0x0005 | 0x0106; // 0x0107 - -/* Bitwise xor */ -const b_xor = 0x0005 ^ 0x0106; // 0x0103 - -/* Bitwise shift left */ -const b_shift_left = 0x06 << 8n; // 0x0600 - -/* Bitwise shift right */ -const b_shift_right = 0x0006 >> 1n; // 0x0003 \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/g.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/g.mligo deleted file mode 100644 index ed1b05c0a4..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/g.mligo +++ /dev/null @@ -1,14 +0,0 @@ -(* Bitwise and *) -let b_and = 0x0005 land 0x0106 (* 0x0004 *) - -(* Bitwise or *) -let b_or = 0x0005 lor 0x0106 (* 0x0107 *) - -(* Bitwise xor *) -let b_xor = 0x0005 lxor 0x0106 (* 0x0103 *) - -(* Bitwise shift left *) -let b_shift_left = 0x06 lsl 8n (* 0x0600 *) - -(* Bitwise shift right *) -let b_shift_right = 0x0006 lsr 1n (* 0x0003 *) \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/h.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/h.jsligo deleted file mode 100644 index 4931c628c6..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/h.jsligo +++ /dev/null @@ -1,10 +0,0 @@ -/* bytes -> nat */ -const test_bytes_nat = nat(0x1234) // 1234n - -/* nat -> bytes */ -const test_nat_bytes = bytes(4660n) // 0x1234 -/* bytes -> int */ -const test_bytes_int = int(0x1234) // 4660 - -/* int -> bytes */ -const test_int_bytes = bytes(4660) // 0x1234 \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/h.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/h.mligo deleted file mode 100644 index 4ad9c1daee..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/h.mligo +++ /dev/null @@ -1,10 +0,0 @@ -(* bytes -> nat *) -let test_bytes_nat = nat 0x1234 (* 1234n *) - -(* nat -> bytes *) -let test_nat_bytes = bytes 4660n (* 0x1234 *) -(* bytes -> int *) -let test_bytes_int = int 0x1234 (* 4660 *) - -(* int -> bytes *) -let test_int_bytes = bytes 4660 (* 0x1234 *) \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.jsligo b/gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.jsligo deleted file mode 100644 index f08dfc7e1d..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.jsligo +++ /dev/null @@ -1,3 +0,0 @@ -const a = "Hello Alice"; -const b = 0x7070; -const bs = (bytes `foo`); \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.mligo b/gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.mligo deleted file mode 100644 index 743a886e74..0000000000 --- a/gitlab-pages/docs/language-basics/src/strings-bytes/ungrouped.mligo +++ /dev/null @@ -1,2 +0,0 @@ -let b : bytes = 0x7070 -let bs : bytes = [%bytes "foo"] \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/strings-bytes.md b/gitlab-pages/docs/language-basics/strings-bytes.md deleted file mode 100644 index d54d7cf97c..0000000000 --- a/gitlab-pages/docs/language-basics/strings-bytes.md +++ /dev/null @@ -1,320 +0,0 @@ ---- -id: strings-bytes -title: Strings & Bytes ---- - -import Syntax from '@theme/Syntax'; - -## Strings - -Strings are defined using the built-in `string` type like this: - - - -``` -let a : string = "Hello Alice" -``` - - - - - -```jsligo -const a = "Hello Alice"; -``` - - - -### Concatenating Strings - - - -Strings can be concatenated using the `^` operator. - -```cameligo group=a -let name : string = "Alice" -let greeting : string = "Hello" -let full_greeting : string = greeting ^ " " ^ name -``` - - - - - -Strings can be concatenated using the `+` operator. - -```jsligo group=a -const name = "Alice"; -const greeting = "Hello"; -const full_greeting = greeting + " " + name; -``` - - - -### Extracting Substrings - -Substrings can be extracted using the predefined function -`String.sub`. The first character has index 0 and the interval of -indices for the substring has inclusive bounds. - - - -```cameligo group=b -let name : string = "Alice" -let slice : string = String.sub 0n 1n name (* slice = "A" *) -``` - - - - - -```jsligo group=b -const name = "Alice"; -const slice = String.sub (0n, 1n, name); // slice == "A" -``` - - - -> ⚠️ Notice that the offset and length of the slice are natural -> numbers. - -### Length of Strings - -The length of a string can be found using a built-in function: - - - -```cameligo group=c -let name : string = "Alice" -let length : nat = String.length name // length = 5 -``` - -> Note that `String.size` is *deprecated*. - - - - - -```jsligo group=c -const name = "Alice"; -const length = String.length(name); // length == 5 -``` - - - -## Bytes - -Byte literals are defined using the prefix `0x` followed by hexadecimal digits like this: - - - -```cameligo -let b : bytes = 0x7070 -``` - - - - - -```jsligo -const b = 0x7070; -``` - - - -Moreover, a string literal can be converted to its bytes representation: - - - -```cameligo -let bs : bytes = [%bytes "foo"] -``` - - - - - -```jsligo -const bs = (bytes `foo`); -``` - - - - -### Concatenating Bytes - -Bytes can be concatenated using the `Bytes.concat` function. - - - -```cameligo group=d -let white : bytes = 0xffff -let black : bytes = 0x0000 -let pixels : bytes = Bytes.concat white black (* 0xffff0000 *) -``` - - - - - -```jsligo group=d -const white = 0xffff; -const black = 0x0000; -const pixels = Bytes.concat(white, black); // 0xffff0000 -``` - - - -### Extracting Bytes - -Bytes can be extracted using the predefined function `Bytes.sub`. The -first parameter takes the start index and the second parameter takes -the number of bytes. Pay special attention to how `bytes` are -indexed. - - - -```cameligo group=e -let b : bytes = 0x12345678 -let slice : bytes = Bytes.sub 1n 2n b (* 0x3456 *) -``` - - - - - -```jsligo group=e -const b = 0x12345678; -const slice = Bytes.sub (1n, 2n, b); // 0x3456 -``` - - - -### Length of Bytes - -The length of `bytes` can be found using a built-in function `Bytes.length`: - - - -```cameligo group=f -let b : bytes = 0x123456 -let length : nat = Bytes.length b (* length = 3 *) -``` - - - - - -```jsligo group=f -const b = 0x123456; -const length = Bytes.length(b); // length = 3 -``` - - - -### Bitwise operators - -You can perform bitwise operation on `bytes` as follows: - - - -```cameligo group=g -(* Bitwise and *) -let b_and = 0x0005 land 0x0106 (* 0x0004 *) - -(* Bitwise or *) -let b_or = 0x0005 lor 0x0106 (* 0x0107 *) - -(* Bitwise xor *) -let b_xor = 0x0005 lxor 0x0106 (* 0x0103 *) - -(* Bitwise shift left *) -let b_shift_left = 0x06 lsl 8n (* 0x0600 *) - -(* Bitwise shift right *) -let b_shift_right = 0x0006 lsr 1n (* 0x0003 *) -``` - - - - - -```jsligo group=g -/* Bitwise and */ -const b_and = 0x0005 & 0x0106; // 0x0004 - -/* Bitwise or */ -const b_or = 0x0005 | 0x0106; // 0x0107 - -/* Bitwise xor */ -const b_xor = 0x0005 ^ 0x0106; // 0x0103 - -/* Bitwise shift left */ -const b_shift_left = 0x06 << 8n; // 0x0600 - -/* Bitwise shift right */ -const b_shift_right = 0x0006 >> 1n; // 0x0003 -``` - - - - -### From `bytes` to `nat` and back - -You can case `bytes` to `nat` using the built-in `nat` function and vice-versa -using using the `bytes` built-in function. - - - -```cameligo group=h -(* bytes -> nat *) -let test_bytes_nat = nat 0x1234 (* 1234n *) - -(* nat -> bytes *) -let test_nat_bytes = bytes 4660n (* 0x1234 *) -``` - - - - - -```jsligo group=h -/* bytes -> nat */ -const test_bytes_nat = nat(0x1234) // 1234n - -/* nat -> bytes */ -const test_nat_bytes = bytes(4660n) // 0x1234 -``` - - - -### From `bytes` to `int` and back - -You can cast `bytes` to `int` using the built-in `int` function and -vice-versa using the `bytes` built-in function. - - - -```cameligo group=h -(* bytes -> int *) -let test_bytes_int = int 0x1234 (* 4660 *) - -(* int -> bytes *) -let test_int_bytes = bytes 4660 (* 0x1234 *) -``` - - - - - -```jsligo group=h -/* bytes -> int */ -const test_bytes_int = int(0x1234) // 4660 - -/* int -> bytes */ -const test_int_bytes = bytes(4660) // 0x1234 -``` - - - - \ No newline at end of file