libphonenumber - Documentation

Introduction

What is libphonenumber?

libphonenumber is a C++ library providing comprehensive phone number parsing and formatting capabilities. It’s a port of Google’s widely used Java libphonenumber library, offering the same robust functionality for C++ applications. The library allows you to accurately parse phone numbers from various regions, format them according to regional standards, validate their validity, and extract relevant information like country calling codes and national numbers.

Why use libphonenumber?

Using libphonenumber offers several key advantages:

Supported Regions and Languages

libphonenumber supports a wide range of countries and regions globally. The library’s metadata is regularly updated to reflect changes in phone number formats and regional standards. The specific supported regions and languages are implicitly defined within the library’s metadata files, which are automatically loaded. You do not need to specify them explicitly in your code unless you are modifying or extending the metadata.

Installation and Setup

The installation process depends on your build system and environment. A typical installation might involve using a package manager (e.g., vcpkg, Conan) or building from source using CMake.

Using a Package Manager (Example with vcpkg):

  1. Install vcpkg following the instructions on the https://vcpkg.io/en/.
  2. Add the libphonenumber port: vcpkg integrate install libphonenumber
  3. Include the library in your project using the appropriate include paths and link against the library.

Building from Source:

  1. Clone the libphonenumber repository from https://github.com/google/libphonenumber.
  2. Create a build directory: mkdir build && cd build
  3. Configure the project using CMake: cmake ..
  4. Build the library: cmake --build .
  5. Install the library (optional): cmake --install .

Specific instructions may vary slightly depending on your chosen build system. Consult the project’s documentation for detailed instructions.

Basic Usage Example

This example demonstrates parsing a phone number and getting its E.164 format:

#include "phonenumberutil.h"

int main() {
  i18n::phonenumbers::PhoneNumberUtil util;
  i18n::phonenumbers::PhoneNumber number;
  std::string number_str = "+16502530000"; //Example number

  // Parse the phone number
  if (util.Parse(number_str, "US", &number)) {
    std::string e164_number = util.Format(number, i18n::phonenumbers::PhoneNumberUtil::E164);
    std::cout << "E.164 formatted number: " << e164_number << std::endl;
  } else {
    std::cerr << "Failed to parse phone number." << std::endl;
  }

  return 0;
}

Remember to link against the libphonenumber library during compilation. This example requires appropriate include paths to be configured. Consult the detailed examples and API documentation for more advanced usage.

Core Functionality

Number Parsing

The core functionality of libphonenumber centers around parsing phone numbers. The PhoneNumberUtil::Parse() method takes a phone number string and an optional region code as input. It attempts to parse the string according to the specified region’s phone number format. If a region code is not provided, the library will attempt to infer the region based on the leading digits of the number. The parsed number is returned as a PhoneNumber object, which contains various components of the phone number, such as the country calling code, national number, and extension. Error handling is crucial; check the return value of Parse() to ensure successful parsing. If parsing fails, the function will return false.

Number Formatting

Once a phone number is parsed, it can be formatted using the PhoneNumberUtil::Format() method. This method takes the PhoneNumber object and a PhoneNumberFormat enum value (e.g., E164, INTERNATIONAL, NATIONAL, RFC3966) as input. It returns a formatted string representation of the phone number according to the specified format. The E164 format is the internationally recognized format (e.g., “+16502530000”). The NATIONAL format uses the local formatting conventions of the number’s region.

Number Validation

libphonenumber offers robust validation capabilities. The PhoneNumberUtil::IsValidNumber() method checks if a given PhoneNumber object represents a valid phone number for its associated region. This involves verifying that the number conforms to the regional number format and that all components (like the country calling code and national number) are present and valid. Additionally, PhoneNumberUtil::IsPossibleNumber() provides a less strict check, determining if a number could be valid based on its leading digits, without checking for all the details of regional formatting rules. Using these methods helps ensure that only valid phone numbers are processed in your application.

Number Type Detection

The library can determine the type of a phone number (e.g., mobile, fixed-line, toll-free, etc.). The PhoneNumberUtil::GetNumberType() method takes a PhoneNumber object as input and returns a PhoneNumberType enum value representing the type of the number. This information is useful for applications that need to handle different types of phone numbers differently (e.g., routing calls to different systems).

