2003-02-24 David Carlton <carlton@math.stanford.edu>
* symtab.c (lookup_partial_symbol): Use strcmp_iw_ordered to do the comparison, not strcmp. * symfile.c (compare_psymbols): Ditto. * defs.h: Declare strcmp_iw_ordered. * utils.c (strcmp_iw_ordered): New function.
This commit is contained in:
parent
c8d6825d9a
commit
0fe19209f9
5 changed files with 101 additions and 45 deletions
80
gdb/utils.c
80
gdb/utils.c
|
@ -2358,6 +2358,86 @@ strcmp_iw (const char *string1, const char *string2)
|
|||
return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
|
||||
}
|
||||
|
||||
/* This is like strcmp except that it ignores whitespace and treats
|
||||
'(' as the first non-NULL character in terms of ordering. Like
|
||||
strcmp (and unlike strcmp_iw), it returns negative if STRING1 <
|
||||
STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2
|
||||
according to that ordering.
|
||||
|
||||
If a list is sorted according to this function and if you want to
|
||||
find names in the list that match some fixed NAME according to
|
||||
strcmp_iw(LIST_ELT, NAME), then the place to start looking is right
|
||||
where this function would put NAME.
|
||||
|
||||
Here are some examples of why using strcmp to sort is a bad idea:
|
||||
|
||||
Whitespace example:
|
||||
|
||||
Say your partial symtab contains: "foo<char *>", "goo". Then, if
|
||||
we try to do a search for "foo<char*>", strcmp will locate this
|
||||
after "foo<char *>" and before "goo". Then lookup_partial_symbol
|
||||
will start looking at strings beginning with "goo", and will never
|
||||
see the correct match of "foo<char *>".
|
||||
|
||||
Parenthesis example:
|
||||
|
||||
In practice, this is less like to be an issue, but I'll give it a
|
||||
shot. Let's assume that '$' is a legitimate character to occur in
|
||||
symbols. (Which may well even be the case on some systems.) Then
|
||||
say that the partial symbol table contains "foo$" and "foo(int)".
|
||||
strcmp will put them in this order, since '$' < '('. Now, if the
|
||||
user searches for "foo", then strcmp will sort "foo" before "foo$".
|
||||
Then lookup_partial_symbol will notice that strcmp_iw("foo$",
|
||||
"foo") is false, so it won't proceed to the actual match of
|
||||
"foo(int)" with "foo". */
|
||||
|
||||
int
|
||||
strcmp_iw_ordered (const char *string1, const char *string2)
|
||||
{
|
||||
while ((*string1 != '\0') && (*string2 != '\0'))
|
||||
{
|
||||
while (isspace (*string1))
|
||||
{
|
||||
string1++;
|
||||
}
|
||||
while (isspace (*string2))
|
||||
{
|
||||
string2++;
|
||||
}
|
||||
if (*string1 != *string2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (*string1 != '\0')
|
||||
{
|
||||
string1++;
|
||||
string2++;
|
||||
}
|
||||
}
|
||||
|
||||
switch (*string1)
|
||||
{
|
||||
/* Characters are non-equal unless they're both '\0'; we want to
|
||||
make sure we get the comparison right according to our
|
||||
comparison in the cases where one of them is '\0' or '('. */
|
||||
case '\0':
|
||||
if (*string2 == '\0')
|
||||
return 0;
|
||||
else
|
||||
return -1;
|
||||
case '(':
|
||||
if (*string2 == '\0')
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
default:
|
||||
if (*string2 == '(')
|
||||
return 1;
|
||||
else
|
||||
return *string1 - *string2;
|
||||
}
|
||||
}
|
||||
|
||||
/* A simple comparison function with opposite semantics to strcmp. */
|
||||
|
||||
int
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue