This commit is contained in:
Looly 2022-09-20 23:02:33 +08:00
parent 7318402204
commit f62a36b045
2 changed files with 23 additions and 2 deletions

View File

@ -85,6 +85,10 @@ public class PunyCode {
} }
// Append delimiter // Append delimiter
if (b > 0) { if (b > 0) {
if(b == length){
// 无需要编码的字符
return output.toString();
}
output.append(DELIMITER); output.append(DELIMITER);
} }
int h = b; int h = b;
@ -158,7 +162,7 @@ public class PunyCode {
if (result.length() != 0) { if (result.length() != 0) {
result.append(CharUtil.DOT); result.append(CharUtil.DOT);
} }
result.append(decode(str)); result.append(StrUtil.startWithIgnoreEquals(str, PUNY_CODE_PREFIX) ? decode(str) : str);
} }
return result.toString(); return result.toString();

View File

@ -16,6 +16,14 @@ public class PunyCodeTest {
Assert.assertEquals(text, decode); Assert.assertEquals(text, decode);
} }
@Test
public void encodeDecodeTest2(){
// 无需编码和解码
String text = "Hutool";
String strPunyCode = PunyCode.encode(text);
Assert.assertEquals("Hutool", strPunyCode);
}
@Test @Test
public void encodeDecodeDomainTest() { public void encodeDecodeDomainTest() {
// 全中文 // 全中文
@ -32,7 +40,7 @@ public class PunyCodeTest {
// 中英文分段 // 中英文分段
final String text = "hutool.中国"; final String text = "hutool.中国";
final String strPunyCode = PunyCode.encodeDomain(text); final String strPunyCode = PunyCode.encodeDomain(text);
Assert.assertEquals("xn--hutool-.xn--fiqs8s", strPunyCode); Assert.assertEquals("hutool.xn--fiqs8s", strPunyCode);
final String decode = PunyCode.decodeDomain(strPunyCode); final String decode = PunyCode.decodeDomain(strPunyCode);
Assert.assertEquals(text, decode); Assert.assertEquals(text, decode);
@ -48,4 +56,13 @@ public class PunyCodeTest {
final String decode = PunyCode.decodeDomain(strPunyCode); final String decode = PunyCode.decodeDomain(strPunyCode);
Assert.assertEquals(text, decode); Assert.assertEquals(text, decode);
} }
@Test
public void encodeEncodeDomainTest2(){
String domain = "赵新虎.com";
String strPunyCode = PunyCode.encodeDomain(domain);
Assert.assertEquals("xn--efvz93e52e.com", strPunyCode);
String decode = PunyCode.decodeDomain(strPunyCode);
Assert.assertEquals(domain, decode);
}
} }