string0.h 3.43 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25 26
#ifndef STRING0_H
/** Defined when <sofia-sip/string0.h> is included. */
Pekka Pessi's avatar
Pekka Pessi committed
27
#define STRING0_H
Pekka Pessi's avatar
Pekka Pessi committed
28

Pekka Pessi's avatar
Pekka Pessi committed
29
/**@file sofia-sip/string0.h
Pekka Pessi's avatar
Pekka Pessi committed
30
 *
31
 * @brief Extra string function.
Pekka Pessi's avatar
Pekka Pessi committed
32
 *
33 34 35
 * String comparison functions accepting NULL pointers: str0cmp(),
 * str0ncmp(), str0casecmp(), str0ncasecmp(). Also includes span functions
 * testing at most @a n bytes: strncspn(), strnspn().
Pekka Pessi's avatar
Pekka Pessi committed
36 37
 */

38
#ifndef SU_CONFIG_H
39
#include <sofia-sip/su_config.h>
40
#endif
Pekka Pessi's avatar
Pekka Pessi committed
41 42 43

#include <string.h>

44 45
SOFIA_BEGIN_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
46
su_inline int str0cmp(char const *a, char const *b)
Pekka Pessi's avatar
Pekka Pessi committed
47 48 49 50 51 52
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strcmp(a, b);
}

Pekka Pessi's avatar
Pekka Pessi committed
53
su_inline int str0ncmp(char const *a, char const *b, size_t n)
Pekka Pessi's avatar
Pekka Pessi committed
54 55 56 57 58 59
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strncmp(a, b, n);
}

Pekka Pessi's avatar
Pekka Pessi committed
60
su_inline int str0casecmp(char const *a, char const *b)
Pekka Pessi's avatar
Pekka Pessi committed
61 62 63 64 65 66
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strcasecmp(a, b);
}

Pekka Pessi's avatar
Pekka Pessi committed
67
su_inline int str0ncasecmp(char const *a, char const *b, size_t n)
Pekka Pessi's avatar
Pekka Pessi committed
68 69 70 71 72 73
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strncasecmp(a, b, n);
}

Pekka Pessi's avatar
Pekka Pessi committed
74 75 76 77 78
#if !SU_HAVE_INLINE
SOFIAPUBFUN size_t strnspn(char const *s, size_t size, char const *term);
SOFIAPUBFUN size_t strncspn(char const *s, size_t ssize, char const *reject);
#else
su_inline size_t strnspn(char const *s, size_t ssize, char const *term)
Pekka Pessi's avatar
Pekka Pessi committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
  size_t n;
  size_t tsize = strlen(term);

  if (tsize == 0) {
    return 0;
  }
  else if (tsize == 1) {
    char c, t = term[0];
    for (n = 0; n < ssize && (c = s[n]) && c == t; n++)
      ;
  }
  else if (tsize == 2) {
    char c, t1 = term[0], t2 = term[1];
    for (n = 0; n < ssize && (c = s[n]) && (c == t1 || c == t2); n++)
      ;
  }
  else {
    size_t i;
    char c, t1 = term[0], t2 = term[1];
    for (n = 0; n < ssize && (c = s[n]) && (c == t1 || c == t2); n++) {
      for (i = 2; i < tsize; i++)
	if (c == term[i])
	  return n;
    }
  }

  return n;
}

Pekka Pessi's avatar
Pekka Pessi committed
109
su_inline size_t strncspn(char const *s, size_t ssize, char const *reject)
Pekka Pessi's avatar
Pekka Pessi committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
{
  size_t n;
  size_t rsize = strlen(reject);

  if (rsize == 0) {
    for (n = 0; n < ssize && s[n]; n++)
      ;
  }
  else if (rsize == 1) {
    char c, rej = reject[0];
    for (n = 0; n < ssize && (c = s[n]) && c != rej; n++)
      ;
  }
  else if (rsize == 2) {
    char c, rej1 = reject[0], rej2 = reject[1];
    for (n = 0; n < ssize && (c = s[n]) && c != rej1 && c != rej2; n++)
      ;
  }
  else {
    size_t i;
    char c, rej1 = reject[0], rej2 = reject[1];
    for (n = 0; n < ssize && (c = s[n]) && c != rej1 && c != rej2; n++) {
      for (i = 2; i < rsize; i++)
	if (c == reject[i])
	  return n;
    }
  }

  return n;
}

#endif

143 144
SOFIA_END_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
145
#endif /* !STRING0_H */