Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf format specifiers for Perl API types

Tags:

perl

xs

Are there macros that provide the correct printf format specifiers for IV, UV, STRLEN, Size_t and SSize_t? None are listed in perlapi.


C provides macros for the format specifiers for the types provided by stdint.h, such as uint32_t.

#include <inttypes.h>
#include <stdint.h>

uint32_t i = ...;
printf("i = %" PRIu32 "\n", i);

Is there something similar to PRIu32 for IV, UV, STRLEN, Size_t and SSize_t?


The larger problem is that I'm trying to suggest a fix for the following compilation warnings produced when installing Sort::Key on Ubuntu on Windows Subsystem for Linux:

Key.xs: In function ‘_keysort’:
Key.xs:237:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘IV {aka long int}’ [-Wformat=]
      croak("unsupported sort type %d", type);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~
Key.xs: In function ‘_multikeysort’:
Key.xs:547:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘STRLEN {aka long unsigned int}’ [-Wformat=]
   croak("wrong number of results returned "
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Key.xs:547:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘IV {aka long int}’ [-Wformat=]
like image 993
ikegami Avatar asked Oct 20 '25 01:10

ikegami


1 Answers

  • For UV, the following macros exist:

    • UVuf (decimal)
    • UVof (octal)
    • UVxf (lc hex)
    • UVXf (uc hex)
  • For IV, the following macro exists:

    • IVdf (decimal)
  • For NV, the following macros exist:

    • NVef ("%e-ish")
    • NVff ("%f-ish")
    • NVgf ("%g-ish")
  • For Size_t and STRLEN, use the builtin z length modifier.[1]

    • %zu (decimal)
    • %zo (octal)
    • %zx (lc hex)
    • %zX (uc hex)
  • For SSize_t, use the builtin z length modifier.[1]

    • %zd (decimal)

For example,

IV iv      = ...;
STRLEN len = ...;

croak("iv=%" IVdf " len=%zu", iv, len);

  1. While Size_t and SSize_t are configurable, they're never different from size_t and ssize_t in practice, and STRLEN is a typedef for Size_t.
like image 99
ikegami Avatar answered Oct 22 '25 04:10

ikegami