structstd.unicode.Wtf16LeIterator[src]

Fields

bytes: []const u8
i: usize

Functions

Functioninit[src]

pub fn init(s: []const u16) Wtf16LeIterator

Parameters

s: []const u16

Source Code

Source code
pub fn init(s: []const u16) Wtf16LeIterator {
    return Wtf16LeIterator{
        .bytes = mem.sliceAsBytes(s),
        .i = 0,
    };
}

FunctionnextCodepoint[src]

pub fn nextCodepoint(it: *Wtf16LeIterator) ?u21

If the next codepoint is encoded by a surrogate pair, returns the codepoint that the surrogate pair represents. If the next codepoint is an unpaired surrogate, returns the codepoint of the unpaired surrogate.

Parameters

Source Code

Source code
pub fn nextCodepoint(it: *Wtf16LeIterator) ?u21 {
    assert(it.i <= it.bytes.len);
    if (it.i == it.bytes.len) return null;
    var code_units: [2]u16 = undefined;
    code_units[0] = mem.readInt(u16, it.bytes[it.i..][0..2], .little);
    it.i += 2;
    surrogate_pair: {
        if (utf16IsHighSurrogate(code_units[0])) {
            if (it.i >= it.bytes.len) break :surrogate_pair;
            code_units[1] = mem.readInt(u16, it.bytes[it.i..][0..2], .little);
            const codepoint = utf16DecodeSurrogatePair(&code_units) catch break :surrogate_pair;
            it.i += 2;
            return codepoint;
        }
    }
    return code_units[0];
}

Source Code

Source code
pub const Wtf16LeIterator = struct {
    bytes: []const u8,
    i: usize,

    pub fn init(s: []const u16) Wtf16LeIterator {
        return Wtf16LeIterator{
            .bytes = mem.sliceAsBytes(s),
            .i = 0,
        };
    }

    /// If the next codepoint is encoded by a surrogate pair, returns the
    /// codepoint that the surrogate pair represents.
    /// If the next codepoint is an unpaired surrogate, returns the codepoint
    /// of the unpaired surrogate.
    pub fn nextCodepoint(it: *Wtf16LeIterator) ?u21 {
        assert(it.i <= it.bytes.len);
        if (it.i == it.bytes.len) return null;
        var code_units: [2]u16 = undefined;
        code_units[0] = mem.readInt(u16, it.bytes[it.i..][0..2], .little);
        it.i += 2;
        surrogate_pair: {
            if (utf16IsHighSurrogate(code_units[0])) {
                if (it.i >= it.bytes.len) break :surrogate_pair;
                code_units[1] = mem.readInt(u16, it.bytes[it.i..][0..2], .little);
                const codepoint = utf16DecodeSurrogatePair(&code_units) catch break :surrogate_pair;
                it.i += 2;
                return codepoint;
            }
        }
        return code_units[0];
    }
}