func buildNationalNumberForParsing()

in phonenumbers.go [3071:3122]


func buildNationalNumberForParsing(
	numberToParse string,
	nationalNumber *Builder) error {

	indexOfPhoneContext := strings.Index(numberToParse, RFC3966_PHONE_CONTEXT)

	phoneContext := extractPhoneContext(numberToParse, indexOfPhoneContext)
	if indexOfPhoneContext >= 0 && !isPhoneContextValid(phoneContext) {
		return ErrNotANumber
	}
	if indexOfPhoneContext > 0 {
		// If the phone context contains a phone number prefix, we need to capture it, whereas domains
		// will be ignored.
		if phoneContext[0] == PLUS_SIGN {
			// Additional parameters might follow the phone context. If so, we will remove them here
			// because the parameters after phone context are not important for parsing the phone
			// number.
			nationalNumber.WriteString(phoneContext)
		}

		// Now append everything between the "tel:" prefix and the phone-context. This should include
		// the national number, an optional extension or isdn-subaddress component. Note we also
		// handle the case when "tel:" is missing, as we have seen in some of the phone number inputs.
		// In that case, we append everything from the beginning.
		indexOfRfc3966Prefix := strings.Index(numberToParse, RFC3966_PREFIX)
		indexOfNationalNumber := 0
		if indexOfRfc3966Prefix >= 0 {
			indexOfNationalNumber = indexOfRfc3966Prefix + len(RFC3966_PREFIX)
		}
		nationalNumber.WriteString(numberToParse[indexOfNationalNumber:indexOfPhoneContext])
	} else {
		// Extract a possible number from the string passed in (this
		// strips leading characters that could not be the start of a
		// phone number.)
		nationalNumber.WriteString(extractPossibleNumber(numberToParse))
	}

	// Delete the isdn-subaddress and everything after it if it is present.
	// Note extension won't appear at the same time with isdn-subaddress
	// according to paragraph 5.3 of the RFC3966 spec,
	indexOfIsdn := strings.Index(nationalNumber.String(), RFC3966_ISDN_SUBADDRESS)
	if indexOfIsdn > 0 {
		natNumBytes := nationalNumber.Bytes()
		nationalNumber.ResetWith(natNumBytes[:indexOfIsdn])
	}
	// If both phone context and isdn-subaddress are absent but other
	// parameters are present, the parameters are left in nationalNumber.
	// This is because we are concerned about deleting content from a
	// potential number string when there is no strong evidence that the
	// number is actually written in RFC3966.
	return nil
}