@ -4,7 +4,8 @@
package org.mozilla.fenix.ext
import android.net.Uri
import android.net.InetAddresses
import android.os.Build
import android.text.Editable
import android.util.Patterns
import android.webkit.URLUtil
@ -44,8 +45,7 @@ fun String.toShortUrl(publicSuffixList: PublicSuffixList): String {
return inputString
}
if ( uri . host ?. isIpv4 ( ) == true ||
uri . isIpv6 ( ) ||
if ( uri . host ?. isIpv4OrIpv6 ( ) == true ||
// If inputString is just a hostname and not a FQDN, use the entire hostname.
uri . host ?. contains ( " . " ) == false
) {
@ -73,13 +73,25 @@ fun String.toShortUrl(publicSuffixList: PublicSuffixList): String {
// impl via FFTV https://searchfox.org/mozilla-mobile/source/firefox-echo-show/app/src/main/java/org/mozilla/focus/utils/FormattedDomain.java#129
@Suppress ( " DEPRECATION " )
fun String . isIpv4 ( ) : Boolean = Patterns . IP_ADDRESS . matcher ( this ) . matches ( )
internal fun String . isIpv4 ( ) : Boolean = Patterns . IP_ADDRESS . matcher ( this ) . matches ( )
// impl via FFiOS: https://github.com/mozilla-mobile/firefox-ios/blob/deb9736c905cdf06822ecc4a20152df7b342925d/Shared/Extensions/NSURLExtensions.swift#L292
// True IPv6 validation is difficult. This is slightly better than nothing
private fun Uri . isIpv6 ( ) : Boolean {
val host = this . host ?: return false
return host . isNotEmpty ( ) && host . contains ( " : " )
internal fun String . isIpv6 ( ) : Boolean {
return this . isNotEmpty ( ) && this . contains ( " : " )
}
/ * *
* Returns true if the string represents a valid Ipv4 or Ipv6 IP address .
* Note : does not validate a dual format Ipv6 ( " y:y:y:y:y:y:x.x.x.x " format ) .
*
* /
fun String . isIpv4OrIpv6 ( ) : Boolean {
return if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . Q ) {
InetAddresses . isNumericAddress ( this )
} else {
this . isIpv4 ( ) || this . isIpv6 ( )
}
}
/ * *
@ -103,3 +115,9 @@ fun String.simplifiedUrl(): String {
* Returns an [ Editable ] for the provided string .
* /
fun String . toEditable ( ) : Editable = Editable . Factory . getInstance ( ) . newEditable ( this )
/ * *
* Returns a Ipv6 address with consecutive sections of zeroes replaced with a double colon .
* /
fun String ?. replaceConsecutiveZeros ( ) : String ? =
this ?. replaceFirst ( " :0 " , " : " ) ?. replace ( " :0 " , " " )