Skip to content

Commit

Permalink
Merge pull request ttacon#23 from loopfz/master
Browse files Browse the repository at this point in the history
Fix many instances of loose regex matches.
  • Loading branch information
ttacon committed Dec 8, 2015
2 parents 6fd9291 + da4de01 commit 1197dfb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
34 changes: 17 additions & 17 deletions phonenumberutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,11 +1733,11 @@ func chooseFormattingPatternForNumber(
leadingDigitsPattern := numFormat.GetLeadingDigitsPattern()
size := len(leadingDigitsPattern)

patP := `^(?:` + numFormat.GetPattern() + `)$` // Strictly match
m, ok := regexCache[numFormat.GetPattern()]
if !ok {
pat := numFormat.GetPattern()
m = regexp.MustCompile(pat)
regexCache[pat] = m
m = regexp.MustCompile(patP)
regexCache[patP] = m
}

if size == 0 {
Expand All @@ -1759,7 +1759,7 @@ func chooseFormattingPatternForNumber(
}

inds := reg.FindStringIndex(nationalNumber)
if len(inds) > 0 && m.MatchString(nationalNumber) {
if len(inds) > 0 && inds[0] == 0 && m.MatchString(nationalNumber) { // inds[0] == 0 ensures strict match of leading digits
return numFormat
}
}
Expand Down Expand Up @@ -2135,9 +2135,9 @@ func getRegionCodeForNumberFromRegionList(
// region codes come from the country calling code map.
var metadata *PhoneMetadata = getMetadataForRegion(regionCode)
if len(metadata.GetLeadingDigits()) > 0 {
pat, ok := regexCache[metadata.GetLeadingDigits()]
patP := "^(?:" + metadata.GetLeadingDigits() + ")" // Non capturing grouping to support OR'ed alternatives (e.g. 555|1[78]|2)
pat, ok := regexCache[patP]
if !ok {
patP := "^" + metadata.GetLeadingDigits()
pat = regexp.MustCompile(patP)
regexCache[patP] = pat
}
Expand Down Expand Up @@ -2271,15 +2271,15 @@ func testNumberLengthAgainstPattern(
numberPattern *regexp.Regexp,
number string) ValidationResult {

if numberPattern.MatchString(number) {
return IS_POSSIBLE
}

inds := numberPattern.FindStringIndex(number)
if len(inds) > 0 {
return TOO_LONG
if len(inds) > 0 && inds[0] == 0 && inds[1] == len(number) {
if inds[1] == len(number) { // Exact match
return IS_POSSIBLE
}
return TOO_LONG // Matches input start but not end
}
return TOO_SHORT

return TOO_SHORT // Does not match input start
}

// Helper method to check whether a number is too short to be a regular
Expand Down Expand Up @@ -2497,12 +2497,12 @@ func maybeExtractCountryCode(
potentialNationalNumber = builder.NewBuilderString(
normalizedNumber[len(defaultCountryCodeString):])
generalDesc = defaultRegionMetadata.GetGeneralDesc()
validNumberPattern, ok = regexCache[generalDesc.GetNationalNumberPattern()]
patP = `^(?:` + generalDesc.GetNationalNumberPattern() + `)$` // Strictly match
validNumberPattern, ok = regexCache[patP]
)
if !ok {
pat := generalDesc.GetNationalNumberPattern()
validNumberPattern = regexp.MustCompile(pat)
regexCache[pat] = validNumberPattern
validNumberPattern = regexp.MustCompile(patP)
regexCache[patP] = validNumberPattern
}
maybeStripNationalPrefixAndCarrierCode(
potentialNationalNumber,
Expand Down
6 changes: 3 additions & 3 deletions phonenumberutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,14 @@ func TestFormat(t *testing.T) {
frmt PhoneNumberFormat
}{
{
in: "01932 869755",
in: "019 3286 9755",
region: "GB",
exp: "019 3286 9755",
exp: "01932 869755",
frmt: NATIONAL,
}, {
in: "+44 (0) 1932 869755",
region: "GB",
exp: "+44 19 3286 9755",
exp: "+44 1932 869755",
frmt: INTERNATIONAL,
}, {
in: "4431234567",
Expand Down

0 comments on commit 1197dfb

Please sign in to comment.