<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/autofill/phone_number.h"

#include "base/basictypes.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/autofill_profile.h"
#include "chrome/browser/autofill/autofill_type.h"
#include "chrome/browser/autofill/field_types.h"
#include "chrome/browser/autofill/phone_number_i18n.h"

namespace {

const char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 };

// The number of digits in a phone number.
const size_t kPhoneNumberLength = 7;

// The number of digits in an area code.
const size_t kPhoneCityCodeLength = 3;

const AutofillType::FieldTypeSubGroup kAutofillPhoneTypes[] = {
  AutofillType::PHONE_NUMBER,
  AutofillType::PHONE_CITY_CODE,
  AutofillType::PHONE_COUNTRY_CODE,
  AutofillType::PHONE_CITY_AND_NUMBER,
  AutofillType::PHONE_WHOLE_NUMBER,
};

const int kAutofillPhoneLength = arraysize(kAutofillPhoneTypes);

void StripPunctuation(string16* number) {
  RemoveChars(*number, kPhoneNumberSeparators, number);
}

}  // namespace

PhoneNumber::PhoneNumber(AutofillProfile* profile)
    : phone_group_(AutofillType::NO_GROUP),
      profile_(profile) {
}

PhoneNumber::PhoneNumber(AutofillType::FieldTypeGroup phone_group,
                         AutofillProfile* profile)
    : phone_group_(phone_group),
      profile_(profile) {
}

PhoneNumber::PhoneNumber(const PhoneNumber& number) : FormGroup() {
  *this = number;
}

PhoneNumber::~PhoneNumber() {}

PhoneNumber& PhoneNumber::operator=(const PhoneNumber& number) {
  if (this == &number)
    return *this;
  phone_group_ = number.phone_group_;
  number_ = number.number_;
  profile_ = number.profile_;
  cached_parsed_phone_ = number.cached_parsed_phone_;
  return *this;
}

void PhoneNumber::GetSupportedTypes(FieldTypeSet* supported_types) const {
  supported_types->insert(GetWholeNumberType());
  supported_types->insert(GetNumberType());
  supported_types->insert(GetCityCodeType());
  supported_types->insert(GetCityAndNumberType());
  supported_types->insert(GetCountryCodeType());
}

string16 PhoneNumber::GetInfo(AutofillFieldType type) const {
  if (type == GetWholeNumberType())
    return number_;

  UpdateCacheIfNeeded();
  if (!cached_parsed_phone_.IsValidNumber())
    return string16();

  if (type == GetNumberType())
    return cached_parsed_phone_.GetNumber();

  if (type == GetCityCodeType())
    return cached_parsed_phone_.GetCityCode();

  if (type == GetCountryCodeType())
    return cached_parsed_phone_.GetCountryCode();

  if (type == GetCityAndNumberType()) {
    string16 city_and_local(cached_parsed_phone_.GetCityCode());
    city_and_local.append(cached_parsed_phone_.GetNumber());
    return city_and_local;
  }

  return string16();
}

void PhoneNumber::SetInfo(AutofillFieldType type, const string16& value) {
  // Store the group the first time we set some info.
  if (phone_group_ == AutofillType::NO_GROUP)
    phone_group_ = AutofillType(type).group();

  FieldTypeSubGroup subgroup = AutofillType(type).subgroup();
  if (subgroup != AutofillType::PHONE_CITY_AND_NUMBER &&
      subgroup != AutofillType::PHONE_WHOLE_NUMBER) {
    // Only full phone numbers should be set directly.
    NOTREACHED();
    return;
  }

  number_ = value;
  cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, locale());
}

