Skip to content

Commit

Permalink
switch to matchEntire everywhere instead of matches. fixes 4 js tests
Browse files Browse the repository at this point in the history
There must be a bug where matches doesn't behave the same on js as jvm or native.
  • Loading branch information
luca992 committed Aug 18, 2023
1 parent ecc6c74 commit f40d8bc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class AsYouTypeFormatter internal constructor(
// so we discard it.
continue
}
if (ELIGIBLE_FORMAT_PATTERN.matches(format.format)) {
if (ELIGIBLE_FORMAT_PATTERN.matchEntire(format.format) != null) {
possibleFormats.add(format)
}
}
Expand Down Expand Up @@ -395,7 +395,9 @@ class AsYouTypeFormatter internal constructor(
}

private fun isDigitOrLeadingPlusSign(nextChar: Char): Boolean {
return (nextChar.isDigit() || (accruedInput.length == 1 && PhoneNumberUtil.PLUS_CHARS_PATTERN.matches(nextChar.toString())))
return (nextChar.isDigit() || (accruedInput.length == 1 && PhoneNumberUtil.PLUS_CHARS_PATTERN.matchEntire(
nextChar.toString()
) != null))
}

/**
Expand All @@ -405,7 +407,7 @@ class AsYouTypeFormatter internal constructor(
fun attemptToFormatAccruedDigits(): String {
for (numberFormat in possibleFormats) {
val r: Regex = regexCache.getRegexForPattern(numberFormat.pattern)
if (r.matches(nationalNumber)) {
if (r.matchEntire(nationalNumber) != null) {
shouldAddSpaceAfterNationalPrefix = NATIONAL_PREFIX_SEPARATORS_PATTERN.containsMatchIn(
numberFormat.nationalPrefixFormattingRule
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class PhoneNumberMatcher(
try {
// Check the candidate doesn't contain any formatting which would indicate that it really
// isn't a phone number.
if (!MATCHING_BRACKETS!!.matches(candidate) || PUB_PAGES.find(candidate) != null) {
if (MATCHING_BRACKETS!!.matchEntire(candidate) == null || PUB_PAGES.find(candidate) != null) {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,18 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
) {
false
} else {
matcher.checkNumberGroupingIsValid(number,
candidate,
util,
object : NumberGroupingChecker {
override fun checkGroups(
util: PhoneNumberUtil,
number: PhoneNumber,
normalizedCandidate: InplaceStringBuilder,
expectedNumberGroups: Array<String>
): Boolean {
return PhoneNumberMatcher.allNumberGroupsRemainGrouped(
util, number, normalizedCandidate, expectedNumberGroups
)
}
})
matcher.checkNumberGroupingIsValid(number, candidate, util, object : NumberGroupingChecker {
override fun checkGroups(
util: PhoneNumberUtil,
number: PhoneNumber,
normalizedCandidate: InplaceStringBuilder,
expectedNumberGroups: Array<String>
): Boolean {
return PhoneNumberMatcher.allNumberGroupsRemainGrouped(
util, number, normalizedCandidate, expectedNumberGroups
)
}
})
}
}
},
Expand Down Expand Up @@ -934,7 +931,7 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
var internationalPrefixForFormatting: String? = ""
if (metadataForRegionCallingFrom.hasPreferredInternationalPrefix()) {
internationalPrefixForFormatting = metadataForRegionCallingFrom.preferredInternationalPrefix
} else if (SINGLE_INTERNATIONAL_PREFIX.matches(internationalPrefix)) {
} else if (SINGLE_INTERNATIONAL_PREFIX.matchEntire(internationalPrefix) != null) {
internationalPrefixForFormatting = internationalPrefix
}
val regionCode = getRegionCodeForCountryCode(countryCallingCode)
Expand Down Expand Up @@ -1230,9 +1227,10 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
// international prefix.
if (metadataForRegionCallingFrom != null) {
val internationalPrefix = metadataForRegionCallingFrom.internationalPrefix
internationalPrefixForFormatting = if (SINGLE_INTERNATIONAL_PREFIX.matches(internationalPrefix)) {
internationalPrefix
} else metadataForRegionCallingFrom.preferredInternationalPrefix
internationalPrefixForFormatting =
if (SINGLE_INTERNATIONAL_PREFIX.matchEntire(internationalPrefix) != null) {
internationalPrefix
} else metadataForRegionCallingFrom.preferredInternationalPrefix
}
val formattedNumber = InplaceStringBuilder(rawInput)
val regionCode = getRegionCodeForCountryCode(countryCode)
Expand Down Expand Up @@ -1331,7 +1329,7 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
numFormat.getLeadingDigitsPattern(size - 1)
).matchesAt(nationalNumber, 0)
) {
if (regexCache.getRegexForPattern(numFormat.pattern).matches(nationalNumber)) {
if (regexCache.getRegexForPattern(numFormat.pattern).matchEntire(nationalNumber) != null) {
return numFormat
}
}
Expand Down Expand Up @@ -1906,7 +1904,7 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
}
val strippedNumber = InplaceStringBuilder(number)
maybeStripExtension(strippedNumber)
return VALID_ALPHA_PHONE_PATTERN.matches(strippedNumber)
return VALID_ALPHA_PHONE_PATTERN.matchEntire(strippedNumber) != null
}

/**
Expand Down Expand Up @@ -2737,11 +2735,11 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
if (phoneContext == null) {
return true
}
return if (phoneContext.length == 0) {
return if (phoneContext.isEmpty()) {
false
} else (RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN.matches(phoneContext) || RFC3966_DOMAINNAME_PATTERN.matches(
} else (RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN.matchEntire(phoneContext) != null || RFC3966_DOMAINNAME_PATTERN.matchEntire(
phoneContext
))
) != null)

// Does phone-context value match pattern of global-number-digits or domainname
}
Expand Down Expand Up @@ -3441,7 +3439,7 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
* normalized in place
*/
fun normalize(number: InplaceStringBuilder): InplaceStringBuilder {
if (VALID_ALPHA_PHONE_PATTERN.matches(number)) {
if (VALID_ALPHA_PHONE_PATTERN.matchEntire(number) != null) {
number.replaceRange(0, number.length, normalizeHelper(number, ALPHA_PHONE_MAPPINGS, true))
} else {
number.replaceRange(0, number.length, normalizeDigitsOnly(number))
Expand Down Expand Up @@ -3617,9 +3615,9 @@ class PhoneNumberUtil internal constructor(// A source of metadata for different
*/
@JvmStatic
fun formattingRuleHasFirstGroupOnly(nationalPrefixFormattingRule: String): Boolean {
return (nationalPrefixFormattingRule.length == 0 || FIRST_GROUP_ONLY_PREFIX_PATTERN.matches(
return (nationalPrefixFormattingRule.isEmpty() || FIRST_GROUP_ONLY_PREFIX_PATTERN.matchEntire(
nationalPrefixFormattingRule
))
) != null)
}

private fun ensureMetadataIsNonNull(phoneMetadata: PhoneMetadata?, message: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RegexBasedMatcher private constructor() : MatcherApi {
return if (!regex.matchesAt(number, 0)) {
false
} else {
if (regex.matches(number)) true else allowPrefixMatch
if (regex.matchEntire(number) != null) true else allowPrefixMatch
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MultiFileModeResourceProvider(

override fun getFor(key: Any): AssetResource? {
val keyAsString = key.toString()
require(ALPHANUMERIC.matches(keyAsString)) { "Invalid key: $keyAsString" }
require(ALPHANUMERIC.matchEntire(keyAsString) != null) { "Invalid key: $keyAsString" }
val path = phoneMetadataFileNamePrefix + keyAsString
return assets.getAssetByFilePath(path)
}
Expand Down

0 comments on commit f40d8bc

Please sign in to comment.