#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
use std::cmp::Ordering;
#[derive(Debug, Clone, Copy)]
pub(super) struct Rational {
numerator: i128,
denominator: u128,
}
impl Rational {
pub(super) fn parse(text: &str) -> Option<Self> {
let (n, d) = text.split_once('/')?;
let numerator = super::canonical_int(n)?;
let denominator = super::canonical_int(d)?;
if denominator <= 0 {
return None;
}
Some(Self {
numerator,
denominator: denominator as u128,
})
}
pub(super) fn compare(self, other: Self) -> (Ordering, u64) {
if self.numerator.is_negative() != other.numerator.is_negative() {
return (self.numerator.cmp(&other.numerator), 1);
}
let (mut a, mut b, mut c, mut d) = (
self.numerator.unsigned_abs(),
self.denominator,
other.numerator.unsigned_abs(),
other.denominator,
);
let mut reverse = self.numerator.is_negative();
let mut steps = 0;
loop {
steps += 1;
let order = (a / b).cmp(&(c / d));
if order != Ordering::Equal {
return (if reverse { order.reverse() } else { order }, steps);
}
let (ar, cr) = (a % b, c % d);
if ar == 0 || cr == 0 {
let order = ar.cmp(&cr);
return (if reverse { order.reverse() } else { order }, steps);
}
(a, b, c, d) = (b, ar, d, cr);
reverse = !reverse;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exhaustive_small_fractions_match_cross_product_oracle() {
for a in -12i128..=12 {
for b in 1u128..=12 {
for c in -12i128..=12 {
for d in 1u128..=12 {
let (x, steps) = Rational {
numerator: a,
denominator: b,
}
.compare(Rational {
numerator: c,
denominator: d,
});
assert_eq!(x, (a * d as i128).cmp(&(c * b as i128)));
assert!((1..=256).contains(&steps));
}
}
}
}
}
#[test]
fn extremes_never_multiply_or_negate_min() {
let min = Rational {
numerator: i128::MIN,
denominator: 1,
};
let max = Rational {
numerator: i128::MAX,
denominator: 1,
};
assert_eq!(min.compare(max).0, Ordering::Less);
assert_eq!(min.compare(min).0, Ordering::Equal);
let x = Rational {
numerator: i128::MAX,
denominator: i128::MAX as u128 - 1,
};
let y = Rational {
numerator: i128::MAX - 1,
denominator: i128::MAX as u128 - 2,
};
assert_eq!(x.compare(y).0, Ordering::Less);
}
#[test]
fn parsing_is_strict() {
for bad in [
"1",
"1/0",
"1/-2",
"01/2",
"-0/2",
"1/+2",
"1/02",
"1/2/3",
" 1/2",
"1/2.0",
"1e2/3",
"1/170141183460469231731687303715884105728",
] {
assert!(Rational::parse(bad).is_none(), "{bad}");
}
for good in [
"0/1",
"-1/2",
"2/4",
"-170141183460469231731687303715884105728/1",
] {
assert!(Rational::parse(good).is_some());
}
}
#[test]
fn equivalent_forms_compare_equal() {
assert_eq!(
Rational::parse("2/4")
.unwrap()
.compare(Rational::parse("1/2").unwrap())
.0,
Ordering::Equal
);
assert_eq!(
Rational::parse("0/17")
.unwrap()
.compare(Rational::parse("0/1").unwrap())
.0,
Ordering::Equal
);
}
}