Skip to content

Commit

Permalink
Merge pull request #8113 from szeiger/wip/stringops-collect
Browse files Browse the repository at this point in the history
Add StringOps#collect methods
  • Loading branch information
SethTisue committed Jun 4, 2019
2 parents 6961265 + f6cc304 commit 43e040f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build.sbt
Expand Up @@ -90,6 +90,14 @@ val mimaPrereleaseHandlingSettings = Seq(
mimaBinaryIssueFilters ++= Seq(
// Drop after 2.13.0 is out, whence src/reflect/mima-filters/ takes over.
ProblemFilters.exclude[Problem]("scala.reflect.internal.*"),


ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.collect$extension"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.collect$extension"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.collect"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.collect"),


),
)

Expand Down
49 changes: 49 additions & 0 deletions src/library/scala/collection/StringOps.scala
Expand Up @@ -248,6 +248,55 @@ final class StringOps(private val s: String) extends AnyVal {
sb.toString
}

/** Builds a new String by applying a partial function to all chars of this String
* on which the function is defined.
*
* @param pf the partial function which filters and maps the String.
* @return a new String resulting from applying the given partial function
* `pf` to each char on which it is defined and collecting the results.
*/
def collect(pf: PartialFunction[Char, Char]): String = {
var i = 0
var matched = true
def d(x: Char): Char = {
matched = false
0
}
val b = new StringBuilder
while(i < s.length) {
matched = true
val v = pf.applyOrElse(s.charAt(i), d)
if(matched) b += v
i += 1
}
b.result()
}

/** Builds a new collection by applying a partial function to all chars of this String
* on which the function is defined.
*
* @param pf the partial function which filters and maps the String.
* @tparam B the element type of the returned collection.
* @return a new collection resulting from applying the given partial function
* `pf` to each char on which it is defined and collecting the results.
*/
def collect[B](pf: PartialFunction[Char, B]): immutable.IndexedSeq[B] = {
var i = 0
var matched = true
def d(x: Char): B = {
matched = false
null.asInstanceOf[B]
}
val b = immutable.IndexedSeq.newBuilder[B]
while(i < s.length) {
matched = true
val v = pf.applyOrElse(s.charAt(i), d)
if(matched) b += v
i += 1
}
b.result()
}

/** Returns a new collection containing the chars from this string followed by the elements from the
* right hand operand.
*
Expand Down
5 changes: 5 additions & 0 deletions test/junit/scala/collection/StringOpsTest.scala
Expand Up @@ -97,4 +97,9 @@ class StringOpsTest {
@Test def withFilterAndThenMap(): Unit = {
assertEquals("hello".withFilter(_ != 'e').map(_.toUpper), "HLLO")
}

@Test def collect: Unit = {
assertEquals("de", "abcdef".collect { case c @ ('b' | 'c') => (c+2).toChar })
assertEquals(Seq('d'.toInt, 'e'.toInt), "abcdef".collect { case c @ ('b' | 'c') => (c+2).toInt })
}
}

0 comments on commit 43e040f

Please sign in to comment.