This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The motivation is the same as for Arrays which were originally wrapped in ArrayView but we later changed it back to WrappedArray (and ImmutableArray): It is very convenient to have an implicit conversion of a String to a Seq[Char] but a View is no longer a Seq in the new collection library.
- Loading branch information
Showing
2 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
collections/src/main/scala/strawman/collection/immutable/WrappedString.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package strawman.collection | ||
package immutable | ||
|
||
import scala.{Char, Int} | ||
import scala.Predef.String | ||
import mutable.{Builder, StringBuilder} | ||
|
||
/** | ||
* This class serves as a wrapper augmenting `String`s with all the operations | ||
* found in indexed sequences. | ||
* | ||
* The difference between this class and `StringOps` is that calling transformer | ||
* methods such as `filter` and `map` will yield an object of type `WrappedString` | ||
* rather than a `String`. | ||
* | ||
* @param self a string contained within this wrapped string | ||
* | ||
* @since 2.8 | ||
* @define Coll `WrappedString` | ||
* @define coll wrapped string | ||
*/ | ||
final class WrappedString(val self: String) extends AbstractSeq[Char] with IndexedSeq[Char] { | ||
|
||
def apply(i: Int): Char = self.charAt(i) | ||
|
||
protected[this] def fromSpecificIterable(coll: strawman.collection.Iterable[Char]): IndexedSeq[Char] = | ||
WrappedString.fromSpecific(coll) | ||
protected[this] def newSpecificBuilder(): Builder[Char, IndexedSeq[Char]] = WrappedString.newBuilder() | ||
def iterableFactory: SeqFactory[IndexedSeq] = ImmutableArray | ||
|
||
override def slice(from: Int, until: Int): WrappedString = { | ||
val start = if (from < 0) 0 else from | ||
if (until <= start || start >= self.length) | ||
return new WrappedString("") | ||
|
||
val end = if (until > length) length else until | ||
new WrappedString(self.substring(start, end)) | ||
} | ||
override def length = self.length | ||
override def toString = self | ||
override def view: StringView = new StringView(self) | ||
} | ||
|
||
/** A companion object for wrapped strings. | ||
* | ||
* @since 2.8 | ||
*/ | ||
object WrappedString extends SpecificIterableFactory[Char, WrappedString] { | ||
def fromSpecific(it: IterableOnce[Char]): WrappedString = { | ||
val b = newBuilder() | ||
it match { | ||
case it: Iterable[_] => | ||
val s = it.knownSize | ||
if(s >= 0) b.sizeHint(s) | ||
case _ => | ||
} | ||
b ++= it | ||
b.result() | ||
} | ||
val empty: WrappedString = new WrappedString("") | ||
def newBuilder(): Builder[Char, WrappedString] = | ||
new StringBuilder().mapResult(x => new WrappedString(x)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters