File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
library/src/scala/collection Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,55 @@ final class StringOps(private val s: String) extends AnyVal {
248
248
sb.toString
249
249
}
250
250
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
+
251
300
/** Returns a new collection containing the chars from this string followed by the elements from the
252
301
* right hand operand.
253
302
*
You can’t perform that action at this time.
0 commit comments