Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
d02dc51456 | |||
a269c141c8 | |||
eac629f456 | |||
8855636817 | |||
72794325d0 | |||
eb85754ece |
4 changed files with 33 additions and 9 deletions
9
LICENSE
Normal file
9
LICENSE
Normal 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.
|
|
@ -22,7 +22,7 @@ std.debug.print("{s}", .{ascii}); // Output: "F".
|
||||||
|
|
||||||
```zig
|
```zig
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const anyascii = @import("anyascii").utf8ToAscii;
|
const utf8ToAscii = @import("anyascii").utf8ToAscii;
|
||||||
|
|
||||||
// A full string of UTF-8 characters to ASCII characters.
|
// A full string of UTF-8 characters to ASCII characters.
|
||||||
const ascii = try utf8ToAscii(allocator, "Blöße");
|
const ascii = try utf8ToAscii(allocator, "Blöße");
|
||||||
|
@ -35,7 +35,7 @@ std.debug.print("{s}", .{ascii}); // Output: "Blosse".
|
||||||
In your project directory:
|
In your project directory:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
zig fetch --save https://code.zeptotech.net/zedd/anyascii.zig/archive/v1.0.1.tar.gz
|
zig fetch --save https://code.zeptotech.net/zedd/anyascii.zig/archive/v1.1.0.tar.gz
|
||||||
```
|
```
|
||||||
|
|
||||||
In `build.zig`:
|
In `build.zig`:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
.{
|
.{
|
||||||
.name = "anyascii.zig",
|
.name = "anyascii.zig",
|
||||||
.version = "1.0.1",
|
.version = "1.1.0",
|
||||||
.dependencies = .{},
|
.dependencies = .{},
|
||||||
|
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
|
27
src/lib.zig
27
src/lib.zig
|
@ -5,7 +5,7 @@ const c = @cImport({
|
||||||
});
|
});
|
||||||
|
|
||||||
/// Convert a unicode codepoint to its ascii equivalent.
|
/// 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.
|
// Call C anyascii function.
|
||||||
var cChars: [*]u8 = undefined;
|
var cChars: [*]u8 = undefined;
|
||||||
const charsCount = c.anyascii(codepoint, @ptrCast(&cChars));
|
const charsCount = c.anyascii(codepoint, @ptrCast(&cChars));
|
||||||
|
@ -18,20 +18,33 @@ pub fn anyascii(allocator: std.mem.Allocator, codepoint: u21) ![]const u8 {
|
||||||
return result;
|
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.
|
/// 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.
|
// Get a UTF8 iterator.
|
||||||
var iterator = (try std.unicode.Utf8View.init(str)).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);
|
var outStr = try std.ArrayList(u8).initCapacity(allocator, str.len | 15);
|
||||||
defer outStr.deinit();
|
defer outStr.deinit();
|
||||||
|
|
||||||
|
// Get a writer to the array list.
|
||||||
|
const writer = outStr.writer().any();
|
||||||
|
|
||||||
// For each codepoint, convert it to ascii.
|
// For each codepoint, convert it to ascii.
|
||||||
while (iterator.nextCodepoint()) |codepoint| {
|
while (iterator.nextCodepoint()) |codepoint| {
|
||||||
const ascii = try anyascii(allocator, codepoint);
|
try anyasciiWrite(writer, codepoint);
|
||||||
defer allocator.free(ascii);
|
|
||||||
try outStr.appendSlice(ascii); //TODO use a writer to avoid this copy
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the built full ascii equivalent.
|
// Return the built full ascii equivalent.
|
||||||
|
@ -57,6 +70,8 @@ fn testAnyascii(expectedAscii: []const u8, utf8str: []const u8) !void {
|
||||||
test utf8ToAscii {
|
test utf8ToAscii {
|
||||||
// These examples are taken from anyascii examples, see https://github.com/anyascii/anyascii/tree/master#examples
|
// 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("Rene Francois Lacote", "René François Lacôte");
|
||||||
try testUtf8ToAscii("Blosse", "Blöße");
|
try testUtf8ToAscii("Blosse", "Blöße");
|
||||||
try testUtf8ToAscii("Tran Hung Dao", "Trần Hưng Đạo");
|
try testUtf8ToAscii("Tran Hung Dao", "Trần Hưng Đạo");
|
||||||
|
|
Loading…
Reference in a new issue