<commit_before>use std::fmt;
use std::hash;

use num::{FromPrimitive};



/// please don't access VA.0 directly.
/// its provided so you can construct VA like:
/// ```
/// use lancelot::arch::VA;
/// let _ = VA(0x0);
/// ```
#[derive(Copy, Clone)]
pub struct VA(pub u64);

impl VA {
    /// Compute the VA given a delta RVA, like `self:va + other:rva`.
    /// Returns None on over/underflow.
    pub fn va(&self, other: RVA) -> Option<VA> {
        if other.0 < 0 {
            Some(VA(match self.0.checked_sub((-other.0) as u64) {
                None => return None,
                Some(v) => v,
            }))
        } else {
            Some(VA(match self.0.checked_add(other.0 as u64) {
                None => return None,
                Some(v) => v,
            }))
        }
    }

    /// Compute the delta RVA from `self:va - other:va`.
    /// Returns None on over/underflow.
    pub fn rva(&self, other: VA) -> Option<RVA> {
        if other.0 > self.0 {
            let v = match other.0.checked_sub(self.0) {
                None => return None,
                Some(v) => v,
            };
            if v > std::i64::MAX as u64{
                return None;
            }
            Some(RVA(-(v as i64)))
        } else {
            let v = match self.0.checked_sub(other.0) {
                None => return None,
                Some(v) => v
            };
            if v > std::i64::MAX as u64 {
                return None;
            }
            Some(RVA(v as i64))
        }
    }
}

impl fmt::Display for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::Debug for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::LowerHex for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}", self.0)
    }
}

impl fmt::UpperHex for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:X}", self.0)
    }
}

impl hash::Hash for VA {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl PartialEq for VA {
    fn eq(&self, other: &VA) -> bool {
        self.0 == other.0
    }
}

impl Eq for VA {}

impl std::convert::From<usize> for VA {
    fn from(v: usize) -> VA {
        // this should only fail when we get to 128-bit architectures
        VA(v as u64)
    }
}

impl std::convert::From<u32> for VA {
    fn from(v: u32) -> VA {
        VA(v as u64)
    }
}

impl std::convert::From<u64> for VA {
    fn from(v: u64) -> VA {
        VA(v)
    }
}

impl std::convert::Into<u64> for VA {
    fn into(self) -> u64 {
        self.0
    }
}




/// please don't access RVA.0 directly.
/// /// its provided so you can construct RVA like:
/// ```
/// use lancelot::arch::RVA;
/// let _ = RVA(0x0);
/// ```
#[derive(Copy, Clone)]
pub struct RVA(pub i64);

impl fmt::Display for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::Debug for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::LowerHex for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}", self.0)
    }
}

impl fmt::UpperHex for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:X}", self.0)
    }
}

impl hash::Hash for RVA {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl PartialEq for RVA {
    fn eq(&self, other: &RVA) -> bool {
        self.0 == other.0
    }
}

impl PartialOrd for RVA {
    fn partial_cmp(&self, other: &RVA) -> Option<std::cmp::Ordering> {
        Some(self.0.cmp(&other.0))
    }
}

impl Ord for RVA {
    fn cmp(&self, other: &RVA) -> std::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

impl Eq for RVA {}

impl std::convert::From<usize> for RVA {
    /// may panic on overflow
    fn from(v: usize) -> RVA {
        RVA(i64::from_usize(v).expect("usize too large for RVA"))
    }
}

impl std::convert::Into<usize> for RVA {
    /// may panic on over/underflow
    fn into(self) -> usize {
        if self.0 < 0 {
            panic!("usize underflow")
        }
        if self.0 as u64 > std::usize::MAX as u64 {
            panic!("usize overflow");
        }
        self.0 as usize
    }
}

impl std::convert::Into<u64> for RVA {
    fn into(self) -> u64 {
        self.0 as u64
    }
}

impl std::convert::Into<i64> for RVA {
    fn into(self) -> i64 {
        self.0
    }
}

impl std::convert::From<i32> for RVA {
    fn from(v: i32) -> RVA {
        RVA(v as i64)
    }<commit_msg>arch: rva: add From<u32><commit_after>use std::fmt;
use std::hash;

use num::{FromPrimitive};



/// please don't access VA.0 directly.
/// its provided so you can construct VA like:
/// ```
/// use lancelot::arch::VA;
/// let _ = VA(0x0);
/// ```
#[derive(Copy, Clone)]
pub struct VA(pub u64);

impl VA {
    /// Compute the VA given a delta RVA, like `self:va + other:rva`.
    /// Returns None on over/underflow.
    pub fn va(&self, other: RVA) -> Option<VA> {
        if other.0 < 0 {
            Some(VA(match self.0.checked_sub((-other.0) as u64) {
                None => return None,
                Some(v) => v,
            }))
        } else {
            Some(VA(match self.0.checked_add(other.0 as u64) {
                None => return None,
                Some(v) => v,
            }))
        }
    }

    /// Compute the delta RVA from `self:va - other:va`.
    /// Returns None on over/underflow.
    pub fn rva(&self, other: VA) -> Option<RVA> {
        if other.0 > self.0 {
            let v = match other.0.checked_sub(self.0) {
                None => return None,
                Some(v) => v,
            };
            if v > std::i64::MAX as u64{
                return None;
            }
            Some(RVA(-(v as i64)))
        } else {
            let v = match self.0.checked_sub(other.0) {
                None => return None,
                Some(v) => v
            };
            if v > std::i64::MAX as u64 {
                return None;
            }
            Some(RVA(v as i64))
        }
    }
}

impl fmt::Display for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::Debug for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::LowerHex for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}", self.0)
    }
}

impl fmt::UpperHex for VA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:X}", self.0)
    }
}

impl hash::Hash for VA {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl PartialEq for VA {
    fn eq(&self, other: &VA) -> bool {
        self.0 == other.0
    }
}

impl Eq for VA {}

impl std::convert::From<usize> for VA {
    fn from(v: usize) -> VA {
        // this should only fail when we get to 128-bit architectures
        VA(v as u64)
    }
}

impl std::convert::From<u32> for VA {
    fn from(v: u32) -> VA {
        VA(v as u64)
    }
}

impl std::convert::From<u64> for VA {
    fn from(v: u64) -> VA {
        VA(v)
    }
}

impl std::convert::Into<u64> for VA {
    fn into(self) -> u64 {
        self.0
    }
}




/// please don't access RVA.0 directly.
/// /// its provided so you can construct RVA like:
/// ```
/// use lancelot::arch::RVA;
/// let _ = RVA(0x0);
/// ```
#[derive(Copy, Clone)]
pub struct RVA(pub i64);

impl fmt::Display for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::Debug for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl fmt::LowerHex for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}", self.0)
    }
}

impl fmt::UpperHex for RVA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:X}", self.0)
    }
}

impl hash::Hash for RVA {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl PartialEq for RVA {
    fn eq(&self, other: &RVA) -> bool {
        self.0 == other.0
    }
}

impl PartialOrd for RVA {
    fn partial_cmp(&self, other: &RVA) -> Option<std::cmp::Ordering> {
        Some(self.0.cmp(&other.0))
    }
}

impl Ord for RVA {
    fn cmp(&self, other: &RVA) -> std::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

impl Eq for RVA {}

impl std::convert::From<usize> for RVA {
    /// may panic on overflow
    fn from(v: usize) -> RVA {
        RVA(i64::from_usize(v).expect("usize too large for RVA"))
    }
}

impl std::convert::Into<usize> for RVA {
    /// may panic on over/underflow
    fn into(self) -> usize {
        if self.0 < 0 {
            panic!("usize underflow")
        }
        if self.0 as u64 > std::usize::MAX as u64 {
            panic!("usize overflow");
        }
        self.0 as usize
    }
}

impl std::convert::Into<u64> for RVA {
    fn into(self) -> u64 {
        self.0 as u64
    }
}

impl std::convert::Into<i64> for RVA {
    fn into(self) -> i64 {
        self.0
    }
}

impl std::convert::From<u32> for RVA {
    fn from(v: u32) -> RVA {
        RVA(v as i64)
    }
}

impl std::convert::From<i32> for RVA {
    fn from(v: i32) -> RVA {
        RVA(v as i64)
    }
{'fraction_non_alphanumeric': 0.17525665200083804, 'fraction_numerical': 0.02262727844123193, 'mean_word_length': 1.8262285375962108, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 104, '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': '7343350', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3465, 'n_words': 922}