Madeorsk
6da1ed2e4c
+ Add anyascii function from its C implementation (`2023-08-01` version). + Add utf8ToAscii helper function. + Add unit tests.
37 lines
1.4 KiB
Markdown
37 lines
1.4 KiB
Markdown
# anyascii.zig
|
|
|
|
This repository allows to use anyascii C implementation from zig, with a helper function `utf8ToAscii` to easily convert any UTF-8 encoded string in an ASCII-only string.
|
|
|
|
Current version of anyascii: `2023-08-01`.
|
|
|
|
## How to use
|
|
|
|
### `anyascii`
|
|
|
|
```zig
|
|
const std = @import("std");
|
|
const anyascii = @import("anyascii").anyascii;
|
|
|
|
// A single UTF-8 codepoint to its ASCII equivalent.
|
|
const ascii = try anyascii(allocator, try std.unicode.utf8Decode("Φ"));
|
|
defer allocator.free(ascii);
|
|
std.debug.print("{s}", .{ascii}); // Output: "F".
|
|
```
|
|
|
|
### `utf8ToAscii`
|
|
|
|
```zig
|
|
const std = @import("std");
|
|
const anyascii = @import("anyascii").utf8ToAscii;
|
|
|
|
// A full string of UTF-8 characters to ASCII characters.
|
|
const ascii = try utf8ToAscii(allocator, "Blöße");
|
|
defer allocator.free(ascii);
|
|
std.debug.print("{s}", .{ascii}); // Output: "Blosse".
|
|
```
|
|
|
|
## What is anyascii?
|
|
|
|
Taken from [official _anyascii_ description](https://github.com/anyascii/anyascii/tree/master#description).
|
|
|
|
AnyAscii provides ASCII-only replacement strings for practically all Unicode characters. Text is converted character-by-character without considering the context. The mappings for each script are based on popular existing romanization systems. Symbolic characters are converted based on their meaning or appearance. All ASCII characters in the input are left unchanged, every other character is replaced with printable ASCII characters.
|