Repository: square/okhttp
File: okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/-MediaTypeCommon.kt (Kotlin)

Code before:
  for (i in parameterNamesAndValues.indices step 2) {
    if (parameterNamesAndValues[i].equals(name, ignoreCase = true)) {
      return parameterNamesAndValues[i + 1]
    }
  }
  return null
}

internal fun MediaType.commonEquals(other: Any?): Boolean = other is MediaType && other.mediaType == mediaType

internal fun MediaType.commonToString(): String = mediaType

internal fun MediaType.commonHashCode(): Int = mediaType.hashCode()

private const val TOKEN = "([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)"
private const val QUOTED = "\"([^\"]*)\""
private val TYPE_SUBTYPE = Regex("$TOKEN/$TOKEN")
private val PARAMETER = Regex(";\\s*(?:$TOKEN=(?:$TOKEN|$QUOTED))?")

/**
 * Returns a media type for this string.
 *
 * @throws IllegalArgumentException if this is not a well-formed media type.
 */
internal fun String.commonToMediaType(): MediaType {
  val typeSubtype = TYPE_SUBTYPE.matchAt(this, 0)
      ?: throw IllegalArgumentException("No subtype found for: \"$this\"")
  val type = typeSubtype.groupValues[1].lowercase()
  val subtype = typeSubtype.groupValues[2].lowercase()

  val parameterNamesAndValues = mutableListOf<String>()
  var s = typeSubtype.range.last + 1
  while (s < length) {
    val parameter = PARAMETER.matchAt(this, s)
    require(parameter != null) {
      "Parameter is not formatted correctly: \"${substring(s)}\" for: \"$this\""
    }

    val name = parameter.groups[1]?.value
    if (name == null) {
      s = parameter.range.last + 1
      continue
    }

    val token = parameter.groups[2]?.value
    val value =
      when {
        token == null -> {
          // Value is "double-quoted". That's valid and our regex group already strips the quotes.
          parameter.groups[3]!!.value
        }

Review comment (suggestion):
Seems like Spotless wants
```suggestion
  val typeSubtype =
    TYPE_SUBTYPE.matchAt(this, 0)
```

Code after:
  for (i in parameterNamesAndValues.indices step 2) {
    if (parameterNamesAndValues[i].equals(name, ignoreCase = true)) {
      return parameterNamesAndValues[i + 1]
    }
  }
  return null
}

internal fun MediaType.commonEquals(other: Any?): Boolean = other is MediaType && other.mediaType == mediaType

internal fun MediaType.commonToString(): String = mediaType

internal fun MediaType.commonHashCode(): Int = mediaType.hashCode()

private const val TOKEN = "([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)"
private const val QUOTED = "\"([^\"]*)\""
private val TYPE_SUBTYPE = Regex("$TOKEN/$TOKEN")
private val PARAMETER = Regex(";\\s*(?:$TOKEN=(?:$TOKEN|$QUOTED))?")

/**
 * Returns a media type for this string.
 *
 * @throws IllegalArgumentException if this is not a well-formed media type.
 */
internal fun String.commonToMediaType(): MediaType {
  val typeSubtype =
    TYPE_SUBTYPE.matchAt(this, 0)
      ?: throw IllegalArgumentException("No subtype found for: \"$this\"")
  val type = typeSubtype.groupValues[1].lowercase()
  val subtype = typeSubtype.groupValues[2].lowercase()

  val parameterNamesAndValues = mutableListOf<String>()
  var s = typeSubtype.range.last + 1
  while (s < length) {
    val parameter = PARAMETER.matchAt(this, s)
    require(parameter != null) {
      "Parameter is not formatted correctly: \"${substring(s)}\" for: \"$this\""
    }

    val name = parameter.groups[1]?.value
    if (name == null) {
      s = parameter.range.last + 1
      continue
    }

    val token = parameter.groups[2]?.value
    val value =
      when {
        token == null -> {
          // Value is "double-quoted". That's valid and our regex group already strips the quotes.
          parameter.groups[3]!!.value