diff --git a/interfaces/ImsdMessages-Number.cppm b/interfaces/ImsdMessages-Number.cppm index 3662289..d8507d3 100644 --- a/interfaces/ImsdMessages-Number.cppm +++ b/interfaces/ImsdMessages-Number.cppm @@ -1,8 +1,156 @@ // SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® +/* +ImsdMessages:Number — the conversation key of an address. + +imsd hands the app whatever the network sent as the originating address: an +international number ("+31612345678"), occasionally a national one, a short +code ("1234") or an alphanumeric sender ("KPN"); the user types numbers in +every local spelling. One conversation per correspondent needs one key per +correspondent, so a number in international form is normalised to E.164 +and one in national form gets the given country code in front — nine +digits or more with a leading 0, or eight or more without one, as a plan +without a trunk prefix writes its numbers, the latter only while the +result is no longer than a number can be; short codes and alphanumeric +senders are their own keys. The key groups; a text goes to the address as dialled +(DialledNumber), nothing added — imsd sends a national number or a short +code with the type unknown and the network reads it by its own dialling +plan. A national number's leading 0 is taken for the trunk prefix and goes +when the country code is added — but under Italy's code and San Marino's, whose +numbers have none: there the 0 is the number's and stays. Known limits, of +the grouping: a number written with its country code and no '+' +("31612345678", the "1" before a North American number) is completed as a +national one; one of a plan whose trunk prefix is not a lone 0 (Hungary's +06, Russia's 8) is completed wrongly; and a national number shorter than +those lengths (Iceland's seven digits) stays its digits. +*/ export module ImsdMessages:Number; import std; -export namespace ImsdMessages { +namespace ImsdMessages { + // ITU-T E.164: the one- and two-digit country codes; every other code is + // three digits long. + constexpr std::array oneDigitCodes = { "1", "7" }; + constexpr std::array twoDigitCodes = { + "20", "27", "30", "31", "32", "33", "34", "36", "39", "40", "41", "43", "44", "45", "46", "47", "48", "49", + "51", "52", "53", "54", "55", "56", "57", "58", "60", "61", "62", "63", "64", "65", "66", "81", "82", "84", + "86", "90", "91", "92", "93", "94", "95", "98", + }; + + bool AllDigits(std::string_view s) { + return !s.empty() && std::ranges::all_of(s, [](char c) { return c >= '0' && c <= '9'; }); + } + + // Italy's plan and San Marino's have no trunk zero: a landline's zero is + // part of its number, abroad too, and a mobile number has none. + bool NoTrunkZero(std::string_view countryCode) { return countryCode == "39" || countryCode == "378"; } + + std::string_view Trimmed(std::string_view raw) { + while (!raw.empty() && std::isspace(static_cast(raw.front()))) raw.remove_prefix(1); + while (!raw.empty() && std::isspace(static_cast(raw.back()))) raw.remove_suffix(1); + return raw; + } + + // The digits of an address, a leading '+' kept: the separators dropped, + // and the trunk zero written in parentheses after a country code — + // "+31 (0)6 12345678", "+44 (0)20 …", "(+44) (0)20 …" — which is not + // dialled. "" for what is not a number: a letter, a '#', a second '+', + // a '+' alone — and that zero after a code without a trunk zero + // (NoTrunkZero): with the zero and without it the address is two + // different numbers, and neither is guessed. + std::string Digits(std::string_view trimmed) { + std::string body(trimmed); + // The international form is read past the separators: a country + // code in parentheses is one too. + const std::size_t lead = body.find_first_not_of(" -./()"); + const std::string_view head = lead == std::string::npos ? std::string_view{} : std::string_view(body).substr(lead); + if (head.starts_with('+') || head.starts_with("00")) { + if (const std::size_t at = body.find("(0)"); at != std::string::npos) { + std::string code; + for (const char c : std::string_view(body).substr(0, at)) { + if (c >= '0' && c <= '9') code.push_back(c); + } + if (head.starts_with("00")) code.erase(0, 2); + if (NoTrunkZero(code)) return ""; + body.erase(at, 3); + } + } + std::string digits; + for (char c : body) { + if (c == ' ' || c == '-' || c == '.' || c == '(' || c == ')' || c == '/') continue; + if (c == '+' && digits.empty()) { + digits.push_back(c); + continue; + } + if (c < '0' || c > '9') return ""; + digits.push_back(c); + } + return digits == "+" ? "" : digits; + } +} + +export namespace ImsdMessages { + // "+" and 7 to 15 digits. + bool IsE164(std::string_view s) { + return s.size() >= 8 && s.size() <= 16 && s[0] == '+' && AllDigits(s.substr(1)); + } + + // The country code of an E.164 number by the ITU length rule; "" when + // the string is not one. + std::string CountryCodeOf(std::string_view e164) { + if (!IsE164(e164)) return ""; + const std::string_view digits = e164.substr(1); + for (std::string_view code : oneDigitCodes) { + if (digits.starts_with(code)) return std::string(code); + } + for (std::string_view code : twoDigitCodes) { + if (digits.starts_with(code)) return std::string(code); + } + return std::string(digits.substr(0, 3)); + } + + // The address a text is sent to: as the user typed or a link gave it, + // its separators gone and nothing added. imsd sends a national number + // or a short code as dialled with the type unknown, and the network + // reads it by its own dialling plan; a country code the app completed + // for the key would be dialled for real. What is not a number stays as + // typed, for imsd to judge (it takes digits, '*' and '#'). + std::string DialledNumber(std::string_view raw) { + const std::string_view trimmed = Trimmed(raw); + const std::string digits = Digits(trimmed); + return digits.empty() ? std::string(trimmed) : digits; + } + + // The conversation key of an address the user typed or the network + // sent — for grouping, never for sending. `defaultCountryCode` ("31") + // completes a national number: nine digits or more whose first is + // the trunk zero, which goes — it stays under a code without one + // (NoTrunkZero), where it is the number's — or eight digits or more + // with no leading zero, as a plan without a trunk prefix writes its + // numbers (a short code is shorter). Empty when none is known, and the + // national number is then its own key. + std::string NormalizeNumber(std::string_view raw, std::string_view defaultCountryCode) { + const std::string_view trimmed = Trimmed(raw); + if (trimmed.empty()) return ""; + const std::string cleaned = Digits(trimmed); + // A letter, a '#', a second '+': not a number. The address is the key. + if (cleaned.empty()) return std::string(trimmed); + if (cleaned[0] == '+') return IsE164(cleaned) ? cleaned : std::string(trimmed); + if (cleaned.starts_with("00") && cleaned.size() >= 9 && cleaned.size() <= 17) return std::format("+{}", cleaned.substr(2)); + if (cleaned[0] == '0' && cleaned.size() >= 9 && !defaultCountryCode.empty()) return std::format("+{}{}", defaultCountryCode, NoTrunkZero(defaultCountryCode) ? cleaned : cleaned.substr(1)); + if (cleaned[0] != '0' && cleaned.size() >= 8 && !defaultCountryCode.empty()) { + // What the code would make longer than a number can be stays as it is. + if (const std::string completed = std::format("+{}{}", defaultCountryCode, cleaned); IsE164(completed)) return completed; + } + return cleaned; + } + + // The number as the UI shows it: "+31 612345678" for E.164, the key + // itself for everything else. + std::string DisplayNumber(std::string_view key) { + const std::string code = CountryCodeOf(key); + if (code.empty()) return std::string(key); + return std::format("+{} {}", code, key.substr(1 + code.size())); + } } diff --git a/tests/Number/main.cpp b/tests/Number/main.cpp index e91eb9e..c323fee 100644 --- a/tests/Number/main.cpp +++ b/tests/Number/main.cpp @@ -2,11 +2,12 @@ // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Number normalisation: the raw address a user types or the network sends -// becomes the conversation key (E.164 for numbers; short codes and -// alphanumeric senders verbatim). At this point only the harness exists: -// the test checks that the core links and the version constant is set. +// becomes the conversation key — E.164 for numbers, short codes and +// alphanumeric senders verbatim — the display form of a key, and the +// dialled form of an address (its separators gone, nothing added). import std; import ImsdMessages; +using namespace ImsdMessages; namespace { @@ -17,9 +18,95 @@ void Check(bool ok, std::string_view what) { if (!ok) ++failures; } +void CheckEqual(std::string_view actual, std::string_view expected, std::string_view what) { + const bool ok = actual == expected; + if (ok) std::println("PASS {}", what); + else std::println("FAIL {} — expected \"{}\", got \"{}\"", what, expected, actual); + if (!ok) ++failures; +} + } int main() { - Check(!ImsdMessages::version.empty(), "the version constant is set"); + Check(!version.empty(), "the version constant is set"); + + struct Case { + std::string_view raw; + std::string_view countryCode; + std::string_view key; + }; + constexpr std::array cases = { { + { "+31612345678", "31", "+31612345678" }, + { "0612345678", "31", "+31612345678" }, + { "06 1234 5678", "31", "+31612345678" }, + { "06-1234-5678", "31", "+31612345678" }, + { "0031612345678", "", "+31612345678" }, + { " +31612345678 ", "", "+31612345678" }, + { "+1 (555) 123-4567", "31", "+15551234567" }, + { "0612345678", "", "0612345678" }, + { "0612345678", "39", "+390612345678" }, + { "0549123456", "378", "+3780549123456" }, + { "021234567", "32", "+3221234567" }, + { "02123456", "32", "02123456" }, + { "1234", "31", "1234" }, + { "KPN", "31", "KPN" }, + { "Odido Info", "31", "Odido Info" }, + { "", "31", "" }, + { "+", "31", "+" }, + { "+3161234567890123456", "31", "+3161234567890123456" }, + { "+31612345678#", "31", "+31612345678#" }, + { "612345678", "34", "+34612345678" }, + { "555 123 4567", "1", "+15551234567" }, + { "41234567", "47", "+4741234567" }, + { "4123456", "47", "4123456" }, + { "612345678", "", "612345678" }, + { "123456789012345", "31", "123456789012345" }, + } }; + for (const Case& c : cases) CheckEqual(NormalizeNumber(c.raw, c.countryCode), c.key, std::format("NormalizeNumber(\"{}\", \"{}\")", c.raw, c.countryCode)); + + CheckEqual(CountryCodeOf("+31653131313"), "31", "country code of the KPN service centre"); + CheckEqual(CountryCodeOf("+15551234567"), "1", "one-digit code 1"); + CheckEqual(CountryCodeOf("+79161234567"), "7", "one-digit code 7"); + CheckEqual(CountryCodeOf("+212612345678"), "212", "three-digit code 212"); + CheckEqual(CountryCodeOf("+4915112345678"), "49", "two-digit code 49"); + CheckEqual(CountryCodeOf("+3912345678"), "39", "two-digit code 39"); + CheckEqual(CountryCodeOf("KPN"), "", "no code for an alphanumeric sender"); + CheckEqual(CountryCodeOf("0612345678"), "", "no code for a national number"); + + CheckEqual(NormalizeNumber("+31 (0)6 1234 5678", "31"), "+31612345678", "the parenthesised trunk zero of an international number is dropped"); + CheckEqual(NormalizeNumber("+44 (0)20 7946 0018", "31"), "+442079460018", "the same under another country code"); + CheckEqual(NormalizeNumber("0031 (0)6 1234 5678", "31"), "+31612345678", "and after the 00 prefix"); + CheckEqual(NormalizeNumber("(06) 1234 5678", "31"), "+31612345678", "parentheses around a national number stay separators"); + CheckEqual(NormalizeNumber("(+44) (0)20 7946 0018", "31"), "+442079460018", "a country code in parentheses is the international form too"); + CheckEqual(NormalizeNumber("(+31) (0)6 1234 5678", ""), "+31612345678", "the same without a default code"); + CheckEqual(NormalizeNumber("(+378) (0)549 123456", "31"), "(+378) (0)549 123456", "after San Marino's code, as after Italy's, no zero is a trunk prefix: not a number, the address is the key"); + + // The dialled form: the separators gone, nothing added — the network's + // own dialling plan reads a national number or a short code. + struct DialCase { + std::string_view raw; + std::string_view dialled; + }; + constexpr std::array dials = { { + { "+31 6 1234 5678", "+31612345678" }, + { "(+44) (0)20 7946 0018", "+442079460018" }, + { "06 1234 5678", "0612345678" }, + { "(06) 1234-5678", "0612345678" }, + { "+31 (0)6 1234 5678", "+31612345678" }, + { "+39 (0)6 1234 5678", "+39 (0)6 1234 5678" }, + { "0039 (0)347 1234567", "0039 (0)347 1234567" }, + { "0031 6 1234 5678", "0031612345678" }, + { "1234", "1234" }, + { " KPN ", "KPN" }, + { "+", "+" }, + { "", "" }, + } }; + for (const DialCase& c : dials) CheckEqual(DialledNumber(c.raw), c.dialled, std::format("DialledNumber(\"{}\")", c.raw)); + + CheckEqual(DisplayNumber("+31612345678"), "+31 612345678", "display of an E.164 number"); + CheckEqual(DisplayNumber("+15551234567"), "+1 5551234567", "display with a one-digit code"); + CheckEqual(DisplayNumber("1234"), "1234", "display of a short code"); + CheckEqual(DisplayNumber("KPN"), "KPN", "display of an alphanumeric sender"); + return failures == 0 ? 0 : 1; }