blob: d58658d2b91a428dfed116248595748a28617d4c [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
8// This file defines the format macros for some integer types.
9
10// To print a 64-bit value in a portable way:
11// int64_t value;
12// printf("xyz:%" PRId64, value);
13// The "d" in the macro corresponds to %d; you can also use PRIu64 etc.
14//
15// For wide strings, prepend "Wide" to the macro:
16// int64_t value;
17// StringPrintf(L"xyz: %" WidePRId64, value);
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".
23
24#include "build/build_config.h"
25
James Robinsonc8f302a2015-05-14 16:38:33 -070026#if defined(OS_POSIX) && (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && \
27 !defined(PRId64)
James Robinson646469d2014-10-03 15:33:28 -070028#error "inttypes.h has already been included before this header file, but "
29#error "without __STDC_FORMAT_MACROS defined."
30#endif
31
James Robinsonc8f302a2015-05-14 16:38:33 -070032#if defined(OS_POSIX) && !defined(__STDC_FORMAT_MACROS)
James Robinson646469d2014-10-03 15:33:28 -070033#define __STDC_FORMAT_MACROS
34#endif
35
36#include <inttypes.h>
37
James Robinsonc8f302a2015-05-14 16:38:33 -070038#if defined(OS_POSIX)
39
James Robinson646469d2014-10-03 15:33:28 -070040// GCC will concatenate wide and narrow strings correctly, so nothing needs to
41// be done here.
42#define WidePRId64 PRId64
43#define WidePRIu64 PRIu64
44#define WidePRIx64 PRIx64
45
46#if !defined(PRIuS)
47#define PRIuS "zu"
48#endif
49
50// The size of NSInteger and NSUInteger varies between 32-bit and 64-bit
51// architectures and Apple does not provides standard format macros and
52// recommends casting. This has many drawbacks, so instead define macros
53// for formatting those types.
54#if defined(OS_MACOSX)
55#if defined(ARCH_CPU_64_BITS)
56#if !defined(PRIdNS)
57#define PRIdNS "ld"
58#endif
59#if !defined(PRIuNS)
60#define PRIuNS "lu"
61#endif
62#if !defined(PRIxNS)
63#define PRIxNS "lx"
64#endif
65#else // defined(ARCH_CPU_64_BITS)
66#if !defined(PRIdNS)
67#define PRIdNS "d"
68#endif
69#if !defined(PRIuNS)
70#define PRIuNS "u"
71#endif
72#if !defined(PRIxNS)
73#define PRIxNS "x"
74#endif
75#endif
76#endif // defined(OS_MACOSX)
77
78#else // OS_WIN
79
James Robinsonc8f302a2015-05-14 16:38:33 -070080#if !defined(PRId64) || !defined(PRIu64) || !defined(PRIx64)
81#error "inttypes.h provided by win toolchain should define these."
James Robinson646469d2014-10-03 15:33:28 -070082#endif
83
84#define WidePRId64 L"I64d"
85#define WidePRIu64 L"I64u"
86#define WidePRIx64 L"I64x"
87
88#if !defined(PRIuS)
89#define PRIuS "Iu"
90#endif
91
92#endif
93
94#endif // BASE_FORMAT_MACROS_H_