Region Code Handling

Region codes are essential for accurate phone number processing. They are typically two-letter country codes (e.g., “US”, “CA”, “GB”). When parsing or formatting numbers, providing the correct region code is vital, as phone number formats differ significantly across regions. If a region code isn’t provided explicitly during parsing, libphonenumber attempts to infer it from the number itself; however, providing the region code explicitly is always recommended for improved accuracy. The library provides helper functions to check region validity and access region-specific information.

AsYouType Functionality

The PhoneNumberUtil::GetAsYouTypeFormatter() method allows for incremental formatting of a phone number as the user types. This is particularly useful for user interfaces where the user inputs a phone number character by character. The formatter keeps track of the input and provides updated formatted output at each step, dynamically adjusting based on the entered digits and inferred region. This leads to a more user-friendly experience by providing immediate feedback to the user.

Metadata Access

libphonenumber uses metadata files to store information about phone number formats for different regions. While you generally don’t need to directly access this metadata, understanding its role is beneficial. The metadata contains rules for parsing, formatting, and validating phone numbers. These files are loaded automatically by the library, but advanced users might explore ways to customize or extend the metadata for specific needs (though this usually isn’t required for standard use). The library abstracts away most of the complexity of working with metadata, allowing developers to focus on higher-level functionality.

Advanced Usage

Customizing Formatting

While libphonenumber provides standard formatting options, you can achieve more fine-grained control. For instance, you might need to customize the formatting of certain number types or regions. While direct manipulation of the underlying formatting rules isn’t readily available, you can work around this using the PhoneNumberUtil::Format() method with different PhoneNumberFormat options and potentially post-processing the resulting string to apply additional formatting rules as needed. This might involve using regular expressions or other string manipulation techniques to add or remove specific characters or patterns.

Handling Invalid Numbers

Robust error handling is essential when dealing with phone numbers. The PhoneNumberUtil::Parse() method returns false if parsing fails. It’s crucial to check this return value and handle the cases where input is not a valid phone number. Your application should gracefully handle these scenarios, perhaps presenting a user-friendly error message or taking alternative action. Consider using PhoneNumberUtil::IsValidNumber() and PhoneNumberUtil::IsPossibleNumber() to further validate numbers before processing them.

Working with different number types (e.g., mobile, landline)

libphonenumber can identify the type of a phone number (e.g., mobile, fixed-line, toll-free) using PhoneNumberUtil::GetNumberType(). This information allows you to tailor your application’s behavior based on the number type. For instance, you might use different routing strategies or display different information depending on whether the number is a mobile or landline. This enables differentiated treatment for various call types and functionalities within your application.

Using the Metadata Manager

Directly accessing the metadata manager is generally not recommended for typical use cases. The library handles loading and managing the metadata automatically. However, understanding the metadata manager’s role is important for advanced scenarios. If you need to manage multiple metadata files or update the metadata dynamically during runtime (a less common scenario), you would interact with the metadata manager, but it’s usually best to rely on the library’s default behavior.

Efficiently handling large datasets

When processing large datasets of phone numbers, efficiency is paramount. Avoid repeatedly parsing the same numbers. If possible, parse numbers once and store the parsed PhoneNumber objects for later use. Batch processing techniques can significantly improve performance compared to processing individual numbers one by one. Consider using techniques like vectorized operations or multithreading to process multiple numbers concurrently.

Integration with other libraries

libphonenumber can integrate well with other libraries and frameworks. You can integrate it into existing systems to enhance their phone number handling capabilities. If you’re using a database, you might store parsed phone numbers (as PhoneNumber objects or their serialized representation) in your database instead of just raw number strings for easier and more efficient manipulation later. Integration may require serialization and deserialization of PhoneNumber objects. This could involve using JSON, Protocol Buffers, or another suitable serialization format. Consider using your chosen programming language’s standard serialization libraries for this purpose.

API Reference

This section provides a brief overview of the key classes and enums within the libphonenumber API. For detailed descriptions of each method and its parameters, refer to the generated documentation (if available) or the library’s header files.

PhoneNumberUtil Class

The PhoneNumberUtil class is the central class in the libphonenumber API. It provides methods for parsing, formatting, validating, and extracting information from phone numbers. Key methods include:

PhoneNumber Object

The PhoneNumber object represents a parsed phone number. It contains various fields representing different parts of the number, such as:

and more. Access these fields using appropriate getter and setter methods.

PhoneNumberFormat Enum

The PhoneNumberFormat enum specifies the desired format for formatting a phone number:

NumberType Enum

The NumberType enum represents the type of a phone number:

RegionCode Enum

While not explicitly an enum in the traditional sense, region codes are two-letter country codes (e.g., “US”, “GB”, “CA”). They are used extensively in various methods to specify the region associated with a phone number. The library does not define a strict RegionCode enum but rather relies on the string representation of the region code.

MetadataManager Class (Advanced Usage)

The MetadataManager class handles loading and managing phone number metadata. Direct interaction with this class is generally not necessary for standard usage. It’s primarily used for advanced scenarios where custom metadata handling or loading is required. Consult the advanced usage section for details on how and when to interact with the MetadataManager.

AsYouTypeFormatter Class

The AsYouTypeFormatter class provides functionality for formatting phone numbers as the user types. It takes a region code during initialization and then uses the inputDigit() method to incrementally format the number as each digit is entered. The getFormattedNumber() method retrieves the currently formatted number. This class is particularly useful for user-friendly interfaces where immediate feedback during number entry is important.

Error Handling and Troubleshooting

Common Errors and Solutions

This section outlines common errors encountered when using libphonenumber and suggests solutions.

Debugging Tips

Debugging libphonenumber issues can be approached as follows:

Troubleshooting Metadata Issues

Problems related to phone number metadata are less frequent but can occur. Here’s how to troubleshoot them:

If you suspect issues with the metadata itself, check the libphonenumber project’s issue tracker or contact the maintainers for assistance. Providing a minimal, reproducible example will significantly aid in diagnosing the problem.

Best Practices and Guidelines

Optimizing performance

For optimal performance when using libphonenumber, consider these best practices:

Security considerations

Security is paramount. While libphonenumber itself does not introduce direct security vulnerabilities, consider these points:

Handling internationalization and localization

libphonenumber excels at handling international phone numbers. To leverage its capabilities effectively:

Working with different character sets

libphonenumber primarily works with UTF-8 encoded strings. Ensure that your input phone number strings are correctly encoded in UTF-8. If you’re working with other character sets, convert them to UTF-8 before using libphonenumber functions. Incorrect encoding can lead to parsing failures or unexpected results. Handle character encoding consistently throughout your application.

Appendix

List of supported regions

libphonenumber supports a comprehensive list of regions globally. The exact list is implicitly defined within the library’s metadata files and is automatically updated as new metadata versions are released. A comprehensive, up-to-date list is not practically maintainable within this document due to the dynamic nature of the supported regions. To obtain the current list, refer to the libphonenumber repository or check the metadata files directly. The library aims for comprehensive global coverage, but minor gaps might exist in very recently added or exceptionally rare regions.

Metadata Download and Updates

The phone number metadata is crucial for the library’s functionality. The library usually handles metadata updates automatically when a new version of the library is released (depending on the installation method and the specifics of the packaging system). However, you might need to manually download updated metadata in some cases. The location of metadata files varies depending on the library’s version and installation method. Check the library’s documentation and/or repository for instructions on finding and updating the metadata files. Regular updates are essential to ensure accuracy and support for the latest phone number formats and regional changes.

Contribution Guidelines

Contributions to libphonenumber are welcome. Before contributing, please review the project’s contribution guidelines (found on the project’s GitHub repository or similar platform). These guidelines typically outline the preferred code style, testing procedures, and contribution workflow. Adhering to the guidelines ensures a smooth and consistent contribution process. Common steps usually include:

The maintainers will review your contributions before merging them into the main codebase.

License Information

libphonenumber is released under an open-source license (the specific license will be stated in the project’s repository – check the LICENSE file). This license grants you certain rights to use, modify, and distribute the library, but it’s essential to review the license text to understand the terms and conditions fully. Respecting the library’s license is mandatory when using, modifying, or distributing the library. Failure to adhere to the license terms might have legal consequences. Always include the appropriate license notice in your projects that use libphonenumber.