Topic: 刚写了一个indexOfIgnoreCase,大家看看有没有问题

  Print this page

1.刚写了一个indexOfIgnoreCase,大家看看有没有问题 Copy to clipboard
Posted by: rainman
Posted on: 2002-12-09 09:17


public static int indexOfIgnoreCase(String body, String p) {
p = p.toLowerCase();
char[] source = body.toCharArray();
int sourceOffset = 0;
int sourceCount = source.length;
char[] target = p.toCharArray();
int targetOffset = 0;
int targetCount = target.length;
int fromIndex = 0;

if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}

char first = target[targetOffset];
int i = sourceOffset + fromIndex;
int max = sourceOffset + (sourceCount - targetCount);

startSearchForFirstChar:while (true) {

/* Look for first character. */
while (i <= max && Character.toLowerCase(source[ i ]) != first) {
i++;
}
if (i > max) {
return -1;
}

/* Found first character, now look at the rest of v2 */
int j = i + 1;
int end = j + targetCount - 1;
int k = targetOffset + 1;
while (j < end) {
if (Character.toLowerCase(source[j++]) != target[k++]) {
i++;
/* Look for str's first char again. */
continue startSearchForFirstChar;
}
}
return i - sourceOffset; /* Found whole string. */
}
}


2.Re:刚写了一个indexOfIgnoreCase,大家看看有没有问题 [Re: rainman] Copy to clipboard
Posted by: floater
Posted on: 2002-12-10 01:40

while (i <= max && Character.toLowerCase(source) != first) {
There is something wrong here, the index after source is out because of []
(brackets)

BTW, how do you generate this layout?

3.Re:刚写了一个indexOfIgnoreCase,大家看看有没有问题 [Re: rainman] Copy to clipboard
Posted by: floater
Posted on: 2002-12-10 01:55

Forgive my ignorance, but why can't you just do this:

body.toLowerCase().indexOf(p.toLowerCase())

4.呵呵,这样也可以啊! [Re: floater] Copy to clipboard
Posted by: rainman
Posted on: 2002-12-10 02:13

但因为body都比较长啊,toLowerCase和indexOf遍历了两次。我想少遍历一次比较快一点啊。

5.不会有区别的 [Re: rainman] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-10 02:27

I think you won't see any difference in performance in this case, unless you will be processing many (like N times, N > 10000) body texts in one task.

You can make a comparison by executing each one of them and collects the time it takes, and I can guarantee that you won't notice any diffence. They will all finish within one millisecond.

6.呵呵,但确实是要运行许多次啊 [Re: snowbug] Copy to clipboard
Posted by: rainman
Posted on: 2002-12-10 02:50

因为是并发访问Music的,而且一个文章会有n个跟贴,需要n*m次方法执行,可以用Code Coverage测试一下看看。如果一个方法被执行的次数很多,我认为能优化还是要优化一下啊。

7.Agreed :) [Re: rainman] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-10 03:09

还可以试试 RegEx,会很快的说

8.呵呵,想过这招啊 [Re: snowbug] Copy to clipboard
Posted by: rainman
Posted on: 2002-12-10 04:18

jdk1.4内部都用这个。但我也要支持jdk1.3来着,所以只好临时写了一个这样的方法。

9.Re:呵呵,想过这招啊 [Re: rainman] Copy to clipboard
Posted by: Biubiu
Posted on: 2002-12-10 10:39

rainman wrote:
jdk1.4内部都用这个。但我也要支持jdk1.3来着,所以只好临时写了一个这样的方法。


为什么不试一下这个?
http://jakarta.apache.org/regexp/index.html


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923