use alloc::format;
use alloc::string::String;
use alloc::string::ToString;
use crate::runtime::Value;
pub(crate) fn the_number_is_a_number(text: &str) -> bool {
let negative = text.starts_with('-');
let body = text.strip_prefix('-').unwrap_or(text);
if the_text_is_a_float(body) {
return false;
}
if negative {
return true;
}
!(body.len() > 1 && body.starts_with('0'))
}
pub(crate) fn the_text_of_the_number_that_fits(text: &str) -> String {
if the_text_is_a_hex(text) || the_text_is_a_float(text) {
return text.to_string();
}
match text.parse::<i64>() {
Ok(_) => text.to_string(),
Err(_) => {
let number = if text.starts_with('-') {
i64::MIN
} else {
i64::MAX
};
number.to_string()
}
}
}
pub(crate) fn the_text_is_a_hex(text: &str) -> bool {
text.strip_prefix("0x")
.or_else(|| text.strip_prefix("0X"))
.is_some()
}
pub(crate) fn the_text_is_a_float(text: &str) -> bool {
!the_text_is_a_hex(text) && (text.contains('.') || text.contains('e') || text.contains('E'))
}
pub(crate) fn the_count_of_the_loop(text: &str) -> i64 {
let text = text.trim();
if let Ok(number) = text.parse::<i64>() {
return number;
}
if the_text_is_a_hex(text) {
return the_whole_number_of_a_hex(text);
}
0
}
pub(crate) fn the_text_is_the_count_of_a_loop(text: &str) -> bool {
let text = text.trim();
text.parse::<i64>().is_ok() || the_text_is_a_hex(text)
}
pub(crate) fn the_whole_number_of_a_hex(text: &str) -> i64 {
let digits = text
.strip_prefix("0x")
.or_else(|| text.strip_prefix("0X"))
.unwrap_or(text);
match u64::from_str_radix(digits, 16) {
Ok(number) if number <= i64::MAX as u64 => number as i64,
_ => i64::MAX,
}
}
pub(crate) fn the_number_of_a_text(text: &str) -> Option<Value> {
let number = the_number_that_the_text_holds(text, true)?;
Some(number)
}
pub(crate) fn the_number_at_the_start_of_a_text(text: &str) -> Option<Value> {
the_number_that_the_text_holds(text, false)
}
pub(crate) fn the_whole_number_at_the_start_of_a_text(text: &str) -> Option<i64> {
let text = text.trim();
let (negative, body) = the_sign_of(text);
if body.is_empty() {
return None;
}
if let Some(hex) = the_hex_of(body) {
let digits: String = hex
.chars()
.take_while(|character| character.is_ascii_hexdigit())
.collect();
if digits.is_empty() {
return None;
}
let number = the_whole_number_of_a_hex(&digits);
return Some(if negative {
number.wrapping_neg()
} else {
number
});
}
let digits: String = body
.chars()
.take_while(|character| character.is_ascii_digit())
.collect();
if digits.is_empty() {
return None;
}
let number: i64 = digits.parse().unwrap_or(i64::MAX);
Some(if negative {
number.wrapping_neg()
} else {
number
})
}
fn the_number_that_the_text_holds(text: &str, whole: bool) -> Option<Value> {
let text = text.trim();
let (negative, body) = the_sign_of(text);
if body.is_empty() {
return None;
}
if let Some(hex) = the_hex_of(body) {
let digits: String = hex
.chars()
.take_while(|character| character.is_ascii_hexdigit())
.collect();
if digits.is_empty() || (whole && digits.len() != hex.len()) {
return None;
}
let number = the_whole_number_of_a_hex(&digits);
return Some(Value::Int(if negative {
number.wrapping_neg()
} else {
number
}));
}
let mut number = String::new();
let mut digits = 0usize;
let mut point = false;
for character in body.chars() {
match character {
'0'..='9' => {
number.push(character);
digits += 1;
}
'.' if !point => {
point = true;
number.push(character);
}
_ => break,
}
}
if digits == 0 {
return None;
}
let rest = body
.chars()
.skip(number.chars().count())
.collect::<String>();
let mut exponent = String::new();
if rest.starts_with('e') || rest.starts_with('E') {
let mut candidate = String::from("e");
for character in rest.chars().skip(1) {
match character {
'+' | '-' if candidate.len() == 1 => candidate.push(character),
'0'..='9' => candidate.push(character),
_ => break,
}
}
let has_digits = candidate
.chars()
.skip(1)
.any(|character| character.is_ascii_digit());
if has_digits && (!whole || point) {
exponent = candidate;
}
}
let rest = rest
.chars()
.skip(exponent.chars().count())
.collect::<String>();
if whole && !rest.is_empty() {
return None;
}
let text = format!("{}{}{}", if negative { "-" } else { "" }, number, exponent);
if number.contains('.') || !exponent.is_empty() {
return text.parse::<f64>().ok().map(Value::Float);
}
match text.parse::<i64>() {
Ok(number) => Some(Value::Int(number)),
Err(_) => Some(Value::Int(if negative { i64::MIN } else { i64::MAX })),
}
}
fn the_sign_of(text: &str) -> (bool, &str) {
match text.strip_prefix('-') {
Some(rest) => (true, rest),
None => (false, text.strip_prefix('+').unwrap_or(text)),
}
}
fn the_hex_of(body: &str) -> Option<&str> {
body.strip_prefix("0x").or_else(|| body.strip_prefix("0X"))
}