[Mono-devel-list] Type.FilterName Fix

Willibald Krenn Willibald.Krenn at gmx.at
Wed Jan 19 07:48:03 EST 2005


Jonathan Pryor schrieb:
> On Wed, 2005-01-19 at 03:28 +0100, Willibald Krenn wrote:
> 
>>However, just as a quick hack:
>>
>>int filterLen = name.Length;
>>if (name [filterLen] != "*")
>>	return name.Equals(m.Name);
>>else
>>	return String.Compare (name, 0, m.Name, 0, filterLen-1, false,
>>		CultureInfo.InvariantCulture) == 0;
> 
> 
> The problem with quick hacks is that they can be wrong. :-)

Ah, I thought so. *g*
(Just as a side-note, the above code would have been correct in Delphi 
as String[0] gives the length of the string IIRC)

> There's an off-by-one error: it should be ``name [filterLen-1] != '*'''.
> And if the string is an empty string, you'll get an exception, so it
> should really be:
> 
> 	if (name != null && name.Length > 0 && 
> 		name [name.Length-1] != '*')
> 		return name.Equals (m.Name);
> 	return string.Compare (name, 0, m.Name, 0, name.Length-1, false,
> 		CultureInfo.InvariantCulture) == 0;
> 
> And it may *still* be wrong (since I haven't actually tested it yet).
> 
> :-)

Actually if name == null, you'll get an access violation, 'cause you 
call name.Length in string.Compare (and string.compare length must be >= 
0)- sooo:

  	if (name != null) {
		int filterLen = name.Length -1;
		if (filterLen >= 0){

			if (name [filterLen] != '*'))
  				return name.Equals (m.Name);

  			return string.Compare (name, 0, m.Name, 0,
			 filterLen, false, CultureInfo.InvariantCulture)
			 == 0;
		}
	}
	return false; /*is this correct for both cases?!*/

Not tested yet - so there still might be some error.. :-)

Any takers?
Willi




More information about the Mono-devel-list mailing list