Merge pull request #4094 from asukavuuyn/v5-dev

fix:charAt越界判断
This commit is contained in:
Golden Looly
2025-10-10 17:32:59 +08:00
committed by GitHub
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){
index = this.position + index;
}
if ((index < 0) || (index > this.position)) {
if ((index < 0) || (index >= this.position)) {
throw new StringIndexOutOfBoundsException(index);
}
return this.value[index];

View File

@@ -131,4 +131,13 @@ public class StrBuilderTest {
helloWorld.insert(6, "Beautiful ");
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));;
}
}