mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
增加字节长度验证,应对数据库字节长度与java串长度算法不一致的问题
This commit is contained in:
parent
b8977ea119
commit
ad09bd41bb
@ -3,6 +3,7 @@ package cn.hutool.core.lang;
|
|||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.exceptions.ValidateException;
|
import cn.hutool.core.exceptions.ValidateException;
|
||||||
import cn.hutool.core.regex.PatternPool;
|
import cn.hutool.core.regex.PatternPool;
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
import cn.hutool.core.util.CreditCodeUtil;
|
import cn.hutool.core.util.CreditCodeUtil;
|
||||||
import cn.hutool.core.math.NumberUtil;
|
import cn.hutool.core.math.NumberUtil;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
@ -11,6 +12,7 @@ import cn.hutool.core.text.StrUtil;
|
|||||||
import cn.hutool.core.util.IdcardUtil;
|
import cn.hutool.core.util.IdcardUtil;
|
||||||
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@ -1198,4 +1200,32 @@ public class Validator {
|
|||||||
throw new ValidateException(errorMsg);
|
throw new ValidateException(errorMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证字符串的字节长度是否符合要求,默认采用"utf-8"编码
|
||||||
|
*
|
||||||
|
* @param str 字符串
|
||||||
|
* @param min 最小长度
|
||||||
|
* @param max 最大长度
|
||||||
|
* @param errorMsg 错误消息
|
||||||
|
*/
|
||||||
|
public static void validateByteLength(CharSequence str, int min, int max, String errorMsg) {
|
||||||
|
validateByteLength(str, min, max, CharsetUtil.UTF_8, errorMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证字符串的字节长度是否符合要求
|
||||||
|
*
|
||||||
|
* @param str 字符串
|
||||||
|
* @param min 最小长度
|
||||||
|
* @param max 最大长度
|
||||||
|
* @param charset 字符编码
|
||||||
|
* @param errorMsg 错误消息
|
||||||
|
*/
|
||||||
|
public static void validateByteLength(CharSequence str, int min, int max, Charset charset, String errorMsg) {
|
||||||
|
int len = StrUtil.byteLength(str, charset);
|
||||||
|
if (len < min || len > max) {
|
||||||
|
throw new ValidateException(errorMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import cn.hutool.core.date.DateUtil;
|
|||||||
import cn.hutool.core.exceptions.ValidateException;
|
import cn.hutool.core.exceptions.ValidateException;
|
||||||
import cn.hutool.core.lang.id.IdUtil;
|
import cn.hutool.core.lang.id.IdUtil;
|
||||||
import cn.hutool.core.regex.PatternPool;
|
import cn.hutool.core.regex.PatternPool;
|
||||||
|
import cn.hutool.core.text.StrUtil;
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -273,6 +275,22 @@ public class ValidatorTest {
|
|||||||
Validator.validateLength(s1, 6, 8, "请输入6到8位的字符!"));
|
Validator.validateLength(s1, 6, 8, "请输入6到8位的字符!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validateByteLengthTest() {
|
||||||
|
String s1 = "abc";
|
||||||
|
int len1 = StrUtil.byteLength(s1, CharsetUtil.UTF_8);
|
||||||
|
Assert.assertEquals(len1, 3);
|
||||||
|
|
||||||
|
String s2 = "我ab";
|
||||||
|
int len2 = StrUtil.byteLength(s2, CharsetUtil.UTF_8);
|
||||||
|
Assert.assertEquals(len2, 5);
|
||||||
|
|
||||||
|
//一个汉字在utf-8编码下,占3个字节。
|
||||||
|
Assert.assertThrows(ValidateException.class, ()->
|
||||||
|
Validator.validateByteLength(s2, 1, 3, "您输入的字符串长度超出限制!")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isChineseNameTest(){
|
public void isChineseNameTest(){
|
||||||
Assert.assertTrue(Validator.isChineseName("阿卜杜尼亚孜·毛力尼亚孜"));
|
Assert.assertTrue(Validator.isChineseName("阿卜杜尼亚孜·毛力尼亚孜"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user