Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Str.join throws NegativeArraySizeException given an empty Seq and a nonempty separator #48

Open
akouryy opened this issue Jul 8, 2022 · 2 comments

Comments

@akouryy
Copy link

akouryy commented Jul 8, 2022

scala> Nil.mkString(",")
val res0: String = ""
                                                                                                                                                                                                                    
scala> fansi.Str.join(Nil, "")
val res1: fansi.Str = 
                                                                                                                                                                                                                    
scala> fansi.Str.join(Nil, ",")
java.lang.NegativeArraySizeException: -1
  at fansi.Str$.join(Fansi.scala:370)
  ... 66 elided

I'm not sure if it's a bug, but allowing (Nil, ",") sounds more consistent.

Scala 3.1.3, fansi 0.3.1

@bwbecker
Copy link

bwbecker commented Mar 1, 2023

Same thing with Scala 2.13.10:

    val s2 = Vector[fansi.Str](fansi.Color.Blue("blue"))
    println("nonEmpty: " + fansi.Str.join(s2, ", ").render)
    val s3 = Vector[fansi.Str]()
    println("empty: " + fansi.Str.join(s3, ", ").render)

Running it:

nonEmpty: blue
[error] java.lang.NegativeArraySizeException: -2
[error] 	at fansi.Str$.join(Fansi.scala:370)

@bwbecker
Copy link

bwbecker commented Mar 1, 2023

Yup, lines 368 and following of Fansi.scala:

  def join(args: Iterable[Str], sep: fansi.Str = fansi.Str("")) = {
    val length = args.iterator.map(_.length + sep.length).sum - sep.length
    val chars = new Array[Char](length)

If the args iterable is empty, length becomes negative by the length of sep, which causes a problem in the next line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants