point_utf16.rs

  1use std::{
  2    cmp::Ordering,
  3    ops::{Add, AddAssign, Sub},
  4};
  5
  6#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash)]
  7pub struct PointUtf16 {
  8    pub row: u32,
  9    pub column: u32,
 10}
 11
 12impl PointUtf16 {
 13    pub const MAX: Self = Self {
 14        row: u32::MAX,
 15        column: u32::MAX,
 16    };
 17
 18    pub fn new(row: u32, column: u32) -> Self {
 19        PointUtf16 { row, column }
 20    }
 21
 22    pub fn zero() -> Self {
 23        PointUtf16::new(0, 0)
 24    }
 25
 26    pub fn is_zero(&self) -> bool {
 27        self.row == 0 && self.column == 0
 28    }
 29
 30    pub fn saturating_sub(self, other: Self) -> Self {
 31        if self < other {
 32            Self::zero()
 33        } else {
 34            self - other
 35        }
 36    }
 37}
 38
 39impl<'a> Add<&'a Self> for PointUtf16 {
 40    type Output = PointUtf16;
 41
 42    fn add(self, other: &'a Self) -> Self::Output {
 43        self + *other
 44    }
 45}
 46
 47impl Add for PointUtf16 {
 48    type Output = PointUtf16;
 49
 50    fn add(self, other: Self) -> Self::Output {
 51        if other.row == 0 {
 52            PointUtf16::new(self.row, self.column + other.column)
 53        } else {
 54            PointUtf16::new(self.row + other.row, other.column)
 55        }
 56    }
 57}
 58
 59impl<'a> Sub<&'a Self> for PointUtf16 {
 60    type Output = PointUtf16;
 61
 62    fn sub(self, other: &'a Self) -> Self::Output {
 63        self - *other
 64    }
 65}
 66
 67impl Sub for PointUtf16 {
 68    type Output = PointUtf16;
 69
 70    fn sub(self, other: Self) -> Self::Output {
 71        debug_assert!(other <= self);
 72
 73        if self.row == other.row {
 74            PointUtf16::new(0, self.column - other.column)
 75        } else {
 76            PointUtf16::new(self.row - other.row, self.column)
 77        }
 78    }
 79}
 80
 81impl<'a> AddAssign<&'a Self> for PointUtf16 {
 82    fn add_assign(&mut self, other: &'a Self) {
 83        *self += *other;
 84    }
 85}
 86
 87impl AddAssign<Self> for PointUtf16 {
 88    fn add_assign(&mut self, other: Self) {
 89        if other.row == 0 {
 90            self.column += other.column;
 91        } else {
 92            self.row += other.row;
 93            self.column = other.column;
 94        }
 95    }
 96}
 97
 98impl PartialOrd for PointUtf16 {
 99    fn partial_cmp(&self, other: &PointUtf16) -> Option<Ordering> {
100        Some(self.cmp(other))
101    }
102}
103
104impl Ord for PointUtf16 {
105    #[cfg(target_pointer_width = "64")]
106    fn cmp(&self, other: &PointUtf16) -> Ordering {
107        let a = ((self.row as usize) << 32) | self.column as usize;
108        let b = ((other.row as usize) << 32) | other.column as usize;
109        a.cmp(&b)
110    }
111
112    #[cfg(target_pointer_width = "32")]
113    fn cmp(&self, other: &PointUtf16) -> Ordering {
114        match self.row.cmp(&other.row) {
115            Ordering::Equal => self.column.cmp(&other.column),
116            comparison @ _ => comparison,
117        }
118    }
119}