1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* stringutils.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* simple string manipulation routines
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-09-01 06:40:42 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.15 1998/09/01 04:33:54 momjian 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-08-25 21:41:52 +02:00
|
|
|
#include "postgres.h"
|
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
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
#ifdef NOT_USED
|
1997-09-08 04:41:22 +02:00
|
|
|
static char *
|
1997-09-07 07:04:48 +02:00
|
|
|
leftTrim(char *s)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *s2 = s;
|
|
|
|
int shift = 0;
|
|
|
|
int j = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
while (isspace(*s))
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
shift++;
|
|
|
|
}
|
|
|
|
if (shift > 0)
|
|
|
|
{
|
|
|
|
while ((s2[j] = s2[j + shift]) != '\0')
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s2;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
char *
|
1997-09-07 07:04:48 +02:00
|
|
|
rightTrim(char *s)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
char *sEnd,
|
|
|
|
*bsEnd;
|
1998-05-13 05:27:07 +02:00
|
|
|
bool in_bs = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
sEnd = s + strlen(s) - 1;
|
|
|
|
while (sEnd >= s && isspace(*sEnd))
|
|
|
|
sEnd--;
|
1998-05-13 05:27:07 +02:00
|
|
|
bsEnd = sEnd;
|
|
|
|
while (bsEnd >= s && *bsEnd == '\\')
|
|
|
|
{
|
|
|
|
in_bs = (in_bs == false);
|
|
|
|
bsEnd--;
|
|
|
|
}
|
|
|
|
if (in_bs && *sEnd)
|
|
|
|
sEnd++;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (sEnd < s)
|
|
|
|
s[0] = '\0';
|
|
|
|
else
|
|
|
|
s[sEnd - s + 1] = '\0';
|
|
|
|
return s;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
#ifdef NOT_USED
|
1997-09-08 04:41:22 +02:00
|
|
|
static char *
|
1997-09-07 07:04:48 +02:00
|
|
|
doubleTrim(char *s)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
strcpy(s, leftTrim(rightTrim(s)));
|
|
|
|
return s;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#ifdef STRINGUTILS_TEST
|
1997-09-07 07:04:48 +02:00
|
|
|
void
|
|
|
|
testStringUtils()
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
static char *tests[] = {" goodbye \n", /* space on both ends */
|
1997-09-07 07:04:48 +02:00
|
|
|
"hello world", /* no spaces to trim */
|
|
|
|
"", /* empty string */
|
|
|
|
"a", /* string with one char */
|
|
|
|
" ", /* string with one whitespace */
|
|
|
|
NULL_STR};
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
int i = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
while (tests[i] != NULL_STR)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *t;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
t = strdup(tests[i]);
|
|
|
|
printf("leftTrim(%s) = ", t);
|
|
|
|
printf("%sEND\n", leftTrim(t));
|
|
|
|
t = strdup(tests[i]);
|
|
|
|
printf("rightTrim(%s) = ", t);
|
|
|
|
printf("%sEND\n", rightTrim(t));
|
|
|
|
t = strdup(tests[i]);
|
|
|
|
printf("doubleTrim(%s) = ", t);
|
|
|
|
printf("%sEND\n", doubleTrim(t));
|
|
|
|
i++;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|