Optimized CompactByteString.apply methods for the case of empty input
This commit is contained in:
parent
68d073203c
commit
a8030b0bf0
1 changed files with 24 additions and 11 deletions
|
|
@ -353,31 +353,42 @@ object CompactByteString {
|
|||
/**
|
||||
* Creates a new CompactByteString by copying a byte array.
|
||||
*/
|
||||
def apply(bytes: Array[Byte]): CompactByteString = ByteString.ByteString1C(bytes.clone)
|
||||
def apply(bytes: Array[Byte]): CompactByteString = {
|
||||
if (bytes.isEmpty) empty
|
||||
else ByteString.ByteString1C(bytes.clone)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new CompactByteString by copying bytes.
|
||||
*/
|
||||
def apply(bytes: Byte*): CompactByteString = {
|
||||
if (bytes.isEmpty) empty
|
||||
else {
|
||||
val ar = new Array[Byte](bytes.size)
|
||||
bytes.copyToArray(ar)
|
||||
CompactByteString(ar)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new CompactByteString by converting from integral numbers to bytes.
|
||||
*/
|
||||
def apply[T](bytes: T*)(implicit num: Integral[T]): CompactByteString =
|
||||
ByteString.ByteString1C(bytes.map(x ⇒ num.toInt(x).toByte)(collection.breakOut))
|
||||
def apply[T](bytes: T*)(implicit num: Integral[T]): CompactByteString = {
|
||||
if (bytes.isEmpty) empty
|
||||
else ByteString.ByteString1C(bytes.map(x ⇒ num.toInt(x).toByte)(collection.breakOut))
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new CompactByteString by copying bytes from a ByteBuffer.
|
||||
*/
|
||||
def apply(bytes: ByteBuffer): CompactByteString = {
|
||||
if (bytes.remaining < 1) empty
|
||||
else {
|
||||
val ar = new Array[Byte](bytes.remaining)
|
||||
bytes.get(ar)
|
||||
ByteString.ByteString1C(ar)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new CompactByteString by encoding a String as UTF-8.
|
||||
|
|
@ -387,8 +398,10 @@ object CompactByteString {
|
|||
/**
|
||||
* Creates a new CompactByteString by encoding a String with a charset.
|
||||
*/
|
||||
def apply(string: String, charset: String): CompactByteString =
|
||||
ByteString.ByteString1C(string.getBytes(charset))
|
||||
def apply(string: String, charset: String): CompactByteString = {
|
||||
if (string.isEmpty) empty
|
||||
else ByteString.ByteString1C(string.getBytes(charset))
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new CompactByteString by copying length bytes starting at offset from
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue