//! The phone numbers written in a message's words, so a tap on one opens the chat with that number, Cotton's or//! WhatsApp's as `chat.start` routes it (owner, FEEDBACK-2 A: "a number tapped anywhere").//!//! A number counts when Cotton can be sure of it, by [`e164_in`]: written with its country code, or without one and valid in the person's own country.//! Where it stands is said in UTF-16 `[start, end)`, as a search's marks are, so the phones mark the very characters.usesuper::shapes::NumberAt;use cotton_store::protocol::phone::{normalize_phone_in, region_of, written_international, Region};/// The fewest digits a number written in words has (shorter runs are prices, years, codes).pubconst DIGITS_MIN: usize = 8;/// The most an E.164 number has.pubconst DIGITS_MAX: usize = 15;/// The numbers in `text`, left to right, each where it stands and as E.164. `me` is the person's own number, whose/// country reads a number written without a code; without it only numbers written with a code count.pubfn numbers_in(text: &str, me: Option<&str>) -> Vec<NumberAt> {let region = me.and_then(|me| region_of(me.trim_start_matches('+')));let chars: Vec<(char, u32)> = {letmut at = 0u32; text.chars() .map(|c| {let here = at; at += c.len_utf16() as u32; (c, here) }) .collect() };let end_of = |i: usize| chars.get(i).map_or(text.encode_utf16().count() as u32, |(_, at)| *at);letmut out = Vec::new();letmut i = 0;while i < chars.len() {let (c, _) = chars[i];let starts = (c == '+' || c == '(' || c.is_ascii_digit()) && (i == 0 || !chars[i - 1].0.is_alphanumeric());if !starts { i += 1;continue; }// A run of digits, at most two separators in a row between them ("+1 (555) 010-7002"); it ends at its// last digit.let (mut j, mut seps, mut last_digit, mut digits) = (i, 0usize, None, 0usize);while j < chars.len() {let ch = chars[j].0;if ch.is_ascii_digit() { digits += 1; last_digit = Some(j); seps = 0; } elseif ch == '+' && (j == i || (j == i + 1 && chars[i].0 == '(')) { } elseif matches!(ch, ' ' | '-' | '.' | '(' | ')') && seps < 2 && (last_digit.is_some() || ch == '(') { seps += 1; } else {break; } j += 1; }letSome(last) = last_digit else { i += 1;continue; };let followed_by_word = chars.get(last + 1).is_some_and(|(n, _)| n.is_alphanumeric());if (DIGITS_MIN..=DIGITS_MAX).contains(&digits) && !followed_by_word {let said: String = chars[i..=last].iter().map(|(c, _)| *c).collect();ifletSome(number) = e164_in(&said, region) { out.push(NumberAt { at: [chars[i].1, end_of(last + 1)], number }); } } i = last + 1; } out}/// A number as written, in E.164, when Cotton can be sure of its country: one/// with a code (valid or not: the writer named the country, and a sign-in/// takes any such number), or one without in the person's own country, valid there.pubfn e164_in(raw: &str, region: Option<Region>) -> Option<String> {ifletSome(number) = written_international(raw) {returnSome(format!("+{number}")); }let number = normalize_phone_in(raw, region)?; region_of(&number)?;Some(format!("+{number}"))}#[cfg(test)]#[path = "numbers_tests.rs"]mod tests;