www.webdeveloper.com
Results 1 to 4 of 4

Thread: printf function

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    51

    printf function

    I can't remember why I needed this but I've made a JavaScript version as close to the real one as possible, I added 2 parameters
    b = boolean value
    B = Binary value (supports %010B etc)
    Code:
    function zxPrintf( text )
    {
        var arg = arguments, a = 0,
    		i, c, b = false, r = '', pad, v, j, pc, pa, ptxt, app = false;
    	if ( !text || text === null )
    		{ return ""; }
    	for ( i = 0; i < text.length; ++i )
    	{
    		c = text[ i ];
            if ( app )
    		{
                if ( pad )
    			{
                    pa = (+ptxt) - v.length;
    				for ( j = 0; j < pa; ++j )
    					{ r += pc; }
    			}
    			r += v;
    			b = false;
    			continue;
    		}
    		if ( b )
    		{
    			switch ( c )
    			{
    			case '0': case ' ':
    				if ( !pad )
    				{
    					pc = c;
    					pad = true;
    				}
    				else
    					{ ptxt += c; }
    				break;
    			case '1': case '2':
    			case '3': case '4':
    			case '5': case '6':
    			case '7': case '8':
    			case '9':
    				if ( pad )
    					{ ptxt += c; }
    				break;
    			case 'd': case 'i': case 'f':
    				v = arg[ ++a ].toString();
    				app = true;
    				break;
    			case 's':
    				v = arg[ ++a ];
    				app = true;
    				break;
    			case 'b':
    				pad = false;
    				v = ( arg[ ++a ] && arg[ a ] !== null ) ? 'true' : 'false';
    				app = true;
    				break;
        		case 'B':
    				v = arg[ ++a ].toString( 2 );
    				app = true;
    				break;
        		case 'x': case 'X':
    				v = arg[ ++a ].toString( 16 );
    				if ( c === 'x' )
    					{ v = v.toLowerCase(); }
    				else
    					{ v = v.toUpperCase(); }
    				app = true;
    				break;
    			default:
    				b = false;
    			}
    			continue;
    		}
    		if ( c === '%' )
    		{
    			if ( text[ i + 1 ] === '%' )
    			{
    				r += '%';
    				++i;
    				continue;
    			}
    			b = true;
    			pad = false;
    			ptxt = '0';
    			app = false;
    			continue;
    		}
    		r += c;
    	}
        if ( app )
    	{
            if ( pad )
    		{
                pa = (+ptxt) - v.length;
    			for ( j = 0; j < pa; ++j )
    				{ r += pc; }
    		}
    		r += v;
    	}
    	return r;
    }
    If something is missing then please post about it, I may take a while to check back but I at least want to know if there is something wrong with my code.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Do you have some examples of how you expect to use the function?
    What values do you expect to pass in the 'text' argument and what do you expect in return?
    Would be nice if you added some comments to your code to make it easier to follow the logic. Kinda barren.

  3. #3
    Join Date
    Aug 2010
    Posts
    51
    Aside from the mentioned additions the function is supposed to behave the same as the C/C++ version but instead of printing into a console it returns the resulting string. I didn't bother commenting it because unless something is missing there should be no need to modify it, but I'll go ahead and try commenting it.

  4. #4
    Join Date
    Aug 2010
    Posts
    51
    This any better?
    Code:
    function zxPrintf( text )
    {
        var arg = arguments, a = 0,
    		i, c, b = false, r = '', pad, v, j, pc, pa, ptxt, app = false;
    	if ( !text || text === null )
    		{ return ""; }
    	for ( i = 0; i < text.length; ++i )
    	{
    		c = text[ i ];
    		if ( app )
    		{
    			// Appends the argument to the resulting string
    			if ( pad )
    			{
    				// Pads with leading 0s or spaces
    				pa = (+ptxt) - v.length;
    				for ( j = 0; j < pa; ++j )
    					{ r += pc; }
    			}
    			r += v;
    			b = false;
    			continue;
    		}
    		if ( b )
    		{
    			// Finds out how it should treat the next argument
    			switch ( c )
    			{
    			case '0': case ' ':
    				if ( !pad )
    				{
    					pc = c;
    					pad = true;
    				}
    				else
    					{ ptxt += c; }
    				break;
    			case '1': case '2':
    			case '3': case '4':
    			case '5': case '6':
    			case '7': case '8':
    			case '9':
    				if ( pad )
    					{ ptxt += c; }
    				break;
    			case 'd': case 'i': case 'f':
    				v = arg[ ++a ].toString();
    				app = true;
    				break;
    			case 's':
    				v = arg[ ++a ];
    				app = true;
    				break;
    			case 'b':
    				pad = false;
    				v = ( arg[ ++a ] && arg[ a ] !== null ) ? 'true' : 'false';
    				app = true;
    				break;
        		case 'B':
    				v = arg[ ++a ].toString( 2 );
    				app = true;
    				break;
        		case 'x': case 'X':
    				v = arg[ ++a ].toString( 16 );
    				if ( c === 'x' )
    					{ v = v.toLowerCase(); }
    				else
    					{ v = v.toUpperCase(); }
    				app = true;
    				break;
    			default:
    				b = false;
    			}
    			continue;
    		}
    		if ( c === '%' )
    		{
    			// Finds next argument reader
    			if ( text[ i + 1 ] === '%' )
    			{
    				r += '%';
    				++i;
    				continue;
    			}
    			b = true;
    			pad = false;
    			ptxt = '0';
    			app = false;
    			continue;
    		}
    		r += c;
    	}
    	if ( app )
    	{
    		// Finds out if anything is leftover for appending
    		if ( pad )
    		{
    			pa = (+ptxt) - v.length;
    			for ( j = 0; j < pa; ++j )
    				{ r += pc; }
    		}
    		r += v;
    	}
    	return r;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles