File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed
src/reflect/scala/reflect/internal
test/junit/scala/reflect/internal Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -296,11 +296,13 @@ trait Names extends api.Names {
296
296
*/
297
297
final def pos (s : String , start : Int ): Int = {
298
298
var i = pos(s.charAt(0 ), start)
299
- while (i + s.length() <= len) {
299
+ val sLen = s.length()
300
+ if (sLen == 1 ) return i
301
+ while (i + sLen <= len) {
300
302
var j = 1
301
303
while (s.charAt(j) == chrs(index + i + j)) {
302
304
j += 1
303
- if (j == s.length() ) return i
305
+ if (j == sLen ) return i
304
306
}
305
307
i = pos(s.charAt(0 ), i + 1 )
306
308
}
Original file line number Diff line number Diff line change @@ -92,4 +92,29 @@ class NamesTest {
92
92
assert(h1 string_== h2)
93
93
assert(h1 string_== h1y)
94
94
}
95
+
96
+ @ Test
97
+ def pos (): Unit = {
98
+ def check (nameString : String , sub : String ) = {
99
+ val name = TermName (nameString)
100
+ val javaResult = name.toString.indexOf(sub) match { case - 1 => name.length case x => x }
101
+ val nameResult = name.pos(sub)
102
+ assertEquals(javaResult, nameResult)
103
+ if (sub.length == 1 ) {
104
+ val nameResultChar = name.pos(sub.head)
105
+ assertEquals(javaResult, nameResultChar)
106
+ }
107
+ }
108
+
109
+ check(" a" , " a" ) // was "String index out of range: 1
110
+ check(" a" , " b" )
111
+ check(" a" , " ab" )
112
+ check(" a" , " ba" )
113
+ check(" ab" , " a" )
114
+ check(" ab" , " b" )
115
+ check(" ab" , " ab" )
116
+ check(" ab" , " ba" )
117
+ check(" " , " x" )
118
+ check(" " , " xy" )
119
+ }
95
120
}
You can’t perform that action at this time.
0 commit comments