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

Unicode String Gen #26

Open
xuwei-k opened this issue Apr 11, 2016 · 3 comments
Open

Unicode String Gen #26

xuwei-k opened this issue Apr 11, 2016 · 3 comments

Comments

@kazzna
Copy link

kazzna commented Apr 20, 2016

Why don't you just use Character.isValidCodePoint(Int)?

if (Character.isValidCodePoint(i)) String.valueOf(Character.toChars(i)) else /* retry code */

@fommil
Copy link

fommil commented Jan 11, 2022

  val unicodeString: Gen[String] = {
    val chars = Gen.choose(0, 0x10FFFF).map { cp =>
      if (Character.isBmpCodePoint(cp)) List(cp.toChar)
      else List(Character.highSurrogate(cp), Character.lowSurrogate(cp))
    }
    Gen.listOf(chars).map(_.flatten.mkString)
  }

references

probably want to have something like this to handle the surrogates during shrinking:

  implicit val strShrinker: Shrink[String] = Shrink.shrink { txt =>
    if (txt.isEmpty) Stream.empty[String]
    else if (txt.length == 1) Stream("")
    else {
      val a = if (txt.head.isSurrogate) txt.drop(2) else txt.drop(1)
      val b = if (txt.last.isSurrogate) txt.take(txt.length - 2) else txt.take(txt.length - 1)
      Stream(a, b).filter(_ != txt).distinct
    }
  }

fommil pushed a commit to fommil/scalaprops that referenced this issue Jan 11, 2022
fommil added a commit to fommil/scalaprops that referenced this issue Jan 17, 2022
fommil added a commit to fommil/scalaprops that referenced this issue Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants