--- nsNavHistoryAutoComplete.cpp.orig 2008-08-12 10:53:55.000000000 -0400 +++ nsNavHistoryAutoComplete.cpp 2008-09-04 09:27:24.000000000 -0400 @@ -41,7 +41,6 @@ * * ***** END LICENSE BLOCK ***** */ - /** * Autocomplete algorithm: * @@ -154,6 +153,43 @@ } /** + * ADDED BY pgengler + * + * Given a string with a URL, removes any leading protocol prefix and 'www.' bit + * + * @param str + * String to remove prefixes from + * + */ +char *RemovePrefix(char *str) +{ + // Chop out any protocol prefix + int len = strlen(str); + for (int i = 0; i < len - 2; i++) { +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: Processing string, which is '%s' (i == %d, len == %d)\n", __FILE__, __LINE__, (str + i), i, len); +#endif + if (str[i] == ':' && str[i + 1] == '/' && str[i + 2] == '/') { + str += (i + 3); + len -= i; + i = -1; +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: Removing protocol prefix; remaining string is '%s'\n", __FILE__, __LINE__, str); +#endif + } else if (str[i] == 'w' && str[i + 1] == 'w' && str[i + 2] == 'w' && str[i + 3] == '.') { + str += (i + 4); + len -= i; + i = -1; +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: Removing 'www' prefix; remaining string is '%s'\n", __FILE__, __LINE__, str); +#endif + break; + } + } + return str; +} + +/** * Returns true if the token matches the target on a word boundary * * @param aToken @@ -171,48 +207,51 @@ if (aTarget.IsEmpty()) return PR_FALSE; - nsAString::const_iterator tokenStart, tokenEnd; - aToken.BeginReading(tokenStart); - aToken.EndReading(tokenEnd); - - nsAString::const_iterator targetStart, targetEnd; - aTarget.BeginReading(targetStart); - aTarget.EndReading(targetEnd); - - // Go straight into checking the token at the beginning of the target because - // the beginning is considered a word boundary - do { - // We're on a word boundary, so prepare to match by copying the iterators - nsAString::const_iterator testToken(tokenStart); - nsAString::const_iterator testTarget(targetStart); - - // Keep trying to match the token one by one until it doesn't match - while (!caseInsensitiveCompare(*testToken, *testTarget)) { - // We matched something, so move down one - testToken++; - testTarget++; - - // Matched the token! We're done! - if (testToken == tokenEnd) - return PR_TRUE; - - // If we ran into the end while matching the token, we won't find it - if (testTarget == targetEnd) - return PR_FALSE; - } - - // Unconditionally move past the current position in the target, but if - // we're not currently on a word boundary, eat up as many non-word boundary - // characters as possible -- don't kill characters if we're currently on a - // word boundary so that we can match tokens that start on a word boundary. - if (!IsWordBoundary(ToLowerCase(*targetStart++))) - while (targetStart != targetEnd && !IsWordBoundary(*targetStart)) - targetStart++; - - // If we hit the end eating up non-boundaries then boundaries, we're done - } while (targetStart != targetEnd); - - return PR_FALSE; +/****** pgengler *******/ + // Get C-style strings to use + char *token = (char *)ToNewCString(aToken); + char *target = (char *)ToNewCString(aTarget); + +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: FindOnBoundary(): token == '%s', target == '%s'\n", __FILE__, __LINE__, token, target); +#endif + + char *sToken = RemovePrefix(token); + char *sTarget = RemovePrefix(target); + +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: target is now '%s'\n", __FILE__, __LINE__, sTarget); +#endif + + // Now start checking token against target + int found = 1; + for (int i = 0; i < strlen(sToken); i++) { + if (sToken[i] != sTarget[i]) { + found = false; + break; + } + } + + // Free up memory +#ifndef DEBUG_pgengler + NS_Free(token); + NS_Free(target); +#endif + + if (found == 1) { +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: fragment '%s' matched target '%s'\n", __FILE__, __LINE__, sToken, sTarget); + NS_Free(token); + NS_Free(target); +#endif + return PR_TRUE; + } + +#ifdef DEBUG_pgengler + NS_Free(token); + NS_Free(target); +#endif + return PR_FALSE; } /** @@ -335,19 +374,28 @@ if (!mCurrentListener) return NS_OK; +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: PerformAutoComplete(): mCurrentSearchString == '%s'\n", __FILE__, __LINE__, (char *)mCurrentSearchString.get()); +#endif mCurrentResult->SetSearchString(mCurrentSearchString); nsresult rv; // Only do some extra searches on the first chunk if (!mCurrentChunkOffset) { // Get adaptive results first - rv = AutoCompleteAdaptiveSearch(); - NS_ENSURE_SUCCESS(rv, rv); +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: calling AutoCompleteAdaptiveSearch()\n", __FILE__, __LINE__); +#endif +/* rv = AutoCompleteAdaptiveSearch(); + NS_ENSURE_SUCCESS(rv, rv);*/ } PRBool moreChunksToSearch = PR_FALSE; // If we constructed a previous search query, use it instead of full if (mDBPreviousQuery) { +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: calling AutoCompletePreviousSearch()\n", __FILE__, __LINE__); +#endif rv = AutoCompletePreviousSearch(); NS_ENSURE_SUCCESS(rv, rv); @@ -356,6 +404,9 @@ if (moreChunksToSearch = mPreviousChunkOffset != -1) mCurrentChunkOffset = mPreviousChunkOffset - mAutoCompleteSearchChunkSize; } else { +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: Calling AutoCompleteFullHistorySearch()\n", __FILE__, __LINE__); +#endif rv = AutoCompleteFullHistorySearch(&moreChunksToSearch); NS_ENSURE_SUCCESS(rv, rv); } @@ -511,6 +562,9 @@ // Bind the parameters right away. We can only use the query once. for (PRUint32 i = 0; i < prevMatchCount; i++) { rv = mDBPreviousQuery->BindStringParameter(i + 1, *urls[i]); +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: Binding param '%s'\n", __FILE__, __LINE__, (char *)NS_ConvertUTF16toUTF8(*urls[i]).get()); +#endif NS_ENSURE_SUCCESS(rv, rv); } @@ -582,6 +636,9 @@ { // Split the search string into multiple search tokens nsString::const_iterator strStart, strEnd; +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: mCurrentSearchString == '%s'\n", __FILE__, __LINE__, (char *)mCurrentSearchString.get()); +#endif mCurrentSearchString.BeginReading(strStart); mCurrentSearchString.EndReading(strEnd); nsString::const_iterator start = strStart, end = strEnd; @@ -603,9 +660,13 @@ inline void nsNavHistory::AddSearchToken(nsAutoString &aToken) { - aToken.Trim("\r\n\t\b"); - if (!aToken.IsEmpty()) - mCurrentSearchTokens.AppendString(aToken); + aToken.Trim("\r\n\t\b"); + if (!aToken.IsEmpty()) { +#ifdef DEBUG_pgengler + fprintf(stderr, "%s:%d: appending '%s' to token array\n", __FILE__, __LINE__, (char *)ToNewCString(aToken)); +#endif + mCurrentSearchTokens.AppendString(aToken); + } } nsresult @@ -616,6 +677,10 @@ nsresult rv = mDBAdaptiveQuery->BindInt32Parameter(0, GetTagsFolder()); NS_ENSURE_SUCCESS(rv, rv); +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: binding mCurrentSearchString == '%s'\n", __FILE__, __LINE__, (char *)NS_ConvertUTF16toUTF8(mCurrentSearchString).get()); +#endif + rv = mDBAdaptiveQuery->BindStringParameter(1, mCurrentSearchString); NS_ENSURE_SUCCESS(rv, rv); @@ -674,10 +739,21 @@ const QueryType aType, PRBool *aHasMoreResults) { +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: in AutoCompleteProcessSearch()\n", __FILE__, __LINE__); +#endif // Unless we're checking if there are any results for the query, don't bother // processing results if we already have enough results - if (!aHasMoreResults && AutoCompleteHasEnoughResults()) + if (!aHasMoreResults && AutoCompleteHasEnoughResults()) { +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: search either has no results, or has returned enough\n", __FILE__, __LINE__); +#endif return NS_OK; + } + +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: search still has results\n", __FILE__, __LINE__); +#endif nsFaviconService* faviconService = nsFaviconService::GetFaviconService(); NS_ENSURE_TRUE(faviconService, NS_ERROR_OUT_OF_MEMORY); @@ -693,10 +769,17 @@ PRBool hasMore = PR_FALSE; // Determine the result of the search while (NS_SUCCEEDED(aQuery->ExecuteStep(&hasMore)) && hasMore) { +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: taking another step through the query results\n", __FILE__, __LINE__); +#endif nsAutoString escapedEntryURL; nsresult rv = aQuery->GetString(kAutoCompleteIndex_URL, escapedEntryURL); NS_ENSURE_SUCCESS(rv, rv); +#ifdef DEBUG_pgengler +fprintf(stderr, "%s:%d: escapedEntryURL == '%s'\n", __FILE__, __LINE__, (char *)ToNewCString(escapedEntryURL)); +#endif + // If we need to filter and have a javascript URI.. skip! if (filterJavascript && StartsWithJS(escapedEntryURL)) continue; @@ -748,15 +831,18 @@ for (PRInt32 i = 0; i < mCurrentSearchTokens.Count() && matchAll; i++) { const nsString *token = mCurrentSearchTokens.StringAt(i); - // Check if the tags match the search term - PRBool matchTags = (*tokenMatchesTarget)(*token, entryTags); - // Check if the title matches the search term - PRBool matchTitle = (*tokenMatchesTarget)(*token, title); + char *sToken = (char *)ToNewCString(*token); + char *sURL = (char *)ToNewCString(entryURL); + +#ifdef DEBUG_pgengler + fprintf(stderr, "%s:%d: sToken == '%s', sURL == '%s'\n", __FILE__, __LINE__, sToken, sURL); +#endif + // Check if the url matches the search term PRBool matchUrl = (*tokenMatchesTarget)(*token, entryURL); // True if any of them match; false makes us quit the loop - matchAll = matchTags || matchTitle || matchUrl; + matchAll = matchUrl; } // Skip if we don't match all terms in the bookmark, tag, title or url