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
Bring back WrappedString #351
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
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,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] { | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Being a fixed-size array it's the most similar to |
||
|
||
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)) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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 nocollection.AbstractIndexedSeq
orimmutable.Abstract*
. I'll do some more experiments and possibly open another PR. First tests show thatimmutable.AbstractSeq
could be worth it, butimmutable.AbstractIndexedSeq
is probably not.