From 9dd87d89ba28a6222ea3abef7f2f792fe56e00ba Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 6 Feb 2018 08:21:36 +0300 Subject: [PATCH] Remove default values in actual functions that have default values in expect Remove suppressions about default values in expect declarations --- libraries/stdlib/common/src/kotlin/TextH.kt | 54 +++++++++++++++---- libraries/stdlib/js/src/kotlin/regex.kt | 6 +-- libraries/stdlib/js/src/kotlin/stringsCode.kt | 12 ++--- .../stdlib/jvm/src/kotlin/text/StringsJVM.kt | 12 ++--- .../stdlib/jvm/src/kotlin/text/regex/Regex.kt | 6 +-- 5 files changed, 61 insertions(+), 29 deletions(-) diff --git a/libraries/stdlib/common/src/kotlin/TextH.kt b/libraries/stdlib/common/src/kotlin/TextH.kt index c13716eacf3..d2755770476 100644 --- a/libraries/stdlib/common/src/kotlin/TextH.kt +++ b/libraries/stdlib/common/src/kotlin/TextH.kt @@ -53,12 +53,25 @@ expect class Regex { fun replace(input: CharSequence, transform: (MatchResult) -> CharSequence): String fun replaceFirst(input: CharSequence, replacement: String): String - // TODO: requires optional parameters - @Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") + /** + * Returns the first match of a regular expression in the [input], beginning at the specified [startIndex]. + * + * @param startIndex An index to start search with, by default 0. Must be not less than zero and not greater than `input.length()` + * @return An instance of [MatchResult] if match was found or `null` otherwise. + */ fun find(input: CharSequence, startIndex: Int = 0): MatchResult? - @Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") + + /** + * Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex]. + */ fun findAll(input: CharSequence, startIndex: Int = 0): Sequence - @Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") + + /** + * Splits the [input] CharSequence around matches of this regular expression. + * + * @param limit Non-negative value specifying the maximum number of substrings the string can be split to. + * Zero by default means no limit is set. + */ fun split(input: CharSequence, limit: Int = 0): List companion object { @@ -103,16 +116,29 @@ public expect fun String.decapitalize(): String public expect fun CharSequence.repeat(n: Int): String -// TODO: requires optional parameters (and named!) -@Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") +/** + * Returns a new string with all occurrences of [oldChar] replaced with [newChar]. + */ expect fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String -@Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") +/** + * Returns a new string obtained by replacing all occurrences of the [oldValue] substring in this string + * with the specified [newValue] string. + */ expect fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String -@Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") +/** + * Returns a new string with the first occurrence of [oldChar] replaced with [newChar]. + */ expect fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String -@Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") +/** + * Returns a new string obtained by replacing the first occurrence of the [oldValue] substring in this string + * with the specified [newValue] string. + */ expect fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String -@Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") +/** + * Returns `true` if this string is equal to [other], optionally ignoring character case. + * + * @param ignoreCase `true` to ignore character case when comparing strings. By default `false`. + */ expect fun String?.equals(other: String?, ignoreCase: Boolean = false): Boolean // From stringsCode.kt @@ -121,7 +147,13 @@ internal inline expect fun String.nativeIndexOf(ch: Char, fromIndex: Int): Int internal inline expect fun String.nativeLastIndexOf(ch: Char, fromIndex: Int): Int expect fun CharSequence.isBlank(): Boolean -@Suppress("EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER") +/** + * Returns `true` if the specified range in this char sequence is equal to the specified range in another char sequence. + * @param thisOffset the start offset in this char sequence of the substring to compare. + * @param other the string against a substring of which the comparison is performed. + * @param otherOffset the start offset in the other char sequence of the substring to compare. + * @param length the length of the substring to compare. + */ expect fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean diff --git a/libraries/stdlib/js/src/kotlin/regex.kt b/libraries/stdlib/js/src/kotlin/regex.kt index 8c2f070242d..5c0f68937c3 100644 --- a/libraries/stdlib/js/src/kotlin/regex.kt +++ b/libraries/stdlib/js/src/kotlin/regex.kt @@ -76,11 +76,11 @@ public actual class Regex actual constructor(pattern: String, options: Set = generateSequence({ find(input, startIndex) }, { match -> match.next() }) + public actual fun findAll(input: CharSequence, startIndex: Int /*= 0*/): Sequence = generateSequence({ find(input, startIndex) }, { match -> match.next() }) /** * Attempts to match the entire [input] CharSequence against the pattern. @@ -144,7 +144,7 @@ public actual class Regex actual constructor(pattern: String, options: Set { + public actual fun split(input: CharSequence, limit: Int /*= 0*/): List { require(limit >= 0) { "Limit must be non-negative, but was $limit" } val matches = findAll(input).let { if (limit == 0) it else it.take(limit - 1) } val result = mutableListOf() diff --git a/libraries/stdlib/js/src/kotlin/stringsCode.kt b/libraries/stdlib/js/src/kotlin/stringsCode.kt index d78f5e7fa1b..9459a828bc6 100644 --- a/libraries/stdlib/js/src/kotlin/stringsCode.kt +++ b/libraries/stdlib/js/src/kotlin/stringsCode.kt @@ -46,7 +46,7 @@ public fun String.matches(regex: String): Boolean { public actual fun CharSequence.isBlank(): Boolean = length == 0 || (if (this is String) this else this.toString()).matches("^[\\s\\xA0]+$") -public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): Boolean = +public actual fun String?.equals(other: String?, ignoreCase: Boolean /*= false*/): Boolean = if (this == null) other == null else if (!ignoreCase) @@ -55,7 +55,7 @@ public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): B other != null && this.toLowerCase() == other.toLowerCase() -public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean +public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean /*= false*/): Boolean = regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase) @@ -107,14 +107,14 @@ public actual fun CharSequence.repeat(n: Int): String { } } -public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String = +public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean /*= false*/): String = nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "gi" else "g"), Regex.escapeReplacement(newValue)) -public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String = +public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean /*= false*/): String = nativeReplace(RegExp(Regex.escape(oldChar.toString()), if (ignoreCase) "gi" else "g"), newChar.toString()) -public actual fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String = +public actual fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean /*= false*/): String = nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "i" else ""), Regex.escapeReplacement(newValue)) -public actual fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String = +public actual fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean /*= false*/): String = nativeReplace(RegExp(Regex.escape(oldChar.toString()), if (ignoreCase) "i" else ""), newChar.toString()) diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt index 79efa4bb52b..55c8fe8e573 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt @@ -39,7 +39,7 @@ internal actual inline fun String.nativeLastIndexOf(str: String, fromIndex: Int) * * @param ignoreCase `true` to ignore character case when comparing strings. By default `false`. */ -public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): Boolean { +public actual fun String?.equals(other: String?, ignoreCase: Boolean /*= false*/): Boolean { if (this === null) return other === null return if (!ignoreCase) @@ -51,7 +51,7 @@ public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): B /** * Returns a new string with all occurrences of [oldChar] replaced with [newChar]. */ -public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String { +public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean /*= false*/): String { if (!ignoreCase) return (this as java.lang.String).replace(oldChar, newChar) else @@ -62,14 +62,14 @@ public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boole * Returns a new string obtained by replacing all occurrences of the [oldValue] substring in this string * with the specified [newValue] string. */ -public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String = +public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean/* = false*/): String = splitToSequence(oldValue, ignoreCase = ignoreCase).joinToString(separator = newValue) /** * Returns a new string with the first occurrence of [oldChar] replaced with [newChar]. */ -public actual fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String { +public actual fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean /*= false*/): String { val index = indexOf(oldChar, ignoreCase = ignoreCase) return if (index < 0) this else this.replaceRange(index, index + 1, newChar.toString()) } @@ -78,7 +78,7 @@ public actual fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: * Returns a new string obtained by replacing the first occurrence of the [oldValue] substring in this string * with the specified [newValue] string. */ -public actual fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { +public actual fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean /*= false*/): String { val index = indexOf(oldValue, ignoreCase = ignoreCase) return if (index < 0) this else this.replaceRange(index, index + oldValue.length, newValue) } @@ -333,7 +333,7 @@ public inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): I * @param otherOffset the start offset in the other char sequence of the substring to compare. * @param length the length of the substring to compare. */ -public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean { +public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean /*= false*/): Boolean { if (this is String && other is String) return this.regionMatches(thisOffset, other, otherOffset, length, ignoreCase) else diff --git a/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt b/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt index 547b9a4260c..e824373f07b 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt @@ -128,12 +128,12 @@ internal constructor(private val nativePattern: Pattern) : Serializable { * @param startIndex An index to start search with, by default 0. Must be not less than zero and not greater than `input.length()` * @return An instance of [MatchResult] if match was found or `null` otherwise. */ - public actual fun find(input: CharSequence, startIndex: Int = 0): MatchResult? = nativePattern.matcher(input).findNext(startIndex, input) + public actual fun find(input: CharSequence, startIndex: Int /*= 0*/): MatchResult? = nativePattern.matcher(input).findNext(startIndex, input) /** * Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex]. */ - public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence = generateSequence({ find(input, startIndex) }, MatchResult::next) + public actual fun findAll(input: CharSequence, startIndex: Int /*= 0*/): Sequence = generateSequence({ find(input, startIndex) }, MatchResult::next) /** * Attempts to match the entire [input] CharSequence against the pattern. @@ -189,7 +189,7 @@ internal constructor(private val nativePattern: Pattern) : Serializable { * @param limit Non-negative value specifying the maximum number of substrings the string can be split to. * Zero by default means no limit is set. */ - public actual fun split(input: CharSequence, limit: Int = 0): List { + public actual fun split(input: CharSequence, limit: Int /*= 0*/): List { require(limit >= 0, { "Limit must be non-negative, but was $limit." } ) return nativePattern.split(input, if (limit == 0) -1 else limit).asList() }