(forw) Code Contribution to glib



----- Forwarded message from kai.poitschke@henkel.de -----
Received: from cs2.CS.Berkeley.EDU (cs2.CS.Berkeley.EDU [169.229.60.56]) by paris.CS.Berkeley.EDU (8.8.3/8.8.2) with ESMTP id EAA13690 for <jmacd@paris.CS.Berkeley.EDU>; Thu, 17 Dec 1998 04:18:24 -0800 (PST)
From: kai.poitschke@henkel.de
Received: from scam.xcf.berkeley.edu (scam.XCF.Berkeley.EDU [128.32.43.201]) by cs2.CS.Berkeley.EDU (8.9.1a/8.6.6.Beta11) with SMTP id EAA17089 for <jmacd@cs.berkeley.edu>; Thu, 17 Dec 1998 04:18:22 -0800 (PST)
Received: (qmail 10159 invoked by uid 27263); 17 Dec 1998 12:19:38 -0000
Delivered-To: jmacd@xcf.berkeley.edu
Received: (qmail 10150 invoked from network); 17 Dec 1998 12:19:37 -0000
Received: from sunsrv1.henkel.de (139.3.73.1)
  by scam.xcf.berkeley.edu with SMTP; 17 Dec 1998 12:19:37 -0000
Received: from www.pc-poitsch2 (root@pc-poitsch2.henkel.de [139.3.59.20])
	by sunsrv1.henkel.de (8.8.8/8.8.8) with ESMTP id NAA17399;
	Thu, 17 Dec 1998 13:17:50 +0100 (MET)
Received: from pc-poitsch2.henkel.de (idis@pc-poitsch2 [139.3.59.20])
	by www.pc-poitsch2 (8.8.8/8.8.8) with ESMTP id NAA14939;
	Thu, 17 Dec 1998 13:17:49 +0100
Message-ID: <XFMail.981217131748.kai.poitschke@henkel.de>
X-Mailer: XFMail 1.3 [p0] on Linux
X-Priority: 3 (Normal)
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
Date: Thu, 17 Dec 1998 13:17:48 +0100 (MET)
To: petm@xcf.berkeley.edu, spencer@xcf.berkeley.edu, jmacd@xcf.berkeley.edu
Subject: Code Contribution to glib
Content-Transfer-Encoding: 8bit

Hello,
I want to contribute 2 new functions to the module gstrfuncs.c
g_strltrim (string, set) and g_strrtrim (string,set)
which remove characters from left or right up to the first character not in set.

Feel free to integrate them into the source. At the bottom you'll find the 
test functions you can add in testglib.c

I hope I meet your coding styleguide.

Kai
---
Unix, WinNT and MS-DOS.  The Good, The Bad and The Ugly.
Kai Poitschke       MailTo:kai.poitschke[at]computer.org
Sülzburgstr. 17     Phone : +49 (0) 221 / 42 21 07
D-50937 Köln        Fax   : +49 (0) 221 / 9 41 61 44


------------------------------------------------------------------------snip



/*---------------------------------------------------------------------------
 *
 * NAME
 *    g_strltrim (string left trim)
 *
 * SYNOPSIS
 *    #include <glib.h>
 *    gchar * g_strltrim (gchar * string, gchar const * set);
 *
 * DESCRIPTION
 *   Removes characters from the left of string. Initial characters are
 *   removed up to the first character not in set.
 *   This is the same functionality like the oracle sql function LTRIM,
 *   except there is no default value.
 *
 * EXAMPLE
 *    strcpy (str1,"xyxXxyLAST WORD");
 *    str_ltrim (str1, "xy"); // This returns "XxyLAST WORD".
 *
 * RETURN VALUE
 *    Pointer to str1.
 *
 * SEE ALSO
 *    g_strrtrim
 *
 * AUTHOR
 *    Kai Poitschke (kai.poitschke@computer.org)
 *---------------------------------------------------------------------------*/
gchar *
g_strltrim (gchar * string, gchar const * set)
{
  register gchar *s;
  register gchar *d;

  if (!set || '\0' == *set || !string || '\0' == *string)
      return (string);

  for ( s = string; *s != '\0' ; s++) {
    if (!strchr (set, *s)) {
      break;
    }
  }
  
  if ( s != string) {
    for (d = string; '\0' != *s; d++, s++ ) {
      *d = *s;
    }
    *d = '\0';
  }
  return (string);
}

/*---------------------------------------------------------------------------
 *
 * NAME
 *    g_strrtrim (string right trim)
 *
 * SYNOPSIS
 *    #include <glib.h>
 *    gchar * g_strrtrim (gchar * string, gchar const * set);
 *
 * DESCRIPTION
 *   Returns string, with final characters removed after the last character
 *   not is set.
 *   This is the same functionality like the oracle sql function RTRIM,
 *   except there is no default value.
 *
 * EXAMPLE
 *    strcpy (str1, "TURNERxyXxy");
 *    str_rtrim (str, "yx");  // This returns "TURNERxyX".
 *
 * RETURN VALUE
 *    Pointer to str1.
 *
 * SEE ALSO
 *    g_strltrim
 *
 * AUTHOR
 *    Kai Poitschke (kai.poitschke@computer.org)
 *---------------------------------------------------------------------------*/
gchar *
g_strrtrim (gchar * string, gchar const * set)
{
  register gchar *s;
  register int len;

  if (!set || '\0' == *set || !string || '\0' == *string)
      return (string);
  len = strlen (string);

  for ( s = &string[len-1]; len > 0; s--, len--) {
    if (!strrchr (set, *s)) {
      break;
    }
  }

  *(++s) = '\0';

  return (string);
}

---------------------------------------------------------------------------snap
/* Add that at the end of testglib.c */

  g_print ("checking g_strrtrim...");
  strcpy (chars, "TestxyXxy");
  g_strrtrim (chars, "yx");
  g_assert ( 0 == strcmp (chars ,"TestxyX"));
  strcpy (chars, "TestxyXxy");
  g_strrtrim (chars, "X");
  g_assert ( 0 == strcmp (chars ,"TestxyXxy"));
  strcpy (chars, "TestxyXxy");
  g_strrtrim (chars, "y");
  g_assert ( 0 == strcmp (chars ,"TestxyXx"));


  g_print ("ok\n");

    g_print ("checking g_strltrim...");
  strcpy (chars, "xyXxyTest");
  g_strltrim (chars, "yx");
  g_assert ( 0 == strcmp (chars ,"XxyTest"));

  strcpy (chars, "xyXxyTest");
  g_strltrim (chars, "X");
  g_assert ( 0 == strcmp (chars ,"xyXxyTest"));

  strcpy (chars, "xyXxyTest");
  g_strltrim (chars, "x");
  g_assert ( 0 == strcmp (chars ,"yXxyTest"));

  g_print ("ok\n");

---
Unix, WinNT and MS-DOS.  The Good, The Bad and The Ugly.
Kai Poitschke       MailTo:kai.poitschke[at]computer.org
Sülzburgstr. 17     Phone : +49 (0) 221 / 42 21 07
D-50937 Köln        Fax   : +49 (0) 221 / 9 41 61 44
----- End forwarded message -----

-- 
-josh



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