1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* stringutils.c--
|
|
|
|
* simple string manipulation routines
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1997-02-13 09:32:20 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.8 1997/02/13 08:31:57 scrappy Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
1996-11-26 04:20:35 +01:00
|
|
|
|
1997-02-13 09:32:20 +01:00
|
|
|
#ifndef HAVE_STRDUP
|
1996-11-26 08:39:11 +01:00
|
|
|
#include "strdup.h"
|
|
|
|
#endif
|
1996-11-26 04:20:35 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "stringutils.h"
|
|
|
|
|
|
|
|
/* all routines assume null-terminated strings! */
|
|
|
|
|
1996-11-14 17:08:05 +01:00
|
|
|
/* The following routines remove whitespaces from the left, right
|
|
|
|
and both sides of a string */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* MODIFIES the string passed in and returns the head of it */
|
1996-11-14 17:08:05 +01:00
|
|
|
|
|
|
|
|
1996-07-28 09:08:15 +02:00
|
|
|
char *leftTrim(char *s)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1996-07-28 09:08:15 +02:00
|
|
|
char *s2 = s;
|
1996-07-09 08:22:35 +02:00
|
|
|
int shift=0;
|
|
|
|
int j=0;
|
|
|
|
|
|
|
|
while (isspace(*s))
|
|
|
|
{ s++; shift++;}
|
|
|
|
if (shift > 0)
|
|
|
|
{
|
|
|
|
while ( (s2[j] = s2[j+shift]) !='\0')
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s2;
|
|
|
|
}
|
|
|
|
|
1996-07-28 09:08:15 +02:00
|
|
|
char *rightTrim(char *s)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1996-07-28 09:08:15 +02:00
|
|
|
char *sEnd;
|
1996-07-09 08:22:35 +02:00
|
|
|
sEnd = s+strlen(s)-1;
|
1996-08-06 22:23:14 +02:00
|
|
|
while (sEnd >= s && isspace(*sEnd))
|
1996-07-09 08:22:35 +02:00
|
|
|
sEnd--;
|
|
|
|
if (sEnd < s)
|
|
|
|
s[0]='\0';
|
|
|
|
else
|
|
|
|
s[sEnd-s+1]='\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
1996-07-28 09:08:15 +02:00
|
|
|
char *doubleTrim(char *s)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
strcpy(s,leftTrim(rightTrim(s)));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STRINGUTILS_TEST
|
|
|
|
void testStringUtils()
|
|
|
|
{
|
1996-07-28 09:08:15 +02:00
|
|
|
static char *tests[] = {" goodbye \n", /* space on both ends */
|
1996-07-09 08:22:35 +02:00
|
|
|
"hello world", /* no spaces to trim */
|
|
|
|
"", /* empty string */
|
|
|
|
"a", /* string with one char*/
|
|
|
|
" ", /* string with one whitespace*/
|
|
|
|
NULL_STR};
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
while (tests[i]!=NULL_STR)
|
|
|
|
{
|
1996-07-28 09:08:15 +02:00
|
|
|
char *t;
|
1996-09-16 08:06:17 +02:00
|
|
|
t = strdup(tests[i]);
|
1996-07-09 08:22:35 +02:00
|
|
|
printf("leftTrim(%s) = ",t);
|
|
|
|
printf("%sEND\n", leftTrim(t));
|
1996-09-16 08:06:17 +02:00
|
|
|
t = strdup(tests[i]);
|
1996-07-09 08:22:35 +02:00
|
|
|
printf("rightTrim(%s) = ",t);
|
|
|
|
printf("%sEND\n", rightTrim(t));
|
1996-09-16 08:06:17 +02:00
|
|
|
t = strdup(tests[i]);
|
1996-07-09 08:22:35 +02:00
|
|
|
printf("doubleTrim(%s) = ",t);
|
|
|
|
printf("%sEND\n", doubleTrim(t));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|