echo -eを追加

このコミットが含まれているのは:
守矢諏訪子 2023-07-31 21:20:38 +09:00
コミット f009420129
1個のファイルの変更62行の追加0行の削除

ファイルの表示

@ -13,12 +13,66 @@ fn help() !void {
try stdout.print("使用法: echo\n", .{});
try stdout.print("文章を表示\n\n", .{});
try stdout.print("-n ニューラインを見逃す\n", .{});
try stdout.print("-e バックスラッシュエスケープ\n", .{});
try stdout.print("-h ヘルプを表示\n", .{});
try stdout.print("-v バージョンを表示\n", .{});
try bw.flush();
}
fn parseEscape(text: [1024]u8) [1024]u8 {
var i: usize = 0;
var output: [1024]u8 = undefined;
var output_len: usize = 0;
while (i < text.len) {
if (text[i] == '\\') {
i += 1;
if (i < text.len) {
if (text[i] == 'n') {
output[output_len] = '\n';
output_len += 1;
} else if (text[i] == 't') {
output[output_len] = '\t';
output_len += 1;
} else if (text[i] == 'r') {
output[output_len] = '\r';
output_len += 1;
} else if (text[i] == 'e') {
if (i + 1 < text.len and text[i + 1] == '[') {
output[output_len] = '\x1B';
output_len += 1;
i += 1;
while (i < text.len and text[i] != 'm') {
output[output_len] = text[i];
output_len += 1;
i += 1;
}
if (i < text.len) {
output[output_len] = text[i];
output_len += 1;
}
}
} else {
output[output_len] = '\\';
output_len += 1;
output[output_len] = text[i];
output_len += 1;
}
output_len += 1;
}
} else {
output[output_len] = text[i];
output_len += 1;
}
i += 1;
}
return output;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
@ -48,6 +102,7 @@ pub fn main() !void {
}
var isnonl = false;
var isescp = false;
for (option.items) |i| {
if (i == 'h') {
try help();
@ -60,12 +115,19 @@ pub fn main() !void {
if (i == 'n') {
isnonl = true;
}
if (i == 'e') {
isescp = true;
}
}
const stdof = io.getStdOut().writer();
var bw = io.bufferedWriter(stdof);
const stdout = bw.writer();
if (isescp) {
text = parseEscape(text);
}
if (isnonl) {
try stdout.print("{s}", .{text[0..text_len]});
} else {