Blame gnulib-tests/anytostr.c

Packit 709fb3
/* anytostr.c -- convert integers to printable strings
Packit 709fb3
Packit 709fb3
   Copyright (C) 2001, 2006, 2008-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Paul Eggert */
Packit 709fb3
Packit 709fb3
/* Tell gcc not to warn about the (i < 0) test, below.  */
Packit 709fb3
#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
Packit 709fb3
# pragma GCC diagnostic ignored "-Wtype-limits"
Packit 709fb3
#elif defined __clang__
Packit 709fb3
# pragma clang diagnostic ignored "-Wtautological-compare"
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include "inttostr.h"
Packit 709fb3
Packit 709fb3
/* Convert I to a printable string in BUF, which must be at least
Packit 709fb3
   INT_BUFSIZE_BOUND (INTTYPE) bytes long.  Return the address of the
Packit 709fb3
   printable string, which need not start at BUF.  */
Packit 709fb3
Packit 709fb3
char * __attribute_warn_unused_result__
Packit 709fb3
anytostr (inttype i, char *buf)
Packit 709fb3
{
Packit 709fb3
  char *p = buf + INT_STRLEN_BOUND (inttype);
Packit 709fb3
  *p = 0;
Packit 709fb3
Packit 709fb3
  if (i < 0)
Packit 709fb3
    {
Packit 709fb3
      do
Packit 709fb3
        *--p = '0' - i % 10;
Packit 709fb3
      while ((i /= 10) != 0);
Packit 709fb3
Packit 709fb3
      *--p = '-';
Packit 709fb3
    }
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
      do
Packit 709fb3
        *--p = '0' + i % 10;
Packit 709fb3
      while ((i /= 10) != 0);
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  return p;
Packit 709fb3
}