mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-12-07 01:28:34 +08:00
fix:charAt越界判断
This commit is contained in:
parent
f3992c706d
commit
043722e1da
@ -486,7 +486,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
|
|||||||
if(index < 0){
|
if(index < 0){
|
||||||
index = this.position + index;
|
index = this.position + index;
|
||||||
}
|
}
|
||||||
if ((index < 0) || (index > this.position)) {
|
if ((index < 0) || (index >= this.position)) {
|
||||||
throw new StringIndexOutOfBoundsException(index);
|
throw new StringIndexOutOfBoundsException(index);
|
||||||
}
|
}
|
||||||
return this.value[index];
|
return this.value[index];
|
||||||
|
|||||||
@ -131,4 +131,13 @@ public class StrBuilderTest {
|
|||||||
helloWorld.insert(6, "Beautiful ");
|
helloWorld.insert(6, "Beautiful ");
|
||||||
Assertions.assertEquals("Hello Beautiful World", helloWorld.toString());
|
Assertions.assertEquals("Hello Beautiful World", helloWorld.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void charAtTest() {
|
||||||
|
final StrBuilder helloWorld = StrBuilder.create("Hello World");
|
||||||
|
Assertions.assertEquals(helloWorld.charAt(-1),'d');
|
||||||
|
Assertions.assertEquals(helloWorld.charAt(0),'H');
|
||||||
|
Assertions.assertEquals(helloWorld.charAt(10),'d');
|
||||||
|
Assertions.assertThrows(StringIndexOutOfBoundsException.class, () -> helloWorld.charAt(11));;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user