blob: 4b89b21f521ce1f80a778f72db052036992bf8cc [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2009 The Chromium Authors
[email protected]dce5df52009-06-29 17:58:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_FORMAT_MACROS_H_
6#define BASE_FORMAT_MACROS_H_
7
[email protected]34b2b002009-11-20 06:53:288// This file defines the format macros for some integer types.
9
10// To print a 64-bit value in a portable way:
[email protected]dce5df52009-06-29 17:58:2511// int64_t value;
12// printf("xyz:%" PRId64, value);
[email protected]34b2b002009-11-20 06:53:2813// The "d" in the macro corresponds to %d; you can also use PRIu64 etc.
[email protected]dce5df52009-06-29 17:58:2514//
15// For wide strings, prepend "Wide" to the macro:
16// int64_t value;
17// StringPrintf(L"xyz: %" WidePRId64, value);
[email protected]34b2b002009-11-20 06:53:2818//
19// To print a size_t value in a portable way:
20// size_t size;
21// printf("xyz: %" PRIuS, size);
22// The "u" in the macro corresponds to %u, and S is for "size".
[email protected]dce5df52009-06-29 17:58:2523
avi9b6f42932015-12-26 22:15:1424#include <stddef.h>
25#include <stdint.h>
26
[email protected]dce5df52009-06-29 17:58:2527#include "build/build_config.h"
28
Xiaohan Wang38e4ebb2022-01-19 06:57:4329#if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && \
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3930 (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
[email protected]dce5df52009-06-29 17:58:2531#error "inttypes.h has already been included before this header file, but "
32#error "without __STDC_FORMAT_MACROS defined."
33#endif
34
Xiaohan Wang38e4ebb2022-01-19 06:57:4335#if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && \
36 !defined(__STDC_FORMAT_MACROS)
[email protected]dce5df52009-06-29 17:58:2537#define __STDC_FORMAT_MACROS
38#endif
39
40#include <inttypes.h>
41
[email protected]fa1c33782010-02-25 17:20:0742#if !defined(PRIuS)
[email protected]34b2b002009-11-20 06:53:2843#define PRIuS "zu"
[email protected]fa1c33782010-02-25 17:20:0744#endif
[email protected]34b2b002009-11-20 06:53:2845
[email protected]3394a7c12014-03-10 20:16:3446// The size of NSInteger and NSUInteger varies between 32-bit and 64-bit
47// architectures and Apple does not provides standard format macros and
48// recommends casting. This has many drawbacks, so instead define macros
49// for formatting those types.
Xiaohan Wang38e4ebb2022-01-19 06:57:4350#if BUILDFLAG(IS_APPLE)
[email protected]3394a7c12014-03-10 20:16:3451#if defined(ARCH_CPU_64_BITS)
52#if !defined(PRIdNS)
53#define PRIdNS "ld"
54#endif
55#if !defined(PRIuNS)
56#define PRIuNS "lu"
57#endif
58#if !defined(PRIxNS)
59#define PRIxNS "lx"
60#endif
61#else // defined(ARCH_CPU_64_BITS)
62#if !defined(PRIdNS)
63#define PRIdNS "d"
64#endif
65#if !defined(PRIuNS)
66#define PRIuNS "u"
67#endif
68#if !defined(PRIxNS)
69#define PRIxNS "x"
70#endif
71#endif
Xiaohan Wang38e4ebb2022-01-19 06:57:4372#endif // BUILDFLAG(IS_APPLE)
[email protected]3394a7c12014-03-10 20:16:3473
[email protected]81e8ebf2010-09-16 07:59:2774#endif // BASE_FORMAT_MACROS_H_