Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
Bug in will_fall( )
Oct 23, 2017, 1:35 am
By GatewaySysop
Bug in do_zap( ), do_brandish( )
Oct 18, 2017, 1:52 pm
By GatewaySysop
Bug in get_exp_worth( )
Oct 10, 2017, 1:26 am
By GatewaySysop
Bug in do_drag( )
Oct 8, 2017, 12:40 am
By GatewaySysop
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Memwatch
Author: Johan Lindh
Submitted by: Vladaar
Beastmaster 6D sound files
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Yandex, Google, Yahoo!

Members: 0
Guests: 7
Stats
Files
Topics
Posts
Members
Newest Member
476
3,704
19,231
608
LAntorcha
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Codebases » SmaugFUSS » str_prefix doubt
Forum Rules | Mark all | Recent Posts

str_prefix doubt
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Feb 24, 2014, 8:36 pm
Go to the top of the page
Go to the bottom of the page

Leia
Fledgling
GroupMembers
Posts21
JoinedJul 8, 2012

I was using the bool str_prefix function and I found that if I compare an empty string, that function always evaluates as FALSE.
I want to compare an empty string because it's an arg that may or may not be empty.

So I added an if( astr[0] == '\0' ) return TRUE; in the middle of the function

/*
 * Compare strings, case insensitive, for prefix matching.
 * Return TRUE if astr not a prefix of bstr
 *   (compatibility with historical functions).
 */
bool str_prefix( const char *astr, const char *bstr )
{
	if( !astr )
	{
		bug( "Strn_cmp: null astr." );
		return TRUE;
	}

	if( !bstr )
	{
		bug( "Strn_cmp: null bstr." );
		return TRUE;
	}

	if( astr[0] == '\0' )   // if astr is empty, it's not a prefix of bstr.
		return TRUE;

	for( ; *astr; astr++, bstr++ )
	{
		if( LOWER( *astr ) != LOWER( *bstr ) )
			return TRUE;
	}

	return FALSE;
}


And for now everything seems fine and calm, but I was wondering if It may cause any known collateral damage.

Thanks.
       
Post is unread #2 Feb 26, 2014, 10:19 am
Go to the top of the page
Go to the bottom of the page

Quixadhal
Conjurer
GroupMembers
Posts398
JoinedMar 8, 2005

Well, technically, the empty string is a prefix of every string, including another empty string. The idea is like this...

"a" is a prefix of "abc", because the substring of "abc" that is equal to the length of string "a" has the same content. Because of that, "" has to be a substring of any other valid string, because there's no way for the length 0 substring of "abc" to be anything other than "".

FWIW, many of these old legacy functions would be better rewritten by using standard OS calls now. strncasecmp() does this, and is far more efficient as part of the OS code... the only thing you might want to keep is the checks for NULL pointers, and maybe a length check (if the first string is LONGER than the second, it can't be a prefix).

bool str_prefix( const char *a, const char *b ) {
    if( !a || !b || strlen(a) > strlen(b) ) {
        bug( "str_prefix called with NULL string" );
        return TRUE;
    }
    return strncasecmp( a, b, strlen( a ) );
}

       
Post is unread #3 Mar 6, 2014, 3:09 am
Go to the top of the page
Go to the bottom of the page

Remcon
Geomancer
GroupAdministrators
Posts1,857
JoinedJul 26, 2005

lol there were many reasons I stopped allowing strings to be empty, before you attempt it though understand there will be lots of crashing along the way to stop allowing empty strings :)
       
Pages:<< prev 1 next >>