Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Bring back WrappedString #351

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it’s time to have AbstractIndexedSeq?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as in the old collections. There's collection.AbstractSeq but no collection.AbstractIndexedSeq or immutable.Abstract*. I'll do some more experiments and possibly open another PR. First tests show that immutable.AbstractSeq could be worth it, but immutable.AbstractIndexedSeq is probably not.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the default IndexedSeq implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being a fixed-size array it's the most similar to String in spirit


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()
val s = it.knownSize
if(s >= 0) b.sizeHint(s)
b ++= it
b.result()
}
val empty: WrappedString = new WrappedString("")
def newBuilder(): Builder[Char, WrappedString] =
new StringBuilder().mapResult(x => new WrappedString(x))
}
4 changes: 2 additions & 2 deletions collections/src/main/scala/strawman/collection/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ class LowPriority {
/** Convert array to WrappedArray. Lower priority than ArrayOps */
implicit def arrayToWrappedArray[T](xs: Array[T]): mutable.IndexedSeq[T] = mutable.WrappedArray.make[T](xs)

/** Convert string to iterable via view. Lower priority than StringOps */
implicit def stringToView(s: String): immutable.StringView = new immutable.StringView(s)
/** Convert String to Seq. Lower priority than StringOps */
implicit def stringToSeq(s: String): immutable.WrappedString = new immutable.WrappedString(s)
}