diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.kt index a7d611e6c..1a0a0521c 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.kt @@ -13,56 +13,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.client.parser -package com.hippo.ehviewer.client.parser; - -import androidx.annotation.Nullable; - -import com.hippo.ehviewer.client.EhUrl; -import com.hippo.yorozuya.NumberUtils; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import com.hippo.ehviewer.client.EhUrl +import com.hippo.yorozuya.NumberUtils +import java.util.regex.Pattern /** - * Like http://exhentai.org/g/1234567/a1b2c3d4e5
+ * Like http://exhentai.org/g/1234567/a1b2c3d4e5

*/ -public final class GalleryDetailUrlParser { +object GalleryDetailUrlParser { + private val URL_STRICT_PATTERN = Pattern.compile( + "https?://(?:" + EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/(?:g|mpv)/(\\d+)/([0-9a-f]{10})", + ) + private val URL_PATTERN = Pattern.compile("(\\d+)/([0-9a-f]{10})(?:[^0-9a-f]|$)") - private static final Pattern URL_STRICT_PATTERN = Pattern.compile( - "https?://(?:" + EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/(?:g|mpv)/(\\d+)/([0-9a-f]{10})"); - - private static final Pattern URL_PATTERN = Pattern.compile( - "(\\d+)/([0-9a-f]{10})(?:[^0-9a-f]|$)"); - - @Nullable - public static Result parse(String url) { - return parse(url, true); - } - - @Nullable - public static Result parse(String url, boolean strict) { - if (url == null) { - return null; - } - - Pattern pattern = strict ? URL_STRICT_PATTERN : URL_PATTERN; - Matcher m = pattern.matcher(url); - if (m.find()) { - Result result = new Result(); - result.gid = NumberUtils.parseLongSafely(m.group(1), -1L); - result.token = m.group(2); - if (result.gid < 0) { - return null; - } - return result; + fun parse(url: String?, strict: Boolean = true): Result? { + url ?: return null + val pattern = if (strict) URL_STRICT_PATTERN else URL_PATTERN + val m = pattern.matcher(url) + return if (m.find()) { + val gid = NumberUtils.parseLongSafely(m.group(1), 0).takeIf { it > 0 } + gid?.let { Result(it, m.group(2)!!) } } else { - return null; + null } } - public static class Result { - public long gid; - public String token; - } + class Result(val gid: Long, val token: String) }