<commit_before>#include <nk/net/ip_address.hpp>
#include <nk/endian.hpp>
#include <algorithm>
#include <limits>
#include <cassert>
#ifndef _WIN32
#include <arpa/inet.h>
#endif

namespace nk {

ip_address::ip_address(any)
{
    set(in6addr_any);
}

bool ip_address::set(const void * const sas, size_t sslen)
{
    const auto ss = static_cast<const sockaddr_storage *>(sas);
    if (ss->ss_family == AF_INET && sslen == sizeof(sockaddr_in)) {
        const auto s = static_cast<const sockaddr_in *>(sas);
        set(s->sin_addr);
    } else if (ss->ss_family == AF_INET6 && sslen == sizeof(sockaddr_in6)) {
        const auto s = static_cast<const sockaddr_in6 *>(sas);
        set(s->sin6_addr);
    } else return false;
    return true;
}

bool ip_address::from_string(std::string_view s)
{
    in6_addr addr;
    if (inet_pton(AF_INET6, s.data(), &addr) != 1) {
        // Automagically try to handle IPv4 addresses as IPV6-mapped.
        char buf[256] = "::ffff:";
        if (s.length() > sizeof buf - 8) return false;
        memcpy(buf + 7, s.data(), s.length());
        buf[s.length() + 7] = 0;
        if (inet_pton(AF_INET6, buf, &addr) != 1)
            return false;
    }
    set(addr);
    return true;
}

std::string ip_address::to_string() const
{
    char b[48]; b[0] = 0;
    inet_ntop(AF_INET6, (void *)&addr_, b, sizeof b);
    return std::string{ b };
}

static bool compare_u32_mask(uint32_t a, uint32_t b, unsigned mask)
{
    assert(mask <= 32);
    const auto m = mask < 32 ? std::numeric_limits<uint32_t>::max() >> mask : 0;
    return (a | m) == (b | m);
}<commit_msg>ip_address: Print v4 addresses without mapped prefix<commit_after>#include <nk/net/ip_address.hpp>
#include <nk/endian.hpp>
#include <algorithm>
#include <limits>
#include <cassert>
#ifndef _WIN32
#include <arpa/inet.h>
#endif

namespace nk {

ip_address::ip_address(any)
{
    set(in6addr_any);
}

bool ip_address::set(const void * const sas, size_t sslen)
{
    const auto ss = static_cast<const sockaddr_storage *>(sas);
    if (ss->ss_family == AF_INET && sslen == sizeof(sockaddr_in)) {
        const auto s = static_cast<const sockaddr_in *>(sas);
        set(s->sin_addr);
    } else if (ss->ss_family == AF_INET6 && sslen == sizeof(sockaddr_in6)) {
        const auto s = static_cast<const sockaddr_in6 *>(sas);
        set(s->sin6_addr);
    } else return false;
    return true;
}

bool ip_address::from_string(std::string_view s)
{
    in6_addr addr;
    if (inet_pton(AF_INET6, s.data(), &addr) != 1) {
        // Automagically try to handle IPv4 addresses as IPV6-mapped.
        char buf[256] = "::ffff:";
        if (s.length() > sizeof buf - 8) return false;
        memcpy(buf + 7, s.data(), s.length());
        buf[s.length() + 7] = 0;
        if (inet_pton(AF_INET6, buf, &addr) != 1)
            return false;
    }
    set(addr);
    return true;
}

std::string ip_address::to_string() const
{
    char b[48]; b[0] = 0;
    if (is_v4()) {
        // So that we don't print v4 with the ::ffff: mapped prefix
        in_addr a4;
        auto c = reinterpret_cast<const char *>(&addr_);
        memcpy(&a4, c + 12, 4);
        inet_ntop(AF_INET, &a4, b, sizeof b);
    } else inet_ntop(AF_INET6, (void *)&addr_, b, sizeof b);
    return std::string{ b };
}

static bool compare_u32_mask(uint32_t a, uint32_t b, unsigned mask)
{
    assert(mask <= 32);
    const auto m = mask < 32 ? std::numeric_limits<uint32_t>::max() >> mask : 0;
    return (a | m) == (b | m);
}
{'fraction_non_alphanumeric': 0.15517241379310345, 'fraction_numerical': 0.025, 'mean_word_length': 3.1146572104018913, 'pattern_counts': {'":': 2, '<': 28, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2042667', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1305, 'n_words': 328}