/
NumericString.scala
2836 lines (2612 loc) · 113 KB
/
NumericString.scala
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2001-2014 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalactic.anyvals
import java.nio.charset.Charset
import java.util.Locale
import scala.collection.immutable.StringOps
import scala.collection.immutable.WrappedString
import scala.collection.mutable.Buffer
import scala.collection.GenSeq
import scala.collection.SeqView
import scala.collection.GenIterable
import scala.collection.GenTraversableOnce
import scala.collection.generic.CanBuildFrom
import org.scalactic.ColCompatHelper
//import scala.collection.parallel.ParSeq
import scala.util.matching.Regex
import scala.language.higherKinds
import scala.util.{Try, Success, Failure}
import org.scalactic.{Validation, Pass, Fail}
import org.scalactic.{Or, Good, Bad}
/**
* An <code>AnyVal</code> for numeric <code>String</code>s.
*
* Note: a <code>NumericString</code> contains only numeric digit characters.
*
* <p>
* Because <code>NumericString</code> is an <code>AnyVal</code>
* it will usually be as efficient as a <code>String</code>, being
* boxed only when a <code>String</code> would have been boxed.
* </p>
*
* <p>
* The <code>NumericString.apply</code> factory method is implemented in
* terms of a macro that checks literals for validity at compile time. Calling
* <code>NumericString.apply</code> with a literal <code>String</code> value
* will either produce a valid <code>NumericString</code> instance at run
* time or an error at compile time. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> import anyvals._
* import anyvals._
*
* scala> NumericString("42")
* res0: org.scalactic.anyvals.NumericString = NumericString(42)
*
* scala> NumericString("abc")
* <console>:11: error: NumericString.apply can only be invoked on String literals that contain numeric characters, i.e., decimal digits '0' through '9', like "123".
* NumericString("abc")
* ^
* </pre>
*
* <p>
* <code>NumericString.apply</code> cannot be used if the value being passed
* is a variable (<em>i.e.</em>, not a literal), because the macro cannot
* determine the validity of variables at compile time (just literals). If
* you try to pass a variable to <code>NumericString.apply</code>, you'll
* get a compiler error that suggests you use a different factory method,
* <code>NumericString.from</code>, instead:
* </p>
*
* <pre class="stREPL">
* scala> val x = "1"
* x: String = 1
*
* scala> NumericString(x)
* <console>:15: error: NumericString.apply can only be invoked on String literals that contain only numeric characters, i.e., decimal digits '0' through '9', like "123" Please use NumericString.from instead.
* NumericString(x)
* ^
* </pre>
*
* <p>
* The <code>NumericString.from</code> factory method will inspect the value
* at runtime and return an <code>Option[NumericString]</code>. If
* the value is valid, <code>NumericString.from</code> will return a
* <code>Some[NumericString]</code>, else it will return a <code>None</code>.
* Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> NumericString.from(x)
* res3: Option[org.scalactic.anyvals.NumericString] = Some(NumericString(1))
*
* scala> val y = "a"
* y: String = a
*
* scala> NumericString.from(y)
* res4: Option[org.scalactic.anyvals.NumericString] = None
* </pre>
*
* @param value The <code>String</code> value underlying this
* <code>NumericString</code>.
*/
final class NumericString private (val value: String) extends AnyVal {
/**
* A string representation of this <code>NumericString</code>.
*/
override def toString: String = s"NumericString($value)"
/**
* Returns length of this `NumericString` in Unicode characters.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return length of this `NumericString`
*/
def length: Int = value.length
/**
* Returns the character at the zero-based `index` within the `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param index zero-based offset within `NumericString`
* @return character found at `index`
*/
def charAt(index: Int): Char = value.charAt(index)
/**
* Returns the integer value of the Unicode code point at the zero-based `index` within the `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param index zero-based offset within `NumericString`
* @return Unicode code point found at 'index'
*/
def codePointAt(index: Int): Int = value.codePointAt(index)
// SKIP-SCALATESTJS,NATIVE-START
/**
* Returns the integer value of the Unicode code point immediately prior to the zero-based `index` within the `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param index zero-based offset within `NumericString`
* @return Unicode code point found immediately prior to 'index'
*/
def codePointBefore(index: Int): Int = value.codePointBefore(index)
// SKIP-SCALATESTJS,NATIVE-END
/**
* Returns the count of complete Unicode code points beginning at zero-based `beginIndex` and ending at (but not including) zero-based `endIndex`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param beginIndex zero-based starting offset within `NumericString`
* @param endIndex zero-based ending offset within `NumericString`, one-past character range of interest
* @return count of complete Unicode code points found from BeginIndex, up to endIndex
*/
def codePointCount(beginIndex: Int, endIndex: Int): Int =
value.codePointCount(beginIndex, endIndex)
/**
* Compares the `NumericString` to `anotherString`, returning an integer <0 if `NumericString` is less than the supplied string,
* 0 if identical, and >0 if `NumericString` is greater than the supplied string.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param anotherString other string we compare `NumericString` against
* @return integer <0, 0 or >0 corresponding to lexicographic ordering of `NumericString` vs. `anotherString`
*/
def compareTo(anotherString: String): Int =
value.compareTo(anotherString)
// We'll leave this one out because numeric characters are the same upper and lower case.
/**
* Compares the `NumericString` to `anotherString` ignoring case, returning an integer <0 if `NumericString` is less than the supplied string,
* 0 if identical, and >0 if `NumericString` is greater than the supplied string.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param anotherString other string we compare `NumericString` against
* @return integer <0, 0 or >0 corresponding to case-insensitive lexicographic ordering
* of `NumericString` vs. `anotherString`
def compareToIgnoreCase(anotherString: String): Int =
value.compareToIgnoreCase(anotherString)
*/
/**
* Concatenates supplied string `str` onto the tail end of the `NumericString` and returns the resulting String.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param str additional string to concatenate onto `NumericString`
* @return string resulting from the concatenation
*/
def concat(str: String): String =
value.concat(str)
/**
* Tests whether this `NumericString` contains a given value as an element.
*
* @param s the element to test.
* @return `true` if this `NumericString` has an element that
* is equal (as determined by `==`) to `elem`,
* `false` otherwise.
*/
def contains(s: CharSequence): Boolean =
value.contains(s)
// SKIP-SCALATESTJS,NATIVE-START
/**
* Returns `true` if the `NumericString` content is the same as the supplied character sequence `cs`, otherwise returns `false`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param cs character sequence for comparison
* @return `true` if `NumericString` content is the same as `cs`
* `false` otherwise.
*/
def contentEquals(cs: CharSequence): Boolean =
value.contentEquals(cs)
// SKIP-SCALATESTJS,NATIVE-END
// We are missing contentEquals(StringBuffer)
/**
* Returns `true` if the `NumericString` content completely matches the supplied `suffix`, when both strings are aligned at their endpoints.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param suffix string for comparison as a suffix
* @return `true` if `NumericString` content completely matches the supplied `suffix`
* `false` otherwise.
*/
def endsWith(suffix: String): Boolean =
value.endsWith(suffix)
/**
* Returns an array of bytes corresponding to the `NumericString` characters, interpreted via the platform's default Charset-mapping of
* Unicode code points.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return array of bytes corresponding to `NumericString` characters
*/
def getBytes: Array[Byte] =
value.getBytes
/**
* Returns an array of bytes corresponding to the `NumericString` characters, interpreted via the supplied `charset` mapping of Unicode code points.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param charset a mapping of Unicode code points to bytes
* @return array of bytes corresponding to `NumericString` characters
*/
def getBytes(charset: Charset): Array[Byte] =
value.getBytes(charset)
/**
* Returns an array of bytes corresponding to the `NumericString` characters, interpreted via the named `charsetName` Charset-mapping of Unicode
* code points.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param charsetName string that names an already-known mapping of Unicode code points to bytes
* @return array of bytes corresponding to `NumericString` characters
*/
def getBytes(charsetName: String): Array[Byte] =
value.getBytes(charsetName)
/**
* Extracts the range of `NumericString` characters beginning at zero-based `srcBegin` through but not including `srcEnd`,
* into the supplied character array `dst`, writing the characters at the zero-based `dstBegin` index forward within that array.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param srcBegin zero-based index where to begin extracting characters from `NumericString`
* @param srcEnd zero-based limit before which to stop extracting characters from `NumericString`
* @param dst supplied character array to write extracted characters into
* @param dstBegin zero-based index within destination array at which to begin writing
* @return Unit -- this function modifies the supplied `dst` array
*/
def getChars(srcBegin: Int, srcEnd: Int, dst: Array[Char], dstBegin: Int): Unit =
value.getChars(srcBegin, srcEnd, dst, dstBegin)
/**
* Returns zero-based index in Unicode code units (logical index of characters) of first-encountered `NumericString` character
* matching the supplied Unicode `ch`; returns -1 if no matching character is found.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param ch Unicode character to look for
* @return zero-based integer index in Unicode code units of first-encountered instance of `ch`
*/
def indexOf(ch: Int): Int =
value.indexOf(ch)
/**
* Returns zero-based index (in Unicode code units: logical index of characters) of first-encountered `NumericString` character
* matching supplied Unicode `ch`, beginning search at zero-based index `fromIndex`; returns -1 if no matching character is found
* or if `fromIndex` is outside the bounds of `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param ch Unicode character to look for
* @param fromIndex zero-based integer index at which to begin search for match of `ch` character
* @return zero-based integer index in Unicode code units of first-encountered instance of `ch` at/beyond `fromIndex`
*/
def indexOf(ch: Int, fromIndex: Int): Int =
value.indexOf(ch, fromIndex)
/**
* Returns zero-based index (in Unicode code units: logical index of characters) of starting-character position
* of first-encountered match of `str` within `NumericString`;
* returns -1 if no fully-matching substring is found.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param str Unicode string to look for
* @return zero-based integer index in Unicode code units,
* of starting position of first-encountered instance of `str` in `NumericString`
*/
def indexOf(str: String): Int =
value.indexOf(str)
/**
* Returns zero-based index (in Unicode code units: logical index of characters) of starting-character position
* of first-encountered match of `str` within `NumericString`, beginning search at zero-based index `fromIndex`;
* returns -1 if no fully-matching substring is found, or if `fromIndex` is outside the bounds of `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param str Unicode string to look for
* @param fromIndex zero-based integer index at which to begin search for match of `str` string
* @return zero-based integer index in Unicode code units, of starting position of
* first-encountered instance of `str` in `NumericString` at/beyond `fromIndex`
*/
def indexOf(str: String, fromIndex: Int): Int =
value.indexOf(str, fromIndex)
/**
* Add this immutable `NumericString`'s `String` value to the pool of interned strings,
* so there is only one copy of the string's representation in memory, shared among all instances.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return String which is now in the pool of interned strings
*/
def intern: String =
value.intern
/**
* Returns `true` if `NumericString` contains no characters (not even whitespace); otherwise returns `false`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return `true` if `NumericString` contains no characters (not even whitespace)
* `false` otherwise.
*/
def isEmpty: Boolean =
value.isEmpty
/**
* Returns zero-based index of the final occurrence of the Unicode character `ch` in the `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param ch Unicode character for which to search backwards
* @return zero-based integer index of the final occurrence of this character in `NumericString`;
* -1 if not found
*/
def lastIndexOf(ch: Int): Int =
value.lastIndexOf(ch)
/**
* Returns zero-based index of the final occurrence of the Unicode character `ch` in the `NumericString`, with search beginning
* at zero-based `fromIndex` and proceeding backwards.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param ch Unicode character for which to search backwards
* @param fromIndex zero-based index of starting position
* @return zero-based index of the final (rightmost) occurrence of this character in `NumericString`;
* -1 if not found
*/
def lastIndexOf(ch: Int, fromIndex: Int): Int =
value.lastIndexOf(ch, fromIndex)
/**
* Returns zero-based index from the beginning of `NumericString` of the first character position for where the string `str`
* fully matched rightmost within `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param str string for comparison
* @return zero-based integer index of first character position of where `str` fully matched rightmost within `NumericString`;
* -1 if not found
*/
def lastIndexOf(str: String): Int =
value.lastIndexOf(str)
/**
* Returns zero-based index from the beginning of `NumericString` of the first character position for where the string `str`
* fully matched rightmost within `NumericString`, with search beginning at zero-based `fromIndex` and proceeding backwards.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param str string for comparison
* @param fromIndex zero-based index of starting position
* @return zero-based integer index of first character position of where `str` fully matched rightmost within `NumericString`;
* -1 if not found
*/
def lastIndexOf(str: String, fromIndex: Int): Int =
value.lastIndexOf(str, fromIndex)
/**
* Returns `true` if the this `NumericString`'s `String` value matches the supplied regular expression, `regex`; otherwise returns `false`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param regex regular-expression string
* @return `true` if `NumericString` content matches supplied regular expression `regex`;
* `false` otherwise.
*/
def matches(regex: String): Boolean =
value.matches(regex)
// SKIP-SCALATESTJS,NATIVE-START
// This method returns the index of the character that is sitting at the position
// computed by starting at index, and hopping over to the right codePointOffset code points.
/**
* Returns the "byte distance" required from start of string,
* to reach the position of the supplied byte `index` plus the number of `codePointOffset` points further.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param index byte index of start position in spacing computation
* @param codePointOffset how many code points to advance (may be variable length per code point)
* @return zero-based offset in bytes from start of `NumericString`, to reach the designated position
*/
def offsetByCodePoints(index: Int, codePointOffset: Int): Int =
value.offsetByCodePoints(index, codePointOffset)
// SKIP-SCALATESTJS,NATIVE-END
/**
* Returns `true` if the given region of text matches completely for the `len` characters beginning at `toffset` in the `NumericString` text
* and at `ooffset` in the supplied `other` string text, with the option to `ignoreCase` during matching; otherwise returns `false`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param ignoreCase if nonzero, comparison ignores case
* @param toffset zero-based offset of start point of comparison in `NumericString` text
* @param other string supplied for comparison
* @param ooffset zero-based offset of start point of comparison in `other` string text
* @param len length of comparison, in characters
* @return `true` if region of text matches completely for given length;
* `false` otherwise.
*/
def regionMatches(ignoreCase: Boolean, toffset: Int, other: String, ooffset: Int, len: Int): Boolean =
value.regionMatches(ignoreCase, toffset, other, ooffset, len)
/**
* Returns `true` if the given region of text matches completely for the `len` characters beginning at `toffset` in the `NumericString` text
* and at `ooffset` in the supplied `other` string text; otherwise returns `false`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param toffset zero-based offset of start point of comparison in `NumericString` text
* @param other string supplied for comparison
* @param ooffset zero-based offset of start point of comparison in `other` string text
* @param len length of comparison, in characters
* @return `true` if region of text matches completely for given length;
* `false` otherwise.
*/
def regionMatches(toffset: Int, other: String, ooffset: Int, len: Int): Boolean =
value.regionMatches(toffset, other, ooffset, len)
/**
* Returns the new `String` resulting from replacing all occurrences of `oldChar` with `newChar`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param oldChar character to replace
* @param newChar character that will take its place
* @return string resulting from zero or more replacement(s)
*/
def replace(oldChar: Char, newChar: Char): String =
value.replace(oldChar, newChar)
/**
* Returns the new `String` resulting from replacing all occurrences of `CharSequence` `target` with `replacement`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param target character sequence to replace
* @param replacement character sequence that will take its place
* @return string resulting from zero or more replacement(s)
*/
def replace(target: CharSequence, replacement: CharSequence): String =
value.replace(target, replacement)
/**
* Returns the new `String` resulting from replacing all `regex` string matches with the `replacement` string.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param regex regular expression string
* @param replacement string to replace in place of any regular expression matches in the original `NumericString`
* @return string resulting from zero or more replacement(s)
*/
def replaceAll(regex: String, replacement: String): String =
value.replaceAll(regex, replacement)
/**
* Returns the new `String` resulting from replacing the first-found `regex` string match with the `replacement` string.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param regex regular expression string
* @param replacement string to replace in place of first regular expression match in the original `NumericString`
* @return string resulting from zero or one replacement(s)
*/
def replaceFirst(regex: String, replacement: String): String =
value.replaceFirst(regex, replacement)
/**
* Returns an array of strings produced by splitting `NumericString` at every location matching the supplied `regex`;
* the `regex`-matching characters are omitted from the output.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param regex string for pattern matching
* @return array of strings produced by splitting `NumericString` at every location matching supplied `regex`
*/
def split(regex: String): Array[String] =
value.split(regex)
/**
* Returns an array of strings produced by splitting `NumericString` at up to `limit` locations matching the supplied `regex`;
* the `regex`-matching characters are omitted from the output.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param regex string for pattern matching
* @param limit maximum number of split output strings that will be generated by this function; further potential splits get ignored
* @return array of strings produced by splitting `NumericString` at every location matching supplied `regex`, up to `limit` occurrences
*/
def split(regex: String, limit: Int): Array[String] =
value.split(regex, limit)
/**
* Returns `true` if the `NumericString` content completely matches the supplied `prefix`, when both strings are aligned at their startpoints
* up to the length of `prefix`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param prefix string for comparison as prefix
* @return `true` if `NumericString` content is the same as `prefix` for the entire length of `prefix`
* `false` otherwise.
*/
def startsWith(prefix: String): Boolean =
value.startsWith(prefix)
/**
* Returns `true` if the `NumericString` content completely matches the supplied `prefix`, starting at `toffset` characters in the `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param prefix string for comparison as prefix
* @param toffset zero-based integer start point for comparison within the `NumericString`
* @return `true` if `NumericString` content is the same as `prefix` for the entire length of `prefix` shifted over,
* `false` otherwise.
*/
def startsWith(prefix: String, toffset: Int): Boolean =
value.startsWith(prefix, toffset)
/**
* Returns the character sequence extracted from `NumericString` beginning at zero-based offset `beginIndex`
* and continuing until (but not including) offset `endIndex`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param beginIndex zero-based integer offset at which character extraction begins
* @param endIndex zero-based integer offset before which character extraction ends
* @return CharSequence of zero or more extracted characters
*/
def subSequence(beginIndex: Int, endIndex: Int): CharSequence =
value.subSequence(beginIndex, endIndex)
/**
* Returns a string extracted from `NumericString` from the zero-based `beginIndex` through the end of the string.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param beginIndex zero-based integer offset at which substring extraction begins (continues through end of `NumericIndex` string)
* @return returns string extracted from `NumericString`
*/
def substring(beginIndex: Int): String =
value.substring(beginIndex)
/**
* Returns a string extracted `NumericString` starting from the zero-based `beginIndex` through but not including the zero-based `endIndex` offset.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param beginIndex zero-based integer offset at which substring extraction begins
* @param endIndex zero-based integer offset before which substring extraction ends
* @return returns string extracted from `NumericString`
*/
def substring(beginIndex: Int, endIndex: Int): String =
value.substring(beginIndex, endIndex)
/**
* Returns an array of Unicode characters corresponding to the `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return array of Unicode characters corresponding to the `NumericString`
*/
def toCharArray: Array[Char] =
value.toCharArray
// SKIP-SCALATESTJS,NATIVE-START
// These should stay because we'll use them in other String AnyVals, but
// they don't make sense to invoke on NumericString because they would have no effect.
// So I commmented them out. Please leave the comments here so we can use these as
// a template for the other AnyVals which we'll add soon.
/**
* Returns the string resulting from converting any upper-case characters in `NumericString` into lower-case.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return string corresponding to the original string but with upper case characters converted to lower case
def toLowerCase: String =
value.toLowerCase
*/
/**
* Returns the string resulting from converting any upper-case characters in `NumericString` into lower-case
* where case mapping is inferred from the `locale`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param locale locale mapping to determine case equivalence
* @return string corresponding to the original string but with upper case characters converted to lower case
def toLowerCase(locale: Locale): String =
value.toLowerCase(locale: Locale)
*/
/**
* Returns the string resulting from converting any lower-case characters in `NumericString` into upper-case.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return string corresponding to the original string but with lower case characters converted to upper case
def toUpperCase: String =
value.toUpperCase
*/
/**
* Returns the string resulting from converting any lower-case characters in `NumericString` into upper-case
* where case mapping is inferred from the `locale`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @param locale locale mapping to determine case equivalence
* @return string corresponding to the original string but with lower case characters converted to upper case
def toUpperCase(locale: Locale): String =
value.toUpperCase(locale: Locale)
*/
// SKIP-SCALATESTJS,NATIVE-END
/**
* Return new string resulting from removing any whitespace characters from the start and end of the `NumericString`.
*
* For more detail, see the documentation for the corresponding method in the Javadoc documentation for java.lang.String.
*
* @return string resulting from removing any whitespace characters from start and end of original string
*/
def trim: String =
value.trim
/**
* Returns a new <code>NumericString</code> concatenating this
* <code>NumericString</code> with the passed <code>NumericString</code>.
*
* @param that the <code>NumericString</code> to append
* @return a new <code>NumericString</code> that concatenates
* this <code>NumericString</code> with <code>that</code>.
*/
def concatNumericString(that: NumericString): NumericString =
new NumericString(value ++ that.value)
/**
* Return a <code>NumericString</code> consisting of the current
* <code>NumericString</code> concatenated <code>n</code> times.
*/
def *(n: Int): NumericString =
new NumericString(value * n)
/**
* Returns a new <code>String</code> concatenating this
* <code>NumericString</code> with the passed <code>String</code>.
*
* @param that the <code>String</code> to append
* @return a new `String` which contains all elements of this
* `NumericString` followed by all elements of that
*/
def ++(that: String): String =
value ++ that
/**
* Returns a new <code>String</code> consisting of this
* <code>NumericString</code> prepended by the passed
* <code>String</code>.
*
* @param that the <code>String</code> to append
* @return a new <code>String</code> which contains all elements of that
* followed by all elements of this <code>NumericString</code>
*/
def ++:(that: String): String =
that ++: value
/**
* Returns a new <code>String</code> consisting of this
* <code>NumericString</code> prepended by the passed
* <code>Char</code>.
*
* @param elem the prepended `Char`
* @return a new <code>String</code> consisting of `elem`
* followed by all characters from this <code>NumericString</code>
*/
def +:(elem: Char): String =
elem +: value
/**
* Applies a binary operator to a start value and all elements
* of this `NumericString`, going left to right.
*
* Note: `/:` is alternate syntax for `foldLeft`; `z /: xs` is the
* same as `xs foldLeft z`.
*
* @param z the start value
* @param op the binary operator
* @return the result of inserting `op` between consecutive
* `Chars` of this `NumericString`, going left to right with the
* start value `z` on the left:
* {{{
* op(...op(op(z, x_1), x_2), ..., x_n)
* }}}
* where `x,,1,,, ..., x,,n,,` are the characters of this
* `NumericString`.
*/
def /:(z: Int)(op: (Int, Char) => Int): Int =
(z /: value)(op)
/**
* Returns a new `String` consisting of this `NumericString` with the
* passed `Char` appended.
*
* @param elem the appended `Char`
* @return a new `String` consisting of all elements of this `NumericString`
* followed by `elem`
*/
def :+(elem: Char): String =
value :+ elem
/**
* Applies a binary operator to all elements of this
* `NumericString` and a start value, going right to
* left.
*
* Note: :\ is alternate syntax for foldRight; xs :\ z is the
* same as xs foldRight z.
*
* @param z the start value
* @param op the binary operator
* @return the result of inserting `op` between consecutive
* characters of this `NumericString`, going right to left with the
* start value `z` on the right:
* {{{
* op(x_1, op(x_2, ... op(x_n, z)...))
* }}}
* where `x,,1,,, ..., x,,n,,` are the characters of this
* `NumericString`.
*/
def :\(z: Int)(op: (Char, Int) => Int): Int =
(value :\ z)(op)
/**
* Returns true if `this` is less than `that`
*/
def <(that: String): Boolean =
(value < that)
/**
* Returns true if `this` is less than or equal to `that`
*/
def <=(that: String): Boolean =
(value <= that)
/**
* Returns true if `this` is greater than `that`
*/
def >(that: String): Boolean =
(value > that)
/**
* Returns true if `this` is greater than or equal to `that`
*/
def >=(that: String): Boolean =
(value >= that)
/**
* Appends string value of this `NumericString` to a string builder.
*
* @param b the string builder to which this `NumericString` gets appended
* @return the string builder b to which this `NumericString` was appended
*/
def addString(b: StringBuilder): StringBuilder =
value.addString(b)
/**
* Appends character elements of this `NumericString` to a string builder
* using a separator string.
*
* @param b the string builder to which elements are appended
* @param sep the separator string
* @return the string builder b to which elements were appended
*/
def addString(b: StringBuilder, sep: String): StringBuilder =
value.addString(b, sep)
/**
* Appends character elements of this `NumericString` to a
* string builder using start, separator, and end strings. The
* written text begins with the string `start` and ends with the
* string `end`. Inside, the characters of this `NumericString`
* are separated by the string `sep`.
*
* @param b the string builder to which elements are appended
* @param start the starting string
* @param sep the separator string
* @param end the ending string
* @return the string builder b to which elements were appended
*/
def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder =
value.addString(b, start, sep, end)
/**
* Aggregates the results of applying an operator to subsequent elements
* of this `NumericString`.
*
* This is a more general form of `fold` and `reduce`. It is
* similar to `foldLeft` in that it doesn't require the result
* to be a supertype of the element type.
*
* `aggregate` splits the elements of this `NumericString` into
* partitions and processes each partition by sequentially
* applying `seqop`, starting with `z` (like `foldLeft`). Those
* intermediate results are then combined by using `combop`
* (like `fold`). The implementation of this operation may
* operate on an arbitrary number of collection partitions
* (even 1), so `combop` may be invoked an arbitrary number of
* times (even 0).
*
* As an example, consider summing up the integer values the
* character elements. The initial value for the sum is
* 0. First, `seqop` transforms each input character to an Int
* and adds it to the sum (of the partition). Then, `combop`
* just needs to sum up the intermediate results of the
* partitions:
* {{{
* NumericString("123").aggregate(0)({ (sum, ch) => sum + ch.toInt }, { (p1, p2) => p1 + p2 })
* }}}
*
* @param B the type of accumulated results
* @param z the initial value for the accumulated result of the partition -
* this will typically be the neutral element for the
* `seqop` operator (e.g. `Nil` for list
* concatenation or `0` for summation) and may be
* evaluated more than once
* @param seqop an operator used to accumulate results within a partition
* @param combop an associative operator used to combine results within a
* partition
*/
def aggregate[B](z: ⇒ B)(seqop: (B, Char) ⇒ B, combop: (B, B) ⇒ B): B =
ColCompatHelper.aggregate(value, z)(seqop, combop)
/**
* Return character at index `index`.
*
* @return the character of this string at index `index`, where
* `0` indicates the first element.
*/
def apply(index: Int): Char =
value(index)
/**
* Method called from equality methods, so that user-defined
* subclasses can refuse to be equal to other collections of
* the same kind.
*
* @param that the object with which this `NumericString` should be compared
* @return `true` if this `NumericString` can possibly equal
* `that`, `false` otherwise. The test takes into
* consideration only the run-time types of objects but
* ignores their elements.
*/
def canEqual(that: Any): Boolean =
that.isInstanceOf[NumericString]
/**
* Returns this string with first character converted to upper case (i.e.
* unchanged for a `NumericString`).
*
* @return the string value of this `NumericString`.
*/
def capitalize: String =
value.capitalize
/*
def chars: java.util.stream.IntStream =
value.chars
def codePoints: java.util.stream.IntStream =
value.codePoints
*/
/**
* Builds a new collection by applying a partial function to
* all characters of this `NumericString` on which the function
* is defined.
*
* @param pf the partial function which filters and maps the elements.
* @tparam B the element type of the returned collection.
* @return a new String resulting from applying the partial
* function `pf` to each element on which it is
* defined and collecting the results. The
* order of the elements is preserved.
*/
def collect(pf: PartialFunction[Char, Char]) =
value.collect(pf)
/**
* Finds the first character of the `NumericString` for
* which the given partial function is defined, and applies
* the partial function to it.
*
* @param pf the partial function
* @return an option value containing pf applied to the first
* value for which it is defined, or `None` if none exists.
*/
def collectFirst[B](pf: PartialFunction[Char, B]): Option[B] =
value.collectFirst(pf)
/**
* Iterates over combinations. A _combination_ of length `n`
* is a subsequence of the original sequence, with the
* elements taken in order. Thus, `"xy"` and `"yy"` are both
* length-2 combinations of `"xyy"`, but `"yx"` is not. If
* there is more than one way to generate the same
* subsequence, only one will be returned.
*
* For example, `"xyyy"` has three different ways to generate
* `"xy"` depending on whether the first, second, or third
* `"y"` is selected. However, since all are identical, only
* one will be chosen. Which of the three will be taken is an
* implementation detail that is not defined.
*
* @return An Iterator which traverses the possible n-element
* combinations of this `NumericString`.
* @example `NumericString("12223").combinations(2) = Iterator(12, 13, 22, 23)`
*/
def combinations(n: Int): Iterator[String] =
value.combinations(n)
/**
* Result of comparing `this` with operand `that`.
*
* Implement this method to determine how instances of A will be sorted.
*
* Returns `x` where:
*
* - `x < 0` when `this < that`
*
* - `x == 0` when `this == that`
*
* - `x > 0` when `this > that`