richedit: Improve efficiency of ME_IsCandidateAnURL.

The code was previously getting the same text in the loop from the
editor, and it was converting each of the prefixes to compare against
for each URL candidate.
This commit is contained in:
Dylan Smith 2009-08-12 09:05:56 -04:00 committed by Alexandre Julliard
parent 82e102107c
commit 0362b1b087
1 changed files with 26 additions and 36 deletions

View File

@ -4766,51 +4766,38 @@ static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor, int sel_min, int sel_
/** /**
* This proc evaluates the selection and returns TRUE if it can be considered an URL * This proc evaluates the selection and returns TRUE if it can be considered an URL
*/ */
static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, int sel_min, int sel_max) static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
{ {
#define MAX_PREFIX_LEN 9
struct prefix_s { struct prefix_s {
const char *text; const WCHAR text[MAX_PREFIX_LEN];
int length; int length;
} prefixes[12] = { }prefixes[] = {
/* Code below depends on these being in decreasing length order! */ {{'p','r','o','s','p','e','r','o',':'}, 9},
{"prospero:", 10}, {{'t','e','l','n','e','t',':'}, 7},
{"telnet:", 8}, {{'g','o','p','h','e','r',':'}, 7},
{"gopher:", 8}, {{'m','a','i','l','t','o',':'}, 7},
{"mailto:", 8}, {{'h','t','t','p','s',':'}, 6},
{"https:", 7}, {{'f','i','l','e',':'}, 5},
{"file:", 6}, {{'n','e','w','s',':'}, 5},
{"news:", 6}, {{'w','a','i','s',':'}, 5},
{"wais:", 6}, {{'n','n','t','p',':'}, 5},
{"nntp:", 6}, {{'h','t','t','p',':'}, 5},
{"http:", 5}, {{'w','w','w','.'}, 4},
{"www.", 5}, {{'f','t','p',':'}, 4},
{"ftp:", 5},
}; };
LPWSTR bufferW = NULL; WCHAR bufferW[MAX_PREFIX_LEN + 1];
WCHAR bufW[32];
unsigned int i; unsigned int i;
ME_Cursor sel_start;
ME_CursorFromCharOfs(editor, sel_min, &sel_start); ME_GetTextW(editor, bufferW, MAX_PREFIX_LEN, start, nChars, 0);
if (sel_max == -1) sel_max = ME_GetTextLength(editor); for (i = 0; i < sizeof(prefixes) / sizeof(*prefixes); i++)
assert(sel_min <= sel_max);
for (i = 0; i < sizeof(prefixes) / sizeof(struct prefix_s); i++)
{ {
if (sel_max - sel_min < prefixes[i].length) continue; if (nChars < prefixes[i].length) continue;
if (bufferW == NULL) { if (!memcmp(prefixes[i].text, bufferW, prefixes[i].length * sizeof(WCHAR)))
bufferW = heap_alloc((sel_max - sel_min + 1) * sizeof(WCHAR));
}
ME_GetTextW(editor, bufferW, sel_max - sel_min, &sel_start,
lstrlenA(prefixes[i].text), 0);
MultiByteToWideChar(CP_ACP, 0, prefixes[i].text, -1, bufW, 32);
if (!lstrcmpW(bufW, bufferW))
{
heap_free(bufferW);
return TRUE; return TRUE;
}
} }
heap_free(bufferW);
return FALSE; return FALSE;
#undef MAX_PREFIX_LEN
} }
/** /**
@ -4837,11 +4824,14 @@ static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_m
if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax)) if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax))
{ {
ME_Cursor candidateStart;
/* Section before candidate is not an URL */ /* Section before candidate is not an URL */
beforeURL[0] = sel_min; beforeURL[0] = sel_min;
beforeURL[1] = cMin; beforeURL[1] = cMin;
if (ME_IsCandidateAnURL(editor, cMin, cMax)) ME_CursorFromCharOfs(editor, cMin, &candidateStart);
if (ME_IsCandidateAnURL(editor, &candidateStart,
(cMax == -1 ? INT_MAX : cMax) - cMin))
{ {
inURL[0] = cMin; inURL[1] = cMax; inURL[0] = cMin; inURL[1] = cMax;
} }