Blame lib/trim.c

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