1
0
mirror of https://github.com/square/okhttp.git synced 2025-07-31 05:04:26 +03:00

Fix/Simplify regex usage (#5059)

* Fix/Simplify regex usage

* Cleanup
This commit is contained in:
Yuri Schimke
2019-05-18 11:33:34 +01:00
committed by Jesse Wilson
parent fbfb5f8461
commit dcf5c8ceb6
4 changed files with 11 additions and 14 deletions

View File

@ -705,7 +705,7 @@ class MockWebServer : ExternalResource(), Closeable {
.url("$scheme://$authority/")
.headers(request.headers)
.build()
val statusParts = response.getStatus().split(" ".toRegex(), 3)
val statusParts = response.getStatus().split(' ', limit = 3)
val fancyResponse = Response.Builder()
.code(Integer.parseInt(statusParts[1]))
.message(statusParts[2])
@ -970,7 +970,7 @@ class MockWebServer : ExternalResource(), Closeable {
return
}
val http2Headers = ArrayList<Header>()
val statusParts = response.getStatus().split(" ".toRegex(), 3).toTypedArray()
val statusParts = response.getStatus().split(' ', limit = 3)
if (statusParts.size < 2) {
throw AssertionError("Unexpected status: ${response.getStatus()}")
}

View File

@ -191,7 +191,7 @@ class Main : Runnable {
}
for (header in headers.orEmpty()) {
val parts = header.split(":".toRegex(), 2).toTypedArray()
val parts = header.split(':', limit = 2)
request.header(parts[0], parts[1])
}
referer?.let {
@ -205,7 +205,7 @@ class Main : Runnable {
private fun mediaType(): MediaType? {
val mimeType = headers?.let {
for (header in it) {
val parts = header.split(":".toRegex()).toTypedArray()
val parts = header.split(':')
if ("Content-Type".equals(parts[0], ignoreCase = true)) {
it.remove(header)
return@let parts[1].trim()

View File

@ -45,7 +45,7 @@ object DnsRecordCodec {
writeShort(0) // additional
val nameBuf = Buffer()
val labels = host.split('.').dropLastWhile { it.isEmpty() }.toTypedArray()
val labels = host.split('.').dropLastWhile { it.isEmpty() }
for (label in labels) {
val utf8ByteCount = label.utf8Size()
if (utf8ByteCount != label.length.toLong()) {

View File

@ -39,7 +39,6 @@ import java.util.concurrent.Executor
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
/**
* A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key
@ -317,8 +316,7 @@ class DiskLruCache internal constructor(
when {
secondSpace != -1 && firstSpace == CLEAN.length && line.startsWith(CLEAN) -> {
val parts = line.substring(secondSpace + 1)
.split(" ".toRegex())
.toTypedArray()
.split(' ')
entry.readable = true
entry.currentEditor = null
entry.setLengths(parts)
@ -670,8 +668,7 @@ class DiskLruCache internal constructor(
}
private fun validateKey(key: String) {
val matcher = LEGAL_KEY_PATTERN.matcher(key)
require(matcher.matches()) { "keys must match regex [a-z0-9_-]{1,120}: \"$key\"" }
require(LEGAL_KEY_PATTERN.matches(key)) { "keys must match regex [a-z0-9_-]{1,120}: \"$key\"" }
}
/**
@ -905,7 +902,7 @@ class DiskLruCache internal constructor(
/** Set lengths using decimal numbers like "10123". */
@Throws(IOException::class)
internal fun setLengths(strings: Array<String>) {
internal fun setLengths(strings: List<String>) {
if (strings.size != valueCount) {
throw invalidLengths(strings)
}
@ -928,8 +925,8 @@ class DiskLruCache internal constructor(
}
@Throws(IOException::class)
private fun invalidLengths(strings: Array<String>): IOException {
throw IOException("unexpected journal line: ${strings.contentToString()}")
private fun invalidLengths(strings: List<String>): IOException {
throw IOException("unexpected journal line: $strings")
}
/**
@ -970,7 +967,7 @@ class DiskLruCache internal constructor(
@JvmField val MAGIC = "libcore.io.DiskLruCache"
@JvmField val VERSION_1 = "1"
@JvmField val ANY_SEQUENCE_NUMBER: Long = -1
@JvmField val LEGAL_KEY_PATTERN = Pattern.compile("[a-z0-9_-]{1,120}")
@JvmField val LEGAL_KEY_PATTERN = "[a-z0-9_-]{1,120}".toRegex()
@JvmField val CLEAN = "CLEAN"
@JvmField val DIRTY = "DIRTY"
@JvmField val REMOVE = "REMOVE"