0
0
mirror of https://github.com/rkd77/elinks.git synced 2025-06-30 22:19:29 -04:00

check_whether_file_exists(): optimize by not using strlen() more than

needed.
This commit is contained in:
Laurent MONIN 2006-01-07 20:59:34 +01:00 committed by Laurent MONIN
parent d0be89a16c
commit 9183e0cf04

View File

@ -115,28 +115,31 @@ end_with_known_tld(unsigned char *s, int slen)
return -1; return -1;
} }
/* XXX: this function writes to @name. */
static int static int
check_whether_file_exists(unsigned char *name) check_whether_file_exists(unsigned char *name)
{ {
/* Check POST_CHAR etc ... */ /* Check POST_CHAR etc ... */
static const unsigned char chars[] = POST_CHAR_S "#?"; static const unsigned char chars[] = POST_CHAR_S "#?";
int i; int i;
int namelen = strlen(name);
if (file_exists(name)) if (file_exists(name))
return strlen(name); return namelen;
for (i = 0; i < sizeof(chars) - 1; i++) { for (i = 0; i < sizeof(chars) - 1; i++) {
unsigned char *pos = strchr(name, chars[i]); unsigned char *pos = memchr(name, chars[i], namelen);
int namelen = -1; int exists;
if (!pos) continue; if (!pos) continue;
*pos = 0; *pos = 0;
if (file_exists(name)) exists = file_exists(name);
namelen = strlen(name);
*pos = chars[i]; *pos = chars[i];
if (namelen >= 0) return namelen; if (exists) {
return pos - name;
}
} }
return -1; return -1;