Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
Development
Nov 28, 2018, 10:10 am
By Keirath
First Immortal
Oct 12, 2018, 12:02 pm
By GatewaySysop
Bug in do_climb( )
Jun 5, 2018, 5:31 pm
By joeyfogas
question on overland code
May 31, 2018, 10:03 am
By joeyfogas
KaVir's Protocol Snip
May 15, 2018, 7:57 pm
By joeyfogas
tintin++ ogg sound player script for linux
Author: Robert Smith
Submitted by: Vladaar
6Dragons ogg Soundpack
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.4
Author: Vladaar
Submitted by: Vladaar
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
Users Online
CommonCrawl, Bing

Members: 0
Guests: 14
Stats
Files
Topics
Posts
Members
Newest Member
481
3,740
19,397
634
BlaineNock
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
Posts23
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,874
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 >>