Compare commits

...

10 commits
v1.0.0 ... main

Author SHA1 Message Date
d02dc51456
Add more tests for small strings. 2025-01-08 23:35:25 +01:00
a269c141c8
Add a license. 2025-01-08 23:13:15 +01:00
eac629f456
Fix import in utf8ToAscii example. 2025-01-08 23:07:41 +01:00
8855636817
Remove const qualifier in result strings. 2025-01-08 22:28:17 +01:00
72794325d0
Version 1.1.0 2025-01-08 22:16:42 +01:00
eb85754ece
anyasciiWrite with zig writer to avoid unnecessary allocations.
+ Add `anyasciiWrite` which writes to a zig writer directly to avoid unnecessary allocations in some use cases.
+ Use `anyasciiWrite` utf8ToAscii to avoid allocation duplicates in the previous implementation.
2025-01-08 22:14:56 +01:00
95f2130a13
Version 1.0.1 2025-01-08 21:59:54 +01:00
2dd23a7580
Include anyascii in the zig package. 2025-01-08 21:56:50 +01:00
7dc30ed1e2
Fix install documentation. 2025-01-08 21:56:17 +01:00
62f118a18c
Add installation instructions. 2025-01-08 20:16:46 +01:00
5 changed files with 52 additions and 11 deletions

9
LICENSE Normal file
View file

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024 Zeptotech
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -22,7 +22,7 @@ std.debug.print("{s}", .{ascii}); // Output: "F".
```zig
const std = @import("std");
const anyascii = @import("anyascii").utf8ToAscii;
const utf8ToAscii = @import("anyascii").utf8ToAscii;
// A full string of UTF-8 characters to ASCII characters.
const ascii = try utf8ToAscii(allocator, "Blöße");
@ -30,6 +30,25 @@ defer allocator.free(ascii);
std.debug.print("{s}", .{ascii}); // Output: "Blosse".
```
### Install
In your project directory:
```shell
zig fetch --save https://code.zeptotech.net/zedd/anyascii.zig/archive/v1.1.0.tar.gz
```
In `build.zig`:
```zig
// Add anyascii.zig dependency.
const anyascii = b.dependency("anyascii.zig", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("anyascii", anyascii.module("anyascii"));
```
## What is anyascii?
Taken from [official _anyascii_ description](https://github.com/anyascii/anyascii/tree/master#description).

View file

@ -1,8 +1,5 @@
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

View file

@ -1,12 +1,13 @@
.{
.name = "anyascii.zig",
.version = "1.0.0",
.version = "1.1.0",
.dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"anyascii",
"README.md",
},
}

View file

@ -5,7 +5,7 @@ const c = @cImport({
});
/// Convert a unicode codepoint to its ascii equivalent.
pub fn anyascii(allocator: std.mem.Allocator, codepoint: u21) ![]const u8 {
pub fn anyascii(allocator: std.mem.Allocator, codepoint: u21) ![]u8 {
// Call C anyascii function.
var cChars: [*]u8 = undefined;
const charsCount = c.anyascii(codepoint, @ptrCast(&cChars));
@ -18,20 +18,33 @@ pub fn anyascii(allocator: std.mem.Allocator, codepoint: u21) ![]const u8 {
return result;
}
/// Convert a unicode codepoint to its ascii equivalent, in the provided writer.
pub fn anyasciiWrite(writer: std.io.AnyWriter, codepoint: u21) !void {
// Call C anyascii function.
var cChars: [*]u8 = undefined;
const charsCount = c.anyascii(codepoint, @ptrCast(&cChars));
// Write every byte from the raw C pointer.
for (0..charsCount) |i| {
try writer.writeByte(cChars[i]);
}
}
/// Convert a given UTF-8 string to its ASCII equivalent using anyascii.
pub fn utf8ToAscii(allocator: std.mem.Allocator, str: []const u8) ![]const u8 {
pub fn utf8ToAscii(allocator: std.mem.Allocator, str: []const u8) ![]u8 {
// Get a UTF8 iterator.
var iterator = (try std.unicode.Utf8View.init(str)).iterator();
// Initialize a out string arraylist where ascii equivalents will be appended.
// Initialize a out string array list where ascii equivalents will be appended.
var outStr = try std.ArrayList(u8).initCapacity(allocator, str.len | 15);
defer outStr.deinit();
// Get a writer to the array list.
const writer = outStr.writer().any();
// For each codepoint, convert it to ascii.
while (iterator.nextCodepoint()) |codepoint| {
const ascii = try anyascii(allocator, codepoint);
defer allocator.free(ascii);
try outStr.appendSlice(ascii); //TODO use a writer to avoid this copy
try anyasciiWrite(writer, codepoint);
}
// Return the built full ascii equivalent.
@ -57,6 +70,8 @@ fn testAnyascii(expectedAscii: []const u8, utf8str: []const u8) !void {
test utf8ToAscii {
// These examples are taken from anyascii examples, see https://github.com/anyascii/anyascii/tree/master#examples
try testUtf8ToAscii("a ", "à ");
try testUtf8ToAscii("a", "à");
try testUtf8ToAscii("Rene Francois Lacote", "René François Lacôte");
try testUtf8ToAscii("Blosse", "Blöße");
try testUtf8ToAscii("Tran Hung Dao", "Trần Hưng Đạo");