fix StrBuilder bug

This commit is contained in:
Looly
2019-09-27 19:35:39 +08:00
parent 60f3970b04
commit 4f811fc984
4 changed files with 123 additions and 66 deletions

View File

@@ -13,6 +13,8 @@
* 【extra】 修复Mail中sslEnable无效问题pr#74@Gitee * 【extra】 修复Mail中sslEnable无效问题pr#74@Gitee
* 【extra】 修复CsvParser中最后一行双引号没有去除的问题pr#73@Gitee * 【extra】 修复CsvParser中最后一行双引号没有去除的问题pr#73@Gitee
* 【crypto】 修复SM2算法在自定义密钥时无效问题issue#I12P5I@Gitee * 【crypto】 修复SM2算法在自定义密钥时无效问题issue#I12P5I@Gitee
* 【core】 修复StopWatch.prettyPrint条件问题issue#I12RAC@Gitee
* 【core】 修复StrBuilder.del无法删除最后一个字符的问题issue#I12R14@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@@ -312,7 +312,7 @@ public class StopWatch {
public String prettyPrint() { public String prettyPrint() {
StringBuilder sb = new StringBuilder(shortSummary()); StringBuilder sb = new StringBuilder(shortSummary());
sb.append(FileUtil.getLineSeparator()); sb.append(FileUtil.getLineSeparator());
if (null != this.taskList) { if (null == this.taskList) {
sb.append("No task info kept"); sb.append("No task info kept");
} else { } else {
sb.append("---------------------------------------------").append(FileUtil.getLineSeparator()); sb.append("---------------------------------------------").append(FileUtil.getLineSeparator());

View File

@@ -1,12 +1,12 @@
package cn.hutool.core.text; package cn.hutool.core.text;
import java.io.Serializable;
import java.util.Arrays;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import java.io.Serializable;
import java.util.Arrays;
/** /**
* 可复用的字符串生成器,非线程安全 * 可复用的字符串生成器,非线程安全
* *
@@ -16,16 +16,23 @@ import cn.hutool.core.util.StrUtil;
public class StrBuilder implements CharSequence, Appendable, Serializable { public class StrBuilder implements CharSequence, Appendable, Serializable {
private static final long serialVersionUID = 6341229705927508451L; private static final long serialVersionUID = 6341229705927508451L;
/** 默认容量 */ /**
* 默认容量
*/
public static final int DEFAULT_CAPACITY = 16; public static final int DEFAULT_CAPACITY = 16;
/** 存放的字符数组 */ /**
* 存放的字符数组
*/
private char[] value; private char[] value;
/** 当前指针位置,或者叫做已经加入的字符数,此位置总在最后一个字符之后 */ /**
* 当前指针位置,或者叫做已经加入的字符数,此位置总在最后一个字符之后
*/
private int position; private int position;
/** /**
* 创建字符串构建器 * 创建字符串构建器
*
* @return {@link StrBuilder} * @return {@link StrBuilder}
*/ */
public static StrBuilder create() { public static StrBuilder create() {
@@ -34,6 +41,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
/** /**
* 创建字符串构建器 * 创建字符串构建器
*
* @param initialCapacity 初始容量 * @param initialCapacity 初始容量
* @return {@link StrBuilder} * @return {@link StrBuilder}
*/ */
@@ -43,6 +51,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
/** /**
* 创建字符串构建器 * 创建字符串构建器
*
* @param strs 初始字符串 * @param strs 初始字符串
* @return {@link StrBuilder} * @return {@link StrBuilder}
* @since 4.0.1 * @since 4.0.1
@@ -52,6 +61,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
} }
// ------------------------------------------------------------------------------------ Constructor start // ------------------------------------------------------------------------------------ Constructor start
/** /**
* 构造 * 构造
*/ */
@@ -83,6 +93,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
// ------------------------------------------------------------------------------------ Constructor end // ------------------------------------------------------------------------------------ Constructor end
// ------------------------------------------------------------------------------------ Append // ------------------------------------------------------------------------------------ Append
/** /**
* 追加对象,对象会被转换为字符串 * 追加对象,对象会被转换为字符串
* *
@@ -140,6 +151,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
} }
// ------------------------------------------------------------------------------------ Insert // ------------------------------------------------------------------------------------ Insert
/** /**
* 追加对象,对象会被转换为字符串 * 追加对象,对象会被转换为字符串
* *
@@ -289,6 +301,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
} }
// ------------------------------------------------------------------------------------ Others // ------------------------------------------------------------------------------------ Others
/** /**
* 将指定段的字符列表写出到目标字符数组中 * 将指定段的字符列表写出到目标字符数组中
* *
@@ -360,38 +373,48 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
*/ */
public StrBuilder delTo(int newPosition) { public StrBuilder delTo(int newPosition) {
if (newPosition < 0) { if (newPosition < 0) {
this.reset(); newPosition = 0;
} else if (newPosition < this.position) {
this.position = newPosition;
} }
return this; return del(newPosition, this.position);
} }
/** /**
* 删除指定长度的字符 * 删除指定长度的字符,规则如下:
* *
* @param start 开始位置(包括) * <pre>
* @param end 结束位置(不包括 * 1、end大于等于最大长度结束按照最大长度计算相当于删除start之后虽有部分性能最好
* 2、end小于start时抛出StringIndexOutOfBoundsException
* 3、start小于0 按照0处理
* 4、start等于end不处理
* 5、start和end都位于长度区间内删除这段内容内存拷贝
* </pre>
*
* @param start 开始位置负数按照0处理包括
* @param end 结束位置,超出最大长度按照最大长度处理(不包括)
* @return this * @return this
* @throws StringIndexOutOfBoundsException 当start > end抛出此异常
*/ */
public StrBuilder del(int start, int end) { public StrBuilder del(int start, int end) throws StringIndexOutOfBoundsException {
if (start < 0) { if (start < 0) {
start = 0; start = 0;
} }
if (end > this.position) {
end = this.position; if (end >= this.position) {
} // end在边界及以外相当于删除后半部分
if (start > end) {
throw new StringIndexOutOfBoundsException("Start is greater than End.");
}
if (end == this.position) {
this.position = start; this.position = start;
return this;
} else if (end < 0) {
// start和end都为0的情况下表示删除全部
end = 0;
} }
int len = end - start; int len = end - start;
// 截取中间部分,需要将后半部分复制到删除的开始位置
if (len > 0) { if (len > 0) {
System.arraycopy(value, start + len, value, start, this.position - end); System.arraycopy(value, start + len, value, start, this.position - end);
this.position -= len; this.position -= len;
} else if (len < 0) {
throw new StringIndexOutOfBoundsException("Start is greater than End.");
} }
return this; return this;
} }
@@ -470,6 +493,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
} }
// ------------------------------------------------------------------------------------ Private method start // ------------------------------------------------------------------------------------ Private method start
/** /**
* 指定位置之后的数据后移指定长度 * 指定位置之后的数据后移指定长度
* *

View File

@@ -86,4 +86,35 @@ public class StrBuilderTest {
builder.append(123).append(456.123D).append(true).append('\n'); builder.append(123).append(456.123D).append(true).append('\n');
Assert.assertEquals("123456.123true\n", builder.toString()); Assert.assertEquals("123456.123true\n", builder.toString());
} }
@Test
public void delTest() {
// 删除全部测试
StrBuilder strBuilder = new StrBuilder("ABCDEFG");
int length = strBuilder.length();
StrBuilder builder = strBuilder.del(0, length);
Assert.assertEquals("", builder.toString());
}
@Test
public void delTest2() {
// 删除中间部分测试
StrBuilder strBuilder = new StrBuilder("ABCDEFG");
int length = strBuilder.length();
StrBuilder builder = strBuilder.del(2,6);
Assert.assertEquals("ABG", builder.toString());
}
@Test
public void delToTest() {
StrBuilder strBuilder = new StrBuilder("ABCDEFG");
// 不处理
StrBuilder builder = strBuilder.delTo(7);
Assert.assertEquals("ABCDEFG", builder.toString());
// 删除全部
builder = strBuilder.delTo(0);
Assert.assertEquals("", builder.toString());
}
} }