-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
200 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const std = @import("std"); | ||
|
||
context: *const anyopaque, | ||
guessFn: *const fn (context: *const anyopaque, text: []const u8) bool, | ||
checkEncodingFn: *const fn (context: *const anyopaque, xml_encoding: []const u8) bool, | ||
transcodeFn: *const fn (context: *const anyopaque, noalias dest: []u8, noalias src: []const u8) TranscodeResult, | ||
|
||
const Encoding = @This(); | ||
|
||
pub const TranscodeResult = struct { | ||
dest_written: usize, | ||
src_read: usize, | ||
}; | ||
|
||
pub fn guess(encoding: Encoding, text: []const u8) bool { | ||
return encoding.guessFn(encoding.context, text); | ||
} | ||
|
||
pub fn checkEncoding(encoding: Encoding, xml_encoding: []const u8) bool { | ||
return encoding.checkEncodingFn(encoding.context, xml_encoding); | ||
} | ||
|
||
pub fn transcode(encoding: Encoding, noalias dest: []u8, noalias src: []const u8) TranscodeResult { | ||
return encoding.transcodeFn(encoding.context, dest, src); | ||
} | ||
|
||
pub const utf8: Encoding = .{ | ||
.context = undefined, | ||
.guessFn = &utf8Guess, | ||
.checkEncodingFn = &utf8CheckEncoding, | ||
.transcodeFn = &utf8Transcode, | ||
}; | ||
|
||
fn utf8Guess(context: *const anyopaque, text: []const u8) bool { | ||
_ = context; | ||
_ = text; | ||
return true; | ||
} | ||
|
||
fn utf8CheckEncoding(context: *const anyopaque, encoding: []const u8) bool { | ||
_ = context; | ||
return std.ascii.eqlIgnoreCase(encoding, "UTF-8"); | ||
} | ||
|
||
fn utf8Transcode(context: *const anyopaque, noalias dest: []u8, noalias src: []const u8) TranscodeResult { | ||
_ = context; | ||
var dest_written: usize = 0; | ||
var src_read: usize = 0; | ||
while (src_read < src.len) { | ||
const cp_len = std.unicode.utf8ByteSequenceLength(src[src_read]) catch break; | ||
if (src_read + cp_len > src.len or dest_written + cp_len > dest.len) break; | ||
switch (cp_len) { | ||
1 => { | ||
dest[dest_written] = src[src_read]; | ||
dest_written += 1; | ||
src_read += 1; | ||
}, | ||
2, 3, 4 => { | ||
const slice = src[src_read..][0..cp_len]; | ||
if (!std.unicode.utf8ValidateSlice(slice)) break; | ||
@memcpy(dest[dest_written..][0..cp_len], slice); | ||
dest_written += cp_len; | ||
src_read += cp_len; | ||
}, | ||
else => unreachable, | ||
} | ||
} | ||
return .{ | ||
.dest_written = dest_written, | ||
.src_read = src_read, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters