=act improve ByteString#dropRight(...) (#21439)

#21439: =act improve ByteString#dropRight(...)
This commit is contained in:
monkey-mas 2016-12-06 23:51:11 +09:00 committed by drewhk
parent 8d05592e2e
commit c38d3850a2
3 changed files with 166 additions and 22 deletions

View file

@ -487,21 +487,29 @@ object ByteString {
}
override def dropRight(n: Int): ByteString =
if (n <= 0) this
else {
val last = bytestrings.last
if (n < last.length) new ByteStrings(bytestrings.init :+ last.dropRight1(n), length - n)
else {
val remaining = bytestrings.init
if (remaining.isEmpty) ByteString.empty
else {
val s = new ByteStrings(remaining, length - last.length)
val remainingToBeDropped = n - last.length
s.dropRight(remainingToBeDropped)
}
if (0 < n && n < length) dropRight0(n)
else if (n >= length) ByteString.empty
else this
private def dropRight0(n: Int): ByteString = {
val byteStringsSize = bytestrings.length
@tailrec def dropRightWithFullDropsAndRemainig(fullDrops: Int, remainingToDrop: Int): ByteString = {
val bs = bytestrings(byteStringsSize - fullDrops - 1)
if (bs.length > remainingToDrop) {
if (fullDrops == byteStringsSize - 1)
bytestrings(0).dropRight(remainingToDrop)
else if (remainingToDrop == 0)
new ByteStrings(bytestrings.dropRight(fullDrops), length - n)
else
new ByteStrings(bytestrings.dropRight(fullDrops + 1) :+ bytestrings(byteStringsSize - fullDrops - 1).dropRight1(remainingToDrop), length - n)
} else {
dropRightWithFullDropsAndRemainig(fullDrops + 1, remainingToDrop - bs.length)
}
}
dropRightWithFullDropsAndRemainig(0, n)
}
override def slice(from: Int, until: Int): ByteString =
if (from <= 0 && until >= length) this
else if (from > length || until <= from) ByteString.empty