Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr SMS Gateway9// File : ios/MacLustrSMS/Core/PhoneNumber.swift10// Description : Mise en forme et validation locale des numéros (Amérique11// du Nord par défaut, E.164 sinon). Le serveur reste12// l'arbitre final.13// Module : ios14// Repository : https://git.spboucher.ai/maclustr-sms15// Project Website : https://sms.spboucher.ai16// Created : 2026-11-3017// Last Modified : 2026-11-3018//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: admin@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation9293enum PhoneNumber {94 /// Normalise en E.164 : 10 chiffres → +1…, 11 chiffres commençant par 1 → +…, « + » suivi de 8 à 15 chiffres conservé. nil si invalide.95 static func e164(_ input: String) -> String? {96 let trimmed = input.trimmingCharacters(in: .whitespaces)97 let digits = trimmed.filter(\.isASCIIDigit)98 if trimmed.hasPrefix("+") {99 guard (8...15).contains(digits.count), digits.first != "0" else { return nil }100 if digits.hasPrefix("1") { return isNANP(String(digits.dropFirst())) && digits.count == 11 ? "+" + digits : nil }101 return "+" + digits102 }103 if digits.count == 10, isNANP(digits) { return "+1" + digits }104 if digits.count == 11, digits.hasPrefix("1"), isNANP(String(digits.dropFirst())) { return "+" + digits }105 return nil106 }107 /// Plan de numérotation nord-américain : indicatif et central ne commencent ni par 0 ni par 1.108 static func isNANP(_ ten: String) -> Bool {109 guard ten.count == 10 else { return false }110 let a = Array(ten)111 return !"01".contains(a[0]) && !"01".contains(a[3])112 }113 /// Affichage lisible : +1 418 555-0123 pour l'Amérique du Nord, sinon la valeur telle quelle.114 static func pretty(_ value: String?) -> String {115 guard let value, !value.isEmpty else { return "—" }116 let d = value.filter(\.isASCIIDigit)117 if value.hasPrefix("+1"), d.count == 11 {118 let a = Array(d)119 return "+1 \(String(a[1...3])) \(String(a[4...6]))-\(String(a[7...10]))"120 }121 return value122 }123 /// Mise en forme pendant la saisie (sans jamais bloquer la frappe).124 static func formatWhileTyping(_ input: String) -> String {125 if input.hasPrefix("+") { return "+" + input.dropFirst().filter { $0.isASCIIDigit || $0 == " " } }126 let d = Array(input.filter(\.isASCIIDigit).prefix(11))127 var digits = d128 var prefix = ""129 if digits.count == 11, digits.first == "1" { prefix = "1 "; digits.removeFirst() }130 guard digits.count <= 10 else { return String(d) }131 switch digits.count {132 case 0...3: return prefix + String(digits)133 case 4...6: return prefix + "\(String(digits[0..<3])) \(String(digits[3...]))"134 default: return prefix + "\(String(digits[0..<3])) \(String(digits[3..<6]))-\(String(digits[6...]))"135 }136 }137}138private extension Character { var isASCIIDigit: Bool { isASCII && isNumber } }139