Skip to content

Commit f0df5bb

Browse files
authored
Merge pull request scala/scala#8113 from szeiger/wip/stringops-collect
Add StringOps#collect methods
2 parents 4375e68 + 798dd51 commit f0df5bb

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

library/src/scala/collection/StringOps.scala

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,55 @@ final class StringOps(private val s: String) extends AnyVal {
248248
sb.toString
249249
}
250250

251+
/** Builds a new String by applying a partial function to all chars of this String
252+
* on which the function is defined.
253+
*
254+
* @param pf the partial function which filters and maps the String.
255+
* @return a new String resulting from applying the given partial function
256+
* `pf` to each char on which it is defined and collecting the results.
257+
*/
258+
def collect(pf: PartialFunction[Char, Char]): String = {
259+
var i = 0
260+
var matched = true
261+
def d(x: Char): Char = {
262+
matched = false
263+
0
264+
}
265+
val b = new StringBuilder
266+
while(i < s.length) {
267+
matched = true
268+
val v = pf.applyOrElse(s.charAt(i), d)
269+
if(matched) b += v
270+
i += 1
271+
}
272+
b.result()
273+
}
274+
275+
/** Builds a new collection by applying a partial function to all chars of this String
276+
* on which the function is defined.
277+
*
278+
* @param pf the partial function which filters and maps the String.
279+
* @tparam B the element type of the returned collection.
280+
* @return a new collection resulting from applying the given partial function
281+
* `pf` to each char on which it is defined and collecting the results.
282+
*/
283+
def collect[B](pf: PartialFunction[Char, B]): immutable.IndexedSeq[B] = {
284+
var i = 0
285+
var matched = true
286+
def d(x: Char): B = {
287+
matched = false
288+
null.asInstanceOf[B]
289+
}
290+
val b = immutable.IndexedSeq.newBuilder[B]
291+
while(i < s.length) {
292+
matched = true
293+
val v = pf.applyOrElse(s.charAt(i), d)
294+
if(matched) b += v
295+
i += 1
296+
}
297+
b.result()
298+
}
299+
251300
/** Returns a new collection containing the chars from this string followed by the elements from the
252301
* right hand operand.
253302
*

0 commit comments

Comments
 (0)