structstd.unicode.Utf16LeIterator[src]

Fields

bytes: []const u8
i: usize

Error Sets

Error SetNextCodepointError[src]

Errors

anyerror means the error set is known only at runtime.

DanglingSurrogateHalf
ExpectedSecondSurrogateHalf
UnexpectedSecondSurrogateHalf

Source Code

Source code
pub const NextCodepointError = error{ DanglingSurrogateHalf, ExpectedSecondSurrogateHalf, UnexpectedSecondSurrogateHalf }

Functions

Functioninit[src]

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

Parameters

s: []const u16

Source Code

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

FunctionnextCodepoint[src]

pub fn nextCodepoint(it: *Utf16LeIterator) NextCodepointError!?u21

Parameters

Source Code

Source code
pub fn nextCodepoint(it: *Utf16LeIterator) NextCodepointError!?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;
    if (utf16IsHighSurrogate(code_units[0])) {
        // surrogate pair
        if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;
        code_units[1] = mem.readInt(u16, it.bytes[it.i..][0..2], .little);
        const codepoint = try utf16DecodeSurrogatePair(&code_units);
        it.i += 2;
        return codepoint;
    } else if (utf16IsLowSurrogate(code_units[0])) {
        return error.UnexpectedSecondSurrogateHalf;
    } else {
        return code_units[0];
    }
}

Source Code

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

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

    pub const NextCodepointError = error{ DanglingSurrogateHalf, ExpectedSecondSurrogateHalf, UnexpectedSecondSurrogateHalf };

    pub fn nextCodepoint(it: *Utf16LeIterator) NextCodepointError!?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;
        if (utf16IsHighSurrogate(code_units[0])) {
            // surrogate pair
            if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;
            code_units[1] = mem.readInt(u16, it.bytes[it.i..][0..2], .little);
            const codepoint = try utf16DecodeSurrogatePair(&code_units);
            it.i += 2;
            return codepoint;
        } else if (utf16IsLowSurrogate(code_units[0])) {
            return error.UnexpectedSecondSurrogateHalf;
        } else {
            return code_units[0];
        }
    }
}