Skip to content

Commit

Permalink
Fix/Simplify regex usage (square#5059)
Browse files Browse the repository at this point in the history
* Fix/Simplify regex usage

* Cleanup
  • Loading branch information
yschimke authored and squarejesse committed May 18, 2019
1 parent fbfb5f8 commit dcf5c8c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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()}")
}
Expand Down
4 changes: 2 additions & 2 deletions okcurl/src/main/java/okhttp3/curl/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
15 changes: 6 additions & 9 deletions okhttp/src/main/java/okhttp3/internal/cache/DiskLruCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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\"" }
}

/**
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}

/**
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit dcf5c8c

Please sign in to comment.