blob: 4d90c593a61690895f152a4b03e597b1c3fc3613 [file] [log] [blame]
agl@chromium.orgd366e2c2009-06-30 02:58:25 +09001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
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
evan@chromium.org3f6b2602009-11-20 15:53:28 +09008// This file defines the format macros for some integer types.
9
10// To print a 64-bit value in a portable way:
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090011// int64_t value;
12// printf("xyz:%" PRId64, value);
evan@chromium.org3f6b2602009-11-20 15:53:28 +090013// The "d" in the macro corresponds to %d; you can also use PRIu64 etc.
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090014//
15// For wide strings, prepend "Wide" to the macro:
16// int64_t value;
17// StringPrintf(L"xyz: %" WidePRId64, value);
evan@chromium.org3f6b2602009-11-20 15:53:28 +090018//
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".
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090023
24#include "build/build_config.h"
25
26#if defined(OS_POSIX)
27
28#if (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
29#error "inttypes.h has already been included before this header file, but "
30#error "without __STDC_FORMAT_MACROS defined."
31#endif
32
33#if !defined(__STDC_FORMAT_MACROS)
34#define __STDC_FORMAT_MACROS
35#endif
36
37#include <inttypes.h>
38
39// GCC will concatenate wide and narrow strings correctly, so nothing needs to
40// be done here.
41#define WidePRId64 PRId64
42#define WidePRIu64 PRIu64
43#define WidePRIx64 PRIx64
44
gregoryd@google.comfad1a462010-02-26 02:20:07 +090045#if !defined(PRIuS)
evan@chromium.org3f6b2602009-11-20 15:53:28 +090046#define PRIuS "zu"
gregoryd@google.comfad1a462010-02-26 02:20:07 +090047#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +090048
sdefresne@chromium.orgc9dcae62014-03-11 05:16:34 +090049// The size of NSInteger and NSUInteger varies between 32-bit and 64-bit
50// architectures and Apple does not provides standard format macros and
51// recommends casting. This has many drawbacks, so instead define macros
52// for formatting those types.
53#if defined(OS_MACOSX)
54#if defined(ARCH_CPU_64_BITS)
55#if !defined(PRIdNS)
56#define PRIdNS "ld"
57#endif
58#if !defined(PRIuNS)
59#define PRIuNS "lu"
60#endif
61#if !defined(PRIxNS)
62#define PRIxNS "lx"
63#endif
64#else // defined(ARCH_CPU_64_BITS)
65#if !defined(PRIdNS)
66#define PRIdNS "d"
67#endif
68#if !defined(PRIuNS)
69#define PRIuNS "u"
70#endif
71#if !defined(PRIxNS)
72#define PRIxNS "x"
73#endif
74#endif
75#endif // defined(OS_MACOSX)
76
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090077#else // OS_WIN
78
gregoryd@google.comddf71512009-09-29 09:33:46 +090079#if !defined(PRId64)
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090080#define PRId64 "I64d"
gregoryd@google.comddf71512009-09-29 09:33:46 +090081#endif
82
83#if !defined(PRIu64)
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090084#define PRIu64 "I64u"
gregoryd@google.comddf71512009-09-29 09:33:46 +090085#endif
86
87#if !defined(PRIx64)
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090088#define PRIx64 "I64x"
gregoryd@google.comddf71512009-09-29 09:33:46 +090089#endif
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090090
91#define WidePRId64 L"I64d"
92#define WidePRIu64 L"I64u"
93#define WidePRIx64 L"I64x"
94
gregoryd@google.comfad1a462010-02-26 02:20:07 +090095#if !defined(PRIuS)
evan@chromium.org3f6b2602009-11-20 15:53:28 +090096#define PRIuS "Iu"
gregoryd@google.comfad1a462010-02-26 02:20:07 +090097#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +090098
agl@chromium.orgd366e2c2009-06-30 02:58:25 +090099#endif
100
dpolukhin@chromium.org21b228d2010-09-16 16:59:27 +0900101#endif // BASE_FORMAT_MACROS_H_