1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +00:00

AK: Rename variables with camelCase names in PrintfImplementation.h (#2095)

zeroPad => zero_pad
leftPad => left_pad
fieldWidth => field_width

These were the only variables with names in camelCase.
We were not consistent with the naming of these variables: some times we
called them zeroPad, leftPad, fieldWidth; other times we called them
zero_pad, left_pad, field_width.

These inconsistencies made the code hard to read, so I changed their
names to snake_case.

Also rename width => field_width in AK::print_hex()
This commit is contained in:
Emanuele Torre 2020-05-04 09:40:17 +02:00 committed by GitHub
parent 3cae4cee81
commit de0ed0e2dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -42,7 +42,7 @@ extern "C" size_t strlen(const char*);
#endif #endif
template<typename PutChFunc, typename T> template<typename PutChFunc, typename T>
ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zeroPad, u8 width) ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zero_pad, u8 field_width)
{ {
int ret = 0; int ret = 0;
@ -53,7 +53,7 @@ ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper
digits = 1; digits = 1;
if (left_pad) { if (left_pad) {
int stop_at = width - digits; int stop_at = field_width - digits;
if (alternate_form) if (alternate_form)
stop_at -= 2; stop_at -= 2;
@ -67,11 +67,11 @@ ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper
putch(bufptr, '0'); putch(bufptr, '0');
putch(bufptr, 'x'); putch(bufptr, 'x');
ret += 2; ret += 2;
width += 2; field_width += 2;
} }
if (zeroPad) { if (zero_pad) {
while (ret < width - digits) { while (ret < field_width - digits) {
putch(bufptr, '0'); putch(bufptr, '0');
++ret; ++ret;
} }
@ -96,7 +96,7 @@ ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper
} }
template<typename PutChFunc> template<typename PutChFunc>
ALWAYS_INLINE int print_number(PutChFunc putch, char*& bufptr, u32 number, bool leftPad, bool zeroPad, u32 fieldWidth) ALWAYS_INLINE int print_number(PutChFunc putch, char*& bufptr, u32 number, bool left_pad, bool zero_pad, u32 field_width)
{ {
u32 divisor = 1000000000; u32 divisor = 1000000000;
char ch; char ch;
@ -117,27 +117,27 @@ ALWAYS_INLINE int print_number(PutChFunc putch, char*& bufptr, u32 number, bool
} }
size_t numlen = p - buf; size_t numlen = p - buf;
if (!fieldWidth || fieldWidth < numlen) if (!field_width || field_width < numlen)
fieldWidth = numlen; field_width = numlen;
if (!leftPad) { if (!left_pad) {
for (unsigned i = 0; i < fieldWidth - numlen; ++i) { for (unsigned i = 0; i < field_width - numlen; ++i) {
putch(bufptr, zeroPad ? '0' : ' '); putch(bufptr, zero_pad ? '0' : ' ');
} }
} }
for (unsigned i = 0; i < numlen; ++i) { for (unsigned i = 0; i < numlen; ++i) {
putch(bufptr, buf[i]); putch(bufptr, buf[i]);
} }
if (leftPad) { if (left_pad) {
for (unsigned i = 0; i < fieldWidth - numlen; ++i) { for (unsigned i = 0; i < field_width - numlen; ++i) {
putch(bufptr, ' '); putch(bufptr, ' ');
} }
} }
return fieldWidth; return field_width;
} }
template<typename PutChFunc> template<typename PutChFunc>
ALWAYS_INLINE int print_u64(PutChFunc putch, char*& bufptr, u64 number, bool leftPad, bool zeroPad, u32 fieldWidth) ALWAYS_INLINE int print_u64(PutChFunc putch, char*& bufptr, u64 number, bool left_pad, bool zero_pad, u32 field_width)
{ {
u64 divisor = 10000000000000000000LLU; u64 divisor = 10000000000000000000LLU;
char ch; char ch;
@ -158,27 +158,27 @@ ALWAYS_INLINE int print_u64(PutChFunc putch, char*& bufptr, u64 number, bool lef
} }
size_t numlen = p - buf; size_t numlen = p - buf;
if (!fieldWidth || fieldWidth < numlen) if (!field_width || field_width < numlen)
fieldWidth = numlen; field_width = numlen;
if (!leftPad) { if (!left_pad) {
for (unsigned i = 0; i < fieldWidth - numlen; ++i) { for (unsigned i = 0; i < field_width - numlen; ++i) {
putch(bufptr, zeroPad ? '0' : ' '); putch(bufptr, zero_pad ? '0' : ' ');
} }
} }
for (unsigned i = 0; i < numlen; ++i) { for (unsigned i = 0; i < numlen; ++i) {
putch(bufptr, buf[i]); putch(bufptr, buf[i]);
} }
if (leftPad) { if (left_pad) {
for (unsigned i = 0; i < fieldWidth - numlen; ++i) { for (unsigned i = 0; i < field_width - numlen; ++i) {
putch(bufptr, ' '); putch(bufptr, ' ');
} }
} }
return fieldWidth; return field_width;
} }
template<typename PutChFunc> template<typename PutChFunc>
ALWAYS_INLINE int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth, u32 fraction_length = 6) ALWAYS_INLINE int print_double(PutChFunc putch, char*& bufptr, double number, bool left_pad, bool zero_pad, u32 field_width, u32 fraction_length = 6)
{ {
int length = 0; int length = 0;
@ -188,7 +188,7 @@ ALWAYS_INLINE int print_double(PutChFunc putch, char*& bufptr, double number, bo
number = 0 - number; number = 0 - number;
} }
length = print_u64(putch, bufptr, (i64)number, leftPad, zeroPad, fieldWidth); length = print_u64(putch, bufptr, (i64)number, left_pad, zero_pad, field_width);
putch(bufptr, '.'); putch(bufptr, '.');
length++; length++;
double fraction = number - (i64)number; double fraction = number - (i64)number;
@ -200,17 +200,17 @@ ALWAYS_INLINE int print_double(PutChFunc putch, char*& bufptr, double number, bo
} }
template<typename PutChFunc> template<typename PutChFunc>
ALWAYS_INLINE int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool leftPad, bool zeroPad, u32 fieldWidth) ALWAYS_INLINE int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool left_pad, bool zero_pad, u32 field_width)
{ {
if (number < 0) { if (number < 0) {
putch(bufptr, '-'); putch(bufptr, '-');
return print_u64(putch, bufptr, 0 - number, leftPad, zeroPad, fieldWidth) + 1; return print_u64(putch, bufptr, 0 - number, left_pad, zero_pad, field_width) + 1;
} }
return print_u64(putch, bufptr, number, leftPad, zeroPad, fieldWidth); return print_u64(putch, bufptr, number, left_pad, zero_pad, field_width);
} }
template<typename PutChFunc> template<typename PutChFunc>
ALWAYS_INLINE int print_octal_number(PutChFunc putch, char*& bufptr, u32 number, bool leftPad, bool zeroPad, u32 fieldWidth) ALWAYS_INLINE int print_octal_number(PutChFunc putch, char*& bufptr, u32 number, bool left_pad, bool zero_pad, u32 field_width)
{ {
u32 divisor = 134217728; u32 divisor = 134217728;
char ch; char ch;
@ -231,23 +231,23 @@ ALWAYS_INLINE int print_octal_number(PutChFunc putch, char*& bufptr, u32 number,
} }
size_t numlen = p - buf; size_t numlen = p - buf;
if (!fieldWidth || fieldWidth < numlen) if (!field_width || field_width < numlen)
fieldWidth = numlen; field_width = numlen;
if (!leftPad) { if (!left_pad) {
for (unsigned i = 0; i < fieldWidth - numlen; ++i) { for (unsigned i = 0; i < field_width - numlen; ++i) {
putch(bufptr, zeroPad ? '0' : ' '); putch(bufptr, zero_pad ? '0' : ' ');
} }
} }
for (unsigned i = 0; i < numlen; ++i) { for (unsigned i = 0; i < numlen; ++i) {
putch(bufptr, buf[i]); putch(bufptr, buf[i]);
} }
if (leftPad) { if (left_pad) {
for (unsigned i = 0; i < fieldWidth - numlen; ++i) { for (unsigned i = 0; i < field_width - numlen; ++i) {
putch(bufptr, ' '); putch(bufptr, ' ');
} }
} }
return fieldWidth; return field_width;
} }
template<typename PutChFunc> template<typename PutChFunc>
@ -273,15 +273,15 @@ ALWAYS_INLINE int print_string(PutChFunc putch, char*& bufptr, const char* str,
} }
template<typename PutChFunc> template<typename PutChFunc>
ALWAYS_INLINE int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool leftPad, bool zeroPad, u32 fieldWidth, bool always_sign) ALWAYS_INLINE int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool left_pad, bool zero_pad, u32 field_width, bool always_sign)
{ {
if (number < 0) { if (number < 0) {
putch(bufptr, '-'); putch(bufptr, '-');
return print_number(putch, bufptr, 0 - number, leftPad, zeroPad, fieldWidth) + 1; return print_number(putch, bufptr, 0 - number, left_pad, zero_pad, field_width) + 1;
} }
if (always_sign) if (always_sign)
putch(bufptr, '+'); putch(bufptr, '+');
return print_number(putch, bufptr, number, leftPad, zeroPad, fieldWidth); return print_number(putch, bufptr, number, left_pad, zero_pad, field_width);
} }
template<typename PutChFunc> template<typename PutChFunc>
@ -294,9 +294,9 @@ ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fm
for (p = fmt; *p; ++p) { for (p = fmt; *p; ++p) {
bool left_pad = false; bool left_pad = false;
bool zeroPad = false; bool zero_pad = false;
bool dot = false; bool dot = false;
unsigned fieldWidth = 0; unsigned field_width = 0;
unsigned fraction_length = 0; unsigned fraction_length = 0;
unsigned long_qualifiers = 0; unsigned long_qualifiers = 0;
bool size_qualifier = false; bool size_qualifier = false;
@ -321,15 +321,15 @@ ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fm
if (*(p + 1)) if (*(p + 1))
goto one_more; goto one_more;
} }
if (!zeroPad && !fieldWidth && *p == '0') { if (! zero_pad && !field_width && *p == '0') {
zeroPad = true; zero_pad = true;
if (*(p + 1)) if (*(p + 1))
goto one_more; goto one_more;
} }
if (*p >= '0' && *p <= '9') { if (*p >= '0' && *p <= '9') {
if (!dot) { if (!dot) {
fieldWidth *= 10; field_width *= 10;
fieldWidth += *p - '0'; field_width += *p - '0';
if (*(p + 1)) if (*(p + 1))
goto one_more; goto one_more;
} else { } else {
@ -340,7 +340,7 @@ ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fm
} }
} }
if (*p == '*') { if (*p == '*') {
fieldWidth = va_arg(ap, int); field_width = va_arg(ap, int);
if (*(p + 1)) if (*(p + 1))
goto one_more; goto one_more;
} }
@ -362,36 +362,36 @@ ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fm
switch (*p) { switch (*p) {
case 's': { case 's': {
const char* sp = va_arg(ap, const char*); const char* sp = va_arg(ap, const char*);
ret += print_string(putch, bufptr, sp ? sp : "(null)", left_pad, fieldWidth, dot); ret += print_string(putch, bufptr, sp ? sp : "(null)", left_pad, field_width, dot);
} break; } break;
case 'd': case 'd':
case 'i': case 'i':
if (long_qualifiers >= 2) if (long_qualifiers >= 2)
ret += print_i64(putch, bufptr, va_arg(ap, i64), left_pad, zeroPad, fieldWidth); ret += print_i64(putch, bufptr, va_arg(ap, i64), left_pad, zero_pad, field_width);
else else
ret += print_signed_number(putch, bufptr, va_arg(ap, int), left_pad, zeroPad, fieldWidth, always_sign); ret += print_signed_number(putch, bufptr, va_arg(ap, int), left_pad, zero_pad, field_width, always_sign);
break; break;
case 'u': case 'u':
if (long_qualifiers >= 2) if (long_qualifiers >= 2)
ret += print_u64(putch, bufptr, va_arg(ap, u64), left_pad, zeroPad, fieldWidth); ret += print_u64(putch, bufptr, va_arg(ap, u64), left_pad, zero_pad, field_width);
else else
ret += print_number(putch, bufptr, va_arg(ap, u32), left_pad, zeroPad, fieldWidth); ret += print_number(putch, bufptr, va_arg(ap, u32), left_pad, zero_pad, field_width);
break; break;
case 'Q': case 'Q':
ret += print_u64(putch, bufptr, va_arg(ap, u64), left_pad, zeroPad, fieldWidth); ret += print_u64(putch, bufptr, va_arg(ap, u64), left_pad, zero_pad, field_width);
break; break;
case 'q': case 'q':
ret += print_hex(putch, bufptr, va_arg(ap, u64), false, false, left_pad, zeroPad, 16); ret += print_hex(putch, bufptr, va_arg(ap, u64), false, false, left_pad, zero_pad, 16);
break; break;
#if !defined(BOOTSTRAPPER) && !defined(KERNEL) #if !defined(BOOTSTRAPPER) && !defined(KERNEL)
case 'g': case 'g':
case 'f': case 'f':
ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zeroPad, fieldWidth, fraction_length); ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zero_pad, field_width, fraction_length);
break; break;
#endif #endif
@ -400,12 +400,12 @@ ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fm
putch(bufptr, '0'); putch(bufptr, '0');
++ret; ++ret;
} }
ret += print_octal_number(putch, bufptr, va_arg(ap, u32), left_pad, zeroPad, fieldWidth); ret += print_octal_number(putch, bufptr, va_arg(ap, u32), left_pad, zero_pad, field_width);
break; break;
case 'X': case 'X':
case 'x': case 'x':
ret += print_hex(putch, bufptr, va_arg(ap, u32), *p == 'X', alternate_form, left_pad, zeroPad, fieldWidth); ret += print_hex(putch, bufptr, va_arg(ap, u32), *p == 'X', alternate_form, left_pad, zero_pad, field_width);
break; break;
case 'w': case 'w':
@ -418,7 +418,7 @@ ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fm
case 'c': { case 'c': {
char s[2] { (char)va_arg(ap, int), 0 }; char s[2] { (char)va_arg(ap, int), 0 };
ret += print_string(putch, bufptr, s, left_pad, fieldWidth, dot); ret += print_string(putch, bufptr, s, left_pad, field_width, dot);
} break; } break;
case '%': case '%':