From 6b24d631ee427366455099a8965083178566ae5e Mon Sep 17 00:00:00 2001 From: Nulo Date: Wed, 3 Nov 2021 08:40:28 -0300 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ README.md | 1 + build.zig | 33 +++++++++++++++++++ src/main.zig | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 build.zig create mode 100644 src/main.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c26d4af --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-out +zig-cache diff --git a/README.md b/README.md new file mode 100644 index 0000000..1a8f314 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# [txt2txt](https://nulo.in/txt2txt.html) diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..c4c0ee8 --- /dev/null +++ b/build.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("tzt2tzt", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + const exe_tests = b.addTest("src/main.zig"); + exe_tests.setBuildMode(mode); + + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&exe_tests.step); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..a9069a8 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,93 @@ +const std = @import("std"); +const diccionario = [_][]const u8{ + "e_/¯¯-____", + "a___/_//", + "r__//¯_/", + "d/¯_/¯__", + "l/_/ _//", + "u___///¯", + "v/¯/¯-__", + "x/¯¯--_/", + "n¯//_-/", + "i_/-/¯¯", + "c¯¯/__-", + "t-/__/¯", + "z//_/__", + "o__-//", + "s-//-_", + "p///_-", + "b__//¯", + "q--_//", + "h/¯-¯/", + "f_¯/__", + "g/ /_", + "y_//", + "ñ//-", + "k--/", + "m/_", + "w-", + "j/", +}; + +pub fn txt2castellano( + allocator: *std.mem.Allocator, + stdin: std.fs.File.Reader, + stdout: std.fs.File, +) !void { + const input = try stdin.readAllAlloc(allocator, 1024 * 1024); + defer allocator.free(input); + + var ventana: []u8 = input[0..]; + ventanaLoop: while (ventana.len != 0) { + for (diccionario) |letra| { + if (std.mem.startsWith(u8, ventana, letra[1..])) { + try stdout.writeAll(letra[0..1]); + ventana = ventana[letra[1..].len..]; + continue :ventanaLoop; + } + } + try stdout.writeAll(ventana[0..1]); + ventana = ventana[1..]; + } +} + +pub fn castellano2txt(stdin: std.fs.File.Reader, stdout: std.fs.File) !void { + stdin: while (stdin.readByte() catch null) |byte| { + for (diccionario) |letra| { + if (letra[0] == byte) { + try stdout.writeAll(letra[1..]); + continue :stdin; + } + } + try stdout.writeAll(&[_]u8{byte}); + } +} + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = &gpa.allocator; + defer _ = gpa.deinit(); + + const stdin = std.io.getStdIn().reader(); + const stdout = std.io.getStdOut(); + const stderr = std.io.getStdErr(); + + var args = std.process.args(); + if (!args.skip()) unreachable; + if (args.next(allocator)) |arg_union| { + const arg = try arg_union; + if (std.mem.eql(u8, arg, "castellano")) { + try castellano2txt(stdin, stdout); + return; + } else if (std.mem.eql(u8, arg, "txt")) { + try txt2castellano(allocator, stdin, stdout); + return; + } + } + + try stderr.writeAll( + \\tzt2tzt {castellano|txt} < {archivo de texto} + \\Castellano si tenés castellano y querés traducir a txt, y viceversa + \\ + ); +}