summaryrefslogtreecommitdiff
path: root/src/library/scala/StringBuilder.scala
blob: 7a51e3a07ae51c26e61d2188b79cbc2349659bd0 (plain) (blame)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2006-2008, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id: $


package scala

import Predef._

/** <p>
 *    A mutable sequence of characters.  This class provides an API compatible
 *    with <code>java.lang.StringBuilder</code>, but with no guarantee of
 *    synchronization.
 *  </p>
 *
 *  @author Stephane Micheloud
 */
@SerialVersionUID(0 - 8525408645367278351L)
@throws(classOf[NullPointerException])
final class StringBuilder(initCapacity: Int, private val initValue: String)
extends (Int => Char) with Proxy {
  if (initCapacity < 0) throw new IllegalArgumentException
  if (initValue eq null) throw new NullPointerException

  /** The value is used for character storage. */
  private var value = new Array[Char](initCapacity + initValue.length)

  /** The count is the number of characters used. */
  private var count: Int = 0

  def this() = this(16, "")

  def this(capacity: Int) = this(capacity, "")

  @throws(classOf[NullPointerException])
  def this(str: String) = this(16, str)

  append(initValue)

  def self = this

  def toArray: Array[Char] = value

  def length: Int = count

  def length_=(n: Int) { setLength(n) }

  @throws(classOf[StringIndexOutOfBoundsException])
  def setLength(n: Int) {
    if (n < 0)
      throw new StringIndexOutOfBoundsException(n)
    if (n > value.length) expandCapacity(n)
    if (count < n)
      while (count < n) {
        value(count) = '\0'; count += 1
      }
    else
      count = n
  }

  def capacity: Int = value.length

  def capacity_=(n: Int) { ensureCapacity(n) }

  def ensureCapacity(n: Int) {
    if (n > value.length) expandCapacity(n)
  }

  private def expandCapacity(n: Int) {
    val newCapacity = (value.length + 1) * 2
    value = StringBuilder.copyOf(
      value,
      if (newCapacity < 0) Math.MAX_INT else if (n > newCapacity) n else newCapacity
    )
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def charAt(index: Int): Char = {
    if (index < 0 || index >= count)
      throw new StringIndexOutOfBoundsException(index)
    value(index)
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def apply(i: Int): Char = charAt(i)

  @throws(classOf[StringIndexOutOfBoundsException])
  def deleteCharAt(index: Int): StringBuilder = {
    if (index < 0 || index >= count)
      throw new StringIndexOutOfBoundsException(index)
    compat.Platform.arraycopy(value, index + 1, value, index, count - index - 1)
    count -= 1
    this
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def setCharAt(index: Int, c: Char) {
    if (index < 0 || index >= count)
      throw new StringIndexOutOfBoundsException(index)
    value(index) = c
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def update(i: Int, c: Char) { setCharAt(i, c) }

  @throws(classOf[StringIndexOutOfBoundsException])
  def substring(start: Int): String = substring(start, count)

  @throws(classOf[StringIndexOutOfBoundsException])
  def substring(start: Int, end: Int): String = {
    if (start < 0)
      throw new StringIndexOutOfBoundsException(start)
    if (end > count)
      throw new StringIndexOutOfBoundsException(end)
    if (start > end)
      throw new StringIndexOutOfBoundsException(end - start)
    new String(value, start, end - start)
  }

  def append(x: Any): StringBuilder =
    append(String.valueOf(x))

  /** Appends the specified string to this character sequence.
   *
   *  @param s
   *  @return
   */
  def append(s: String): StringBuilder = {
    val str = if (s == null) "null" else s
    val len = str.length
    if (len > 0) {
      val newCount = count + len
      if (newCount > value.length) expandCapacity(newCount)
      compat.Platform.arraycopy(str.toArray: Array[Char], 0, value, count, len)
      count = newCount
    }
    this
  }

  /** Appends the specified string builder to this sequence.
   *
   *  @param sb
   *  @return
   */
  def append(sb: StringBuilder): StringBuilder =
    if (sb == null)
      append("null")
    else {
      val len = sb.length
      val newCount = count + len
      if (newCount > value.length) expandCapacity(newCount)
      compat.Platform.arraycopy(sb.toArray, 0, value, count, len)
      count = newCount
      this
    }

  def append(x: Array[Char]): StringBuilder =
    append(x, 0, x.length)

  def append(x: Array[Char], offset: Int, len: Int): StringBuilder = {
    val newCount = count + len
    if (newCount > value.length) expandCapacity(newCount)
    compat.Platform.arraycopy(x, offset, value, count, len)
    count = newCount
    this
  }

  def append(x: Boolean): StringBuilder = {
    if (x) {
      val newCount = count + 4
      if (newCount > value.length) expandCapacity(newCount)
      value(count) = 't'; count += 1
      value(count) = 'r'; count += 1
      value(count) = 'u'; count += 1
      value(count) = 'e'; count += 1
    } else {
      val newCount = count + 5
      if (newCount > value.length) expandCapacity(newCount)
      value(count) = 'f'; count += 1
      value(count) = 'a'; count += 1
      value(count) = 'l'; count += 1
      value(count) = 's'; count += 1
      value(count) = 'e'; count += 1
    }
    this
  }

  def append(x: Char): StringBuilder = {
    val newCount = count + 1
    if (newCount > value.length) expandCapacity(newCount)
    value(count) = x; count += 1
    this
  }

  def append(x: Int): StringBuilder =
    append(String.valueOf(x))

  def append(x: Long): StringBuilder =
    append(String.valueOf(x))

  def append(x: Float): StringBuilder =
    append(String.valueOf(x))

  def append(x: Double): StringBuilder =
    append(String.valueOf(x))

  @throws(classOf[StringIndexOutOfBoundsException])
  def delete(start: Int, end: Int): StringBuilder = {
    if (start < 0 || start > end)
      throw new StringIndexOutOfBoundsException(start)
    val end0 = if (end > count) count else end
    val len = end0 - start
    if (len > 0) {
      compat.Platform.arraycopy(value, start + len, value, start, count - end0)
      count -= len
    }
    this
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def replace(start: Int, end: Int, str: String) {
    if (start < 0 || start > count || start > end)
      throw new StringIndexOutOfBoundsException(start)

    val end0 = if (end > count) count else end
    val len = str.length()
    val newCount = count + len - (end0 - start)
    if (newCount > value.length) expandCapacity(newCount)

    compat.Platform.arraycopy(value, end, value, start + len, count - end)
    compat.Platform.arraycopy(str.toArray, 0, value, start, len)
    count = newCount
    this
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(index: Int, str: Array[Char], offset: Int, len: Int): StringBuilder = {
    if (index < 0 || index > count)
      throw new StringIndexOutOfBoundsException(index)
    if (offset < 0 || len < 0 || offset > str.length - len)
      throw new StringIndexOutOfBoundsException(
                "offset " + offset + ", len " + len +
                ", str.length " + str.length)
    val newCount = count + len
    if (newCount > value.length) expandCapacity(newCount)
    compat.Platform.arraycopy(value, index, value, index + len, count - index)
    compat.Platform.arraycopy(str, offset, value, index, len)
    count = newCount
    this
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Any): StringBuilder =
    insert(at, String.valueOf(x))

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: String): StringBuilder = {
    if (at < 0 || at > count)
      throw new StringIndexOutOfBoundsException(at)
    val str = if (x == null) "null" else x
    val len = str.length
    val newCount = count + len
    if (newCount > value.length) expandCapacity(newCount)
    compat.Platform.arraycopy(value, at, value, at + len, count - at)
    compat.Platform.arraycopy(str.toArray: Array[Char], 0, value, at, len)
    count = newCount
    this
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Array[Char]): StringBuilder = {
    if (at < 0 || at > count)
      throw new StringIndexOutOfBoundsException(at)
    val len = x.length
    val newCount = count + len
    if (newCount > value.length) expandCapacity(newCount)
    compat.Platform.arraycopy(value, at, value, at + len, count - at)
    compat.Platform.arraycopy(x, 0, value, at, len)
    count = newCount
    this
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Boolean): StringBuilder =
    insert(at, String.valueOf(x))

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Char): StringBuilder = {
    if (at < 0 || at > count)
      throw new StringIndexOutOfBoundsException(at)
    val newCount = count + 1
    if (newCount > value.length) expandCapacity(newCount)
    compat.Platform.arraycopy(value, at, value, at + 1, count - at)
    value(at) = x
    count = newCount
    this
  }

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Int): StringBuilder =
    insert(at, String.valueOf(x))

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Long): StringBuilder =
    insert(at, String.valueOf(x))

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Float): StringBuilder =
    insert(at, String.valueOf(x))

  @throws(classOf[StringIndexOutOfBoundsException])
  def insert(at: Int, x: Double): StringBuilder =
    insert(at, String.valueOf(x))

  @throws(classOf[NullPointerException])
  def indexOf(str: String): Int = indexOf(str, 0)

  @throws(classOf[NullPointerException])
  def indexOf(str: String, fromIndex: Int): Int =
    StringBuilder.indexOf(value, 0, count, str.toArray, 0, str.length(), fromIndex)

  @throws(classOf[NullPointerException])
  def lastIndexOf(str: String): Int = lastIndexOf(str, count)

  @throws(classOf[NullPointerException])
  def lastIndexOf(str: String, fromIndex: Int): Int =
    StringBuilder.lastIndexOf(value, 0, count, str.toArray, 0, str.length(), fromIndex)

  def reverse(): StringBuilder = {
    var hasSurrogate = false
    val n = count - 1
    var j = (n-1) >> 1
    while (j >= 0) {
      val temp = value(j)
      val temp2 = value(n - j)
      if (!hasSurrogate)
        hasSurrogate =
          (temp >= StringBuilder.MIN_SURROGATE && temp <= StringBuilder.MAX_SURROGATE) ||
       	  (temp2 >= StringBuilder.MIN_SURROGATE && temp2 <= StringBuilder.MAX_SURROGATE)
      value(j) = temp2
      value(n - j) = temp
      j -= 1
    }
    if (hasSurrogate) {
      // Reverse back all valid surrogate pairs
      var i = 0
      while (i < count - 1) {
        val c2 = value(i)
	if (StringBuilder.isLowSurrogate(c2)) {
          val c1 = value(i + 1)
          if (StringBuilder.isHighSurrogate(c1)) {
            value(i) = c1; i += 1
            value(i) = c2
          }
        }
        i += 1
      }
    }
    this
  }

  override def toString(): String = new String(value, 0, count)

  @throws(classOf[java.io.IOException])
  private def writeObject(s: java.io.ObjectOutputStream) {
    s.defaultWriteObject()
    s.writeInt(count)
    s.writeObject(value)
  }

  @throws(classOf[java.io.IOException])
  @throws(classOf[ClassNotFoundException])
  private def readObject(s: java.io.ObjectInputStream ) {
    s.defaultReadObject()
    count = s.readInt()
    value = s.readObject().asInstanceOf[Array[Char]]
  }

}


object StringBuilder {

  private val MIN_HIGH_SURROGATE = '\uD800'
  private val MAX_HIGH_SURROGATE = '\uDBFF'

  private val MIN_LOW_SURROGATE = '\uDC00'
  private val MAX_LOW_SURROGATE = '\uDFFF'

  // constants <code>java.langCharacter.MIN-/MAX_SURROGATE</code> exist since 1.5
  private val MIN_SURROGATE = MIN_HIGH_SURROGATE
  private val MAX_SURROGATE = MAX_LOW_SURROGATE

  // methods <code>java.langCharacter.isLow-/isHighSurrogate</code> exist since 1.5
  private def isLowSurrogate(ch: Char): Boolean =
    MIN_LOW_SURROGATE <= ch && ch <= MAX_LOW_SURROGATE

  private def isHighSurrogate(ch: Char): Boolean =
    MIN_HIGH_SURROGATE <= ch && ch <= MAX_HIGH_SURROGATE

  // method <code>java.util.Arrays.copyOf</code> exists since 1.6
  private def copyOf(src: Array[Char], newLength: Int): Array[Char] = {
    val dest = new Array[Char](newLength)
    val (start, end) =
      if (src.length < newLength) (src.length, newLength)
      else (newLength, src.length)
    compat.Platform.arraycopy(src, 0, dest, 0, start)
    // For any indices that are valid in the copy but not the original,
    // the copy will contain '\\u000'.
    for (i <- start until end) dest(i) = '\0'
    dest
  }

  private def indexOf(source: Array[Char], sourceOffset: Int, sourceCount: Int,
                      target: Array[Char], targetOffset: Int, targetCount: Int,
                      fromIndex: Int): Int =
    if (fromIndex >= sourceCount)
      if (targetCount == 0) sourceCount else -1
    else {
      val inx = if (fromIndex < 0) 0 else fromIndex
      if (targetCount == 0)
        inx
      else {
        val first  = target(targetOffset)
        val max = sourceOffset + (sourceCount - targetCount)

        var i = sourceOffset + inx
        while (i <= max) {
          /* Look for first character. */
          if (source(i) != first) {
            i += 1
            while (i <= max && source(i) != first) i += 1
          }
          /* Found first character, now look at the rest of v2 */
          if (i <= max) {
            var j = i + 1
            val end = j + targetCount - 1
            var k = targetOffset + 1
            while (j < end && source(j) == target(k)) {
              j += 1
              k += 1
            }
            if (j == end) {
              /* Found whole string. */
              return i - sourceOffset
            }
          } // if
          i += 1
        } // while
        -1
      }
    }

  private def lastIndexOf(source: Array[Char], sourceOffset: Int, sourceCount: Int,
                          target: Array[Char], targetOffset: Int, targetCount: Int,
                          fromIndex: Int): Int = {
    val rightIndex = sourceCount - targetCount
    if (fromIndex < 0) return -1
    val inx = if (fromIndex > rightIndex) rightIndex else fromIndex
    // Empty string always matches
    if (targetCount == 0) return inx

    val strLastIndex = targetOffset + targetCount - 1
    val strLastChar = target(strLastIndex)
    val min = sourceOffset + targetCount - 1
    var i = min + fromIndex

    while (true) {
      while (i >= min && source(i) != strLastChar) i -= 1
      if (i < min) return -1
      var j = i - 1
      val start = j - (targetCount - 1)
      var k = strLastIndex - 1
      var outerWhile = false
      while (j > start && !outerWhile) {
        if (source(j) != target(k)) {
          j -= 1
          k -= 1
          i -= 1
          outerWhile = true
        }
      }
      if (!outerWhile) return start - sourceOffset + 1
    }
    -1
  }
}