Process URLs more accurately

This commit is contained in:
Alexander S. Aganichev 2001-11-04 21:32:17 +00:00
parent ee54b9664d
commit 8b58c9b51a

View File

@ -29,7 +29,7 @@
// ------------------------------------------------------------------ // ------------------------------------------------------------------
bool strncont(const char *beginword, const char *stylestopchars, int n) static bool strncont(const char *beginword, const char *stylestopchars, int n)
{ {
for(; (n > 0) and (*beginword != NUL); n--, beginword++) { for(; (n > 0) and (*beginword != NUL); n--, beginword++) {
if(strchr(stylestopchars, *beginword) != NULL) if(strchr(stylestopchars, *beginword) != NULL)
@ -41,7 +41,7 @@ bool strncont(const char *beginword, const char *stylestopchars, int n)
// ------------------------------------------------------------------ // ------------------------------------------------------------------
bool in_ftn_domained_address(const char *ptr, const char *txt) { static bool in_ftn_domained_address(const char *ptr, const char *txt) {
while((ptr != txt) and (not isspace(*ptr) and not isalpha(*ptr))) { while((ptr != txt) and (not isspace(*ptr) and not isalpha(*ptr))) {
if(isdigit(ptr[0]) and ((ptr[1] == ':') or (ptr[1] == '/')) and isdigit(ptr[2])) if(isdigit(ptr[0]) and ((ptr[1] == ':') or (ptr[1] == '/')) and isdigit(ptr[2]))
@ -52,6 +52,24 @@ bool in_ftn_domained_address(const char *ptr, const char *txt) {
} }
// ------------------------------------------------------------------
static const char *url_begin(const char *ptr) {
if(strnieql(ptr, "http://", 7))
return ptr+7;
if(strnieql(ptr, "ftp://", 6))
return ptr+6;
if(strnieql(ptr, "www.", 4))
return ptr+4;
if(strnieql(ptr, "ftp.", 4))
return ptr+4;
if(strnieql(ptr, "mailto:", 7))
return ptr+7;
return NULL;
}
// ------------------------------------------------------------------ // ------------------------------------------------------------------
inline bool isstylechar(char c) { inline bool isstylechar(char c) {
@ -132,14 +150,14 @@ void Container::StyleCodeHighlight(const char* text, int row, int col, bool dohi
} }
} }
else { else {
if(not strnicmp(ptr, "http://", 7) or not strnicmp(ptr, "ftp://", 6) or const char *begin;
(not strnicmp(ptr, "www.", 4) and not isspace(ptr[4])) or
(not strnicmp(ptr, "ftp.", 4) and not isspace(ptr[4])) or if((begin = url_begin(ptr)) != NULL) {
not strnicmp(ptr, "mailto:", 7)) { const char *end = begin+strcspn(begin, " \t\"\'<>()[]");
const char *end = ptr+4+strcspn(ptr+4, " \t\"\'<>()[]");
if(ispunct(end[-1]) and (end[-1] != '/')) if(ispunct(end[-1]) and (end[-1] != '/'))
--end; --end;
if(begin < end) {
strxcpy(buf, txptr, (uint)(ptr-txptr)+1); strxcpy(buf, txptr, (uint)(ptr-txptr)+1);
prints(row, col+sclen, color, buf); prints(row, col+sclen, color, buf);
sclen += strlen(buf); sclen += strlen(buf);
@ -149,6 +167,7 @@ void Container::StyleCodeHighlight(const char* text, int row, int col, bool dohi
txptr = end; txptr = end;
ptr = end-1; ptr = end-1;
} }
}
else if(((ptr == text) or not in_ftn_domained_address(ptr, text)) and else if(((ptr == text) or not in_ftn_domained_address(ptr, text)) and
(isascii(*ptr) and not isspace(*ptr) and not ispunct(*ptr) and (isascii(*ptr) and not isspace(*ptr) and not ispunct(*ptr) and
((ptr == text) or (not isxalnum(ptr[-1]) and (ptr[-1] != '@'))))) { ((ptr == text) or (not isxalnum(ptr[-1]) and (ptr[-1] != '@'))))) {
@ -165,14 +184,19 @@ void Container::StyleCodeHighlight(const char* text, int row, int col, bool dohi
commerce_at = strpbrk(ptr, " \t:/@"); commerce_at = strpbrk(ptr, " \t:/@");
} }
if((commerce_at != NULL) and (*commerce_at == '@')) { if((commerce_at != NULL) and (*commerce_at == '@')) {
bool dot_found = false; int dots_found = 0;
++commerce_at; ++commerce_at;
while((*commerce_at != NUL) and (isalnum(*commerce_at) or (*commerce_at == '.') or (*commerce_at == '-'))) { while((*commerce_at != NUL) and (isalnum(*commerce_at) or (*commerce_at == '.') or (*commerce_at == '-'))) {
if(*commerce_at == '.') if(*commerce_at == '.')
dot_found = true; ++dots_found;
++commerce_at; ++commerce_at;
} }
if(dot_found) { while((commerce_at[-1] == '.') or (commerce_at[-1] == '-')) {
--commerce_at;
if(*commerce_at == '.')
--dots_found;
}
if(dots_found) {
strxcpy(buf, txptr, (uint)(ptr-txptr)+1); strxcpy(buf, txptr, (uint)(ptr-txptr)+1);
prints(row, col+sclen, color, buf); prints(row, col+sclen, color, buf);
sclen += strlen(buf); sclen += strlen(buf);