Skip to main content

dr_sscanf

Function dr_sscanf 

Source
pub unsafe extern "C" fn dr_sscanf(
    str_: *const c_char,
    fmt: *const c_char,
    ...
) -> c_int
Expand description

Utility routine to parse strings that match a pre-defined format string, similar to the sscanf() C routine.

@param[in] str String to parse. @param[in] fmt Format string controlling parsing. @param[out] … All remaining parameters interpreted as output parameter pointers. The type of each parameter must match the type implied by the corresponding format specifier in \p fmt. \return The number of specifiers matched.

The benefit of using dr_sscanf() over native sscanf() is that DR’s implementation is standalone, signal-safe, and cross-platform. On Linux, sscanf() has been observed to call malloc(). On Windows, sscanf() will call strlen(), which can break when using mapped files.

The behavior of dr_sscanf() is mostly identical to that of the sscanf() C routine.

Supported format specifiers:

  • %s: Matches a sequence of non-whitespace characters. The string is copied into the provided output buffer. To avoid buffer overflow, the caller should use a width specifier.
  • %c: Matches any single character.
  • %d: Matches a signed decimal integer.
  • %u: Matches an unsigned decimal integer.
  • %x: Matches an unsigned hexadecimal integer, with or without a leading 0x.
  • %p: Matches a pointer-sized hexadecimal integer as %x does.
  • %%: Matches a literal % character. Does not store output.

Supported format modifiers:

  • *: The * modifier causes the scan to match the specifier, but not store any output. No output parameter is consumed for this specifier, and one should not be passed.
  • 0-9: A decimal integer preceding the specifier gives the width to match. For strings, this indicates the maximum number of characters to copy. For integers, this indicates the maximum number of digits to parse.
  • h: Marks an integer specifier as short.
  • l: Marks an integer specifier as long.
  • ll: Marks an integer specifier as long long. Use this for 64-bit integers.

\warning dr_sscanf() does \em not support parsing floating point numbers yet.