From 47d580cbc5d7fc4020b0e03492dff1fc8bc79616 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 28 Jan 2016 22:04:47 +0300 Subject: [PATCH] Inline-only in kotlin.text --- js/js.libraries/src/core/char.kt | 10 ++ libraries/stdlib/src/kotlin/text/Char.kt | 13 +- libraries/stdlib/src/kotlin/text/CharJVM.kt | 54 +++++--- .../stdlib/src/kotlin/text/CharsetsJVM.kt | 3 +- .../stdlib/src/kotlin/text/StringBuilder.kt | 4 +- .../src/kotlin/text/StringBuilderJVM.kt | 48 ++++--- libraries/stdlib/src/kotlin/text/Strings.kt | 75 +++++++---- .../stdlib/src/kotlin/text/StringsJVM.kt | 117 ++++++++++++------ .../src/kotlin/text/regex/RegexExtensions.kt | 9 +- 9 files changed, 221 insertions(+), 112 deletions(-) diff --git a/js/js.libraries/src/core/char.kt b/js/js.libraries/src/core/char.kt index 4a6de4a3856..a6ee97019a4 100644 --- a/js/js.libraries/src/core/char.kt +++ b/js/js.libraries/src/core/char.kt @@ -22,3 +22,13 @@ public fun Char.isWhitespace(): Boolean = toString().matches("[\\s\\xA0]") @native public fun Char.toLowerCase(): Char = noImpl @native public fun Char.toUpperCase(): Char = noImpl + +/** + * Returns `true` if this character is a Unicode high-surrogate code unit (also known as leading-surrogate code unit). + */ +public fun Char.isHighSurrogate(): Boolean = this in Char.MIN_HIGH_SURROGATE..Char.MAX_HIGH_SURROGATE + +/** + * Returns `true` if this character is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit). + */ +public fun Char.isLowSurrogate(): Boolean = this in Char.MIN_LOW_SURROGATE..Char.MAX_LOW_SURROGATE diff --git a/libraries/stdlib/src/kotlin/text/Char.kt b/libraries/stdlib/src/kotlin/text/Char.kt index b9cbee47e38..51e0a922f01 100644 --- a/libraries/stdlib/src/kotlin/text/Char.kt +++ b/libraries/stdlib/src/kotlin/text/Char.kt @@ -22,7 +22,8 @@ package kotlin.text /** * Concatenates this Char and a String. */ -public operator fun Char.plus(other: String) : String = this.toString() + other +@kotlin.internal.InlineOnly +public inline operator fun Char.plus(other: String) : String = this.toString() + other /** * Returns `true` if this character is equal to the [other] character, optionally ignoring character case. @@ -43,16 +44,6 @@ public fun Char.equals(other: Char, ignoreCase: Boolean = false): Boolean { return false } -/** - * Returns `true` if this character is a Unicode high-surrogate code unit (also known as leading-surrogate code unit). - */ -public fun Char.isHighSurrogate(): Boolean = this in Char.MIN_HIGH_SURROGATE..Char.MAX_HIGH_SURROGATE - -/** - * Returns `true` if this character is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit). - */ -public fun Char.isLowSurrogate(): Boolean = this in Char.MIN_LOW_SURROGATE..Char.MAX_LOW_SURROGATE - /** * Returns `true` if this character is a Unicode surrogate code unit. */ diff --git a/libraries/stdlib/src/kotlin/text/CharJVM.kt b/libraries/stdlib/src/kotlin/text/CharJVM.kt index a8b58a6e00b..c9b31339fd5 100644 --- a/libraries/stdlib/src/kotlin/text/CharJVM.kt +++ b/libraries/stdlib/src/kotlin/text/CharJVM.kt @@ -22,44 +22,52 @@ package kotlin.text /** * Returns `true` if this character (Unicode code point) is defined in Unicode. */ -public fun Char.isDefined(): Boolean = Character.isDefined(this) +@kotlin.internal.InlineOnly +public inline fun Char.isDefined(): Boolean = Character.isDefined(this) /** * Returns `true` if this character is a letter. */ -public fun Char.isLetter(): Boolean = Character.isLetter(this) +@kotlin.internal.InlineOnly +public inline fun Char.isLetter(): Boolean = Character.isLetter(this) /** * Returns `true` if this character is a letter or digit. */ -public fun Char.isLetterOrDigit(): Boolean = Character.isLetterOrDigit(this) +@kotlin.internal.InlineOnly +public inline fun Char.isLetterOrDigit(): Boolean = Character.isLetterOrDigit(this) /** * Returns `true` if this character (Unicode code point) is a digit. */ -public fun Char.isDigit(): Boolean = Character.isDigit(this) +@kotlin.internal.InlineOnly +public inline fun Char.isDigit(): Boolean = Character.isDigit(this) /** * Returns `true` if this character (Unicode code point) should be regarded as an ignorable * character in a Java identifier or a Unicode identifier. */ -public fun Char.isIdentifierIgnorable(): Boolean = Character.isIdentifierIgnorable(this) +@kotlin.internal.InlineOnly +public inline fun Char.isIdentifierIgnorable(): Boolean = Character.isIdentifierIgnorable(this) /** * Returns `true` if this character is an ISO control character. */ -public fun Char.isISOControl(): Boolean = Character.isISOControl(this) +@kotlin.internal.InlineOnly +public inline fun Char.isISOControl(): Boolean = Character.isISOControl(this) /** * Returns `true` if this character (Unicode code point) may be part of a Java identifier as other than the first character. */ -public fun Char.isJavaIdentifierPart(): Boolean = Character.isJavaIdentifierPart(this) +@kotlin.internal.InlineOnly +public inline fun Char.isJavaIdentifierPart(): Boolean = Character.isJavaIdentifierPart(this) /** * Returns `true` if this character is permissible as the first character in a Java identifier. */ -public fun Char.isJavaIdentifierStart(): Boolean = Character.isJavaIdentifierStart(this) +@kotlin.internal.InlineOnly +public inline fun Char.isJavaIdentifierStart(): Boolean = Character.isJavaIdentifierStart(this) /** * Determines whether a character is whitespace according to the Unicode standard. @@ -70,34 +78,40 @@ public fun Char.isWhitespace(): Boolean = Character.isWhitespace(this) || Charac /** * Returns `true` if this character is upper case. */ -public fun Char.isUpperCase(): Boolean = Character.isUpperCase(this) +@kotlin.internal.InlineOnly +public inline fun Char.isUpperCase(): Boolean = Character.isUpperCase(this) /** * Returns `true` if this character is lower case. */ -public fun Char.isLowerCase(): Boolean = Character.isLowerCase(this) +@kotlin.internal.InlineOnly +public inline fun Char.isLowerCase(): Boolean = Character.isLowerCase(this) /** * Converts this character to uppercase. */ -public fun Char.toUpperCase(): Char = Character.toUpperCase(this) +@kotlin.internal.InlineOnly +public inline fun Char.toUpperCase(): Char = Character.toUpperCase(this) /** * Converts this character to lowercase. */ -public fun Char.toLowerCase(): Char = Character.toLowerCase(this) +@kotlin.internal.InlineOnly +public inline fun Char.toLowerCase(): Char = Character.toLowerCase(this) /** * Returns `true` if this character is a titlecase character. */ -public fun Char.isTitleCase(): Boolean = Character.isTitleCase(this) +@kotlin.internal.InlineOnly +public inline fun Char.isTitleCase(): Boolean = Character.isTitleCase(this) /** * Converts this character to titlecase. * * @see Character.toTitleCase */ -public fun Char.toTitleCase(): Char = Character.toTitleCase(this) +@kotlin.internal.InlineOnly +public inline fun Char.toTitleCase(): Char = Character.toTitleCase(this) /** * Returns a value indicating a character's general category. @@ -109,6 +123,18 @@ public val Char.category: CharCategory get() = CharCategory.valueOf(Character.ge */ public val Char.directionality: CharDirectionality get() = CharDirectionality.valueOf(Character.getDirectionality(this).toInt()) +/** + * Returns `true` if this character is a Unicode high-surrogate code unit (also known as leading-surrogate code unit). + */ +@kotlin.internal.InlineOnly +public inline fun Char.isHighSurrogate(): Boolean = Character.isHighSurrogate(this) + +/** + * Returns `true` if this character is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit). + */ +@kotlin.internal.InlineOnly +public inline fun Char.isLowSurrogate(): Boolean = Character.isLowSurrogate(this) + // TODO Provide name for JVM7+ ///** // * Returns the Unicode name of this character, or `null` if the code point of this character is unassigned. diff --git a/libraries/stdlib/src/kotlin/text/CharsetsJVM.kt b/libraries/stdlib/src/kotlin/text/CharsetsJVM.kt index c2506e36a71..1bcf350661c 100644 --- a/libraries/stdlib/src/kotlin/text/CharsetsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/CharsetsJVM.kt @@ -8,7 +8,8 @@ import java.nio.charset.* * * @throws UnsupportedCharsetException If the specified named charset is not available. */ -public fun charset(charsetName: String): Charset = Charset.forName(charsetName) +@kotlin.internal.InlineOnly +public inline fun charset(charsetName: String): Charset = Charset.forName(charsetName) /** * Constant definitions for the standard [charsets](Charset). These diff --git a/libraries/stdlib/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/src/kotlin/text/StringBuilder.kt index 4566a5f6846..0d5d16cc25d 100644 --- a/libraries/stdlib/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/src/kotlin/text/StringBuilder.kt @@ -6,6 +6,7 @@ package kotlin.text /** * Builds new string by populating newly created [StringBuilder] using provided [builderAction] and then converting it to [String]. */ +@kotlin.internal.InlineOnly public inline fun buildString(builderAction: StringBuilder.() -> Unit): String = StringBuilder().apply(builderAction).toString() /** @@ -39,4 +40,5 @@ public fun StringBuilder.append(vararg value: Any?): StringBuilder { * Sets the character at the specified [index] to the specified [value]. */ @kotlin.jvm.JvmVersion -public operator fun StringBuilder.set(index: Int, value: Char): Unit = this.setCharAt(index, value) +@kotlin.internal.InlineOnly +public inline operator fun StringBuilder.set(index: Int, value: Char): Unit = this.setCharAt(index, value) diff --git a/libraries/stdlib/src/kotlin/text/StringBuilderJVM.kt b/libraries/stdlib/src/kotlin/text/StringBuilderJVM.kt index b1c6dec4fd2..35cbb88efbe 100644 --- a/libraries/stdlib/src/kotlin/text/StringBuilderJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringBuilderJVM.kt @@ -10,52 +10,68 @@ private val LINE_SEPARATOR: String by lazy { System.getProperty("line.separator" public fun Appendable.appendln(): Appendable = append(LINE_SEPARATOR) /** Appends value to the given Appendable and line separator after it. */ -public fun Appendable.appendln(value: CharSequence?): Appendable = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun Appendable.appendln(value: CharSequence?): Appendable = append(value).appendln() /** Appends value to the given Appendable and line separator after it. */ -public fun Appendable.appendln(value: Char): Appendable = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun Appendable.appendln(value: Char): Appendable = append(value).appendln() /** Appends a line separator to this StringBuilder. */ public fun StringBuilder.appendln(): StringBuilder = append(LINE_SEPARATOR) /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: StringBuffer?): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: StringBuffer?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: CharSequence?): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: CharSequence?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: String?): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: String?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Any?): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Any?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: StringBuilder?): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: StringBuilder?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: CharArray): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: CharArray): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Char): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Char): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Boolean): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Boolean): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Int): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Int): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Short): StringBuilder = append(value.toInt()).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Short): StringBuilder = append(value.toInt()).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Byte): StringBuilder = append(value.toInt()).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Byte): StringBuilder = append(value.toInt()).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Long): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Long): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Float): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Float): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ -public fun StringBuilder.appendln(value: Double): StringBuilder = append(value).append(LINE_SEPARATOR) +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendln(value: Double): StringBuilder = append(value).appendln() diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index 62d9587023d..470c5ce97a9 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -11,7 +11,7 @@ import kotlin.text.Regex /** * Returns a sub sequence of this char sequence having leading and trailing characters matching the [predicate] trimmed. */ -inline public fun CharSequence.trim(predicate: (Char) -> Boolean): CharSequence { +public inline fun CharSequence.trim(predicate: (Char) -> Boolean): CharSequence { var startIndex = 0 var endIndex = length - 1 var startFound = false @@ -40,13 +40,13 @@ inline public fun CharSequence.trim(predicate: (Char) -> Boolean): CharSequence /** * Returns a string with leading and trailing characters matching the [predicate] trimmed. */ -inline public fun String.trim(predicate: (Char) -> Boolean): String +public inline fun String.trim(predicate: (Char) -> Boolean): String = (this as CharSequence).trim(predicate).toString() /** * Returns a sub sequence of this char sequence having leading characters matching the [predicate] trimmed. */ -inline public fun CharSequence.trimStart(predicate: (Char) -> Boolean): CharSequence { +public inline fun CharSequence.trimStart(predicate: (Char) -> Boolean): CharSequence { for (index in this.indices) if (!predicate(this[index])) return subSequence(index, length) @@ -57,13 +57,13 @@ inline public fun CharSequence.trimStart(predicate: (Char) -> Boolean): CharSequ /** * Returns a string with leading characters matching the [predicate] trimmed. */ -inline public fun String.trimStart(predicate: (Char) -> Boolean): String +public inline fun String.trimStart(predicate: (Char) -> Boolean): String = (this as CharSequence).trimStart(predicate).toString() /** * Returns a sub sequence of this char sequence having trailing characters matching the [predicate] trimmed. */ -inline public fun CharSequence.trimEnd(predicate: (Char) -> Boolean): CharSequence { +public inline fun CharSequence.trimEnd(predicate: (Char) -> Boolean): CharSequence { for (index in this.indices.reversed()) if (!predicate(this[index])) return substring(0, index + 1) @@ -74,7 +74,7 @@ inline public fun CharSequence.trimEnd(predicate: (Char) -> Boolean): CharSequen /** * Returns a string with trailing characters matching the [predicate] trimmed. */ -inline public fun String.trimEnd(predicate: (Char) -> Boolean): String +public inline fun String.trimEnd(predicate: (Char) -> Boolean): String = (this as CharSequence).trimEnd(predicate).toString() /** @@ -115,7 +115,8 @@ public fun CharSequence.trim(): CharSequence = trim { it.isWhitespace() } /** * Returns a string with leading and trailing whitespace trimmed. */ -public fun String.trim(): String = (this as CharSequence).trim().toString() +@kotlin.internal.InlineOnly +public inline fun String.trim(): String = (this as CharSequence).trim().toString() /** * Returns a sub sequence of this char sequence having leading whitespace removed. @@ -125,7 +126,8 @@ public fun CharSequence.trimStart(): CharSequence = trimStart { it.isWhitespace( /** * Returns a string with leading whitespace removed. */ -public fun String.trimStart(): String = (this as CharSequence).trimStart().toString() +@kotlin.internal.InlineOnly +public inline fun String.trimStart(): String = (this as CharSequence).trimStart().toString() /** * Returns a sub sequence of this char sequence having trailing whitespace removed. @@ -135,7 +137,8 @@ public fun CharSequence.trimEnd(): CharSequence = trimEnd { it.isWhitespace() } /** * Returns a string with trailing whitespace removed. */ -public fun String.trimEnd(): String = (this as CharSequence).trimEnd().toString() +@kotlin.internal.InlineOnly +public inline fun String.trimEnd(): String = (this as CharSequence).trimEnd().toString() /** * Returns a char sequence with content of this char sequence padded at the beginning @@ -206,17 +209,20 @@ public fun String.padEnd(length: Int, padChar: Char = ' '): String /** * Returns `true` if this nullable char sequence is either `null` or empty. */ -public fun CharSequence?.isNullOrEmpty(): Boolean = this == null || this.length == 0 +@kotlin.internal.InlineOnly +public inline fun CharSequence?.isNullOrEmpty(): Boolean = this == null || this.length == 0 /** * Returns `true` if this char sequence is empty (contains no characters). */ -public fun CharSequence.isEmpty(): Boolean = length == 0 +@kotlin.internal.InlineOnly +public inline fun CharSequence.isEmpty(): Boolean = length == 0 /** * Returns `true` if this char sequence is not empty. */ -public fun CharSequence.isNotEmpty(): Boolean = length > 0 +@kotlin.internal.InlineOnly +public inline fun CharSequence.isNotEmpty(): Boolean = length > 0 // implemented differently in JVM and JS //public fun String.isBlank(): Boolean = length() == 0 || all { it.isWhitespace() } @@ -225,12 +231,14 @@ public fun CharSequence.isNotEmpty(): Boolean = length > 0 /** * Returns `true` if this char sequence is not empty and contains some characters except of whitespace characters. */ -public fun CharSequence.isNotBlank(): Boolean = !isBlank() +@kotlin.internal.InlineOnly +public inline fun CharSequence.isNotBlank(): Boolean = !isBlank() /** * Returns `true` if this nullable char sequence is either `null` or empty or consists solely of whitespace characters. */ -public fun CharSequence?.isNullOrBlank(): Boolean = this == null || this.isBlank() +@kotlin.internal.InlineOnly +public inline fun CharSequence?.isNullOrBlank(): Boolean = this == null || this.isBlank() /** * Iterator for characters of the given char sequence. @@ -244,7 +252,8 @@ public operator fun CharSequence.iterator(): CharIterator = object : CharIterato } /** Returns the string if it is not `null`, or the empty string otherwise. */ -public fun String?.orEmpty(): String = this ?: "" +@kotlin.internal.InlineOnly +public inline fun String?.orEmpty(): String = this ?: "" /** * Returns the range of valid character indices for this char sequence. @@ -289,7 +298,8 @@ public fun CharSequence.subSequence(range: IntRange): CharSequence = subSequence /** * Returns a substring of chars from a range of this char sequence specified by [startIndex] and [endIndex] indices. */ -public fun CharSequence.substring(startIndex: Int, endIndex: Int = length): String = subSequence(startIndex, endIndex).toString() +@kotlin.internal.InlineOnly +public inline fun CharSequence.substring(startIndex: Int, endIndex: Int = length): String = subSequence(startIndex, endIndex).toString() /** * Returns a substring of chars from a [range] of this char sequence. @@ -389,7 +399,8 @@ public fun CharSequence.replaceRange(startIndex: Int, endIndex: Int, replacement * @param startIndex the index of the first character to be replaced. * @param endIndex the index of the first character after the replacement to keep in the string. */ -public fun String.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): String +@kotlin.internal.InlineOnly +public inline fun String.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): String = (this as CharSequence).replaceRange(startIndex, endIndex, replacement).toString() /** @@ -406,8 +417,9 @@ public fun CharSequence.replaceRange(range: IntRange, replacement: CharSequence) * * The end index of the [range] is included in the part to be replaced. */ -public fun String.replaceRange(range: IntRange, replacement: CharSequence): String - = replaceRange(range.start, range.endInclusive + 1, replacement) +@kotlin.internal.InlineOnly +public inline fun String.replaceRange(range: IntRange, replacement: CharSequence): String + = (this as CharSequence).replaceRange(range, replacement).toString() /** * Returns a char sequence with content of this char sequence where its part at the given range is removed. @@ -437,7 +449,8 @@ public fun CharSequence.removeRange(startIndex: Int, endIndex: Int): CharSequenc * * [endIndex] is not included in the removed part. */ -public fun String.removeRange(startIndex: Int, endIndex: Int): String +@kotlin.internal.InlineOnly +public inline fun String.removeRange(startIndex: Int, endIndex: Int): String = (this as CharSequence).removeRange(startIndex, endIndex).toString() /** @@ -452,7 +465,9 @@ public fun CharSequence.removeRange(range: IntRange): CharSequence = removeRange * * The end index of the [range] is included in the removed part. */ -public fun String.removeRange(range: IntRange): String = removeRange(range.start, range.endInclusive + 1) +@kotlin.internal.InlineOnly +public inline fun String.removeRange(range: IntRange): String + = (this as CharSequence).removeRange(range).toString() /** * If this char sequence starts with the given [prefix], returns a new char sequence @@ -619,27 +634,31 @@ public fun String.replaceBeforeLast(delimiter: String, replacement: String, miss * The [replacement] can consist of any combination of literal text and $-substitutions. To treat the replacement string * literally escape it with the [kotlin.text.Regex.Companion.escapeReplacement] method. */ -public fun CharSequence.replace(regex: Regex, replacement: String): String = regex.replace(this, replacement) +@kotlin.internal.InlineOnly +public inline fun CharSequence.replace(regex: Regex, replacement: String): String = regex.replace(this, replacement) /** * Returns a new string obtained by replacing each substring of this char sequence that matches the given regular expression * with the result of the given function [transform] that takes [MatchResult] and returns a string to be used as a * replacement for that match. */ -public fun CharSequence.replace(regex: Regex, transform: (MatchResult) -> CharSequence): String = regex.replace(this, transform) +@kotlin.internal.InlineOnly +public inline fun CharSequence.replace(regex: Regex, transform: (MatchResult) -> CharSequence): String = regex.replace(this, transform) /** * Replaces the first occurrence of the given regular expression [regex] in this char sequence with specified [replacement] expression. * * @param replacement A replacement expression that can include substitutions. See [Regex.replaceFirst] for details. */ -public fun CharSequence.replaceFirst(regex: Regex, replacement: String): String = regex.replaceFirst(this, replacement) +@kotlin.internal.InlineOnly +public inline fun CharSequence.replaceFirst(regex: Regex, replacement: String): String = regex.replaceFirst(this, replacement) /** * Returns `true` if this char sequence matches the given regular expression. */ -public fun CharSequence.matches(regex: Regex): Boolean = regex.matches(this) +@kotlin.internal.InlineOnly +public inline fun CharSequence.matches(regex: Regex): Boolean = regex.matches(this) /** * Implementation of [regionMatches] for CharSequences. @@ -981,7 +1000,8 @@ public operator fun CharSequence.contains(char: Char, ignoreCase: Boolean = fals /** * Returns `true` if this char sequence contains at least one match of the specified regular expression [regex]. */ -public operator fun CharSequence.contains(regex: Regex): Boolean = regex.containsMatchIn(this) +@kotlin.internal.InlineOnly +public inline operator fun CharSequence.contains(regex: Regex): Boolean = regex.containsMatchIn(this) // rangesDelimitedBy @@ -1139,7 +1159,8 @@ public fun CharSequence.split(vararg delimiters: Char, ignoreCase: Boolean = fal * @param limit Non-negative value specifying the maximum number of substrings to return. * Zero by default means no limit is set. */ -public fun CharSequence.split(regex: Regex, limit: Int = 0): List = regex.split(this, limit) +@kotlin.internal.InlineOnly +public inline fun CharSequence.split(regex: Regex, limit: Int = 0): List = regex.split(this, limit) /** * Splits this char sequence to a sequence of lines delimited by any of the following character sequences: CRLF, LF or CR. diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index f4ebc3d7418..e83302bfe88 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -12,22 +12,26 @@ import java.util.* /** * Returns the index within this string of the first occurrence of the specified character, starting from the specified offset. */ -internal fun String.nativeIndexOf(ch: Char, fromIndex: Int): Int = (this as java.lang.String).indexOf(ch.toInt(), fromIndex) +@kotlin.internal.InlineOnly +internal inline fun String.nativeIndexOf(ch: Char, fromIndex: Int): Int = (this as java.lang.String).indexOf(ch.toInt(), fromIndex) /** * Returns the index within this string of the first occurrence of the specified substring, starting from the specified offset. */ -internal fun String.nativeIndexOf(str: String, fromIndex: Int): Int = (this as java.lang.String).indexOf(str, fromIndex) +@kotlin.internal.InlineOnly +internal inline fun String.nativeIndexOf(str: String, fromIndex: Int): Int = (this as java.lang.String).indexOf(str, fromIndex) /** * Returns the index within this string of the last occurrence of the specified character. */ -internal fun String.nativeLastIndexOf(ch: Char, fromIndex: Int): Int = (this as java.lang.String).lastIndexOf(ch.toInt(), fromIndex) +@kotlin.internal.InlineOnly +internal inline fun String.nativeLastIndexOf(ch: Char, fromIndex: Int): Int = (this as java.lang.String).lastIndexOf(ch.toInt(), fromIndex) /** * Returns the index within this string of the last occurrence of the specified character, starting from the specified offset. */ -internal fun String.nativeLastIndexOf(str: String, fromIndex: Int): Int = (this as java.lang.String).lastIndexOf(str, fromIndex) +@kotlin.internal.InlineOnly +internal inline fun String.nativeLastIndexOf(str: String, fromIndex: Int): Int = (this as java.lang.String).lastIndexOf(str, fromIndex) /** * Returns `true` if this string is equal to [other], optionally ignoring character case. @@ -81,17 +85,20 @@ public fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: B /** * Returns a copy of this string converted to upper case using the rules of the default locale. */ -public fun String.toUpperCase(): String = (this as java.lang.String).toUpperCase() +@kotlin.internal.InlineOnly +public inline fun String.toUpperCase(): String = (this as java.lang.String).toUpperCase() /** * Returns a copy of this string converted to lower case using the rules of the default locale. */ -public fun String.toLowerCase(): String = (this as java.lang.String).toLowerCase() +@kotlin.internal.InlineOnly +public inline fun String.toLowerCase(): String = (this as java.lang.String).toLowerCase() /** * Returns a new character array containing the characters from this string. */ -public fun String.toCharArray(): CharArray = (this as java.lang.String).toCharArray() +@kotlin.internal.InlineOnly +public inline fun String.toCharArray(): CharArray = (this as java.lang.String).toCharArray() /** * Copies characters from this string into the [destination] character array and returns that array. @@ -101,7 +108,8 @@ public fun String.toCharArray(): CharArray = (this as java.lang.String).toCharAr * @param startIndex the start offset (inclusive) of the substring to copy. * @param endIndex the end offset (exclusive) of the substring to copy. */ -public fun String.toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = length): CharArray { +@kotlin.internal.InlineOnly +public inline fun String.toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = length): CharArray { (this as java.lang.String).getChars(startIndex, endIndex, destination, destinationOffset) return destination } @@ -110,13 +118,15 @@ public fun String.toCharArray(destination: CharArray, destinationOffset: Int = 0 * Uses this string as a format string and returns a string obtained by substituting the specified arguments, * using the default locale. */ -public fun String.format(vararg args: Any?): String = java.lang.String.format(this, *args) +@kotlin.internal.InlineOnly +public inline fun String.format(vararg args: Any?): String = java.lang.String.format(this, *args) /** * Uses this string as a format string and returns a string obtained by substituting the specified arguments, using * the specified locale. */ -public fun String.format(locale: Locale, vararg args : Any?) : String = java.lang.String.format(locale, this, *args) +@kotlin.internal.InlineOnly +public inline fun String.format(locale: Locale, vararg args : Any?) : String = java.lang.String.format(locale, this, *args) /** * Splits this char sequence around matches of the given regular expression. @@ -133,12 +143,14 @@ public fun CharSequence.split(regex: Pattern, limit: Int = 0): List /** * Returns a substring of this string starting with the specified index. */ -public fun String.substring(startIndex: Int): String = (this as java.lang.String).substring(startIndex) +@kotlin.internal.InlineOnly +public inline fun String.substring(startIndex: Int): String = (this as java.lang.String).substring(startIndex) /** * Returns the substring of this string starting and ending at the specified indices. */ -public fun String.substring(startIndex: Int, endIndex: Int): String = (this as java.lang.String).substring(startIndex, endIndex) +@kotlin.internal.InlineOnly +public inline fun String.substring(startIndex: Int, endIndex: Int): String = (this as java.lang.String).substring(startIndex, endIndex) /** * Returns `true` if this string starts with the specified prefix. @@ -193,7 +205,8 @@ public fun String(bytes: ByteArray, offset: Int, length: Int, charsetName: Strin * @param length the number of bytes to be converted. * @param charset the character set to use. */ -public fun String(bytes: ByteArray, offset: Int, length: Int, charset: Charset): String = java.lang.String(bytes, offset, length, charset) as String +@kotlin.internal.InlineOnly +public inline fun String(bytes: ByteArray, offset: Int, length: Int, charset: Charset): String = java.lang.String(bytes, offset, length, charset) as String /** * Converts the data from the specified array of bytes to characters using the specified character set @@ -206,7 +219,8 @@ public fun String(bytes: ByteArray, charsetName: String): String = java.lang.Str * Converts the data from the specified array of bytes to characters using the specified character set * and returns the conversion result as a string. */ -public fun String(bytes: ByteArray, charset: Charset): String = java.lang.String(bytes, charset) as String +@kotlin.internal.InlineOnly +public inline fun String(bytes: ByteArray, charset: Charset): String = java.lang.String(bytes, charset) as String /** * Converts the data from a portion of the specified array of bytes to characters using the UTF-8 character set @@ -216,53 +230,63 @@ public fun String(bytes: ByteArray, charset: Charset): String = java.lang.String * @param offset the offset in the array of the data to be converted. * @param length the number of bytes to be converted. */ -public fun String(bytes: ByteArray, offset: Int, length: Int): String = java.lang.String(bytes, offset, length, Charsets.UTF_8) as String +@kotlin.internal.InlineOnly +public inline fun String(bytes: ByteArray, offset: Int, length: Int): String = java.lang.String(bytes, offset, length, Charsets.UTF_8) as String /** * Converts the data from the specified array of bytes to characters using the UTF-8 character set * and returns the conversion result as a string. */ -public fun String(bytes: ByteArray): String = java.lang.String(bytes, Charsets.UTF_8) as String +@kotlin.internal.InlineOnly +public inline fun String(bytes: ByteArray): String = java.lang.String(bytes, Charsets.UTF_8) as String /** * Converts the characters in the specified array to a string. */ -public fun String(chars: CharArray): String = java.lang.String(chars) as String +@kotlin.internal.InlineOnly +public inline fun String(chars: CharArray): String = java.lang.String(chars) as String /** * Converts the characters from a portion of the specified array to a string. */ -public fun String(chars: CharArray, offset: Int, length: Int): String = java.lang.String(chars, offset, length) as String +@kotlin.internal.InlineOnly +public inline fun String(chars: CharArray, offset: Int, length: Int): String = java.lang.String(chars, offset, length) as String /** * Converts the code points from a portion of the specified Unicode code point array to a string. */ -public fun String(codePoints: IntArray, offset: Int, length: Int): String = java.lang.String(codePoints, offset, length) as String +@kotlin.internal.InlineOnly +public inline fun String(codePoints: IntArray, offset: Int, length: Int): String = java.lang.String(codePoints, offset, length) as String /** * Converts the contents of the specified StringBuffer to a string. */ -public fun String(stringBuffer: java.lang.StringBuffer): String = java.lang.String(stringBuffer) as String +@kotlin.internal.InlineOnly +public inline fun String(stringBuffer: java.lang.StringBuffer): String = java.lang.String(stringBuffer) as String /** * Converts the contents of the specified StringBuilder to a string. */ -public fun String(stringBuilder: java.lang.StringBuilder): String = java.lang.String(stringBuilder) as String +@kotlin.internal.InlineOnly +public inline fun String(stringBuilder: java.lang.StringBuilder): String = java.lang.String(stringBuilder) as String /** * Returns the character (Unicode code point) at the specified index. */ -public fun String.codePointAt(index: Int): Int = (this as java.lang.String).codePointAt(index) +@kotlin.internal.InlineOnly +public inline fun String.codePointAt(index: Int): Int = (this as java.lang.String).codePointAt(index) /** * Returns the character (Unicode code point) before the specified index. */ -public fun String.codePointBefore(index: Int): Int = (this as java.lang.String).codePointBefore(index) +@kotlin.internal.InlineOnly +public inline fun String.codePointBefore(index: Int): Int = (this as java.lang.String).codePointBefore(index) /** * Returns the number of Unicode code points in the specified text range of this String. */ -public fun String.codePointCount(beginIndex: Int, endIndex: Int): Int = (this as java.lang.String).codePointCount(beginIndex, endIndex) +@kotlin.internal.InlineOnly +public inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int = (this as java.lang.String).codePointCount(beginIndex, endIndex) /** * Compares two strings lexicographically, optionally ignoring case differences. @@ -283,17 +307,20 @@ public fun String.concat(other: String): String = (this as java.lang.String).con /** * Returns `true` if this string is equal to the contents of the specified CharSequence. */ -public fun String.contentEquals(charSequence: CharSequence): Boolean = (this as java.lang.String).contentEquals(charSequence) +@kotlin.internal.InlineOnly +public inline fun String.contentEquals(charSequence: CharSequence): Boolean = (this as java.lang.String).contentEquals(charSequence) /** * Returns `true` if this string is equal to the contents of the specified StringBuffer. */ -public fun String.contentEquals(stringBuilder: StringBuffer): Boolean = (this as java.lang.String).contentEquals(stringBuilder) +@kotlin.internal.InlineOnly +public inline fun String.contentEquals(stringBuilder: StringBuffer): Boolean = (this as java.lang.String).contentEquals(stringBuilder) /** * Returns a canonical representation for this string object. */ -public fun String.intern(): String = (this as java.lang.String).intern() +@kotlin.internal.InlineOnly +public inline fun String.intern(): String = (this as java.lang.String).intern() /** * Returns `true` if this string is empty or consists solely of whitespace characters. @@ -303,7 +330,8 @@ public fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[i /** * Returns the index within this string that is offset from the given [index] by [codePointOffset] code points. */ -public fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int = (this as java.lang.String).offsetByCodePoints(index, codePointOffset) +@kotlin.internal.InlineOnly +public inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int = (this as java.lang.String).offsetByCodePoints(index, codePointOffset) /** * Returns `true` if the specified range in this char sequence is equal to the specified range in another char sequence. @@ -335,53 +363,62 @@ public fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int /** * Returns a copy of this string converted to lower case using the rules of the specified locale. */ -public fun String.toLowerCase(locale: java.util.Locale): String = (this as java.lang.String).toLowerCase(locale) +@kotlin.internal.InlineOnly +public inline fun String.toLowerCase(locale: java.util.Locale): String = (this as java.lang.String).toLowerCase(locale) /** * Returns a copy of this string converted to upper case using the rules of the specified locale. */ -public fun String.toUpperCase(locale: java.util.Locale): String = (this as java.lang.String).toUpperCase(locale) +@kotlin.internal.InlineOnly +public inline fun String.toUpperCase(locale: java.util.Locale): String = (this as java.lang.String).toUpperCase(locale) /** * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. */ -public fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this) +@kotlin.internal.InlineOnly +public inline fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this) /** * Parses the string as a signed [Byte] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. */ -public fun String.toByte(): Byte = java.lang.Byte.parseByte(this) +@kotlin.internal.InlineOnly +public inline fun String.toByte(): Byte = java.lang.Byte.parseByte(this) /** * Parses the string as a [Short] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. */ -public fun String.toShort(): Short = java.lang.Short.parseShort(this) +@kotlin.internal.InlineOnly +public inline fun String.toShort(): Short = java.lang.Short.parseShort(this) /** * Parses the string as an [Int] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. */ -public fun String.toInt(): Int = java.lang.Integer.parseInt(this) +@kotlin.internal.InlineOnly +public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this) /** * Parses the string as a [Long] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. */ -public fun String.toLong(): Long = java.lang.Long.parseLong(this) +@kotlin.internal.InlineOnly +public inline fun String.toLong(): Long = java.lang.Long.parseLong(this) /** * Parses the string as a [Float] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. */ -public fun String.toFloat(): Float = java.lang.Float.parseFloat(this) +@kotlin.internal.InlineOnly +public inline fun String.toFloat(): Float = java.lang.Float.parseFloat(this) /** * Parses the string as a [Double] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. */ -public fun String.toDouble(): Double = java.lang.Double.parseDouble(this) +@kotlin.internal.InlineOnly +public inline fun String.toDouble(): Double = java.lang.Double.parseDouble(this) /** * Encodes the contents of this string using the specified character set and returns the resulting byte array. @@ -392,7 +429,8 @@ public fun String.toByteArray(charset: String): ByteArray = (this as java.lang.S /** * Encodes the contents of this string using the specified character set and returns the resulting byte array. */ -public fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray = (this as java.lang.String).getBytes(charset) +@kotlin.internal.InlineOnly +public inline fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray = (this as java.lang.String).getBytes(charset) @@ -401,7 +439,8 @@ public fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray = (t * with the specified [flags] from [Pattern] or'd together * so that strings can be split or matched on. */ -public fun String.toPattern(flags: Int = 0): java.util.regex.Pattern { +@kotlin.internal.InlineOnly +public inline fun String.toPattern(flags: Int = 0): java.util.regex.Pattern { return java.util.regex.Pattern.compile(this, flags) } diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt index d48b7965ec2..0e2ab1a1461 100644 --- a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt +++ b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt @@ -6,17 +6,20 @@ package kotlin.text /** * Converts the string into a regular expression [Regex] with the default options. */ -public fun String.toRegex(): Regex = Regex(this) +@kotlin.internal.InlineOnly +public inline fun String.toRegex(): Regex = Regex(this) /** * Converts the string into a regular expression [Regex] with the specified single [option]. */ -public fun String.toRegex(option: RegexOption): Regex = Regex(this, option) +@kotlin.internal.InlineOnly +public inline fun String.toRegex(option: RegexOption): Regex = Regex(this, option) /** * Converts the string into a regular expression [Regex] with the specified set of [options]. */ -public fun String.toRegex(options: Set): Regex = Regex(this, options) +@kotlin.internal.InlineOnly +public inline fun String.toRegex(options: Set): Regex = Regex(this, options) /** * Converts this [Pattern] to an instance of [Regex].