Mercurial > hg > octave-nkf
diff liboctave/util/oct-inttypes.h @ 18277:284e5c87f27b stable
Fix saving int8 and uint8 in plain text format (bug #40980)
* oct-inttypes.h (operator<<): Specialise this operator's
octave_int<T> overloads for T = int8_t and T = uint8_t so that it
calls non-char versions of std::operator<<
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Sun, 29 Dec 2013 10:11:30 -0500 |
parents | 0cd39f7f2409 |
children | 545a77c3206e 37c300acfcfd |
line wrap: on
line diff
--- a/liboctave/util/oct-inttypes.h +++ b/liboctave/util/oct-inttypes.h @@ -1023,6 +1023,50 @@ return is; } +// We need to specialise for char and unsigned char because +// std::operator<< and std::operator>> are overloaded to input and +// output the ASCII character values instead of a representation of +// their numerical value (e.g. os << char(10) outputs a space instead +// of outputting the characters '1' and '0') + +template <> +inline std::ostream& +operator << (std::ostream& os, const octave_int<int8_t>& ival) +{ + os << static_cast<int> (ival.value ()); + return os; +} + +template <> +inline std::ostream& +operator << (std::ostream& os, const octave_int<uint8_t>& ival) +{ + os << static_cast<unsigned int> (ival.value ()); + return os; +} + + +template <> +inline std::istream& +operator >> (std::istream& is, octave_int<int8_t>& ival) +{ + int tmp = 0; + is >> tmp; + ival = static_cast<int8_t> (tmp); + return is; +} + +template <> +inline std::istream& +operator >> (std::istream& is, octave_int<uint8_t>& ival) +{ + unsigned int tmp = 0; + is >> tmp; + ival = static_cast<uint8_t> (tmp); + return is; +} + + // Bitwise operations #define OCTAVE_INT_BITCMP_OP(OP) \