跳转至

ev-hyprland-expected-parse-hex-002

字段 值
项目 hyprwm/Hyprland
固定 commit 0aa6745f705f
路径 src/config/shared/parserUtils/ParserUtils.cpp
行段 L16-L21
特性 std::expected
许可证 BSD-3-Clause
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-29
测试代码 否
发现方式 regex

用法

把十六进制颜色分量解析成整数,失败时带上下文返回 std::unexpected

为什么典型

错误信息包含原始输入,调用方可以组合多个解析结果,并在解引用前统一检查,避免异常穿透解析层。

摘录

static std::expected<uint64_t, std::string> parseHex(std::string_view value) {
    auto res = value.starts_with("0x") ? strToNumber<uint64_t>(value) : strToNumber<uint64_t>(std::string{"0x"} + value);
    if (!res)
        return std::unexpected(std::format("invalid hex \"{}\"", value));
    return *res;
}

备注

生产路径:Hyprland 配置解析层。

查看固定 commit 源码 · 返回证据索引