This commit is contained in:
agra
2026-02-14 22:26:58 +02:00
parent e7d2abdf0c
commit 3fd14bafac
4 changed files with 231 additions and 403 deletions

46
src/unescape.zig Normal file
View File

@@ -0,0 +1,46 @@
const std = @import("std");
/// Process escape sequences in a raw string literal.
pub fn unescapeString(allocator: std.mem.Allocator, raw: []const u8) ![]u8 {
var result = try allocator.alloc(u8, raw.len);
var i: usize = 0;
var j: usize = 0;
while (i < raw.len) {
if (raw[i] == '\\' and i + 1 < raw.len) {
i += 1;
switch (raw[i]) {
'n' => {
result[j] = '\n';
},
't' => {
result[j] = '\t';
},
'r' => {
result[j] = '\r';
},
'\\' => {
result[j] = '\\';
},
'"' => {
result[j] = '"';
},
'0' => {
result[j] = 0;
},
'`' => {
result[j] = '`';
},
else => {
result[j] = raw[i];
},
}
j += 1;
i += 1;
} else {
result[j] = raw[i];
j += 1;
i += 1;
}
}
return result[0..j];
}