Blame gettext-runtime/gnulib-lib/trim.c

Packit Bot 06c835
/* Removes leading and/or trailing whitespaces
Packit Bot 06c835
   Copyright (C) 2006-2015 Free Software Foundation, Inc.
Packit Bot 06c835
Packit Bot 06c835
   This program is free software: you can redistribute it and/or modify
Packit Bot 06c835
   it under the terms of the GNU General Public License as published by
Packit Bot 06c835
   the Free Software Foundation; either version 3 of the License, or
Packit Bot 06c835
   (at your option) any later version.
Packit Bot 06c835
Packit Bot 06c835
   This program is distributed in the hope that it will be useful,
Packit Bot 06c835
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 06c835
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Bot 06c835
   GNU General Public License for more details.
Packit Bot 06c835
Packit Bot 06c835
   You should have received a copy of the GNU General Public License
Packit Bot 06c835
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Bot 06c835
Packit Bot 06c835
/* Written by Davide Angelocola <davide.angelocola@gmail.com> */
Packit Bot 06c835
Packit Bot 06c835
#include <config.h>
Packit Bot 06c835
Packit Bot 06c835
/* Specification.  */
Packit Bot 06c835
#include "trim.h"
Packit Bot 06c835
Packit Bot 06c835
#include <ctype.h>
Packit Bot 06c835
#include <string.h>
Packit Bot 06c835
#include <stddef.h>
Packit Bot 06c835
#include <stdlib.h>
Packit Bot 06c835
Packit Bot 06c835
#include "mbchar.h"
Packit Bot 06c835
#include "mbiter.h"
Packit Bot 06c835
#include "xalloc.h"
Packit Bot 06c835
Packit Bot 06c835
/* Use this to suppress gcc's "...may be used before initialized" warnings. */
Packit Bot 06c835
#ifdef lint
Packit Bot 06c835
# define IF_LINT(Code) Code
Packit Bot 06c835
#else
Packit Bot 06c835
# define IF_LINT(Code) /* empty */
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
char *
Packit Bot 06c835
trim2 (const char *s, int how)
Packit Bot 06c835
{
Packit Bot 06c835
  char *d;
Packit Bot 06c835
Packit Bot 06c835
  d = strdup (s);
Packit Bot 06c835
Packit Bot 06c835
  if (!d)
Packit Bot 06c835
    xalloc_die ();
Packit Bot 06c835
Packit Bot 06c835
  if (MB_CUR_MAX > 1)
Packit Bot 06c835
    {
Packit Bot 06c835
      mbi_iterator_t i;
Packit Bot 06c835
Packit Bot 06c835
      /* Trim leading whitespaces. */
Packit Bot 06c835
      if (how != TRIM_TRAILING)
Packit Bot 06c835
        {
Packit Bot 06c835
          mbi_init (i, d, strlen (d));
Packit Bot 06c835
Packit Bot 06c835
          for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
Packit Bot 06c835
            ;
Packit Bot 06c835
Packit Bot 06c835
          memmove (d, mbi_cur_ptr (i), strlen (mbi_cur_ptr (i)) + 1);
Packit Bot 06c835
        }
Packit Bot 06c835
Packit Bot 06c835
      /* Trim trailing whitespaces. */
Packit Bot 06c835
      if (how != TRIM_LEADING)
Packit Bot 06c835
        {
Packit Bot 06c835
          unsigned int state = 0;
Packit Bot 06c835
          char *r IF_LINT (= NULL); /* used only while state = 2 */
Packit Bot 06c835
Packit Bot 06c835
          mbi_init (i, d, strlen (d));
Packit Bot 06c835
Packit Bot 06c835
          for (; mbi_avail (i); mbi_advance (i))
Packit Bot 06c835
            {
Packit Bot 06c835
              if (state == 0 && mb_isspace (mbi_cur (i)))
Packit Bot 06c835
                continue;
Packit Bot 06c835
Packit Bot 06c835
              if (state == 0 && !mb_isspace (mbi_cur (i)))
Packit Bot 06c835
                {
Packit Bot 06c835
                  state = 1;
Packit Bot 06c835
                  continue;
Packit Bot 06c835
                }
Packit Bot 06c835
Packit Bot 06c835
              if (state == 1 && !mb_isspace (mbi_cur (i)))
Packit Bot 06c835
                continue;
Packit Bot 06c835
Packit Bot 06c835
              if (state == 1 && mb_isspace (mbi_cur (i)))
Packit Bot 06c835
                {
Packit Bot 06c835
                  state = 2;
Packit Bot 06c835
                  r = (char *) mbi_cur_ptr (i);
Packit Bot 06c835
                }
Packit Bot 06c835
              else if (state == 2 && mb_isspace (mbi_cur (i)))
Packit Bot 06c835
                {
Packit Bot 06c835
                  /* empty */
Packit Bot 06c835
                }
Packit Bot 06c835
              else
Packit Bot 06c835
                {
Packit Bot 06c835
                  state = 1;
Packit Bot 06c835
                }
Packit Bot 06c835
            }
Packit Bot 06c835
Packit Bot 06c835
          if (state == 2)
Packit Bot 06c835
            *r = '\0';
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
  else
Packit Bot 06c835
    {
Packit Bot 06c835
      char *p;
Packit Bot 06c835
Packit Bot 06c835
      /* Trim leading whitespaces. */
Packit Bot 06c835
      if (how != TRIM_TRAILING)
Packit Bot 06c835
        {
Packit Bot 06c835
          for (p = d; *p && isspace ((unsigned char) *p); p++)
Packit Bot 06c835
            ;
Packit Bot 06c835
Packit Bot 06c835
          memmove (d, p, strlen (p) + 1);
Packit Bot 06c835
        }
Packit Bot 06c835
Packit Bot 06c835
      /* Trim trailing whitespaces. */
Packit Bot 06c835
      if (how != TRIM_LEADING)
Packit Bot 06c835
        {
Packit Bot 06c835
          for (p = d + strlen (d) - 1;
Packit Bot 06c835
               p >= d && isspace ((unsigned char) *p); p--)
Packit Bot 06c835
            *p = '\0';
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
Packit Bot 06c835
  return d;
Packit Bot 06c835
}