zigzedd/zlugify
[MIRROR] Generate ASCII slugs from unicode strings.
Generate ASCII slugs from unicode strings
zlugify is part of zedd, a collection of useful libraries for zig.
zlugify is a library to generate slugs from all types of UTF-8 encoded strings. It uses anyascii.zig to convert UTF-8 encoded strings into ASCII-only strings.
zlugify 1.0.1 is made and tested with zig 0.13.0.
In your project directory:
$ zig fetch --save https://code.zeptotech.net/zedd/zlugify/archive/v1.0.1.tar.gz
In build.zig
:
// Add zlugify dependency.
const zlugify = b.dependency("zlugify", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zlugify", zlugify.module("zlugify"));
These examples are highly inspired from the test cases that you can find at the end of lib.zig
.
const slugify = @import("zlugify").slugify;
const slug = try slugify(allocator, " This is a test.\t\n");
defer allocator.free(slug);
try std.testing.expectEqualStrings("this-is-a-test", slug);
const slugify = @import("zlugify").slugify;
const slug = try slugify(allocator, "SôMÈThing \t ÉLSÈ");
defer allocator.free(slug);
try std.testing.expectEqualStrings("something-else", slug);
const slugify = @import("zlugify").slugify;
const slug = try slugify(allocator, "埼玉 県");
defer allocator.free(slug);
try std.testing.expectEqualStrings("qiyu-xian", slug);
const slugify = @import("zlugify").slugify;
const slug = try slugify(allocator, "𝒔𝒍𝒖𝒈𝒊𝒇𝒚 𝒂 𝒔𝒕𝒓𝒊𝒏𝒈");
defer allocator.free(slug);
try std.testing.expectEqualStrings("slugify-a-string", slug);
const slugify = @import("zlugify").slugify;
const slug = try slugify(allocator, "hello 🦊");
defer allocator.free(slug);
try std.testing.expectEqualStrings("hello-fox", slug);
const slugifySeparator = @import("zlugify").slugify;
const slug = try slugifySeparator(allocator, "tôi yêu những chú kỳ lân", '_');
defer allocator.free(slug);
try std.testing.expectEqualStrings("toi_yeu_nhung_chu_ky_lan", slug);