Implement CharSequence.regionMatches and generalize depending String extensions to take CharSequence.

Add hidden declarations for replaced overloads with String receiver in hand-written code.
This commit is contained in:
Ilya Gorbunov
2015-10-14 05:22:44 +03:00
parent 47560d3db1
commit e155e426c2
5 changed files with 132 additions and 346 deletions
+2 -8
View File
@@ -54,14 +54,8 @@ public fun String?.equals(anotherString: String?, ignoreCase: Boolean = false):
anotherString != null && this.toLowerCase() == anotherString.toLowerCase()
public fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean {
if ((otherOffset < 0) || (thisOffset < 0) || (thisOffset > length() - length)
|| (otherOffset > other.length() - length)) {
return false;
}
return substring(thisOffset, thisOffset + length).equals(other.substring(otherOffset, otherOffset + length), ignoreCase)
}
public fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean
= regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase)
/**
+97 -20
View File
@@ -533,6 +533,22 @@ public fun CharSequence.replaceFirst(regex: Regex, replacement: String): String
*/
public fun CharSequence.matches(regex: Regex): Boolean = regex.matches(this)
/**
* Implementation of [regionMatches] for CharSequences.
* Invoked when it's already known that arguments are not Strings, so that no additional type checks are performed.
*/
internal fun CharSequence.regionMatchesImpl(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean): Boolean {
if ((otherOffset < 0) || (thisOffset < 0) || (thisOffset > this.length - length)
|| (otherOffset > other.length - length)) {
return false;
}
for (index in 0..length-1) {
if (!this[thisOffset + index].equals(other[otherOffset + index], ignoreCase))
return false
}
return true
}
/**
* Returns `true` if this string starts with the specified character.
@@ -546,6 +562,35 @@ public fun CharSequence.startsWith(char: Char, ignoreCase: Boolean = false): Boo
public fun CharSequence.endsWith(char: Char, ignoreCase: Boolean = false): Boolean =
this.length() > 0 && this[lastIndex].equals(char, ignoreCase)
/**
* Returns `true` if this CharSequence starts with the specified prefix.
*/
public fun CharSequence.startsWith(prefix: CharSequence, ignoreCase: Boolean = false): Boolean {
if (!ignoreCase && this is String && prefix is String)
return this.startsWith(prefix)
else
return regionMatchesImpl(0, prefix, 0, prefix.length(), ignoreCase)
}
/**
* Returns `true` if a substring of this CharSequence starting at the specified offset [thisOffset] starts with the specified prefix.
*/
public fun CharSequence.startsWith(prefix: CharSequence, thisOffset: Int, ignoreCase: Boolean = false): Boolean {
if (!ignoreCase && this is String && prefix is String)
return this.startsWith(prefix, thisOffset)
else
return regionMatchesImpl(thisOffset, prefix, 0, prefix.length(), ignoreCase)
}
/**
* Returns `true` if this CharSequence ends with the specified suffix.
*/
public fun CharSequence.endsWith(suffix: CharSequence, ignoreCase: Boolean = false): Boolean {
if (!ignoreCase && this is String && suffix is String)
return this.endsWith(suffix)
else
return regionMatchesImpl(length() - suffix.length(), suffix, 0, suffix.length(), ignoreCase)
}
// common prefix and suffix
@@ -637,19 +682,47 @@ public fun CharSequence.lastIndexOfAny(chars: CharArray, startIndex: Int = lastI
findAnyOf(chars, startIndex, ignoreCase, last = true)?.first ?: -1
private fun CharSequence.indexOf(other: CharSequence, startIndex: Int, endIndex: Int, ignoreCase: Boolean, last: Boolean = false): Int {
val indices = if (!last)
startIndex.coerceAtLeast(0)..endIndex.coerceAtMost(length())
else
startIndex.coerceAtMost(lastIndex) downTo endIndex.coerceAtLeast(0)
private fun String.findAnyOf(strings: Collection<String>, startIndex: Int, ignoreCase: Boolean, last: Boolean): Pair<Int, String>? {
if (!ignoreCase && strings.size() == 1) {
if (this is String && other is String) { // smart cast
for (index in indices) {
if (other.regionMatches(0, this, index, other.length(), ignoreCase))
return index
}
} else {
for (index in indices) {
if (other.regionMatchesImpl(0, this, index, other.length(), ignoreCase))
return index
}
}
return -1
}
private fun CharSequence.findAnyOf(strings: Collection<String>, startIndex: Int, ignoreCase: Boolean, last: Boolean): Pair<Int, String>? {
if (!ignoreCase && strings.size == 1) {
val string = strings.single()
val index = if (!last) nativeIndexOf(string, startIndex) else nativeLastIndexOf(string, startIndex)
val index = if (!last) indexOf(string, startIndex) else lastIndexOf(string, startIndex)
return if (index < 0) null else index to string
}
val indices = if (!last) Math.max(startIndex, 0)..length() else Math.min(startIndex, lastIndex) downTo 0
for (index in indices) {
val matchingString = strings.firstOrNull { it.regionMatches(0, this, index, it.length(), ignoreCase) }
if (matchingString != null)
return index to matchingString
val indices = if (!last) startIndex.coerceAtLeast(0)..length() else startIndex.coerceAtMost(lastIndex) downTo 0
if (this is String) {
for (index in indices) {
val matchingString = strings.firstOrNull { it.regionMatches(0, this, index, it.length(), ignoreCase) }
if (matchingString != null)
return index to matchingString
}
} else {
for (index in indices) {
val matchingString = strings.firstOrNull { it.regionMatchesImpl(0, this, index, it.length(), ignoreCase) }
if (matchingString != null)
return index to matchingString
}
}
return null
@@ -666,7 +739,7 @@ private fun String.findAnyOf(strings: Collection<String>, startIndex: Int, ignor
* the beginning to the end of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
public fun String.findAnyOf(strings: Collection<String>, startIndex: Int = 0, ignoreCase: Boolean = false): Pair<Int, String>? =
public fun CharSequence.findAnyOf(strings: Collection<String>, startIndex: Int = 0, ignoreCase: Boolean = false): Pair<Int, String>? =
findAnyOf(strings, startIndex, ignoreCase, last = false)
/**
@@ -681,7 +754,7 @@ public fun String.findAnyOf(strings: Collection<String>, startIndex: Int = 0, ig
* the end toward the beginning of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
public fun String.findLastAnyOf(strings: Collection<String>, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Pair<Int, String>? =
public fun CharSequence.findLastAnyOf(strings: Collection<String>, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Pair<Int, String>? =
findAnyOf(strings, startIndex, ignoreCase, last = true)
/**
@@ -695,7 +768,7 @@ public fun String.findLastAnyOf(strings: Collection<String>, startIndex: Int = l
* the beginning to the end of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
public fun String.indexOfAny(strings: Collection<String>, startIndex: Int = 0, ignoreCase: Boolean = false): Int =
public fun CharSequence.indexOfAny(strings: Collection<String>, startIndex: Int = 0, ignoreCase: Boolean = false): Int =
findAnyOf(strings, startIndex, ignoreCase, last = false)?.first ?: -1
/**
@@ -710,7 +783,7 @@ public fun String.indexOfAny(strings: Collection<String>, startIndex: Int = 0, i
* the end toward the beginning of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
public fun String.lastIndexOfAny(strings: Collection<String>, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int =
public fun CharSequence.lastIndexOfAny(strings: Collection<String>, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int =
findAnyOf(strings, startIndex, ignoreCase, last = true)?.first ?: -1
@@ -735,9 +808,9 @@ public fun CharSequence.indexOf(char: Char, startIndex: Int = 0, ignoreCase: Boo
* @param ignoreCase `true` to ignore character case when matching a string. By default `false`.
* @returns An index of the first occurrence of [string] or -1 if none is found.
*/
public fun String.indexOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int {
return if (ignoreCase)
indexOfAny(listOf(string), startIndex, ignoreCase)
public fun CharSequence.indexOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int {
return if (ignoreCase || this !is String)
indexOf(string, startIndex, length(), ignoreCase)
else
nativeIndexOf(string, startIndex)
}
@@ -763,9 +836,9 @@ public fun CharSequence.lastIndexOf(char: Char, startIndex: Int = lastIndex, ign
* @param ignoreCase `true` to ignore character case when matching a string. By default `false`.
* @returns An index of the first occurrence of [string] or -1 if none is found.
*/
public fun String.lastIndexOf(string: String, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int {
return if (ignoreCase)
lastIndexOfAny(listOf(string), startIndex, ignoreCase)
public fun CharSequence.lastIndexOf(string: String, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int {
return if (ignoreCase || this !is String)
indexOf(string, startIndex, 0, ignoreCase, last = true)
else
nativeLastIndexOf(string, startIndex)
}
@@ -775,8 +848,12 @@ public fun String.lastIndexOf(string: String, startIndex: Int = lastIndex, ignor
*
* @param ignoreCase `true` to ignore character case when comparing strings. By default `false`.
*/
public operator fun String.contains(seq: CharSequence, ignoreCase: Boolean = false): Boolean =
indexOf(seq.toString(), ignoreCase = ignoreCase) >= 0
public operator fun CharSequence.contains(other: CharSequence, ignoreCase: Boolean = false): Boolean =
if (other is String)
indexOf(other, ignoreCase = ignoreCase) >= 0
else
indexOf(other, 0, length(), ignoreCase) >= 0
/**
@@ -329,39 +329,6 @@ public fun String.removeRange(firstIndex: Int, lastIndex: Int): String {
public fun String.removeRange(range: IntRange): String = removeRange(range.start, range.end + 1)
/*
/**
* If this string starts with the given [prefix], returns a copy of this string
* with the prefix removed. Otherwise, returns this string.
*/
public fun String.removePrefix(prefix: String): String {
if (startsWith(prefix)) {
return substring(prefix.length())
}
return this
}
/**
* If this string ends with the given [suffix], returns a copy of this string
* with the suffix removed. Otherwise, returns this string.
*/
public fun String.removeSuffix(suffix: String): String {
if (endsWith(suffix)) {
return substring(0, length() - suffix.length())
}
return this
}
/**
* Removes from a string both the given [prefix] and [suffix] if and only if
* it starts with the [prefix] and ends with the [suffix].
* Otherwise returns this string unchanged.
*/
public fun String.removeSurrounding(prefix: String, suffix: String): String {
if (startsWith(prefix) && endsWith(suffix)) {
return substring(prefix.length(), length() - suffix.length())
}
return this
}
/**
* Removes the given [delimiter] string from both the start and the end of this string
@@ -541,7 +508,6 @@ public fun String.lastIndexOfAny(chars: CharArray, startIndex: Int = lastIndex,
findAnyOf(chars, startIndex, ignoreCase, last = true)?.first ?: -1
/*
private fun String.findAnyOf(strings: Collection<String>, startIndex: Int, ignoreCase: Boolean, last: Boolean): Pair<Int, String>? {
if (!ignoreCase && strings.size() == 1) {
val string = strings.single()
@@ -570,6 +536,7 @@ private fun String.findAnyOf(strings: Collection<String>, startIndex: Int, ignor
* the beginning to the end of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.findAnyOf(strings: Collection<String>, startIndex: Int = 0, ignoreCase: Boolean = false): Pair<Int, String>? =
findAnyOf(strings, startIndex, ignoreCase, last = false)
@@ -585,6 +552,7 @@ public fun String.findAnyOf(strings: Collection<String>, startIndex: Int = 0, ig
* the end toward the beginning of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.findLastAnyOf(strings: Collection<String>, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Pair<Int, String>? =
findAnyOf(strings, startIndex, ignoreCase, last = true)
@@ -599,6 +567,7 @@ public fun String.findLastAnyOf(strings: Collection<String>, startIndex: Int = l
* the beginning to the end of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.indexOfAny(strings: Collection<String>, startIndex: Int = 0, ignoreCase: Boolean = false): Int =
findAnyOf(strings, startIndex, ignoreCase, last = false)?.first ?: -1
@@ -614,9 +583,10 @@ public fun String.indexOfAny(strings: Collection<String>, startIndex: Int = 0, i
* the end toward the beginning of this string, and finds at each position the first element in [strings]
* that matches this string at that position.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.lastIndexOfAny(strings: Collection<String>, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int =
findAnyOf(strings, startIndex, ignoreCase, last = true)?.first ?: -1
*/
// indexOf
@@ -634,7 +604,6 @@ public fun String.indexOf(char: Char, startIndex: Int = 0, ignoreCase: Boolean =
nativeIndexOf(char, startIndex)
}
/*
/**
* Returns the index within this string of the first occurrence of the specified [string], starting from the specified [startIndex].
*
@@ -642,13 +611,14 @@ public fun String.indexOf(char: Char, startIndex: Int = 0, ignoreCase: Boolean =
* @returns An index of the first occurrence of [string] or -1 if none is found.
*/
@kotlin.jvm.JvmOverloads
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.indexOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int {
return if (ignoreCase)
indexOfAny(listOf(string), startIndex, ignoreCase)
else
nativeIndexOf(string, startIndex)
}
*/
/**
* Returns the index within this string of the last occurrence of the specified character, starting from the specified [startIndex].
*
@@ -664,7 +634,7 @@ public fun String.lastIndexOf(char: Char, startIndex: Int = lastIndex, ignoreCas
nativeLastIndexOf(char, startIndex)
}
/*
/**
* Returns the index within this string of the last occurrence of the specified [string], starting from the specified [startIndex].
*
@@ -672,6 +642,7 @@ public fun String.lastIndexOf(char: Char, startIndex: Int = lastIndex, ignoreCas
* @param ignoreCase `true` to ignore character case when matching a string. By default `false`.
* @returns An index of the first occurrence of [string] or -1 if none is found.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.lastIndexOf(string: String, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int {
return if (ignoreCase)
lastIndexOfAny(listOf(string), startIndex, ignoreCase)
@@ -684,6 +655,7 @@ public fun String.lastIndexOf(string: String, startIndex: Int = lastIndex, ignor
*
* @param ignoreCase `true` to ignore character case when comparing strings. By default `false`.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public operator fun String.contains(seq: CharSequence, ignoreCase: Boolean = false): Boolean =
indexOf(seq.toString(), ignoreCase = ignoreCase) >= 0
@@ -693,9 +665,10 @@ public operator fun String.contains(seq: CharSequence, ignoreCase: Boolean = fal
*
* @param ignoreCase `true` to ignore character case when comparing characters. By default `false`.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public operator fun String.contains(char: Char, ignoreCase: Boolean = false): Boolean =
indexOf(char, ignoreCase = ignoreCase) >= 0
*/
// rangesDelimitedBy
@@ -56,32 +56,7 @@ public fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: B
return if (index < 0) this else this.replaceRange(index, index + oldValue.length(), newValue)
}
/**
* 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()
/**
* 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()
/**
* Returns a new character array containing the characters from this string.
*/
public fun String.toCharArray(): CharArray = (this as java.lang.String).toCharArray()
/**
* 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)
/**
* 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)
*/
/**
* Splits this string around matches of the given regular expression.
@@ -89,149 +64,14 @@ public fun String.format(locale: Locale, vararg args : Any?) : String = java.lan
* @param limit Non-negative value specifying the maximum number of substrings to return.
* Zero by default means no limit is set.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.split(regex: Pattern, limit: Int = 0): List<String>
{
require(limit >= 0, { "Limit must be non-negative, but was $limit" } )
return regex.split(this, if (limit == 0) -1 else limit).asList()
}
/**
* Returns a substring of this string starting with the specified index.
*/
public fun String.substring(beginIndex: Int): String = (this as java.lang.String).substring(beginIndex)
/**
* Returns the substring of this string starting and ending at the specified indices.
*/
public fun String.substring(beginIndex: Int, endIndex: Int): String = (this as java.lang.String).substring(beginIndex, endIndex)
/**
* Returns `true` if this string starts with the specified prefix.
*/
public fun String.startsWith(prefix: String, ignoreCase: Boolean = false): Boolean {
if (!ignoreCase)
return (this as java.lang.String).startsWith(prefix)
else
return regionMatches(0, prefix, 0, prefix.length(), ignoreCase)
}
/**
* Returns `true` if a substring of this string starting at the specified offset [thisOffset] starts with the specified prefix.
*/
public fun String.startsWith(prefix: String, thisOffset: Int, ignoreCase: Boolean = false): Boolean {
if (!ignoreCase)
return (this as java.lang.String).startsWith(prefix, thisOffset)
else
return regionMatches(thisOffset, prefix, 0, prefix.length(), ignoreCase)
}
/**
* Returns `true` if this string ends with the specified suffix.
*/
public fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean {
if (!ignoreCase)
return (this as java.lang.String).endsWith(suffix)
else
return regionMatches(length() - suffix.length(), suffix, 0, suffix.length(), ignoreCase = true)
}
// "constructors" for String
/**
* Converts the data from a portion of the specified array of bytes to characters using the specified character set
* and returns the conversion result as a string.
*
* @param bytes the source array for the conversion.
* @param offset the offset in the array of the data to be converted.
* @param length the number of bytes to be converted.
* @param charsetName the name of the character set to use.
*/
public fun String(bytes: ByteArray, offset: Int, length: Int, charsetName: String): String = java.lang.String(bytes, offset, length, charsetName) as String
/**
* Converts the data from a portion of the specified array of bytes to characters using the specified character set
* and returns the conversion result as a string.
*
* @param bytes the source array for the conversion.
* @param offset the offset in the array of the data to be converted.
* @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
/**
* 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, charsetName: String): String = java.lang.String(bytes, charsetName) as String
/**
* 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
/**
* Converts the data from a portion of the specified array of bytes to characters using the UTF-8 character set
* and returns the conversion result as a string.
*
* @param bytes the source array for the conversion.
* @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
/**
* 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
/**
* Converts the characters in the specified array to a string.
*/
public 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
/**
* 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
/**
* Converts the contents of the specified StringBuffer to a string.
*/
public 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
///**
// * Replaces the first substring of this string that matches the given regular expression with the given replacement.
// */
//deprecated("Use replaceFirst(Regex, String) or replaceFirstLiteral(String, String) instead.", ReplaceWith("replaceFirst(regex.toRegex(), replacement)"))
//public fun String.replaceFirst(regex: String, replacement: String): String = (this as java.lang.String).replaceFirst(regex, replacement)
/**
* Returns the character (Unicode code point) at the specified index.
*/
public 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)
/**
* 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)
/*
/**
* Compares two strings lexicographically, optionally ignoring case differences.
@@ -258,111 +98,14 @@ public fun String.contentEquals(cs: CharSequence): Boolean = (this as java.lang.
*/
public fun String.contentEquals(sb: StringBuffer): Boolean = (this as java.lang.String).contentEquals(sb)
/**
* Copies the characters from a substring of this string into the specified character array.
* @param srcBegin the start offset (inclusive) of the substring to copy.
* @param srcEnd the end offset (exclusive) of the substring to copy.
* @param dst the array to copy to.
* @param dstBegin the position in the array to copy to.
*/
public fun String.getChars(srcBegin: Int, srcEnd: Int, dst: CharArray, dstBegin: Int): Unit = (this as java.lang.String).getChars(srcBegin, srcEnd, dst, dstBegin)
/**
* Returns a canonical representation for this string object.
*/
public fun String.intern(): String = (this as java.lang.String).intern()
*/
/**
* Returns `true` if this string is empty or consists solely of whitespace characters.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.isBlank(): Boolean = length() == 0 || all { it.isWhitespace() }
/**
* 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)
/**
* Returns `true` if the specified range in this string is equal to the specified range in another string.
* @param thisOffset the start offset in this string 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 string of the substring to compare.
* @param length the length of the substring to compare.
*/
public fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean =
if (!ignoreCase)
(this as java.lang.String).regionMatches(thisOffset, other, otherOffset, length)
else
(this as java.lang.String).regionMatches(ignoreCase, thisOffset, other, otherOffset, length)
/**
* 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)
/**
* 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)
/**
* 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)
/**
* 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)
/**
* 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)
/**
* 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)
/**
* 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)
/**
* 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)
/**
* Returns the list of all characters in this string.
*/
public fun String.toCharList(): List<Char> = toCharArray().toList()
/**
* Returns a subsequence of this sequence.
*
* @param start the start index (inclusive).
* @param end the end index (exclusive).
*/
public operator fun CharSequence.get(start: Int, end: Int): CharSequence = subSequence(start, end)
/**
* Encodes the contents of this string using the specified character set and returns the resulting byte array.
*/
public fun String.toByteArray(charset: String): ByteArray = (this as java.lang.String).getBytes(charset)
/**
* 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)
/*
/**
* Returns a subsequence of this sequence specified by given [range].
@@ -371,15 +114,6 @@ public fun CharSequence.slice(range: IntRange): CharSequence {
return subSequence(range.start, range.end + 1) // inclusive
}
/**
* Converts the string into a regular expression [Pattern] optionally
* 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 {
return java.util.regex.Pattern.compile(this, flags)
}
/**
* Returns a copy of this string having its first letter uppercased, or the original string,
* if it's empty or already starts with an upper case letter.
+15 -7
View File
@@ -111,7 +111,7 @@ public fun String.format(locale: Locale, vararg args : Any?) : String = java.lan
* @param limit Non-negative value specifying the maximum number of substrings to return.
* Zero by default means no limit is set.
*/
public fun String.split(regex: Pattern, limit: Int = 0): List<String>
public fun CharSequence.split(regex: Pattern, limit: Int = 0): List<String>
{
require(limit >= 0, { "Limit must be non-negative, but was $limit" } )
return regex.split(this, if (limit == 0) -1 else limit).asList()
@@ -234,12 +234,6 @@ public fun String(stringBuffer: java.lang.StringBuffer): String = java.lang.Stri
*/
public fun String(stringBuilder: java.lang.StringBuilder): String = java.lang.String(stringBuilder) as String
///**
// * Replaces the first substring of this string that matches the given regular expression with the given replacement.
// */
//deprecated("Use replaceFirst(Regex, String) or replaceFirstLiteral(String, String) instead.", ReplaceWith("replaceFirst(regex.toRegex(), replacement)"))
//public fun String.replaceFirst(regex: String, replacement: String): String = (this as java.lang.String).replaceFirst(regex, replacement)
/**
* Returns the character (Unicode code point) at the specified index.
*/
@@ -305,6 +299,20 @@ public fun CharSequence.isBlank(): Boolean = length() == 0 || indices.all { this
*/
public fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int = (this as java.lang.String).offsetByCodePoints(index, codePointOffset)
/**
* Returns `true` if the specified range in this CharSequence is equal to the specified range in another CharSequence.
* @param thisOffset the start offset in this CharSequence 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 CharSequence of the substring to compare.
* @param length the length of the substring to compare.
*/
public 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
return regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase)
}
/**
* Returns `true` if the specified range in this string is equal to the specified range in another string.
* @param thisOffset the start offset in this string of the substring to compare.