User Name:


Forgot your password?
Vote for Us!
AFKMud 2.2.2
Mar 3, 2019 5:35 pm
By Samson
Nov 28, 2018 12:10 pm
By Keirath
First Immortal
Oct 12, 2018 2:02 pm
By GatewaySysop
Bug in do_climb( )
Jun 5, 2018 7:31 pm
By joeyfogas
question on overland code
May 31, 2018 12:03 pm
By joeyfogas
SmaugFUSS 1.9.3
Author: Various
Submitted by: Samson
AFKMud 2.2.2
Author: AFKMud Team
Submitted by: Samson
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
Users Online
CommonCrawl, Bing

Members: 0
Guests: 13
Newest Member
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds » 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 10:36 pm
Go to the top of the page
Go to the bottom of the page

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.

Post is unread #2 Feb 26, 2014 12:19 pm
Go to the top of the page
Go to the bottom of the page

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 5:09 am
Go to the top of the page
Go to the bottom of the page

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 >>