sofia-sip/string0.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2005 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef STRING0_H
00026 
00027 #define STRING0_H
00028 
00038 #ifndef SU_CONFIG_H
00039 #include <sofia-sip/su_config.h>
00040 #endif
00041 
00042 #include <string.h>
00043 
00044 SOFIA_BEGIN_DECLS
00045 
00046 #if SU_HAVE_INLINE
00047 su_inline
00048 #else
00049 SOFIAPUBFUN
00050 #endif
00051 int 
00052   str0cmp(char const *a, char const *b),
00053   str0ncmp(char const *a, char const *b, size_t n),
00054   str0casecmp(char const *a, char const *b),
00055   str0ncasecmp(char const *a, char const *b, size_t n);
00056 
00057 #if SU_HAVE_INLINE
00058 su_inline
00059 #else
00060 SOFIAPUBFUN
00061 #endif
00062 size_t
00063 strnspn(char const *s, size_t size, char const *term),
00064   strncspn(char const *s, size_t ssize, char const *reject);
00065 
00066 #if SU_HAVE_INLINE
00067 int str0cmp(char const *a, char const *b)
00068 {
00069   if (a == NULL) a = "";
00070   if (b == NULL) b = "";
00071   return strcmp(a, b);
00072 }
00073 
00074 int str0ncmp(char const *a, char const *b, size_t n)
00075 {
00076   if (a == NULL) a = "";
00077   if (b == NULL) b = "";
00078   return strncmp(a, b, n);
00079 }
00080 
00081 int str0casecmp(char const *a, char const *b)
00082 {
00083   if (a == NULL) a = "";
00084   if (b == NULL) b = "";
00085   return strcasecmp(a, b);
00086 }
00087 
00088 int str0ncasecmp(char const *a, char const *b, size_t n)
00089 {
00090   if (a == NULL) a = "";
00091   if (b == NULL) b = "";
00092   return strncasecmp(a, b, n);
00093 }
00094 
00095 size_t strnspn(char const *s, size_t ssize, char const *term)
00096 {
00097   size_t n;
00098   size_t tsize = strlen(term);
00099 
00100   if (tsize == 0) {
00101     return 0;
00102   }
00103   else if (tsize == 1) {
00104     char c, t = term[0];
00105     for (n = 0; n < ssize && (c = s[n]) && c == t; n++)
00106       ;
00107   }
00108   else if (tsize == 2) {
00109     char c, t1 = term[0], t2 = term[1];
00110     for (n = 0; n < ssize && (c = s[n]) && (c == t1 || c == t2); n++)
00111       ;
00112   }
00113   else {
00114     size_t i;
00115     char c, t1 = term[0], t2 = term[1];
00116     for (n = 0; n < ssize && (c = s[n]) && (c == t1 || c == t2); n++) {
00117       for (i = 2; i < tsize; i++)
00118         if (c == term[i])
00119           return n;
00120     }
00121   }
00122 
00123   return n;
00124 }
00125 
00126 size_t strncspn(char const *s, size_t ssize, char const *reject)
00127 {
00128   size_t n;
00129   size_t rsize = strlen(reject);
00130 
00131   if (rsize == 0) {
00132     for (n = 0; n < ssize && s[n]; n++)
00133       ;
00134   }
00135   else if (rsize == 1) {
00136     char c, rej = reject[0];
00137     for (n = 0; n < ssize && (c = s[n]) && c != rej; n++)
00138       ;
00139   }
00140   else if (rsize == 2) {
00141     char c, rej1 = reject[0], rej2 = reject[1];
00142     for (n = 0; n < ssize && (c = s[n]) && c != rej1 && c != rej2; n++)
00143       ;
00144   }
00145   else {
00146     size_t i;
00147     char c, rej1 = reject[0], rej2 = reject[1];
00148     for (n = 0; n < ssize && (c = s[n]) && c != rej1 && c != rej2; n++) {
00149       for (i = 2; i < rsize; i++)
00150         if (c == reject[i])
00151           return n;
00152     }
00153   }
00154 
00155   return n;
00156 }
00157 
00158 #endif
00159 
00160 SOFIA_END_DECLS
00161 
00162 #endif /* !STRING0_H */

Sofia-SIP 1.12.1 - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.