bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: last trim code


From: Bruno Haible
Subject: Re: last trim code
Date: Tue, 5 Sep 2006 14:30:54 +0200
User-agent: KMail/1.9.1

[Re-added bug-gnulib in CC.]

Hello Davide,

> On Monday 04 September 2006 18:56, you wrote:
> > I see also you've partially adopted the GNU style for this code. Fine!
> > Indeed, before we put code into gnulib, we reindent it to GNU style.
> > Another element of this style is to put a space before the opening
> > parenthesis of function definitions, function calls and macro calls:
> >       for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
> > etc.
> done
> 
> > This and the handling of platforms without multibyte functions (see my
> > last mail), and the code is ready for committing.
> done 

Thanks for the updates. The code appears to be correct as is, at a simple
glance. I've added it to gnulib.

Now, since you are the maintainer of this code, please report to this
mailing list updates that you make and that you think are reliable enough.
We will contact you before making changes to your code, except for
obviously correct and unquestionable ones.


2006-09-05  Bruno Haible  <address@hidden>

        * MODULES.html.sh (String handling): Add trim.

2006-09-05  Davide Angelocola <address@hidden>

        * modules/trim: New file.
        * lib/trim.h: New file.
        * lib/trim.c: New file.

============================= modules/trim =============================
Description:
trim() removes leading and/or trailing whitespaces

Files:
lib/trim.c
lib/trim.h

Depends-on:
xalloc
mbiter

configure.ac:

Makefile.am:
EXTRA_DIST += trim.h

Include:
#include "trim.h"

License:
GPL

Maintainer:
Davide Angelocola
============================== lib/trim.h =============================
/* Removes leading and/or trailing whitespaces
   Copyright (C) 2006 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.
   If not, write to the Free Software Foundation,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */

/* Written by Davide Angelocola <address@hidden> */

/* Trim mode. */
#define TRIM_TRAILING 0
#define TRIM_LEADING 1
#define TRIM_BOTH 2

/* Removes trailing and leading whitespaces. */
#define trim(s) trim2((s), TRIM_BOTH)

/* Removes trailing whitespaces. */
#define trim_trailing(s) trim2((s), TRIM_TRAILING)

/* Removes leading whitespaces. */
#define trim_leading(s) trim2((s), TRIM_LEADING)

char *trim2(const char *, int);

============================== lib/trim.c =============================
/* Removes leading and/or trailing whitespaces
   Copyright (C) 2006 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.
   If not, write to the Free Software Foundation,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */

/* Written by Davide Angelocola <address@hidden> */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#if HAVE_MBRTOWC 
# include <stddef.h>
# include "mbchar.h"
# include "mbiter.h"
# include "mbuiter.h"           /* FIXME: for MB_CUR_MAX */
#else
# include <ctype.h>
#endif

#include "xalloc.h"
#include "trim.h"

char *
trim2(const char *s, int how)
{
  char *d;
    
  d = strdup(s);

  if (!d)
    xalloc_die();
  
#if HAVE_MBRTOWC
  if (MB_CUR_MAX > 1)
    {
      mbi_iterator_t i;
      
      /* Trim leading whitespaces. */
      if (how != TRIM_TRAILING) 
        {
          mbi_init (i, d, strlen (d));
      
          for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
            ;
          
          memmove (d, mbi_cur_ptr (i), strlen (mbi_cur_ptr (i)) + 1);
        }
  
      /* Trim trailing whitespaces. */
      if (how != TRIM_LEADING) 
        {
          int state = 0;
          char *r;
          
          mbi_init (i, d, strlen (d));

          for (; mbi_avail (i); mbi_advance (i)) 
            {
              if (state == 0 && mb_isspace (mbi_cur (i))) 
                {
                  state = 0;
                  continue;
                }
              
              if (state == 0 && !mb_isspace (mbi_cur (i)))
                {
                  state = 1;
                  continue;
                }
              
              if (state == 1 && !mb_isspace (mbi_cur (i)))
                {
                  state = 1;
                  continue;
                }
              
              if (state == 1 && mb_isspace (mbi_cur (i))) 
                {
                  state = 2;
                  r = (char *) mbi_cur_ptr (i);
                }
              else if (state == 2 && mb_isspace (mbi_cur (i))) 
                {
                  state = 2;
                } 
              else 
                {
                  state = 1;
                }
            }
          
          if (state == 2) 
            *r = '\0';
        }
    }
  else
#endif /* HAVE_MBRTOWC */
    {
      char *p;
      
      /* Trim leading whitespaces. */
      if (how != TRIM_TRAILING) {
        for (p = d; *p && isspace (*p); p++)
          ;                     

        memmove (d, p, strlen (p) + 1);
      }

      /* Trim trailing whitespaces. */
      if (how != TRIM_LEADING) {
        for (p = d + strlen (d) - 1; p >= d && isspace (*p); p--)
          *p = '\0';
      }
    }
  
  return d;
}
  




reply via email to

[Prev in Thread] Current Thread [Next in Thread]