/** * Helper method to get the national-number part of a number, formatted without any national * prefix, and return it as a set of digit blocks that would be formatted together. */ private static String[] GetNationalNumberGroups(PhoneNumberUtil util, PhoneNumber number, NumberFormat formattingPattern) { if (formattingPattern == null) { // This will be in the format +CC-DG;ext=EXT where DG represents groups of digits. String rfc3966Format = util.Format(number, PhoneNumberFormat.RFC3966); // We remove the extension part from the formatted string before splitting it into different // groups. int endIndex = rfc3966Format.IndexOf(';'); if (endIndex < 0) { endIndex = rfc3966Format.Length; } // The country-code will have a '-' following it. int startIndex = rfc3966Format.IndexOf('-') + 1; return(rfc3966Format.Substring(startIndex, endIndex - startIndex).Split(new [] { '-' })); } else { // We format the NSN only, and split that according to the separator. String nationalSignificantNumber = util.GetNationalSignificantNumber(number); return(util.FormatNsnUsingPattern(nationalSignificantNumber, formattingPattern, PhoneNumberFormat.RFC3966).Split(new [] { '-' })); } }
private bool CreateFormattingTemplate(NumberFormat format) { string numberPattern = format.Pattern; // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4}. In those cases we quickly return. if (numberPattern.IndexOf('|') != -1) { return(false); } // Replace anything in the form of [..] with \d numberPattern = CHARACTER_CLASS_PATTERN.Replace(numberPattern, "\\d"); // Replace any standalone digit (not the one in d{}) with \d numberPattern = STANDALONE_DIGIT_PATTERN.Replace(numberPattern, "\\d"); formattingTemplate.Length = 0; string tempTemplate = GetFormattingTemplate(numberPattern, format.Format); if (tempTemplate.Length > 0) { formattingTemplate.Append(tempTemplate); return(true); } return(false); }
// Returns true if a new template is created as opposed to reusing the existing template. private bool MaybeCreateNewTemplate() { // When there are multiple available formats, the formatter uses the first format where a // formatting template could be created. while (possibleFormats.Count > 0) { NumberFormat numberFormat = possibleFormats[0]; string pattern = numberFormat.Pattern; if (currentFormattingPattern.Equals(pattern)) { return(false); } if (CreateFormattingTemplate(numberFormat)) { currentFormattingPattern = pattern; // With a new formatting template, the matched position using the old template needs to be // reset. lastMatchPosition = 0; return(true); } else { possibleFormats.RemoveAt(0); } } ableToFormat = false; return(false); }
private bool CreateFormattingTemplate(NumberFormat format) { var numberPattern = format.Pattern; // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4}. In those cases we quickly return. if (numberPattern.IndexOf('|') != -1) { return(false); } // Replace anything in the form of [..] with \d numberPattern = CharacterClassPattern.Replace(numberPattern, "\\d"); // Replace any standalone digit (not the one in d{}) with \d numberPattern = StandaloneDigitPattern.Replace(numberPattern, "\\d"); formattingTemplate.Length = 0; var tempTemplate = GetFormattingTemplate(numberPattern, format.Format); if (tempTemplate.Length > 0) { formattingTemplate.Append(tempTemplate); return(true); } return(false); }
/// <summary> /// Helper method to get the national-number part of a number, formatted without any national /// prefix, and return it as a set of digit blocks that should be formatted together according to /// the formatting pattern passed in. /// </summary> private static IList <string> GetNationalNumberGroups(PhoneNumberUtil util, PhoneNumber number, NumberFormat formattingPattern) { // If a format is provided, we format the NSN only, and split that according to the separator. var nationalSignificantNumber = util.GetNationalSignificantNumber(number); return(util.FormatNsnUsingPattern(nationalSignificantNumber, formattingPattern, PhoneNumberFormat.RFC3966).Split('-')); }
public static bool IsNationalPrefixPresentIfRequired(PhoneNumber number, PhoneNumberUtil util) { // First, check how we deduced the country code. If it was written in international format, then // the national prefix is not required. if (number.CountryCodeSource != PhoneNumber.Types.CountryCodeSource.FROM_DEFAULT_COUNTRY) { return(true); } String phoneNumberRegion = util.GetRegionCodeForCountryCode(number.CountryCode); PhoneMetadata metadata = util.GetMetadataForRegion(phoneNumberRegion); if (metadata == null) { return(true); } // Check if a national prefix should be present when formatting this number. String nationalNumber = util.GetNationalSignificantNumber(number); NumberFormat formatRule = util.ChooseFormattingPatternForNumber(metadata.NumberFormatList, nationalNumber); // To do this, we check that a national prefix formatting rule was present and that it wasn't // just the first-group symbol ($1) with punctuation. if ((formatRule != null) && formatRule.NationalPrefixFormattingRule.Length > 0) { if (formatRule.NationalPrefixOptionalWhenFormatting) { // The national-prefix is optional in these cases, so we don't need to check if it was // present. return(true); } // Remove the first-group symbol. String candidateNationalPrefixRule = formatRule.NationalPrefixFormattingRule; // We assume that the first-group symbol will never be _before_ the national prefix. candidateNationalPrefixRule = candidateNationalPrefixRule.Substring(0, candidateNationalPrefixRule.IndexOf("${1}")); candidateNationalPrefixRule = PhoneNumberUtil.NormalizeDigitsOnly(candidateNationalPrefixRule); if (candidateNationalPrefixRule.Length == 0) { // National Prefix not needed for this number. return(true); } // Normalize the remainder. String rawInputCopy = PhoneNumberUtil.NormalizeDigitsOnly(number.RawInput); StringBuilder rawInput = new StringBuilder(rawInputCopy); // Check if we found a national prefix and/or carrier code at the start of the raw input, and // return the result. return(util.MaybeStripNationalPrefixAndCarrierCode(rawInput, metadata, null)); } return(true); }
private bool CreateFormattingTemplate(NumberFormat format) { var numberPattern = format.Pattern; formattingTemplate.Length = 0; var tempTemplate = GetFormattingTemplate(numberPattern, format.Format); if (tempTemplate.Length > 0) { formattingTemplate.Append(tempTemplate); return(true); } return(false); }
private void NarrowDownPossibleFormats(String leadingDigits) { int indexOfLeadingDigitsPattern = leadingDigits.Length - MIN_LEADING_DIGITS_LENGTH; for (int i = 0; i != possibleFormats.Count;) { NumberFormat format = possibleFormats[i]; if (format.LeadingDigitsPatternCount > indexOfLeadingDigitsPattern) { var leadingDigitsPattern = regexCache.GetPatternForRegex( format.LeadingDigitsPatternList[indexOfLeadingDigitsPattern]); var m = leadingDigitsPattern.MatchBeginning(leadingDigits); if (!m.Success) { possibleFormats.RemoveAt(i); continue; } } // else the particular format has no more specific leadingDigitsPattern, and it should be // retained. ++i; } }
public static Builder CreateBuilder(NumberFormat prototype) { return(new Builder().MergeFrom(prototype)); }
public Builder MergeFrom(NumberFormat other) { if (other == global::PhoneNumbers.NumberFormat.DefaultInstance) return this; if (other.HasPattern) { Pattern = other.Pattern; } if (other.HasFormat) { Format = other.Format; } if (other.leadingDigitsPattern_.Count != 0) { result.leadingDigitsPattern_.AddRange(other.leadingDigitsPattern_); } if (other.HasNationalPrefixFormattingRule) { NationalPrefixFormattingRule = other.NationalPrefixFormattingRule; } if (other.HasNationalPrefixOptionalWhenFormatting) { NationalPrefixOptionalWhenFormatting = other.NationalPrefixOptionalWhenFormatting; } if (other.HasDomesticCarrierCodeFormattingRule) { DomesticCarrierCodeFormattingRule = other.DomesticCarrierCodeFormattingRule; } return this; }
public Builder Clear() { result = new NumberFormat(); return this; }
public NumberFormat BuildPartial() { if (result == null) { throw new global::System.InvalidOperationException("build() has already been called on this Builder"); } NumberFormat returnMe = result; result = null; return returnMe; }
public static Builder CreateBuilder(NumberFormat prototype) { return (Builder) new Builder().MergeFrom(prototype); }
private bool CreateFormattingTemplate(NumberFormat format) { String numberPattern = format.Pattern; // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4}. In those cases we quickly return. if (numberPattern.IndexOf('|') != -1) { return false; } // Replace anything in the form of [..] with \d numberPattern = CHARACTER_CLASS_PATTERN.Replace(numberPattern, "\\d"); // Replace any standalone digit (not the one in d{}) with \d numberPattern = STANDALONE_DIGIT_PATTERN.Replace(numberPattern, "\\d"); formattingTemplate.Length = 0; String tempTemplate = GetFormattingTemplate(numberPattern, format.Format); if (tempTemplate.Length > 0) { formattingTemplate.Append(tempTemplate); return true; } return false; }
public static void SetLeadingDigitsPatterns(XmlElement numberFormatElement, NumberFormat.Builder format) { foreach (XmlElement e in numberFormatElement.GetElementsByTagName(LEADING_DIGITS)) { format.AddLeadingDigitsPattern(ValidateRE(e.InnerText, true)); } }
/** * Extracts the pattern for the national format. * * @throws RuntimeException if multiple or no formats have been encountered. * @return the national format string. */ // @VisibleForTesting public static String LoadNationalFormat(PhoneMetadata.Builder metadata, XmlElement numberFormatElement, NumberFormat.Builder format) { SetLeadingDigitsPatterns(numberFormatElement, format); format.SetPattern(ValidateRE(numberFormatElement.GetAttribute(PATTERN))); var formatPattern = numberFormatElement.GetElementsByTagName(FORMAT); if (formatPattern.Count != 1) { //LOGGER.log(Level.SEVERE, // "Only one format pattern for a numberFormat element should be defined."); throw new Exception("Invalid number of format patterns for country: " + metadata.Id); } String nationalFormat = formatPattern[0].InnerText; format.SetFormat(nationalFormat); return nationalFormat; }
// Note that carrierCode is optional - if NULL or an empty string, no carrier code replacement // will take place. private String FormatNsnUsingPattern(String nationalNumber, NumberFormat formattingPattern, PhoneNumberFormat numberFormat, String carrierCode) { String numberFormatRule = formattingPattern.Format; var m = regexCache.GetPatternForRegex(formattingPattern.Pattern); String formattedNationalNumber = ""; if (numberFormat == PhoneNumberFormat.NATIONAL && carrierCode != null && carrierCode.Length > 0 && formattingPattern.DomesticCarrierCodeFormattingRule.Length > 0) { // Replace the $CC in the formatting rule with the desired carrier code. var carrierCodeFormattingRule = formattingPattern.DomesticCarrierCodeFormattingRule; carrierCodeFormattingRule = CC_PATTERN.Replace(carrierCodeFormattingRule, carrierCode, 1); // Now replace the $FG in the formatting rule with the first group and the carrier code // combined in the appropriate way. var r = FIRST_GROUP_PATTERN.Replace(numberFormatRule, carrierCodeFormattingRule, 1); formattedNationalNumber = m.Replace(nationalNumber, r); } else { // Use the national prefix formatting rule instead. var nationalPrefixFormattingRule = formattingPattern.NationalPrefixFormattingRule; if (numberFormat == PhoneNumberFormat.NATIONAL && nationalPrefixFormattingRule != null && nationalPrefixFormattingRule.Length > 0) { var r = FIRST_GROUP_PATTERN.Replace(numberFormatRule, nationalPrefixFormattingRule, 1); formattedNationalNumber = m.Replace(nationalNumber, r); } else { formattedNationalNumber = m.Replace(nationalNumber, numberFormatRule); } } if (numberFormat == PhoneNumberFormat.RFC3966) { // Strip any leading punctuation. if (SEPARATOR_PATTERN.MatchBeginning(formattedNationalNumber).Success) { formattedNationalNumber = SEPARATOR_PATTERN.Replace(formattedNationalNumber, "", 1); } // Replace the rest with a dash between each number group. formattedNationalNumber = SEPARATOR_PATTERN.Replace(formattedNationalNumber, "-"); } return formattedNationalNumber; }
// Simple wrapper of formatNsnUsingPattern for the common case of no carrier code. internal String FormatNsnUsingPattern(String nationalNumber, NumberFormat formattingPattern, PhoneNumberFormat numberFormat) { return FormatNsnUsingPattern(nationalNumber, formattingPattern, numberFormat, null); }