Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2009 The Chromium Authors |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 2 | // 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] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 8 | // This file defines the format macros for some integer types. |
9 | |||||
10 | // To print a 64-bit value in a portable way: | ||||
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 11 | // int64_t value; |
12 | // printf("xyz:%" PRId64, value); | ||||
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 13 | // The "d" in the macro corresponds to %d; you can also use PRIu64 etc. |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 14 | // |
15 | // For wide strings, prepend "Wide" to the macro: | ||||
16 | // int64_t value; | ||||
17 | // StringPrintf(L"xyz: %" WidePRId64, value); | ||||
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 18 | // |
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] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 23 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 24 | #include <stddef.h> |
25 | #include <stdint.h> | ||||
26 | |||||
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 27 | #include "build/build_config.h" |
28 | |||||
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 29 | #if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && \ |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 30 | (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64) |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 31 | #error "inttypes.h has already been included before this header file, but " |
32 | #error "without __STDC_FORMAT_MACROS defined." | ||||
33 | #endif | ||||
34 | |||||
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 35 | #if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && \ |
36 | !defined(__STDC_FORMAT_MACROS) | ||||
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 37 | #define __STDC_FORMAT_MACROS |
38 | #endif | ||||
39 | |||||
40 | #include <inttypes.h> | ||||
41 | |||||
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 42 | #if BUILDFLAG(IS_WIN) |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 43 | |
44 | #if !defined(PRId64) || !defined(PRIu64) || !defined(PRIx64) | ||||
45 | #error "inttypes.h provided by win toolchain should define these." | ||||
46 | #endif | ||||
47 | |||||
48 | #define WidePRId64 L"I64d" | ||||
49 | #define WidePRIu64 L"I64u" | ||||
50 | #define WidePRIx64 L"I64x" | ||||
51 | |||||
52 | #if !defined(PRIuS) | ||||
53 | #define PRIuS "Iu" | ||||
54 | #endif | ||||
55 | |||||
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 56 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
chcunningham | 08e78a4 | 2015-05-10 22:56:00 | [diff] [blame] | 57 | |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 58 | // GCC will concatenate wide and narrow strings correctly, so nothing needs to |
59 | // be done here. | ||||
60 | #define WidePRId64 PRId64 | ||||
61 | #define WidePRIu64 PRIu64 | ||||
62 | #define WidePRIx64 PRIx64 | ||||
63 | |||||
[email protected] | fa1c3378 | 2010-02-25 17:20:07 | [diff] [blame] | 64 | #if !defined(PRIuS) |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 65 | #define PRIuS "zu" |
[email protected] | fa1c3378 | 2010-02-25 17:20:07 | [diff] [blame] | 66 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 67 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 68 | #endif // BUILDFLAG(IS_WIN) |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 69 | |
[email protected] | 3394a7c1 | 2014-03-10 20:16:34 | [diff] [blame] | 70 | // The size of NSInteger and NSUInteger varies between 32-bit and 64-bit |
71 | // architectures and Apple does not provides standard format macros and | ||||
72 | // recommends casting. This has many drawbacks, so instead define macros | ||||
73 | // for formatting those types. | ||||
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 74 | #if BUILDFLAG(IS_APPLE) |
[email protected] | 3394a7c1 | 2014-03-10 20:16:34 | [diff] [blame] | 75 | #if defined(ARCH_CPU_64_BITS) |
76 | #if !defined(PRIdNS) | ||||
77 | #define PRIdNS "ld" | ||||
78 | #endif | ||||
79 | #if !defined(PRIuNS) | ||||
80 | #define PRIuNS "lu" | ||||
81 | #endif | ||||
82 | #if !defined(PRIxNS) | ||||
83 | #define PRIxNS "lx" | ||||
84 | #endif | ||||
85 | #else // defined(ARCH_CPU_64_BITS) | ||||
86 | #if !defined(PRIdNS) | ||||
87 | #define PRIdNS "d" | ||||
88 | #endif | ||||
89 | #if !defined(PRIuNS) | ||||
90 | #define PRIuNS "u" | ||||
91 | #endif | ||||
92 | #if !defined(PRIxNS) | ||||
93 | #define PRIxNS "x" | ||||
94 | #endif | ||||
95 | #endif | ||||
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 96 | #endif // BUILDFLAG(IS_APPLE) |
[email protected] | 3394a7c1 | 2014-03-10 20:16:34 | [diff] [blame] | 97 | |
[email protected] | 81e8ebf | 2010-09-16 07:59:27 | [diff] [blame] | 98 | #endif // BASE_FORMAT_MACROS_H_ |