in phonenumbers.go [2680:2741]
func maybeStripNationalPrefixAndCarrierCode(
number *Builder,
metadata *PhoneMetadata,
carrierCode *Builder) bool {
numberLength := len(number.String())
possibleNationalPrefix := metadata.GetNationalPrefixForParsing()
if numberLength == 0 || len(possibleNationalPrefix) == 0 {
// Early return for numbers of zero length.
return false
}
possibleNationalPrefix = "^(?:" + possibleNationalPrefix + ")" // Strictly match from string start
// Attempt to parse the first digits as a national prefix.
prefixMatcher := regexFor(possibleNationalPrefix)
if prefixMatcher.MatchString(number.String()) {
natRulePattern := "^(?:" + metadata.GetGeneralDesc().GetNationalNumberPattern() + ")$" // Strictly match
nationalNumberRule := regexFor(natRulePattern)
// Check if the original number is viable.
isViableOriginalNumber := nationalNumberRule.Match(number.Bytes())
// prefixMatcher.group(numOfGroups) == null implies nothing was
// captured by the capturing groups in possibleNationalPrefix;
// therefore, no transformation is necessary, and we just
// remove the national prefix.
groups := prefixMatcher.FindSubmatchIndex(number.Bytes())
numOfGroups := len(groups)/2 - 1 // groups is a list of index pairs, idx0,idx1 defines the whole match, idx2+ submatches.
// Subtract one to ignore group(0) in count
transformRule := metadata.GetNationalPrefixTransformRule()
if len(transformRule) == 0 || groups[numOfGroups*2] < 0 { // Negative idx means subgroup did not match
// If the original number was viable, and the resultant number
// is not, we return.
if isViableOriginalNumber &&
!nationalNumberRule.MatchString(
number.String()[groups[1]:]) { // groups[1] == last match idx
return false
}
if len(carrierCode.Bytes()) != 0 &&
numOfGroups > 0 &&
groups[numOfGroups*2] > 0 { // Negative idx means subgroup did not match
carrierCode.Write(number.Bytes()[groups[numOfGroups*2]:groups[numOfGroups*2+1]])
}
number.ResetWith(number.Bytes()[groups[1]:])
return true
} else {
// Check that the resultant number is still viable. If not,
// return. Check this by copying the string buffer and
// making the transformation on the copy first.
numString := number.String()
transformedNumBytes := []byte(prefixMatcher.ReplaceAllString(numString, transformRule))
if isViableOriginalNumber &&
!nationalNumberRule.Match(transformedNumBytes) {
return false
}
if len(carrierCode.Bytes()) != 0 && numOfGroups > 1 && groups[2] != -1 { // Check group(1) got a submatch
carrC := numString[groups[2]:groups[3]] // group(1) idxs
carrierCode.WriteString(carrC)
}
number.ResetWith(transformedNumBytes)
return true
}
}
return false
}