// Normalize phones if |type| is a whole number:
//   (650)2345678 -> 6502345678
//   1-800-FLOWERS -> 18003569377
// If the phone cannot be normalized, returns the stored value verbatim.
string16 PhoneNumber::GetCanonicalizedInfo(AutofillFieldType type) const {
  string16 phone = GetInfo(type);
  if (type != GetWholeNumberType())
    return phone;

  string16 normalized_phone = autofill_i18n::NormalizePhoneNumber(phone,
                                                                  locale());
  if (!normalized_phone.empty())
    return normalized_phone;
<commit_msg>Remove overly defensive NOTREACHED()<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/autofill/phone_number.h"

#include "base/basictypes.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/autofill_profile.h"
#include "chrome/browser/autofill/autofill_type.h"
#include "chrome/browser/autofill/field_types.h"
#include "chrome/browser/autofill/phone_number_i18n.h"

namespace {

const char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 };

// The number of digits in a phone number.
const size_t kPhoneNumberLength = 7;

// The number of digits in an area code.
const size_t kPhoneCityCodeLength = 3;

const AutofillType::FieldTypeSubGroup kAutofillPhoneTypes[] = {
  AutofillType::PHONE_NUMBER,
  AutofillType::PHONE_CITY_CODE,
  AutofillType::PHONE_COUNTRY_CODE,
  AutofillType::PHONE_CITY_AND_NUMBER,
  AutofillType::PHONE_WHOLE_NUMBER,
};

const int kAutofillPhoneLength = arraysize(kAutofillPhoneTypes);

void StripPunctuation(string16* number) {
  RemoveChars(*number, kPhoneNumberSeparators, number);
}

}  // namespace

PhoneNumber::PhoneNumber(AutofillProfile* profile)
    : phone_group_(AutofillType::NO_GROUP),
      profile_(profile) {
}

PhoneNumber::PhoneNumber(AutofillType::FieldTypeGroup phone_group,
                         AutofillProfile* profile)
    : phone_group_(phone_group),
      profile_(profile) {
}

PhoneNumber::PhoneNumber(const PhoneNumber& number) : FormGroup() {
  *this = number;
}

PhoneNumber::~PhoneNumber() {}

PhoneNumber& PhoneNumber::operator=(const PhoneNumber& number) {
  if (this == &number)
    return *this;
  phone_group_ = number.phone_group_;
  number_ = number.number_;
  profile_ = number.profile_;
  cached_parsed_phone_ = number.cached_parsed_phone_;
  return *this;
}

void PhoneNumber::GetSupportedTypes(FieldTypeSet* supported_types) const {
  supported_types->insert(GetWholeNumberType());
  supported_types->insert(GetNumberType());
  supported_types->insert(GetCityCodeType());
  supported_types->insert(GetCityAndNumberType());
  supported_types->insert(GetCountryCodeType());
}

string16 PhoneNumber::GetInfo(AutofillFieldType type) const {
  if (type == GetWholeNumberType())
    return number_;

  UpdateCacheIfNeeded();
  if (!cached_parsed_phone_.IsValidNumber())
    return string16();

  if (type == GetNumberType())
    return cached_parsed_phone_.GetNumber();

  if (type == GetCityCodeType())
    return cached_parsed_phone_.GetCityCode();

  if (type == GetCountryCodeType())
    return cached_parsed_phone_.GetCountryCode();

  if (type == GetCityAndNumberType()) {
    string16 city_and_local(cached_parsed_phone_.GetCityCode());
    city_and_local.append(cached_parsed_phone_.GetNumber());
    return city_and_local;
  }

  return string16();
}

void PhoneNumber::SetInfo(AutofillFieldType type, const string16& value) {
  // Store the group the first time we set some info.
  if (phone_group_ == AutofillType::NO_GROUP)
    phone_group_ = AutofillType(type).group();

  FieldTypeSubGroup subgroup = AutofillType(type).subgroup();
  if (subgroup != AutofillType::PHONE_CITY_AND_NUMBER &&
      subgroup != AutofillType::PHONE_WHOLE_NUMBER) {
    // Only full phone numbers should be set directly.  The remaining field
    // field types are read-only.
    return;
  }

  number_ = value;
  cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, locale());
}

// Normalize phones if |type| is a whole number:
//   (650)2345678 -> 6502345678
//   1-800-FLOWERS -> 18003569377
// If the phone cannot be normalized, returns the stored value verbatim.
string16 PhoneNumber::GetCanonicalizedInfo(AutofillFieldType type) const {
  string16 phone = GetInfo(type);
  if (type != GetWholeNumberType())
    return phone;

  string16 normalized_phone = autofill_i18n::NormalizePhoneNumber(phone,
                                                                  locale());
  if (!normalized_phone.empty())
    return normalized_phone;

{'fraction_non_alphanumeric': 0.11273209549071618, 'fraction_numerical': 0.016397395707740535, 'mean_word_length': 4.736514522821577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2219461', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2604, 'n_words': 543}