LCOV - code coverage report
Current view: top level - snapdev - ostream_int128.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 30 100.0 %
Date: 2021-08-21 10:14:39 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Output __int128 or unsigned __int128 to an iostream
       2             : // Copyright (c) 2021  Made to Order Software Corp.  All Rights Reserved
       3             : //
       4             : // This program is free software; you can redistribute it and/or modify
       5             : // it under the terms of the GNU General Public License as published by
       6             : // the Free Software Foundation; either version 2 of the License, or
       7             : // (at your option) any later version.
       8             : //
       9             : // This program is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             : // GNU General Public License for more details.
      13             : //
      14             : // You should have received a copy of the GNU General Public License
      15             : // along with this program; if not, write to the Free Software
      16             : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      17             : #pragma once
      18             : 
      19             : /** \file
      20             :  * \brief Print large integers to any iostream.
      21             :  *
      22             :  * This function prints large integers (128 bits) to the specified iostream.
      23             :  */
      24             : 
      25             : // C++ lib
      26             : //
      27             : #include    <ostream>
      28             : #include    <sstream>
      29             : 
      30             : 
      31             : namespace snap
      32             : {
      33             : 
      34             : /** \brief Convert an __int128 to a string.
      35             :  *
      36             :  * This function converts an __int128 to a string.
      37             :  *
      38             :  * \param[in] x  An __int128 number.
      39             :  *
      40             :  * \return A string representing that __int128 number.
      41             :  */
      42             : #pragma GCC diagnostic push
      43             : #pragma GCC diagnostic ignored "-Wpedantic"
      44             : #pragma GCC diagnostic ignored "-Wstrict-overflow"
      45        2022 : std::string to_string(__int128 x)
      46             : {
      47        2022 :     char buf[42];
      48             : 
      49        2022 :     int idx(sizeof(buf));
      50        2022 :     unsigned __int128 y(x < 0
      51        2022 :         ? static_cast<unsigned __int128>(-x)
      52             :         : static_cast<unsigned __int128>(x));
      53        2022 :     constexpr unsigned __int128 const limit(9);
      54       36086 :     while(y > limit)
      55             :     {
      56       17032 :         --idx;
      57       17032 :         buf[idx] = y % 10 + '0';
      58       17032 :         y /= 10;
      59             :     }
      60        2022 :     --idx;
      61        2022 :     buf[idx] = y + '0';
      62        2022 :     if(x < 0)
      63             :     {
      64        1011 :         --idx;
      65        1011 :         buf[idx] = '-';
      66             :     }
      67             : 
      68        2022 :     return std::string(buf + idx, sizeof(buf) - idx);
      69             : }
      70             : #pragma GCC diagnostic pop
      71             : 
      72             : 
      73             : /** \brief Convert an unsigned __int128 to a string.
      74             :  *
      75             :  * This function converts an unsigned __int128 to a string.
      76             :  *
      77             :  * \param[in] x  An unsigned __int128 number.
      78             :  *
      79             :  * \return A string representing that unsigned __int128 number.
      80             :  */
      81             : #pragma GCC diagnostic push
      82             : #pragma GCC diagnostic ignored "-Wpedantic"
      83           1 : std::string to_string(unsigned __int128 x)
      84             : {
      85           1 :     char buf[42];
      86             : 
      87           1 :     int idx(sizeof(buf));
      88          77 :     while(x > 9)
      89             :     {
      90          38 :         --idx;
      91          38 :         buf[idx] = x % 10 + '0';
      92          38 :         x /= 10;
      93             :     }
      94           1 :     --idx;
      95           1 :     buf[idx] = x + '0';
      96             : 
      97           1 :     return std::string(buf + idx, sizeof(buf) - idx);
      98             : }
      99             : #pragma GCC diagnostic pop
     100             : 
     101             : }
     102             : 
     103             : 
     104             : 
     105             : //namespace snap -- no namespace for such definitions, it works better that way
     106             : 
     107             : 
     108             : /** \brief Output an __int128 number.
     109             :  *
     110             :  * \warning
     111             :  * This implementation does not offer proper formatting.
     112             :  * It will first generate a string then output that string
     113             :  * which means the result is not what you'd expect if you
     114             :  * used formatting such as std::hex and std::setw().
     115             :  *
     116             :  * \param[in] os  The output stream.
     117             :  * \param[in] x  The value to be output in \p os.
     118             :  *
     119             :  * \return A reference to \p os.
     120             :  */
     121             : #pragma GCC diagnostic push
     122             : #pragma GCC diagnostic ignored "-Wpedantic"
     123        2022 : std::ostream & operator << (std::ostream & os, __int128 x)
     124             : {
     125        2022 :     return os << snap::to_string(x);
     126             : }
     127             : #pragma GCC diagnostic pop
     128             : 
     129             : 
     130             : /** \brief Output an unsigned __int128 number.
     131             :  *
     132             :  * \warning
     133             :  * This implementation does not offer proper formatting.
     134             :  * It will first generate a string then output that string
     135             :  * which means the result is not what you'd expect if you
     136             :  * used formatting such as std::hex and std::setw().
     137             :  *
     138             :  * \param[in] os  The output stream.
     139             :  * \param[in] x  The value to be output in \p os.
     140             :  *
     141             :  * \return A reference to \p os.
     142             :  */
     143             : #pragma GCC diagnostic push
     144             : #pragma GCC diagnostic ignored "-Wpedantic"
     145           1 : std::ostream & operator << (std::ostream & os, unsigned __int128 x)
     146             : {
     147           1 :     return os << snap::to_string(x);
     148             : }
     149             : #pragma GCC diagnostic pop
     150             : 
     151             : 
     152             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13