long AnsiString::pos(const char* plainString) const {
const size_t patternLength = strlen(plainString);
if (patternLength == 0) return -1;
size_t stringLength = count;
int partialMatch[patternLength]; // int* partialMatch = new int[patternLength];
KMPBuildPartialMatchTable(plainString, partialMatch);
int currentStringCharacter = 0;
int currentPatternCharacter = 0;
while (currentStringCharacter < stringLength) {
if (currentPatternCharacter == -1) {
currentStringCharacter++;
currentPatternCharacter = 0;
}
else if (items[currentStringCharacter] == plainString[currentPatternCharacter]) {
currentStringCharacter++;
currentPatternCharacter++;
if (currentPatternCharacter == patternLength) return currentStringCharacter - currentPatternCharacter;
} else {
currentPatternCharacter = partialMatch[currentPatternCharacter];
}
}
// delete(partialMatch);
return -1;
}
I get an error in the implementaation of this claas method using visual c++.
int partialMatch[ patternLength ] ; // expression must have a constant value
(I'm using VS in other language so you could find some differences).
I declared patternLength as a constant as you can see. A solution is commented in the ccode but i don't want to use dynamic memory allocation. Some idea ?