Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

feat: add string escape sequences #215

Merged
merged 1 commit into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions pages/book/expressions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,69 @@ Read more about booleans and [`Bool{:tact}`](/book/types#booleans) type in the d
A string literal is zero or more characters enclosed in double (`"`) quotation marks. All string literals are objects of [`String{:tact}`][p] type.

```tact
"foo"
"1234"
"foo";
"1234";
```

Tact strings support a range of [escape sequences](https://en.wikipedia.org/wiki/Escape_sequence) starting with a backslash `\\` character:

* `\\{:tact}` — literal backslash
* `\"{:tact}` — double quote
* `\n{:tact}` — newline
* `\r{:tact}` — carriage return
* `\t{:tact}` — tab
* `\v{:tact}` — vertical tab
* `\b{:tact}` — backspace
* `\f{:tact}` — form feed
* `\x00{:tact}` through `\xFF{:tact}` — [code point](https://en.wikipedia.org/wiki/Code_point), must be exactly two hex digits long
* `\u0000{:tact}` through `\uFFFF{:tact}` — [Unicode code point][unicode], must be exactly four hex digits long
* `\u{0}{:tact}` through `\u{FFFFFF}{:tact}` — [Unicode code point][unicode], can be from $1$ to $6$ hex digits long

[unicode]: https://en.wikipedia.org/wiki/Unicode#Codespace_and_code_points

```tact
// \\
"escape \\ if \\ you \\ can \\";

// \"
"this \"literally\" works";

// \n
"line \n another line";

// \r
"Shutters \r Like \r This \r One";

// Note, that at the moment Tact strings can't have escape characters in them:
"line \n another"; // SYNTAX ERROR!, see: https://github.com/tact-lang/tact/issues/25
// \t
"spacing \t granted!";

// This means, that double quotes inside strings are also prohibited:
"this \"can't be!\""; // SYNTAX ERROR!
// \v
"those \v words \v are \v aligned";

// \b
"rm\b\bcreate!";

// \f
"form \f feed";

// \x00 - \xFF
"this \x22literally\x22 works"; // \x22 represents a double quote

// \u0000 - \uFFFF
"danger, \u26A1 high voltage \u26A1"; // \u26A1 represents the ⚡ emoji

// \u{0} - \u{FFFFFF}
"\u{1F602} LOL \u{1F602}"; // \u{1F602} represents the 😂 emoji
```

Read more about strings and [`String{:tact}`][p] type there: [Primitive types][p].
<Callout>

Read more about strings and [`String{:tact}`][p] type:\
[Primitive types in the Book][p]\
[Strings and StringBuilders in the Reference](/ref/api-strings)

</Callout>


### `null` literal

Expand Down
Loading