Minor in stdlib: move String.trimLeading and String.trimTrailing to common place.

This commit is contained in:
Zalim Bashorov
2014-11-17 23:48:45 +03:00
parent 14fd6eaaeb
commit eae86e035f
4 changed files with 68 additions and 68 deletions
@@ -24,6 +24,26 @@ public fun String.trimTrailing(postfix: String): String {
return answer
}
/** Returns a new String containing the everything but the leading whitespace characters */
public fun String.trimLeading(): String {
var count = 0
while ((count < this.length) && (this[count] <= ' ')) {
count++
}
return if (count > 0) substring(count) else this
}
/** Returns a new String containing the everything but the trailing whitespace characters */
public fun String.trimTrailing(): String {
var count = this.length
while (count > 0 && this[count - 1] <= ' ') {
count--
}
return if (count < this.length) substring(0, count) else this
}
/** Returns true if the string is not null and not empty */
public fun String?.isNotEmpty(): Boolean = this != null && this.length() > 0
@@ -274,26 +274,6 @@ public inline fun <T : Appendable> String.takeWhileTo(result: T, predicate: (Cha
deprecated("Use toList() instead.")
public fun String.toCollection(): Collection<Char> = toCollection(ArrayList<Char>(this.length()))
/** Returns a new String containing the everything but the leading whitespace characters */
public fun String.trimLeading(): String {
var count = 0
while ((count < this.length) && (this[count] <= ' ')) {
count++
}
return if (count > 0) substring(count) else this
}
/** Returns a new String containing the everything but the trailing whitespace characters */
public fun String.trimTrailing(): String {
var count = this.length
while (count > 0 && this[count - 1] <= ' ') {
count--
}
return if (count < this.length) substring(0, count) else this
}
/**
* Replaces every *regexp* occurence in the text with the value retruned by the given function *body* that can handle
* particular occurance using [[MatchResult]] provided.