Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › zig › string_literal › parseEscapeSequence Public Function parseEscapeSequence Doc Source Public Function std.zig.string_literal.parseEscapeSequence
pub fn parseEscapeSequence (slice : []const u8 , offset : *usize ) ParsedCharLiteral Params slice : []const u8 offset : *usize File zig/string_literal.zig: 151
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 pub fn parseEscapeSequence (slice : []const u8 , offset : *usize ) ParsedCharLiteral {
assert (slice .len > offset .*);
assert (slice [offset .*] == '\\' );
if (slice .len == offset .* + 1 )
return .{ .failure = .{ .invalid_escape_character = offset .* + 1 } };
offset .* += 2 ;
switch (slice [offset .* - 1 ]) {
'n' => return .{ .success = '\n' },
'r' => return .{ .success = '\r' },
'\\' => return .{ .success = '\\' },
't' => return .{ .success = '\t' },
'\'' => return .{ .success = '\'' },
'"' => return .{ .success = '"' },
'x' => {
var value : u8 = 0 ;
var i : usize = offset .*;
while (i < offset .* + 2 ) : (i += 1 ) {
if (i == slice .len ) return .{ .failure = .{ .expected_hex_digit = i } };
const c = slice [i ];
switch (c ) {
'0' ...'9' => {
value *= 16 ;
value += c - '0' ;
},
'a' ...'f' => {
value *= 16 ;
value += c - 'a' + 10 ;
},
'A' ...'F' => {
value *= 16 ;
value += c - 'A' + 10 ;
},
else => {
return .{ .failure = .{ .expected_hex_digit = i } };
},
}
}
offset .* = i ;
return .{ .success = value };
},
'u' => {
var i : usize = offset .*;
if (i >= slice .len or slice [i ] != '{' ) return .{ .failure = .{ .expected_lbrace = i } };
i += 1 ;
if (i >= slice .len ) return .{ .failure = .{ .expected_hex_digit_or_rbrace = i } };
if (slice [i ] == '}' ) return .{ .failure = .{ .empty_unicode_escape_sequence = i } };
var value : u32 = 0 ;
while (i < slice .len ) : (i += 1 ) {
const c = slice [i ];
switch (c ) {
'0' ...'9' => {
value *= 16 ;
value += c - '0' ;
},
'a' ...'f' => {
value *= 16 ;
value += c - 'a' + 10 ;
},
'A' ...'F' => {
value *= 16 ;
value += c - 'A' + 10 ;
},
'}' => {
i += 1 ;
break ;
},
else => return .{ .failure = .{ .expected_hex_digit_or_rbrace = i } },
}
if (value > 0x10ffff ) {
return .{ .failure = .{ .invalid_unicode_codepoint = i } };
}
} else {
return .{ .failure = .{ .expected_rbrace = i } };
}
offset .* = i ;
return .{ .success = @as (u21 , @intCast (value )) };
},
else => return .{ .failure = .{ .invalid_escape_character = offset .* - 1 } },
}
}
Parse an escape sequence from
slice[offset..]. If parsing is successful, offset is updated to reflect the characters consumed.