| ... | @@ -871,6 +871,13 @@ pub fn main() void { | ... | @@ -871,6 +871,13 @@ pub fn main() void { |
| 871 | However, it is possible to embed non-UTF-8 bytes into a string literal using <code>\xNN</code> notation. | 871 | However, it is possible to embed non-UTF-8 bytes into a string literal using <code>\xNN</code> notation. |
| 872 | </p> | 872 | </p> |
| 873 | <p> | 873 | <p> |
| | 874 | Indexing into a string containing non-ASCII bytes will return individual bytes, whether valid |
| | 875 | UTF-8 or not. |
| | 876 | The {#link|Zig Standard Library#} provides routines for checking the validity of UTF-8 encoded |
| | 877 | strings, accessing their code points and other encoding/decoding related tasks in |
| | 878 | {#syntax#}std.unicode{#endsyntax#}. |
| | 879 | </p> |
| | 880 | <p> |
| 874 | Unicode code point literals have type {#syntax#}comptime_int{#endsyntax#}, the same as | 881 | Unicode code point literals have type {#syntax#}comptime_int{#endsyntax#}, the same as |
| 875 | {#link|Integer Literals#}. All {#link|Escape Sequences#} are valid in both string literals | 882 | {#link|Integer Literals#}. All {#link|Escape Sequences#} are valid in both string literals |
| 876 | and Unicode code point literals. | 883 | and Unicode code point literals. |
| ... | @@ -894,9 +901,12 @@ pub fn main() void { | ... | @@ -894,9 +901,12 @@ pub fn main() void { |
| 894 | print("{}\n", .{'e' == '\x65'}); // true | 901 | print("{}\n", .{'e' == '\x65'}); // true |
| 895 | print("{d}\n", .{'\u{1f4a9}'}); // 128169 | 902 | print("{d}\n", .{'\u{1f4a9}'}); // 128169 |
| 896 | print("{d}\n", .{'💯'}); // 128175 | 903 | print("{d}\n", .{'💯'}); // 128175 |
| 897 | print("{}\n", .{mem.eql(u8, "hello", "h\x65llo")}); // true | | |
| 898 | print("0x{x}\n", .{"\xff"[0]}); // non-UTF-8 strings are possible with \xNN notation. | | |
| 899 | print("{u}\n", .{'âš¡'}); | 904 | print("{u}\n", .{'âš¡'}); |
| | 905 | print("{}\n", .{mem.eql(u8, "hello", "h\x65llo")}); // true |
| | 906 | print("{}\n", .{mem.eql(u8, "💯", "\xf0\x9f\x92\xaf")}); // also true |
| | 907 | const invalid_utf8 = "\xff\xfe"; // non-UTF-8 strings are possible with \xNN notation. |
| | 908 | print("0x{x}\n", .{invalid_utf8[1]}); // indexing them returns individual bytes... |
| | 909 | print("0x{x}\n", .{"💯"[1]}); // ...as does indexing part-way through non-ASCII characters |
| 900 | } | 910 | } |
| 901 | {#code_end#} | 911 | {#code_end#} |
| 902 | {#see_also|Arrays|Source Encoding#} | 912 | {#see_also|Arrays|Source Encoding#} |