aboutsummaryrefslogtreecommitdiff
path: root/std/unicode.zig
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-03-06 01:42:04 -0500
committerGitHub <noreply@github.com>2018-03-06 01:42:04 -0500
commitd34d36619eae07d046d1bc20f849c2c398927992 (patch)
tree41171c7f451a252f7d58b3d8b6a1cc5ff4e44b92 /std/unicode.zig
parentc787837ce5023fdef579bf92097216ce9dfacf34 (diff)
parent8fd7e9115c95c78c7f0fdcf749c3109124c1605f (diff)
downloadzig-d34d36619eae07d046d1bc20f849c2c398927992.tar.gz
zig-d34d36619eae07d046d1bc20f849c2c398927992.zip
Merge pull request #814 from jacobdufault/utf8-view
Make Utf8View public, add comments, and make iterator lowercase.
Diffstat (limited to 'std/unicode.zig')
-rw-r--r--std/unicode.zig20
1 files changed, 14 insertions, 6 deletions
diff --git a/std/unicode.zig b/std/unicode.zig
index 81bbc2aab6..66779f401a 100644
--- a/std/unicode.zig
+++ b/std/unicode.zig
@@ -96,7 +96,15 @@ pub fn utf8ValidateSlice(s: []const u8) bool {
return true;
}
-const Utf8View = struct {
+/// Utf8View makes it easy to iterate the code points of a utf-8 encoded string.
+///
+/// ```
+/// var utf8 = (try std.unicode.Utf8View.init("hi there")).iterator();
+/// while (utf8.nextCodepointSlice()) |codepoint| {
+/// std.debug.warn("got codepoint {}\n", codepoint);
+/// }
+/// ```
+pub const Utf8View = struct {
bytes: []const u8,
pub fn init(s: []const u8) !Utf8View {
@@ -124,7 +132,7 @@ const Utf8View = struct {
}
}
- pub fn Iterator(s: &const Utf8View) Utf8Iterator {
+ pub fn iterator(s: &const Utf8View) Utf8Iterator {
return Utf8Iterator {
.bytes = s.bytes,
.i = 0,
@@ -165,13 +173,13 @@ const Utf8Iterator = struct {
test "utf8 iterator on ascii" {
const s = Utf8View.initComptime("abc");
- var it1 = s.Iterator();
+ var it1 = s.iterator();
debug.assert(std.mem.eql(u8, "a", ??it1.nextCodepointSlice()));
debug.assert(std.mem.eql(u8, "b", ??it1.nextCodepointSlice()));
debug.assert(std.mem.eql(u8, "c", ??it1.nextCodepointSlice()));
debug.assert(it1.nextCodepointSlice() == null);
- var it2 = s.Iterator();
+ var it2 = s.iterator();
debug.assert(??it2.nextCodepoint() == 'a');
debug.assert(??it2.nextCodepoint() == 'b');
debug.assert(??it2.nextCodepoint() == 'c');
@@ -189,13 +197,13 @@ test "utf8 view bad" {
test "utf8 view ok" {
const s = Utf8View.initComptime("東京市");
- var it1 = s.Iterator();
+ var it1 = s.iterator();
debug.assert(std.mem.eql(u8, "東", ??it1.nextCodepointSlice()));
debug.assert(std.mem.eql(u8, "京", ??it1.nextCodepointSlice()));
debug.assert(std.mem.eql(u8, "市", ??it1.nextCodepointSlice()));
debug.assert(it1.nextCodepointSlice() == null);
- var it2 = s.Iterator();
+ var it2 = s.iterator();
debug.assert(??it2.nextCodepoint() == 0x6771);
debug.assert(??it2.nextCodepoint() == 0x4eac);
debug.assert(??it2.nextCodepoint() == 0x5e02);