Repository navigation
Expand file tree
/
Copy pathWrappedString.scala
More file actions
140 lines (121 loc) · 4.74 KB
/
Copy pathWrappedString.scala
File metadata and controls
140 lines (121 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.collection
package immutable
import scala.Predef.{wrapString => _, assert}
import scala.collection.Stepper.EfficientSplit
import scala.collection.convert.impl.CharStringStepper
import scala.collection.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
*
* @define Coll `WrappedString`
* @define coll wrapped string
*/
@SerialVersionUID(3L)
final class WrappedString(private val self: String) extends AbstractSeq[Char] with IndexedSeq[Char]
with IndexedSeqOps[Char, IndexedSeq, WrappedString]
with Serializable {
def apply(i: Int): Char = self.charAt(i)
override protected def fromSpecific(coll: scala.collection.IterableOnce[Char]): WrappedString = WrappedString.fromSpecific(coll)
override protected def newSpecificBuilder: Builder[Char, WrappedString] = WrappedString.newBuilder
override def empty: WrappedString = WrappedString.empty
override def slice(from: Int, until: Int): WrappedString = {
val start = if (from < 0) 0 else from
if (until <= start || start >= self.length)
return WrappedString.empty
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)
override def stepper[S <: Stepper[_]](implicit shape: StepperShape[Char, S]): S with EfficientSplit = {
val st = new CharStringStepper(self, 0, self.length)
val r =
if (shape.shape == StepperShape.CharShape) st
else {
assert(shape.shape == StepperShape.ReferenceShape, s"unexpected StepperShape: $shape")
AnyStepper.ofParIntStepper(st)
}
r.asInstanceOf[S with EfficientSplit]
}
override def startsWith[B >: Char](that: IterableOnce[B], offset: Int = 0): Boolean =
that match {
case s: WrappedString => self.startsWith(s.self, offset)
case _ => super.startsWith(that, offset)
}
override def endsWith[B >: Char](that: collection.Iterable[B]): Boolean =
that match {
case s: WrappedString => self.endsWith(s.self)
case _ => super.endsWith(that)
}
override def indexOf[B >: Char](elem: B, from: Int = 0): Int = elem match {
case c: Char => self.indexOf(c, from)
case _ => super.indexOf(elem, from)
}
override def lastIndexOf[B >: Char](elem: B, end: Int = length - 1): Int =
elem match {
case c: Char => self.lastIndexOf(c, end)
case _ => super.lastIndexOf(elem, end)
}
override def copyToArray[B >: Char](xs: Array[B], start: Int, len: Int): Int =
(xs: Any) match {
case chs: Array[Char] =>
val copied = IterableOnce.elemsToCopyToArray(length, chs.length, start, len)
self.getChars(0, copied, chs, start)
copied
case _ => super.copyToArray(xs, start, len)
}
override def appendedAll[B >: Char](suffix: IterableOnce[B]): IndexedSeq[B] =
suffix match {
case s: WrappedString => new WrappedString(self concat s.self)
case _ => super.appendedAll(suffix)
}
override def sameElements[B >: Char](o: IterableOnce[B]) = o match {
case s: WrappedString => self == s.self
case _ => super.sameElements(o)
}
override protected[this] def className = "WrappedString"
override protected final def applyPreferredMaxLength: Int = Int.MaxValue
override def equals(other: Any): Boolean = other match {
case that: WrappedString =>
this.self == that.self
case _ =>
super.equals(other)
}
}
/** A companion object for wrapped strings.
*/
@SerialVersionUID(3L)
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))
implicit class UnwrapOp(private val value: WrappedString) extends AnyVal {
def unwrap: String = value.self
}
}