fix:charAt越界判断

This commit is contained in:
asukavuuyn 2025-10-10 00:12:05 +08:00
parent f3992c706d
commit 043722e1da
2 changed files with 10 additions and 1 deletions

View File

@ -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];

View File

@ -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));;
}
} }