Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Dec 5, 2021
1 parent 219ec0c commit 50e6dd1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 7 additions & 4 deletions fansi/src/fansi/Fansi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import scala.collection.mutable
* giving 20% (on `++`) to >1000% (on `splitAt`, `subString`
* and `Str.parse`) speedups
*/
case class Str private(private val chars: Array[Char], private val colors: Array[Str.State]) {
class Str private(private val chars: Array[Char], private val colors: Array[Str.State]) {
require(chars.length == colors.length)
override def hashCode() = util.Arrays.hashCode(chars) + util.Arrays.hashCode(colors)
override def equals(other: Any) = other match{
Expand All @@ -42,7 +42,7 @@ case class Str private(private val chars: Array[Char], private val colors: Array
System.arraycopy(colors, 0, colors2, 0, length)
System.arraycopy(other.colors, 0, colors2, length, other.length)

Str(chars2, colors2)
new Str(chars2, colors2)
}

/**
Expand Down Expand Up @@ -335,7 +335,7 @@ object Str{
}
}

Str(
new Str(
util.Arrays.copyOfRange(chars, 0, destIndex),
util.Arrays.copyOfRange(colors, 0, destIndex)
)
Expand All @@ -354,7 +354,10 @@ object Str{
new fansi.Str(chars.clone(), colors.clone())
}

def join(args: Str*) = {
def apply(args: Str*) = {
join(args)
}
def join(args: TraversableOnce[Str]) = {
val length = args.iterator.map(_.length).sum
val chars = new Array[Char](length)
val colors = new Array[State](length)
Expand Down
8 changes: 7 additions & 1 deletion fansi/test/src/fansi/FansiTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ object FansiTests extends TestSuite{

assert(concated == expected)
}
test("apply"){
val concated = fansi.Str(fansi.Str(rgbOps), fansi.Str(rgbOps)).render
val expected = rgbOps ++ RTC ++ rgbOps ++ RTC

assert(concated == expected)
}
test("join"){
val concated = fansi.Str.join(fansi.Str(rgbOps), fansi.Str(rgbOps)).render
val concated = fansi.Str.join(Seq(fansi.Str(rgbOps), fansi.Str(rgbOps))).render
val expected = rgbOps ++ RTC ++ rgbOps ++ RTC

assert(concated == expected)
Expand Down

0 comments on commit 50e6dd1

Please sign in to comment.