aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/stringutils.c
blob: cae0e7405befee4f7dff51072581dd8a6f4477e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*-------------------------------------------------------------------------
 *
 * stringutils.c--
 *    simple string manipulation routines
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.10 1997/08/25 19:41:52 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include "postgres.h"
#ifndef HAVE_STRDUP
#include "strdup.h"
#endif

#include "stringutils.h"

/* all routines assume null-terminated strings! */

/* The following routines remove whitespaces from the left, right
   and both sides of a string */
/* MODIFIES the string passed in and returns the head of it */

#ifdef NOT_USED
static char *leftTrim(char *s)  
{
  char *s2 = s;
  int shift=0;
  int j=0;

  while (isspace(*s))
    { s++; shift++;}
  if (shift > 0)
    {
      while ( (s2[j] = s2[j+shift]) !='\0')
	j++;
    }

  return s2;
}
#endif

char *rightTrim(char *s)
{
  char *sEnd;
  sEnd = s+strlen(s)-1;
  while (sEnd >= s && isspace(*sEnd))
    sEnd--;
  if (sEnd < s)
    s[0]='\0';
  else
    s[sEnd-s+1]='\0';
  return s;
}

#ifdef NOT_USED
static char *doubleTrim(char *s)
{
  strcpy(s,leftTrim(rightTrim(s)));
  return s;
}
#endif

#ifdef STRINGUTILS_TEST
void testStringUtils()
{
  static char *tests[] = {" goodbye  \n", /* space on both ends */
			  "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)
    {
      char *t;
      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++;
    }

}

#endif