| ... | ... | @@ -1608,6 +1608,7 @@ pub fn tokenize(comptime T: type, buffer: []const T, delimiter_bytes: []const T) |
| 1608 | 1608 | test "tokenize" { |
| 1609 | 1609 | var it = tokenize(u8, " abc def ghi ", " "); |
| 1610 | 1610 | try testing.expect(eql(u8, it.next().?, "abc")); |
| 1611 | try testing.expect(eql(u8, it.peek().?, "def")); |
| 1611 | 1612 | try testing.expect(eql(u8, it.next().?, "def")); |
| 1612 | 1613 | try testing.expect(eql(u8, it.next().?, "ghi")); |
| 1613 | 1614 | try testing.expect(it.next() == null); |
| ... | ... | @@ -1626,9 +1627,11 @@ test "tokenize" { |
| 1626 | 1627 | |
| 1627 | 1628 | it = tokenize(u8, "|", "|"); |
| 1628 | 1629 | try testing.expect(it.next() == null); |
| 1630 | try testing.expect(it.peek() == null); |
| 1629 | 1631 | |
| 1630 | 1632 | it = tokenize(u8, "", "|"); |
| 1631 | 1633 | try testing.expect(it.next() == null); |
| 1634 | try testing.expect(it.peek() == null); |
| 1632 | 1635 | |
| 1633 | 1636 | it = tokenize(u8, "hello", ""); |
| 1634 | 1637 | try testing.expect(eql(u8, it.next().?, "hello")); |
| ... | ... | @@ -1650,11 +1653,13 @@ test "tokenize" { |
| 1650 | 1653 | test "tokenize (multibyte)" { |
| 1651 | 1654 | var it = tokenize(u8, "a|b,c/d e", " /,|"); |
| 1652 | 1655 | try testing.expect(eql(u8, it.next().?, "a")); |
| 1656 | try testing.expect(eql(u8, it.peek().?, "b")); |
| 1653 | 1657 | try testing.expect(eql(u8, it.next().?, "b")); |
| 1654 | 1658 | try testing.expect(eql(u8, it.next().?, "c")); |
| 1655 | 1659 | try testing.expect(eql(u8, it.next().?, "d")); |
| 1656 | 1660 | try testing.expect(eql(u8, it.next().?, "e")); |
| 1657 | 1661 | try testing.expect(it.next() == null); |
| 1662 | try testing.expect(it.peek() == null); |
| 1658 | 1663 | |
| 1659 | 1664 | var it16 = tokenize( |
| 1660 | 1665 | u16, |
| ... | ... | @@ -1778,8 +1783,17 @@ pub fn TokenIterator(comptime T: type) type { |
| 1778 | 1783 | |
| 1779 | 1784 | const Self = @This(); |
| 1780 | 1785 | |
| 1781 | | /// Returns a slice of the next token, or null if tokenization is complete. |
| 1786 | /// Returns a slice of the current token, or null if tokenization is |
| 1787 | /// complete, and advances to the next token. |
| 1782 | 1788 | pub fn next(self: *Self) ?[]const T { |
| 1789 | const result = self.peek() orelse return null; |
| 1790 | self.index += result.len; |
| 1791 | return result; |
| 1792 | } |
| 1793 | |
| 1794 | /// Returns a slice of the current token, or null if tokenization is |
| 1795 | /// complete. Does not advance to the next token. |
| 1796 | pub fn peek(self: *Self) ?[]const T { |
| 1783 | 1797 | // move to beginning of token |
| 1784 | 1798 | while (self.index < self.buffer.len and self.isSplitByte(self.buffer[self.index])) : (self.index += 1) {} |
| 1785 | 1799 | const start = self.index; |
| ... | ... | @@ -1788,8 +1802,8 @@ pub fn TokenIterator(comptime T: type) type { |
| 1788 | 1802 | } |
| 1789 | 1803 | |
| 1790 | 1804 | // move to end of token |
| 1791 | | while (self.index < self.buffer.len and !self.isSplitByte(self.buffer[self.index])) : (self.index += 1) {} |
| 1792 | | const end = self.index; |
| 1805 | var end = start; |
| 1806 | while (end < self.buffer.len and !self.isSplitByte(self.buffer[end])) : (end += 1) {} |
| 1793 | 1807 | |
| 1794 | 1808 | return self.buffer[start..end]; |
| 1795 | 1809 | } |