Initial commit

This commit is contained in:
Cat /dev/Nulo 2021-11-03 08:40:28 -03:00
commit 6b24d631ee
4 changed files with 129 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
zig-out
zig-cache

1
README.md Normal file
View file

@ -0,0 +1 @@
# [txt2txt](https://nulo.in/txt2txt.html)

33
build.zig Normal file
View file

@ -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);
}

93
src/main.zig Normal file
View file

@ -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
\\
);
}