Skip to content

Commit

Permalink
[libc++][nfc] Move functions to a generic place.
Browse files Browse the repository at this point in the history
This allows the floating-point formatter to use the same functions as
the integral formatter. This was tested in D114001.
  • Loading branch information
mordante committed Nov 19, 2021
1 parent 3624c4d commit ed86610
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
43 changes: 43 additions & 0 deletions libcxx/include/__format/formatter.h
Expand Up @@ -60,6 +60,49 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter {
}
};

namespace __format_spec {

_LIBCPP_HIDE_FROM_ABI inline char* __insert_sign(char* __buf, bool __negative,
_Flags::_Sign __sign) {
if (__negative)
*__buf++ = '-';
else
switch (__sign) {
case _Flags::_Sign::__default:
case _Flags::_Sign::__minus:
// No sign added.
break;
case _Flags::_Sign::__plus:
*__buf++ = '+';
break;
case _Flags::_Sign::__space:
*__buf++ = ' ';
break;
}

return __buf;
}

_LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char c) {
switch (c) {
case 'a':
return 'A';
case 'b':
return 'B';
case 'c':
return 'C';
case 'd':
return 'D';
case 'e':
return 'E';
case 'f':
return 'F';
}
return c;
}

} // namespace __format_spec

namespace __formatter {

/** The character types that formatters are specialized for. */
Expand Down
39 changes: 0 additions & 39 deletions libcxx/include/__format/formatter_integral.h
Expand Up @@ -133,45 +133,6 @@ _LIBCPP_HIDE_FROM_ABI constexpr size_t __buffer_size() noexcept
+ 1; // Reserve space for the sign.
}

_LIBCPP_HIDE_FROM_ABI inline char* __insert_sign(char* __buf, bool __negative,
_Flags::_Sign __sign) {
if (__negative)
*__buf++ = '-';
else
switch (__sign) {
case _Flags::_Sign::__default:
case _Flags::_Sign::__minus:
// No sign added.
break;
case _Flags::_Sign::__plus:
*__buf++ = '+';
break;
case _Flags::_Sign::__space:
*__buf++ = ' ';
break;
}

return __buf;
}

_LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char c) {
switch (c) {
case 'a':
return 'A';
case 'b':
return 'B';
case 'c':
return 'C';
case 'd':
return 'D';
case 'e':
return 'E';
case 'f':
return 'F';
}
return c;
}

/**
* Determines the required grouping based on the size of the input.
*
Expand Down

0 comments on commit ed86610

Please sign in to comment.