authorgravatar for jacobdufault@gmail.comJacob Dufault <jacobdufault@gmail.com> 2018-03-05 21:42:01-08:00
committergravatar for jacobdufault@gmail.comJacob Dufault <jacobdufault@gmail.com> 2018-03-05 21:42:01-08:00
log8fd7e9115c95c78c7f0fdcf749c3109124c1605f
treedb3f90a6342e39dfd8c4dcea22159b503c0304a3
parent6568be575cb87c2f54aad2dfa20d1f35471d2224

Make Utf8View public, add comments, and make iterator lowercase.


1 files changed, 14 insertions(+), 6 deletions(-)

std/unicode.zig+14-6
......@@ -96,7 +96,15 @@ pub fn utf8ValidateSlice(s: []const u8) bool {
9696 return true;
9797}
9898
99const Utf8View = struct {
99/// Utf8View makes it easy to iterate the code points of a utf-8 encoded string.
100///
101/// ```
102/// var utf8 = (try std.unicode.Utf8View.init("hi there")).iterator();
103/// while (utf8.nextCodepointSlice()) |codepoint| {
104/// std.debug.warn("got codepoint {}\n", codepoint);
105/// }
106/// ```
107pub const Utf8View = struct {
100108 bytes: []const u8,
101109
102110 pub fn init(s: []const u8) !Utf8View {
......@@ -124,7 +132,7 @@ const Utf8View = struct {
124132 }
125133 }
126134
127 pub fn Iterator(s: &const Utf8View) Utf8Iterator {
135 pub fn iterator(s: &const Utf8View) Utf8Iterator {
128136 return Utf8Iterator {
129137 .bytes = s.bytes,
130138 .i = 0,
......@@ -165,13 +173,13 @@ const Utf8Iterator = struct {
165173test "utf8 iterator on ascii" {
166174 const s = Utf8View.initComptime("abc");
167175
168 var it1 = s.Iterator();
176 var it1 = s.iterator();
169177 debug.assert(std.mem.eql(u8, "a", ??it1.nextCodepointSlice()));
170178 debug.assert(std.mem.eql(u8, "b", ??it1.nextCodepointSlice()));
171179 debug.assert(std.mem.eql(u8, "c", ??it1.nextCodepointSlice()));
172180 debug.assert(it1.nextCodepointSlice() == null);
173181
174 var it2 = s.Iterator();
182 var it2 = s.iterator();
175183 debug.assert(??it2.nextCodepoint() == 'a');
176184 debug.assert(??it2.nextCodepoint() == 'b');
177185 debug.assert(??it2.nextCodepoint() == 'c');
......@@ -189,13 +197,13 @@ test "utf8 view bad" {
189197test "utf8 view ok" {
190198 const s = Utf8View.initComptime("東京市");
191199
192 var it1 = s.Iterator();
200 var it1 = s.iterator();
193201 debug.assert(std.mem.eql(u8, "東", ??it1.nextCodepointSlice()));
194202 debug.assert(std.mem.eql(u8, "京", ??it1.nextCodepointSlice()));
195203 debug.assert(std.mem.eql(u8, "市", ??it1.nextCodepointSlice()));
196204 debug.assert(it1.nextCodepointSlice() == null);
197205
198 var it2 = s.Iterator();
206 var it2 = s.iterator();
199207 debug.assert(??it2.nextCodepoint() == 0x6771);
200208 debug.assert(??it2.nextCodepoint() == 0x4eac);
201209 debug.assert(??it2.nextCodepoint() == 0x5e02);