This commit is contained in:
Looly
2025-12-26 00:14:55 +08:00
parent 4816333c85
commit 7a2b86a4bf
2 changed files with 10 additions and 6 deletions

View File

@@ -4784,18 +4784,22 @@ public class CharSequenceUtil {
throw new IndexOutOfBoundsException("Invalid range: [" + startInclude + ", " + endExclude + ")");
}
if (Math.abs(moveLength) > len) {
// 循环位移,当越界时循环
moveLength = moveLength % len;
}
// 分离“移动块”和“剩余轨道”
String block = str.subSequence(startInclude, endExclude).toString();
String rest = new StringBuilder(str).delete(startInclude, endExclude).toString();
final String block = str.subSequence(startInclude, endExclude).toString();
final String rest = new StringBuilder(str).delete(startInclude, endExclude).toString();
int restLen = rest.length();
final int restLen = rest.length();
if (restLen == 0) {
return str.toString(); // 全选时位移无意义
}
// 计算循环周期:剩余字符数 + 1代表块可以存在的不同位置点
int totalPositions = restLen + 1;
final int totalPositions = restLen + 1;
// 计算移动后的新位置 (处理正负数)
int newPos = (startInclude + moveLength % totalPositions + totalPositions) % totalPositions;
final int newPos = (startInclude + moveLength % totalPositions + totalPositions) % totalPositions;
// 重新组装
return rest.substring(0, newPos) + // 放入新位置前的剩余字符
block + // 放入整体块

View File

@@ -23,7 +23,7 @@
<slf4j.version>1.7.36</slf4j.version>
<logback.version>1.4.14</logback.version>
<log4j.version>1.2.17</log4j.version>
<log4j2.version>2.20.0</log4j2.version>
<log4j2.version>2.25.3</log4j2.version>
<commons-logging.version>1.3.3</commons-logging.version>
<tinylog.version>1.3.6</tinylog.version>
<tinylog2.version>2.7.0</tinylog2.version>