Skip to content

Commit 7d6e6e1

Browse files
sh0heiSethTisue
authored andcommitted
Throw exception on init last when empty string
1 parent 03b265b commit 7d6e6e1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

library/src/scala/collection/StringOps.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,14 +1201,16 @@ final class StringOps(private val s: String) extends AnyVal {
12011201
def withFilter(p: Char => Boolean): StringOps.WithFilter = new StringOps.WithFilter(p, s)
12021202

12031203
/** The rest of the string without its first char.
1204-
* @note $unicodeunaware
1205-
*/
1206-
def tail: String = slice(1, s.length)
1204+
* @throws UnsupportedOperationException if the string is empty.
1205+
* @note $unicodeunaware
1206+
*/
1207+
def tail: String = if(s.isEmpty) throw new UnsupportedOperationException("tail of empty String") else slice(1, s.length)
12071208

12081209
/** The initial part of the string without its last char.
1209-
* @note $unicodeunaware
1210-
*/
1211-
def init: String = slice(0, s.length-1)
1210+
* @throws UnsupportedOperationException if the string is empty.
1211+
* @note $unicodeunaware
1212+
*/
1213+
def init: String = if(s.isEmpty) throw new UnsupportedOperationException("init of empty String") else slice(0, s.length-1)
12121214

12131215
/** A string containing the first `n` chars of this string.
12141216
* @note $unicodeunaware

0 commit comments

Comments
 (0)