[libc-commits] [PATCH] D131302: [libc] move to combined integer converter

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Aug 9 22:49:37 PDT 2022


sivachandra added inline comments.


================
Comment at: libc/src/stdio/printf_core/int_converter.h:62-76
+  {
+    cpp::optional<cpp::StringView> opt_str;
+    if (to_lower(to_conv.conv_name) == 'x') {
+      opt_str = IntegerToString<uintmax_t, 16>::convert(
+          num, bufref, is_lower(to_conv.conv_name));
+    } else if (to_conv.conv_name == 'o') {
+      opt_str = IntegerToString<uintmax_t, 8>::convert(num, bufref, true);
----------------
A way to avoid this ugliness would be to move this over to an inline function like this:

```
cpp:optional<cpp::StringView> int_to_str(char conv_name) {
  if (to_lower(conv_name) == 'x') {
    return IntegerToString<uintmax_t, 16>::convert(
        num, bufref, is_lower(to_conv.conv_name));
  } else if (conv_name == 'o') {
    return IntegerToString<uintmax_t, 8>::convert(num, bufref, true);
  } else {
    return IntegerToString<uintmax_t, 10>::convert(num, bufref, true);
  }
}
```

And then use it as:

```
auto str = int_to_string(to_conv.conv_name);
if (!str)
  return INT_CONVERSION_ERROR;
```

Also, feel free to add `operator->` to `cpp:optional` so that you can call `str->size()` and `str->data()`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131302/new/

https://reviews.llvm.org/D131302



More information about the libc-commits mailing list