mfbt/IntegerPrintfMacros.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/IntegerPrintfMacros.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,40 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* Implements the C99 <inttypes.h> interface, minus the SCN* format macros. */
    1.10 +
    1.11 +#ifndef mozilla_IntegerPrintfMacros_h_
    1.12 +#define mozilla_IntegerPrintfMacros_h_
    1.13 +
    1.14 +/*
    1.15 + * MSVC++ doesn't include <inttypes.h>, even in versions shipping <stdint.h>, so
    1.16 + * we have to reimplement it there.  Note: <inttypes.h> #includes <stdint.h>.
    1.17 + *
    1.18 + * Note that this header DOES NOT implement <inttypes.h>'s scanf macros.  MSVC's
    1.19 + * scanf doesn't have sufficient format specifier support to implement them
    1.20 + * (specifically, to implement scanning into an 8-bit location).
    1.21 + *
    1.22 + * http://stackoverflow.com/questions/3036396/scanfd-char-char-as-int-format-string
    1.23 + *
    1.24 + * Moreover, scanf is a footgun: if the input number exceeds the bounds of the
    1.25 + * target type, behavior is undefined (in the compiler sense: that is, this code
    1.26 + * could overwrite your hard drive with zeroes):
    1.27 + *
    1.28 + *   uint8_t u;
    1.29 + *   sscanf("256", "%" SCNu8, &u); // BAD
    1.30 + *
    1.31 + * This header will sometimes provide SCN* macros, by dint of being implemented
    1.32 + * using <inttypes.h>.  But for these reasons, *never* use them!
    1.33 + */
    1.34 +
    1.35 +#if defined(MOZ_CUSTOM_INTTYPES_H)
    1.36 +#  include MOZ_CUSTOM_INTTYPES_H
    1.37 +#elif defined(_MSC_VER)
    1.38 +#  include "mozilla/MSIntTypes.h"
    1.39 +#else
    1.40 +#  include <inttypes.h>
    1.41 +#endif
    1.42 +
    1.43 +#endif  /* mozilla_IntegerPrintfMacros_h_ */

mercurial