pebble / common / fmt.c

/** \file
 * Format integers
 */
#include "fmt.h"

static char
hexdigit(
	uint8_t x
)
{
	if (x <= 9)
		return '0' + x - 0;
	else
	if (x <= 15)
		return 'A' + x - 0xA;
	else
		return '?';
}


unsigned
fmt_i16(
	char * buf,
	int16_t v_in,
	int base
)
{
	uint16_t v = v_in;

	if (v_in < 0)
	{
		buf[0] = '-';
		v = -v_in;
	} else
	if (v_in > 0)
		buf[0] = '+';
	else
		buf[0] = ' ';

	if (base == 10)
	{
		buf[5] = hexdigit(v % 10); v /= 10;
		buf[4] = hexdigit(v % 10); v /= 10;
		buf[3] = hexdigit(v % 10); v /= 10;
		buf[2] = hexdigit(v % 10); v /= 10;
		buf[1] = hexdigit(v % 10); v /= 10;
		return 6;
	} else
	if (base == 16)
	{
		buf[4] = hexdigit(v % 16); v /= 16;
		buf[3] = hexdigit(v % 16); v /= 16;
		buf[2] = hexdigit(v % 16); v /= 16;
		buf[1] = hexdigit(v % 16);
		return 5;
	}

	buf[1] = '?';
	return 2;
}


unsigned
fmt_i32(
	char * buf,
	int32_t v_in,
	int base
)
{
	uint32_t v = v_in;

	if (v_in < 0)
	{
		buf[0] = '-';
		v = -v_in;
	} else
	if (v_in > 0)
		buf[0] = '+';
	else
		buf[0] = ' ';

	if (base == 10)
	{
		buf[10] = hexdigit(v % 10); v /= 10;
		buf[9] = hexdigit(v % 10); v /= 10;
		buf[8] = hexdigit(v % 10); v /= 10;
		buf[7] = hexdigit(v % 10); v /= 10;
		buf[6] = hexdigit(v % 10); v /= 10;
		buf[5] = hexdigit(v % 10); v /= 10;
		buf[4] = hexdigit(v % 10); v /= 10;
		buf[3] = hexdigit(v % 10); v /= 10;
		buf[2] = hexdigit(v % 10); v /= 10;
		buf[1] = hexdigit(v % 10); v /= 10;
		return 11;
	} else
	if (base == 16)
	{
		buf[8] = hexdigit(v % 16); v /= 16;
		buf[7] = hexdigit(v % 16); v /= 16;
		buf[6] = hexdigit(v % 16); v /= 16;
		buf[5] = hexdigit(v % 16);
		buf[4] = hexdigit(v % 16); v /= 16;
		buf[3] = hexdigit(v % 16); v /= 16;
		buf[2] = hexdigit(v % 16); v /= 16;
		buf[1] = hexdigit(v % 16);
		return 9;
	}

	buf[1] = '?';
	return 2;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.