File: country_codes.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 3,978 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Please refer to ISO 3166-1 for information about the two-character country
// codes; http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 is useful. In the
// following (C++) code, we pack the two letters of the country code into an int
// value we call the CountryId.

#ifndef COMPONENTS_COUNTRY_CODES_COUNTRY_CODES_H_
#define COMPONENTS_COUNTRY_CODES_COUNTRY_CODES_H_

#include <array>
#include <string>

#include "base/component_export.h"

namespace country_codes {

// Class representing a Country Identifier based on ISO 3166-1 two-letter
// country codes.
class CountryId {
 public:
  // Constructs the default instance of CountryId pointing to an unspecified
  // country.
  constexpr CountryId() : CountryId(kUnknownCountryCode) {}

  // Constructs a CountryId from a two-character country code (ISO 3166-1).
  // If the provided code does not have exactly two uppercase letter characters,
  // the constructed CountryId will report invalid.
  explicit constexpr CountryId(std::string_view code) {
    if (code.size() != 2  // Country code is exactly 2 letters long
        || code[0] < 'A' || code[0] > 'Z'  // Country code requires exactly
        || code[1] < 'A' || code[1] > 'Z'  // two uppercase letters.
    ) {
      code = kUnknownCountryCode;
    }
    country_code_[0] = code[0];
    country_code_[1] = code[1];
  }

  // Deserializes a CountryId from an integer code.
  // This call complements the `Serialize()` method, and can be used to
  // de-serialize CountryId from persisted information.
  // If the supplied integer fails validation, the constructed CountryId will
  // report invalid.
  static constexpr CountryId Deserialize(int code) {
    // We only use the lowest 16 bits to build two ASCII characters. If there is
    // more than that, the ID is invalid.
    if (code <= 0 || code > 0xffff) {
      return CountryId();
    }

    char country_code[2]{static_cast<char>(code >> 8),
                         static_cast<char>(code & 0xff)};
    return CountryId(std::string_view(country_code, std::size(country_code)));
  }

  // Returns the integer representation of the CountryId. This is exposed for
  // serialization and testing but should not be used for any other purpose.
  constexpr int Serialize() const {
    return country_code_[0] << 8 | country_code_[1];
  }

  constexpr auto operator<=>(const CountryId& other) const = default;

  // Determines whether this instance of CountryId represents a valid country.
  constexpr bool IsValid() const {
    return CountryCode() != kUnknownCountryCode;
  }

  // Returns associated country code.
  // This call alone may not sufficiently help you determine the country, and
  // will return a country code associated with unknown or invalid territory,
  // when `IsValid()` returns `false`.
  constexpr std::string_view CountryCode() const {
    return std::string_view(country_code_.data(), country_code_.size());
  }

 private:
  // ISRC uses ZZ as a pseudo country code, if
  // - the origin is unknown, or
  // - the territory was not issued a country code through the ISRC agency, or
  // otherwise 'ZZ' is used for testing and to denote an unknown, or invalid
  // territory, and does not reference a real country.
  // See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#ZZ
  static constexpr std::string_view kUnknownCountryCode = "ZZ";

  // Note: this must be a std::array, and not a char[] array to enforce proper
  // creation of `std::string_view`. This is because arrays implicitly decay to
  // pointers, which may confuse std::string_view and result in security issues.
  std::array<char, 2> country_code_{};
};

// Returns the identifier for the user current country.
COMPONENT_EXPORT(COMPONENTS_COUNTRY_CODES)
CountryId GetCurrentCountryID();

}  // namespace country_codes

#endif  // COMPONENTS_COUNTRY_CODES_COUNTRY_CODES_H_