| ... | ... | @@ -310,7 +310,7 @@ pub fn main() !void { |
| 310 | 310 | <p> |
| 311 | 311 | The two arguments passed to the <code>stdout.print()</code> function, <code>"Hello, {s}!\n"</code> |
| 312 | 312 | and <code>.{"world"}</code>, are evaluated at {#link|compile-time|comptime#}. The code sample is |
| 313 | | purposely written to show how to perform {#link|string|String Literals and Character Literals#} |
| 313 | purposely written to show how to perform {#link|string|String Literals and Unicode Code Point Literals#} |
| 314 | 314 | substitution in the <code>print</code> function. The curly-braces inside of the first argument |
| 315 | 315 | are substituted with the compile-time known value inside of the second argument |
| 316 | 316 | (known as an {#link|anonymous struct literal|Anonymous Struct Literals#}). The <code>\n</code> |
| ... | ... | @@ -682,18 +682,31 @@ pub fn main() void { |
| 682 | 682 | </div> |
| 683 | 683 | {#see_also|Optionals|undefined#} |
| 684 | 684 | {#header_close#} |
| 685 | | {#header_open|String Literals and Character Literals#} |
| 685 | {#header_open|String Literals and Unicode Code Point Literals#} |
| 686 | 686 | <p> |
| 687 | | String literals are single-item constant {#link|Pointers#} to null-terminated UTF-8 encoded byte arrays. |
| 687 | String literals are single-item constant {#link|Pointers#} to null-terminated byte arrays. |
| 688 | 688 | The type of string literals encodes both the length, and the fact that they are null-terminated, |
| 689 | 689 | and thus they can be {#link|coerced|Type Coercion#} to both {#link|Slices#} and |
| 690 | 690 | {#link|Null-Terminated Pointers|Sentinel-Terminated Pointers#}. |
| 691 | 691 | Dereferencing string literals converts them to {#link|Arrays#}. |
| 692 | 692 | </p> |
| 693 | 693 | <p> |
| 694 | | Character literals have type {#syntax#}comptime_int{#endsyntax#}, the same as |
| 694 | The encoding of a string in Zig is de-facto assumed to be UTF-8. |
| 695 | Because Zig source code is {#link|UTF-8 encoded|Source Encoding#}, any non-ASCII bytes appearing within a string literal |
| 696 | in source code carry their UTF-8 meaning into the content of the string in the Zig program; |
| 697 | the bytes are not modified by the compiler. |
| 698 | However, it is possible to embbed non-UTF-8 bytes into a string literal using <code>\xNN</code> notation. |
| 699 | </p> |
| 700 | <p> |
| 701 | Unicode code point literals have type {#syntax#}comptime_int{#endsyntax#}, the same as |
| 695 | 702 | {#link|Integer Literals#}. All {#link|Escape Sequences#} are valid in both string literals |
| 696 | | and character literals. |
| 703 | and Unicode code point literals. |
| 704 | </p> |
| 705 | <p> |
| 706 | In many other programming languages, a Unicode code point literal is called a "character literal". |
| 707 | However, there is <a href="https://unicode.org/glossary">no precise technical definition of a "character"</a> |
| 708 | in recent versions of the Unicode specification (as of Unicode 13.0). |
| 709 | In Zig, a Unicode code point literal corresponds to the Unicode definition of a code point. |
| 697 | 710 | </p> |
| 698 | 711 | {#code_begin|test#} |
| 699 | 712 | const expect = @import("std").testing.expect; |
| ... | ... | @@ -709,6 +722,7 @@ test "string literals" { |
| 709 | 722 | expect('\u{1f4a9}' == 128169); |
| 710 | 723 | expect('💯' == 128175); |
| 711 | 724 | expect(mem.eql(u8, "hello", "h\x65llo")); |
| 725 | expect("\xff"[0] == 0xff); // non-UTF-8 strings are possible with \xNN notation. |
| 712 | 726 | } |
| 713 | 727 | {#code_end#} |
| 714 | 728 | {#see_also|Arrays|Zig Test|Source Encoding#} |
| ... | ... | @@ -749,11 +763,11 @@ test "string literals" { |
| 749 | 763 | </tr> |
| 750 | 764 | <tr> |
| 751 | 765 | <td><code>\xNN</code></td> |
| 752 | | <td>hexadecimal 8-bit character code (2 digits)</td> |
| 766 | <td>hexadecimal 8-bit byte value (2 digits)</td> |
| 753 | 767 | </tr> |
| 754 | 768 | <tr> |
| 755 | 769 | <td><code>\u{NNNNNN}</code></td> |
| 756 | | <td>hexadecimal Unicode character code UTF-8 encoded (1 or more digits)</td> |
| 770 | <td>hexadecimal Unicode code point UTF-8 encoded (1 or more digits)</td> |
| 757 | 771 | </tr> |
| 758 | 772 | </table> |
| 759 | 773 | </div> |
| ... | ... | @@ -7414,7 +7428,7 @@ test "main" { |
| 7414 | 7428 | This function returns a compile time constant pointer to null-terminated, |
| 7415 | 7429 | fixed-size array with length equal to the byte count of the file given by |
| 7416 | 7430 | {#syntax#}path{#endsyntax#}. The contents of the array are the contents of the file. |
| 7417 | | This is equivalent to a {#link|string literal|String Literals and Character Literals#} |
| 7431 | This is equivalent to a {#link|string literal|String Literals and Unicode Code Point Literals#} |
| 7418 | 7432 | with the file contents. |
| 7419 | 7433 | </p> |
| 7420 | 7434 | <p> |