|
Phone Generator 2.1.1
Phone numbers valid in each country's numbering plan for C++23
|
This guide covers every feature of the phone-generator library. For a quick overview, see the README. For the data and how it was built, see Data and Method. For the full API reference, run doxygen Doxyfile from the repository root and open doc/api/html/index.html.
Copy dasmig/phonegen.hpp, dasmig/core.hpp and the dasmig/core/ directory into your include path, keeping them together. Supply resources/ next to the program, or define DASMIG_PHONE_EMBED to compile the lite tier in.
instance() loads the lite tier on first use (or full if there is no lite), from the compiled-in copy or from the first of resources/, ../resources, phone-generator/resources and ../phone-generator/resources that has it. lite holds the 195 sovereign states, full the territories as well (the ones libphonenumber has a plan for).
Each load replaces the data and every layer loaded before.
weighted = false draws countries and ranges uniformly.std::invalid_argument.+54 9 11 5811-5808 internationally and 11 15-5811-5808 nationally.safe = true makes a number that is unlikely to reach a real person:
reserved is true. Confirmed ranges are in the US and Canada (555-0100 to 0199 under every area code where those numbers are valid), GB (mobile 07700 900000 to 900999), Germany (Bundesnetzagentur drama numbers) and Australia ((02) 5550 xxxx). has_reserved(country, type) tells which countries have a range.reserved is false and the number is drawn outside the country's allocated ranges: it will usually fail validation and is unlikely to be anyone's, but nothing guarantees it. There is no error, so a batch over random countries does not fail.get_phone(request, seed) gives the same result for the same request, seed and data, on every platform, without advancing the instance engine.get_phone(request) draws a fresh seed from the instance engine, initially seeded from system entropy. There are no generator-level seed() or unseed() controls; for deterministic sequences, keep a dasmig::core::rng in the caller and pass its next value as the seed.p.seed() is the seed a result was generated from: it replays it with the same request and data."country", "phone-type", "phone-range", "phone-digits"), so adding a field later does not change the others.The shipped data is a base layer in four tables (see Data and Method): countries, ranges, formats and reserved. A layer adds, changes or removes rows of one table without editing the shipped files.
dasmig::core::tsv_error with the file, line and column, and the data stays as it was.layers(table) lists what is loaded for a table: source, mode, rows added, replaced and removed, and warnings such as unknown columns.load(...) replaces the base and drops every layer.Every result records the seed and the data it came from:
to_json() gives the result as JSON, the form the WebAssembly layer and the game consume. Seeds are strings because JavaScript numbers cannot hold every 64-bit value. to_json(writer) writes into an existing dasmig::core::json_writer, for composing larger documents.
Define DASMIG_PHONE_EMBED before including the header to compile the lite tier into the program. load(dataset::lite) and instance() then use it and need no files. The compiled-in data has the same data_hash() as the files, so seeds replay the same either way.
dasmig/phonegen_data.hpp is generated from resources/lite by make embed; CI checks it is up to date. It is about 670 KB.
Each phg instance is independent. Concurrent calls to get_phone(request, seed) on the same instance are safe; calls that use the instance engine (get_phone(request)), loading and extending require external synchronisation (e.g. a std::mutex).
get_phone_batch returns a std::vector, and for_each_phone streams the items to a sink in index order without holding the batch.
i is exactly get_phone(request, core::item_seed(batch_seed, first + i)). A range can be generated directly (first = 1000 starts at item 1000), two batches that follow each other equal one, and the output is the same for any threads (0 means the hardware count). Without a batch_seed one is drawn like the single getter draws its own.core::batch_options has count, first, threads and unique. The request describes one item; the options describe the batch..unique = core::unique_rule::by<phone>(key) regenerates an item whose key already appeared in the batch, with a deterministic retry seed, and throws std::runtime_error naming the item when the attempts run out. A small country has far fewer valid numbers than a million, and the reserved ranges hold only 100 to 1,000 each.core::jsonl_writer writes one to_json() per line, core::csv_writer flattens each result's JSON into dotted columns. Any callable (std::uint64_t index, phone&) works.| Exception | When |
|---|---|
std::runtime_error | Any generation call before data is loaded; a unique batch that runs out of attempts |
std::invalid_argument | An unknown country; a type the country has no numbers of; a country with no numbers |
dasmig::core::tsv_error | A malformed table or layer (message gives file, line and column); the data is unchanged |