#include "xstring.h"
#include <stdarg.h>
+#define xlat_verbose(style_) ((style_) & XLAT_STYLE_VERBOSITY_MASK)
+#define xlat_format(style_) ((style_) & XLAT_STYLE_FORMAT_MASK)
+
static inline enum xlat_style
get_xlat_style(enum xlat_style style)
{
- if ((style & XLAT_STYLE_VERBOSITY_MASK) == XLAT_STYLE_DEFAULT)
+ if (xlat_verbose(style) == XLAT_STYLE_DEFAULT)
return style | xlat_verbosity;
return style;
}
+static inline const char *
+sprint_xlat_val(uint64_t val, enum xlat_style style)
+{
+ static char buf[sizeof(val) * 3];
+
+ switch (xlat_format(style)) {
+ case XLAT_STYLE_FMT_U:
+ xsprintf(buf, "%" PRIu64, val);
+ break;
+
+ case XLAT_STYLE_FMT_X:
+ xsprintf(buf, "%#" PRIx64, val);
+ break;
+ }
+
+ return buf;
+}
+
+static inline void
+print_xlat_val(uint64_t val, enum xlat_style style)
+{
+ tprints(sprint_xlat_val(val, style));
+}
+
const char *
xlookup(const struct xlat *xlat, const uint64_t val)
{
{
style = get_xlat_style(style);
- if (style == XLAT_STYLE_RAW) {
- tprintf("%#" PRIx64, val);
+ if (xlat_verbose(style) == XLAT_STYLE_RAW) {
+ print_xlat_val(val, style);
return 0;
}
const char *str = xlookup(xlat, val);
if (str) {
- if (style == XLAT_STYLE_VERBOSE) {
- tprintf("%#" PRIx64, val);
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
+ print_xlat_val(val, style);
tprints_comment(str);
} else {
tprints(str);
}
}
/* No hits -- print raw # instead. */
- tprintf("%#" PRIx64, val);
+ print_xlat_val(val, style);
tprints_comment(dflt);
va_end(args);
{
style = get_xlat_style(style);
- if (style == XLAT_STYLE_RAW)
- return xsnprintf(buf, size, "%#x", val);
+ if (xlat_verbose(style) == XLAT_STYLE_RAW)
+ return xsnprintf(buf, size, "%s", sprint_xlat_val(val, style));
const char *const str = xlookup(x, val);
if (str) {
- if (style == XLAT_STYLE_VERBOSE)
- return xsnprintf(buf, size, "%#x /* %s */", val, str);
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
+ return xsnprintf(buf, size, "%s /* %s */",
+ sprint_xlat_val(val, style), str);
else
return xsnprintf(buf, size, "%s", str);
}
if (dflt)
- return xsnprintf(buf, size, "%#x /* %s */", val, dflt);
+ return xsnprintf(buf, size, "%s /* %s */",
+ sprint_xlat_val(val, style), dflt);
- return xsnprintf(buf, size, "%#x", val);
+ return xsnprintf(buf, size, "%s", sprint_xlat_val(val, style));
}
/**
{
style = get_xlat_style(style);
- if (style == XLAT_STYLE_RAW) {
- tprintf("%#" PRIx64, val);
+ if (xlat_verbose(style) == XLAT_STYLE_RAW) {
+ print_xlat_val(val, style);
return 0;
}
const char *s = xlat_search(xlat, xlat_size, val);
if (s) {
- if (style == XLAT_STYLE_VERBOSE) {
- tprintf("%#" PRIx64, val);
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
+ print_xlat_val(val, style);
tprints_comment(s);
} else {
tprints(s);
return 1;
}
- tprintf("%#" PRIx64, val);
+ print_xlat_val(val, style);
tprints_comment(dflt);
return 0;
outptr = stpcpy(outstr, prefix);
style = get_xlat_style(style);
- if (style == XLAT_STYLE_RAW) {
+ if (xlat_verbose(style) == XLAT_STYLE_RAW) {
if (!flags)
return NULL;
- outptr = xappendstr(outstr, outptr, "%#" PRIx64, flags);
+ outptr = xappendstr(outstr, outptr, "%s",
+ sprint_xlat_val(flags, style));
return outstr;
}
if (flags == 0 && xlat->val == 0 && xlat->str) {
- if (style == XLAT_STYLE_VERBOSE) {
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
outptr = xappendstr(outstr, outptr, "0 /* %s */",
xlat->str);
} else {
return outstr;
}
- if (style == XLAT_STYLE_VERBOSE && flags)
- outptr = xappendstr(outstr, outptr, "%#" PRIx64, flags);
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE && flags)
+ outptr = xappendstr(outstr, outptr, "%s",
+ sprint_xlat_val(flags, style));
for (; flags && xlat->str; xlat++) {
if (xlat->val && (flags & xlat->val) == xlat->val) {
if (found)
*outptr++ = '|';
- else if (style == XLAT_STYLE_VERBOSE)
+ else if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
outptr = stpcpy(outptr, " /* ");
outptr = stpcpy(outptr, xlat->str);
if (flags) {
if (found)
*outptr++ = '|';
- if (found || style != XLAT_STYLE_VERBOSE)
- outptr = xappendstr(outstr, outptr, "%#" PRIx64, flags);
+ if (found || xlat_verbose(style) != XLAT_STYLE_VERBOSE)
+ outptr = xappendstr(outstr, outptr, "%s",
+ sprint_xlat_val(flags, style));
} else {
if (!found)
return NULL;
}
- if (found && style == XLAT_STYLE_VERBOSE)
+ if (found && xlat_verbose(style) == XLAT_STYLE_VERBOSE)
outptr = stpcpy(outptr, " */");
return outstr;
{
style = get_xlat_style(style);
- if (style == XLAT_STYLE_RAW) {
+ if (xlat_verbose(style) == XLAT_STYLE_RAW) {
if (flags || dflt) {
- tprintf("%#" PRIx64, flags);
+ print_xlat_val(flags, style);
return 1;
}
unsigned int n = 0;
va_list args;
- if (style == XLAT_STYLE_VERBOSE) {
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
init_sep = " /* ";
if (flags)
- tprintf("%#" PRIx64, flags);
+ print_xlat_val(flags, style);
}
va_start(args, xlat);
for (; (flags || !n) && xlat->str; ++xlat) {
if ((flags == xlat->val) ||
(xlat->val && (flags & xlat->val) == xlat->val)) {
- if (style == XLAT_STYLE_VERBOSE && !flags)
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE
+ && !flags)
tprints("0");
tprintf("%s%s",
(n++ ? "|" : init_sep), xlat->str);
if (n) {
if (flags) {
- tprintf("|%#" PRIx64, flags);
+ tprints("|");
+ print_xlat_val(flags, style);
n++;
}
- if (style == XLAT_STYLE_VERBOSE)
+ if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
tprints(" */");
} else {
if (flags) {
- if (style != XLAT_STYLE_VERBOSE)
- tprintf("%#" PRIx64, flags);
+ if (xlat_verbose(style) != XLAT_STYLE_VERBOSE)
+ print_xlat_val(flags, style);
tprints_comment(dflt);
} else {
if (dflt)
{
style = get_xlat_style(style);
- switch (style) {
+ switch (xlat_verbose(style)) {
case XLAT_STYLE_RAW:
- tprintf("%#" PRIx64, val);
+ print_xlat_val(val, style);
break;
case XLAT_STYLE_ABBREV:
ATTRIBUTE_FALLTHROUGH;
case XLAT_STYLE_VERBOSE:
- tprintf("%#" PRIx64, val);
+ print_xlat_val(val, style);
tprints_comment(str);
}
}