<commit_before>// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use prelude::*;
use libc::{size_t, ssize_t, c_int, c_void, c_uint};
use rt::uv::uvll;
use rt::uv::uvll::*;
use rt::uv::{AllocCallback, ConnectionCallback, ReadCallback, UdpReceiveCallback, UdpSendCallback};
use rt::uv::{Loop, Watcher, Request, UvError, Buf, NativeHandle, NullCallback,
             status_to_maybe_uv_error};
use rt::io::net::ip::{IpAddr, Ipv4, Ipv6};
use rt::uv::last_uv_error;
use vec;
use str;
use from_str::{FromStr};
use num;

enum UvIpAddr {
    UvIpv4(*sockaddr_in),
    UvIpv6(*sockaddr_in6),
}

fn sockaddr_to_UvIpAddr(addr: *uvll::sockaddr) -> UvIpAddr {
    unsafe {
        assert!((is_ip4_addr(addr) || is_ip6_addr(addr)));
        assert!(!(is_ip4_addr(addr) && is_ip6_addr(addr)));
        match addr {
            _ if is_ip4_addr(addr) => UvIpv4(as_sockaddr_in(addr)),
            _ if is_ip6_addr(addr) => UvIpv6(as_sockaddr_in6(addr)),
            _ => fail!(),
        }
    }
}

fn ip_as_uv_ip<T>(addr: IpAddr, f: &fn(UvIpAddr) -> T) -> T {
    let malloc = match addr {
        Ipv4(*) => malloc_ip4_addr,
        Ipv6(*) => malloc_ip6_addr,
    };
    let wrap = match addr {
        Ipv4(*) => UvIpv4,
        Ipv6(*) => UvIpv6,
    };
    let ip_str = match addr {
        Ipv4(x1, x2, x3, x4, _) =>
            fmt!("%u.%u.%u.%u", x1 as uint, x2 as uint, x3 as uint, x4 as uint),
        Ipv6(x1, x2, x3, x4, x5, x6, x7, x8, _) =>
            fmt!("%x:%x:%x:%x:%x:%x:%x:%x",
                  x1 as uint, x2 as uint, x3 as uint, x4 as uint,
                  x5 as uint, x6 as uint, x7 as uint, x8 as uint),
    };
    let port = match addr {
        Ipv4(_, _, _, _, p) | Ipv6(_, _, _, _, _, _, _, _, p) => p as int
    };
    let free = match addr {
        Ipv4(*) => free_ip4_addr,
        Ipv6(*) => free_ip6_addr,
    };

    let addr = unsafe { malloc(ip_str, port) };
    do (|| {
        f(wrap(addr))
    }).finally {
        unsafe { free(addr) };
    }
}

fn uv_ip_as_ip<T>(addr: UvIpAddr, f: &fn(IpAddr) -> T) -> T {
    let ip_size = match addr {
        UvIpv4(*) => 4/*groups of*/ * 3/*digits separated by*/ + 3/*periods*/,
        UvIpv6(*) => 8/*groups of*/ * 4/*hex digits separated by*/ + 7 /*colons*/,
    };
    let ip_name = {
        let buf = vec::from_elem(ip_size + 1 /*null terminated*/, 0u8);
        unsafe {
            match addr {
                UvIpv4(addr) => uvll::ip4_name(addr, vec::raw::to_ptr(buf), ip_size as u64),
                UvIpv6(addr) => uvll::ip6_name(addr, vec::raw::to_ptr(buf), ip_size as u64),
            }
        };
        buf<commit_msg>std::rt: size_t, not u64<commit_after>// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use prelude::*;
use libc::{size_t, ssize_t, c_int, c_void, c_uint};
use rt::uv::uvll;
use rt::uv::uvll::*;
use rt::uv::{AllocCallback, ConnectionCallback, ReadCallback, UdpReceiveCallback, UdpSendCallback};
use rt::uv::{Loop, Watcher, Request, UvError, Buf, NativeHandle, NullCallback,
             status_to_maybe_uv_error};
use rt::io::net::ip::{IpAddr, Ipv4, Ipv6};
use rt::uv::last_uv_error;
use vec;
use str;
use from_str::{FromStr};
use num;

enum UvIpAddr {
    UvIpv4(*sockaddr_in),
    UvIpv6(*sockaddr_in6),
}

fn sockaddr_to_UvIpAddr(addr: *uvll::sockaddr) -> UvIpAddr {
    unsafe {
        assert!((is_ip4_addr(addr) || is_ip6_addr(addr)));
        assert!(!(is_ip4_addr(addr) && is_ip6_addr(addr)));
        match addr {
            _ if is_ip4_addr(addr) => UvIpv4(as_sockaddr_in(addr)),
            _ if is_ip6_addr(addr) => UvIpv6(as_sockaddr_in6(addr)),
            _ => fail!(),
        }
    }
}

fn ip_as_uv_ip<T>(addr: IpAddr, f: &fn(UvIpAddr) -> T) -> T {
    let malloc = match addr {
        Ipv4(*) => malloc_ip4_addr,
        Ipv6(*) => malloc_ip6_addr,
    };
    let wrap = match addr {
        Ipv4(*) => UvIpv4,
        Ipv6(*) => UvIpv6,
    };
    let ip_str = match addr {
        Ipv4(x1, x2, x3, x4, _) =>
            fmt!("%u.%u.%u.%u", x1 as uint, x2 as uint, x3 as uint, x4 as uint),
        Ipv6(x1, x2, x3, x4, x5, x6, x7, x8, _) =>
            fmt!("%x:%x:%x:%x:%x:%x:%x:%x",
                  x1 as uint, x2 as uint, x3 as uint, x4 as uint,
                  x5 as uint, x6 as uint, x7 as uint, x8 as uint),
    };
    let port = match addr {
        Ipv4(_, _, _, _, p) | Ipv6(_, _, _, _, _, _, _, _, p) => p as int
    };
    let free = match addr {
        Ipv4(*) => free_ip4_addr,
        Ipv6(*) => free_ip6_addr,
    };

    let addr = unsafe { malloc(ip_str, port) };
    do (|| {
        f(wrap(addr))
    }).finally {
        unsafe { free(addr) };
    }
}

fn uv_ip_as_ip<T>(addr: UvIpAddr, f: &fn(IpAddr) -> T) -> T {
    let ip_size = match addr {
        UvIpv4(*) => 4/*groups of*/ * 3/*digits separated by*/ + 3/*periods*/,
        UvIpv6(*) => 8/*groups of*/ * 4/*hex digits separated by*/ + 7 /*colons*/,
    };
    let ip_name = {
        let buf = vec::from_elem(ip_size + 1 /*null terminated*/, 0u8);
        unsafe {
            match addr {
                UvIpv4(addr) => uvll::ip4_name(addr, vec::raw::to_ptr(buf), ip_size as size_t),
                UvIpv6(addr) => uvll::ip6_name(addr, vec::raw::to_ptr(buf), ip_size as size_t),
            }
        };
        buf
{'fraction_non_alphanumeric': 0.16625432953983177, 'fraction_numerical': 0.026389576117433613, 'mean_word_length': 2.509259259259259, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4518485', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2312, 'n_words': 554}