Java 不同的JRE使用什么子字符串搜索算法?
Java 不同的JRE使用什么子字符串搜索算法?,java,algorithm,string,substring,Java,Algorithm,String,Substring,java.lang.StringJavaDoc没有提到默认的indexOf(String)substring搜索算法。所以我的问题是-不同的JRE使用哪些子字符串算法?JDK中有src.zip,它显示了实现: /** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the strin
java.lang.String
JavaDoc没有提到默认的indexOf(String)
substring搜索算法。所以我的问题是-不同的JRE使用哪些子字符串算法?JDK中有src.zip,它显示了实现:
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to begin searching from.
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j] ==
target[k]; j++, k++);
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
/**
*由String和StringBuffer共享以执行搜索的代码。这个
*源是正在搜索的字符数组,目标是
*正在搜索的字符串。
*
*@param source正在搜索的字符。
*@param sourceOffset源字符串的偏移量。
*@param sourceCount源字符串的计数。
*@param目标是要搜索的字符。
*@param targetOffset目标字符串的偏移量。
*@param targetCount目标字符串的计数。
*@param fromIndex开始搜索的索引。
*/
静态int indexOf(char[]source,int sourceOffset,int sourceCount,
char[]目标,int targetOffset,int targetCount,
int fromIndex){
if(fromIndex>=sourceCount){
返回(targetCount==0?sourceCount:-1);
}
如果(从索引<0){
fromIndex=0;
}
如果(targetCount==0){
从索引返回;
}
char first=目标[targetOffset];
int max=sourceOffset+(sourceCount-targetCount);
对于(int i=sourceOffset+fromIndex;i由于大部分时间indexOf用于合理的小字符串中的小子字符串,因此我认为可以假设使用了一种相当直接的算法,如Victor所示。有更先进的算法更适用于大字符串,但这些算法对relati的性能都较差ve短字符串。fwiw(如果此Q是关于不同算法的性能)在适当的硬件上,并使用足够新的oracle jvm(6u21和更高版本,详见),字符串。indexOf
通过相关的SSE 4.2内部函数实现。请参阅本中的第2.3章,以下是目前的发现:
Oracle JDK 1.6/1.7、OpenJDK 6/7
static int indexOf(char[]source,int sourceOffset,int sourceCount,
char[]目标,int targetOffset,int targetCount,
int fromIndex){
if(fromIndex>=sourceCount){
返回(targetCount==0?sourceCount:-1);
}
如果(从索引<0){
fromIndex=0;
}
如果(targetCount==0){
从索引返回;
}
char first=目标[targetOffset];
int max=sourceOffset+(sourceCount-targetCount);
for(int i=sourceOffset+fromIndex;i count)返回-1;//句柄子计数>计数| |开始>=count
int o1=偏移量+i,o2=次偏移量;
而(++o2
SabreSDK
public int indexOf(String str,int from index)
{
如果(从索引<0)
fromIndex=0;
int limit=count-str.count;
对于(;fromIndex,这可能有助于理解:因为Java是跨平台的,所以偏离参考开源代码(处理本机代码/平台相关问题的类除外)没有多大价值@Harry Joy,我明白了,默认Sun(Oracle)JRE使用简单的实现,没有任何技巧——只是一步一步的比较。JRockit和其他的呢?可能它们使用的是相同的。我不确定这一点。@Harry Joy我也是,这就是问题所在。那么其他JRE呢(我的意思是,非标准的)?@Friented Spider我不知道:)但是由于JavaDoc或JLS中没有提到任何限制,所以它可以是任何合理的我自己也有Sun的JDK,所以我想知道其他的实现。@Frozed:为了确定,你必须查看每个JVM运行库的API源代码。JVM没有强制性的标准字符串搜索算法。如果没有足够的答案,我会这么做。@Frozed Spider:如果你真的那么想知道的话具体的实现细节你不应该问。你应该自己去检查,因为依赖答案的准确性是愚蠢的。还要记住,实现可能会因版本而异。
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j] ==
target[k]; j++, k++);
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
public int indexOf(String subString, int start) {
if (start < 0) start = 0;
int subCount = subString.count;
if (subCount > 0) {
if (subCount + start > count) return -1;
char[] target = subString.value;
int subOffset = subString.offset;
char firstChar = target[subOffset];
int end = subOffset + subCount;
while (true) {
int i = indexOf(firstChar, start);
if (i == -1 || subCount + i > count) return -1; // handles subCount > count || start >= count
int o1 = offset + i, o2 = subOffset;
while (++o2 < end && value[++o1] == target[o2]);
if (o2 == end) return i;
start = i + 1;
}
} else return start < count ? start : count;
}
public int indexOf(String str, int fromIndex)
{
if (fromIndex < 0)
fromIndex = 0;
int limit = count - str.count;
for ( ; fromIndex <= limit; fromIndex++)
if (regionMatches(fromIndex, str, 0, str.count))
return fromIndex;
return -1;
}