in phonenumbers.go [1475:1571]
func FormatInOriginalFormat(number *PhoneNumber, regionCallingFrom string) string {
rawInput := number.GetRawInput()
if len(rawInput) == 0 && !hasFormattingPatternForNumber(number) {
// We check if we have the formatting pattern because without that, we might format the number
// as a group without national prefix.
return rawInput
}
if number.GetCountryCodeSource() == 0 {
return Format(number, NATIONAL)
}
var formattedNumber string
switch number.GetCountryCodeSource() {
case PhoneNumber_FROM_NUMBER_WITH_PLUS_SIGN:
formattedNumber = Format(number, INTERNATIONAL)
case PhoneNumber_FROM_NUMBER_WITH_IDD:
formattedNumber = FormatOutOfCountryCallingNumber(number, regionCallingFrom)
case PhoneNumber_FROM_NUMBER_WITHOUT_PLUS_SIGN:
formattedNumber = Format(number, INTERNATIONAL)[1:]
case PhoneNumber_FROM_DEFAULT_COUNTRY:
// Fall-through to default case.
fallthrough
default:
regionCode := GetRegionCodeForCountryCode(int(number.GetCountryCode()))
// We strip non-digits from the NDD here, and from the raw
// input later, so that we can compare them easily.
nationalPrefix := GetNddPrefixForRegion(
regionCode, true /* strip non-digits */)
nationalFormat := Format(number, NATIONAL)
if len(nationalPrefix) == 0 {
// If the region doesn't have a national prefix at all,
// we can safely return the national format without worrying
// about a national prefix being added.
formattedNumber = nationalFormat
break
}
// Otherwise, we check if the original number was entered with
// a national prefix.
if rawInputContainsNationalPrefix(rawInput, nationalPrefix, regionCode) {
// If so, we can safely return the national format.
formattedNumber = nationalFormat
break
}
// Metadata cannot be null here because GetNddPrefixForRegion()
// (above) returns null if there is no metadata for the region.
metadata := getMetadataForRegion(regionCode)
nationalNumber := GetNationalSignificantNumber(number)
formatRule :=
chooseFormattingPatternForNumber(metadata.GetNumberFormat(), nationalNumber)
// The format rule could still be null here if the national
// number was 0 and there was no raw input (this should not
// be possible for numbers generated by the phonenumber library
// as they would also not have a country calling code and we
// would have exited earlier).
if formatRule == nil {
formattedNumber = nationalFormat
break
}
// When the format we apply to this number doesn't contain
// national prefix, we can just return the national format.
// TODO: Refactor the code below with the code in
// isNationalPrefixPresentIfRequired.
candidateNationalPrefixRule := formatRule.GetNationalPrefixFormattingRule()
// We assume that the first-group symbol will never be _before_
// the national prefix.
indexOfFirstGroup := strings.Index(candidateNationalPrefixRule, "$1")
if indexOfFirstGroup <= 0 {
formattedNumber = nationalFormat
break
}
candidateNationalPrefixRule =
candidateNationalPrefixRule[0:indexOfFirstGroup]
candidateNationalPrefixRule = NormalizeDigitsOnly(candidateNationalPrefixRule)
if len(candidateNationalPrefixRule) == 0 {
// National prefix not used when formatting this number.
formattedNumber = nationalFormat
break
}
// Otherwise, we need to remove the national prefix from our output.
numFormatCopy := &NumberFormat{}
proto.Merge(numFormatCopy, formatRule)
numFormatCopy.NationalPrefixFormattingRule = nil
var numberFormats = []*NumberFormat{numFormatCopy}
formattedNumber = FormatByPattern(number, NATIONAL, numberFormats)
}
rawInput = number.GetRawInput()
// If no digit is inserted/removed/modified as a result of our
// formatting, we return the formatted phone number; otherwise we
// return the raw input the user entered.
if len(formattedNumber) != 0 && len(rawInput) > 0 {
normalizedFormattedNumber := normalizeDiallableCharsOnly(formattedNumber)
normalizedRawInput := normalizeDiallableCharsOnly(rawInput)
if normalizedFormattedNumber != normalizedRawInput {
formattedNumber = rawInput
}
}
return formattedNumber
}