struct Wtf8View [src]
Wtf8View iterates the code points of a WTF-8 encoded string,
including surrogate halves.
var wtf8 = (try std.unicode.Wtf8View.init("hi there")).iterator();
while (wtf8.nextCodepointSlice()) |codepoint| {
// note: codepoint could be a surrogate half which is invalid
// UTF-8, avoid printing or otherwise sending/emitting this directly
}
Fields
bytes: []const u8
Members
- init (Function)
- initComptime (Function)
- initUnchecked (Function)
- iterator (Function)
Source
pub const Wtf8View = struct {
bytes: []const u8,
pub fn init(s: []const u8) error{InvalidWtf8}!Wtf8View {
if (!wtf8ValidateSlice(s)) {
return error.InvalidWtf8;
}
return initUnchecked(s);
}
pub fn initUnchecked(s: []const u8) Wtf8View {
return Wtf8View{ .bytes = s };
}
pub inline fn initComptime(comptime s: []const u8) Wtf8View {
return comptime if (init(s)) |r| r else |err| switch (err) {
error.InvalidWtf8 => {
@compileError("invalid wtf8");
},
};
}
pub fn iterator(s: Wtf8View) Wtf8Iterator {
return Wtf8Iterator{
.bytes = s.bytes,
.i = 0,
};
}
}