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

Commit 3609ef9

Browse files
authored
feat: string escape sequences (#215)
1 parent 8e63230 commit 3609ef9

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

pages/book/expressions.mdx

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,69 @@ Read more about booleans and [`Bool{:tact}`](/book/types#booleans) type in the d
6868
A string literal is zero or more characters enclosed in double (`"`) quotation marks. All string literals are objects of [`String{:tact}`][p] type.
6969

7070
```tact
71-
"foo"
72-
"1234"
71+
"foo";
72+
"1234";
73+
```
74+
75+
Tact strings support a range of [escape sequences](https://en.wikipedia.org/wiki/Escape_sequence) starting with a backslash `\\` character:
76+
77+
* `\\{:tact}` — literal backslash
78+
* `\"{:tact}` — double quote
79+
* `\n{:tact}` — newline
80+
* `\r{:tact}` — carriage return
81+
* `\t{:tact}` — tab
82+
* `\v{:tact}` — vertical tab
83+
* `\b{:tact}` — backspace
84+
* `\f{:tact}` — form feed
85+
* `\x00{:tact}` through `\xFF{:tact}`[code point](https://en.wikipedia.org/wiki/Code_point), must be exactly two hex digits long
86+
* `\u0000{:tact}` through `\uFFFF{:tact}`[Unicode code point][unicode], must be exactly four hex digits long
87+
* `\u{0}{:tact}` through `\u{FFFFFF}{:tact}`[Unicode code point][unicode], can be from $1$ to $6$ hex digits long
88+
89+
[unicode]: https://en.wikipedia.org/wiki/Unicode#Codespace_and_code_points
90+
91+
```tact
92+
// \\
93+
"escape \\ if \\ you \\ can \\";
94+
95+
// \"
96+
"this \"literally\" works";
97+
98+
// \n
99+
"line \n another line";
100+
101+
// \r
102+
"Shutters \r Like \r This \r One";
73103
74-
// Note, that at the moment Tact strings can't have escape characters in them:
75-
"line \n another"; // SYNTAX ERROR!, see: https://github.com/tact-lang/tact/issues/25
104+
// \t
105+
"spacing \t granted!";
76106
77-
// This means, that double quotes inside strings are also prohibited:
78-
"this \"can't be!\""; // SYNTAX ERROR!
107+
// \v
108+
"those \v words \v are \v aligned";
109+
110+
// \b
111+
"rm\b\bcreate!";
112+
113+
// \f
114+
"form \f feed";
115+
116+
// \x00 - \xFF
117+
"this \x22literally\x22 works"; // \x22 represents a double quote
118+
119+
// \u0000 - \uFFFF
120+
"danger, \u26A1 high voltage \u26A1"; // \u26A1 represents the ⚡ emoji
121+
122+
// \u{0} - \u{FFFFFF}
123+
"\u{1F602} LOL \u{1F602}"; // \u{1F602} represents the 😂 emoji
79124
```
80125

81-
Read more about strings and [`String{:tact}`][p] type there: [Primitive types][p].
126+
<Callout>
127+
128+
Read more about strings and [`String{:tact}`][p] type:\
129+
[Primitive types in the Book][p]\
130+
[Strings and StringBuilders in the Reference](/ref/api-strings)
131+
132+
</Callout>
133+
82134

83135
### `null` literal
84136

0 commit comments

Comments
 (0)