Repository: bitcoin/bitcoin
File: src/util/strencodings.cpp (C++)

Code before:
        }
    }
    return result;
}

bool IsHex(std::string_view str)
{
    for (char c : str) {
        if (HexDigit(c) < 0) return false;
    }
    return (str.size() > 0) && (str.size()%2 == 0);
}

bool IsHexNumber(std::string_view str)
{
    if (str.substr(0, 2) == "0x") str.remove_prefix(2);
    for (char c : str) {
        if (HexDigit(c) < 0) return false;
    }
    // Return false for empty string or "0x".
    return str.size() > 0;
}

template <typename Byte>
std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
{
    std::vector<Byte> vch;
    vch.reserve(str.size() / 2); // two hex characters form a single byte

    auto it = str.begin();
    while (it != str.end()) {
        if (IsSpace(*it)) {
            ++it;
            continue;
        }
        auto c1 = HexDigit(*(it++));
        if (it == str.end()) return std::nullopt;
        auto c2 = HexDigit(*(it++));
        if (c1 < 0 || c2 < 0) return std::nullopt;
        vch.push_back(Byte(c1 << 4) | Byte(c2));
    }
    return vch;
}
template std::optional<std::vector<std::byte>> TryParseHex(std::string_view);
template std::optional<std::vector<uint8_t>> TryParseHex(std::string_view);

bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
{
    bool valid = false;
    size_t colon = in.find_last_of(':');
    // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator

Review comment (security):
Given that:
* this is only used in a single place in prod (which accept `std::string_view` as well and has an `std::string` input)
* we're always copying the result before returning
* the overwhelming majority of inputs will probably have the correct size already, i.e. don't need change.
* the performance of the method seems to be important

Could we either return an `std::optional<std::string_view>` instead or take an `std::string& input` to be able to return it?

i.e.
```suggestion
std::optional<std::string> TrySanitizeHexNumber(std::string input, int result_size)
{
    input = util::RemovePrefixView(input, "0x");
    const auto final_size{(result_size < 0) ? input.size() : static_cast<size_t>(result_size)};
    if (input.empty() || (input.size() > final_size)) [[unlikely]] return std::nullopt;
    for (char c : input) {
        if (HexDigit(c) < 0) [[unlikely]] return std::nullopt;
    }
    if (input.size() == final_size) [[likely]] return input;

    std::string result(final_size, '0');
    std::copy(input.begin(), input.end(), result.begin() + (final_size - input.size()));
    return result;
}
```

Code after:
        }
    }
    return result;
}

bool IsHex(std::string_view str)
{
    for (char c : str) {
        if (HexDigit(c) < 0) return false;
    }
    return (str.size() > 0) && (str.size()%2 == 0);
}

template <typename Byte>
std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
{
    std::vector<Byte> vch;
    vch.reserve(str.size() / 2); // two hex characters form a single byte

    auto it = str.begin();
    while (it != str.end()) {
        if (IsSpace(*it)) {
            ++it;
            continue;
        }
        auto c1 = HexDigit(*(it++));
        if (it == str.end()) return std::nullopt;
        auto c2 = HexDigit(*(it++));
        if (c1 < 0 || c2 < 0) return std::nullopt;
        vch.push_back(Byte(c1 << 4) | Byte(c2));
    }
    return vch;
}
template std::optional<std::vector<std::byte>> TryParseHex(std::string_view);
template std::optional<std::vector<uint8_t>> TryParseHex(std::string_view);

bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
{
    bool valid = false;
    size_t colon = in.find_last_of(':');
    // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
    bool fHaveColon = colon != in.npos;
    bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
    bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(':', colon - 1) != in.npos)};
    if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
        uint16_t n;
        if (ParseUInt16(in.substr(colon + 1), &n)) {
            in = in.substr(0, colon);
            portOut = n;
            valid = (portOut != 0);
        }