Skip to content

Commit

Permalink
Increase regex readability (#391)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Krzemiński <[email protected]>
  • Loading branch information
Vampire and krzema12 authored Feb 6, 2025
1 parent 5fd5278 commit b28044a
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ class Anchor(val value: String) {
'*',
'&',
)
private val SPACES_PATTERN = Regex("\\s")
private val SPACES_PATTERN = Regex("""\s""")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import okio.ByteString.Companion.decodeBase64
import it.krzeminski.snakeyaml.engine.kmp.constructor.ConstructScalar
import it.krzeminski.snakeyaml.engine.kmp.nodes.Node

private val SPACES_PATTERN = Regex("""\s""")

/**
* Create instances bytes for binary
*/
Expand All @@ -25,7 +27,7 @@ class ConstructYamlBinary : ConstructScalar() {
// Ignore white spaces for base64 encoded scalar
// TODO decodeBase64() doesn't seem to require removing whitespace - perhaps this can be removed?
// I'm leaving it in because I'm not confident about edge case test coverage for whitespaces.
val noWhiteSpaces = constructScalar(node).replace("\\s".toRegex(), "")
val noWhiteSpaces = constructScalar(node).replace(SPACES_PATTERN, "")
return noWhiteSpaces.decodeBase64()?.toByteArray() ?: byteArrayOf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,6 @@ class Emitter(

private const val SPACE = " "

private val HANDLE_FORMAT = Regex("^![-_\\w]*!$")
private val HANDLE_FORMAT = Regex("""^![-_\w]*!$""")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class CoreScalarResolver : BaseScalarResolver(
* Be aware that this regex will also match integers.
*/
val FLOAT = Regex(
"^([-+]?(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?)" + // float
"|([-+]?\\.(?:inf|Inf|INF))" + // infinity
"|(\\.(?:nan|NaN|NAN))$", // not a number
"""^(?:([-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?)""" + // float
"""|([-+]?\.(?:inf|Inf|INF))""" + // infinity
"""|(\.(?:nan|NaN|NAN)))$""", // not a number
)

/** Integer as defined in Core */
@JvmField
val INT = Regex(
"^(([-+]?[0-9]+)" + // (base 10)
"^(?:([-+]?[0-9]+)" + // (base 10)
"|(0o[0-7]+)" + // (base 8)
"|(0x[0-9a-fA-F]+))$", // (base 16)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class JsonScalarResolver : BaseScalarResolver(
/**
* Float as defined in JSON (Number which is Float)
*/
val FLOAT = Regex("^(-?(0|[1-9][0-9]*)(\\.[0-9]*)?([eE][-+]?[0-9]+)?)|(-?\\.inf)|(\\.nan)$")
val FLOAT = Regex("""^(?:(-?(0|[1-9][0-9]*)(\.[0-9]*)?([eE][-+]?[0-9]+)?)|(-?\.inf)|(\.nan))$""")

/**
* Integer as defined in JSON (Number which is Integer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fun stringFromResources(@Language("file-reference") path: String): String {
// In this particular library, we produce uniform line breaks (\n)
// on all OSes, hence it's desired to normalize them even if in
// the resource file, we have a different kind of line breaks.
.replace(Regex("\\r\\n?"), "\n")
.replace(Regex("""\r\n?"""), "\n")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class InheritedTokensTest: FunSpec({
// This is needed because Java's split omits the last empty line -
// looks like Kotlin's implementation is correct.
.trimEnd()
val split = tokenFileData.split(Regex("\\s+"))
val split = tokenFileData.split(Regex("""\s+"""))
val tokens2 = buildList {
addAll(split)
}
Expand Down

0 comments on commit b28044a

Please sign in to comment.