java字符范围_java判断字符范围(URI的实现)

核心思想:字符有个数字,然后再进行移位,再与原先的字符进行与比较,如下:

private static long lowMask(String chars) {

int n = chars.length();

long m = 0;

for (int i = 0; i < n; i++) {

char c = chars.charAt(i);

if (c < 64)

m |= (1L << c);

}

return m;

}

// Compute the high-order mask for the characters in the given string

private static long highMask(String chars) {

int n = chars.length();

long m = 0;

for (int i = 0; i < n; i++) {

char c = chars.charAt(i);

if ((c >= 64) && (c < 128))

m |= (1L << (c - 64));

}

return m;

}

// Compute a low-order mask for the characters

// between first and last, inclusive

private static long lowMask(char first, char last) {

long m = 0;

long result = 0;

int f = Math.max(Math.min(first, 63), 0);

int l = Math.max(Math.min(last, 63), 0);

for (int i = f; i <= l; i++){

long temp = 1L << i;

result += temp;

m |= 1L << i;

//m |= i;

}

return m;

}

// Compute a high-order mask for the characters

// between first and last, inclusive

private static long highMask(char first, char last) {

long m = 0;

int f = Math.max(Math.min(first, 127), 64) - 64;

int l = Math.max(Math.min(last, 127), 64) - 64;

for (int i = f; i <= l; i++)

m |= 1L << i;

return m;

}

// Tell whether the given character is permitted by the given mask pair

private static boolean match(char c, long lowMask, long highMask) {

if (c < 64)

return ((1L << c) & lowMask) != 0;

if (c < 128)

return ((1L << (c - 64)) & highMask) != 0;

return false;

}

@Test

public void testMatch(){

long lowMask = lowMask('0','9');

System.out.println("x=="+(int)'x');

boolean ismatch = match('x',lowMask,0l);

System.out.println("lowMask============="+lowMask+","+ismatch);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值