[GRASS-SVN] r47419 - in grass/trunk: include lib/gis
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Aug 4 04:40:51 EDT 2011
Author: martinl
Date: 2011-08-04 01:40:51 -0700 (Thu, 04 Aug 2011)
New Revision: 47419
Modified:
grass/trunk/include/gisdefs.h
grass/trunk/lib/gis/gislib.dox
grass/trunk/lib/gis/strings.c
Log:
gislib: add G_strncasecmp()
Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h 2011-08-04 08:38:11 UTC (rev 47418)
+++ grass/trunk/include/gisdefs.h 2011-08-04 08:40:51 UTC (rev 47419)
@@ -576,6 +576,7 @@
/* strings.c */
int G_strcasecmp(const char *, const char *);
+int G_strncasecmp(const char *, const char *, int);
char *G_store(const char *);
char *G_strchg(char *, char, char);
char *G_str_replace(const char *, const char *, const char *);
Modified: grass/trunk/lib/gis/gislib.dox
===================================================================
--- grass/trunk/lib/gis/gislib.dox 2011-08-04 08:38:11 UTC (rev 47418)
+++ grass/trunk/lib/gis/gislib.dox 2011-08-04 08:40:51 UTC (rev 47419)
@@ -1974,6 +1974,8 @@
- G_strcasecmp()
+ - G_strncasecmp()
+
String compare ignoring case (upper or lower).
- G_strstr()
Modified: grass/trunk/lib/gis/strings.c
===================================================================
--- grass/trunk/lib/gis/strings.c 2011-08-04 08:38:11 UTC (rev 47418)
+++ grass/trunk/lib/gis/strings.c 2011-08-04 08:40:51 UTC (rev 47419)
@@ -26,6 +26,8 @@
#define NULL 0
#endif
+static int _strncasecmp(const char *, const char *, int);
+
/*!
\brief String compare ignoring case (upper or lower)
@@ -43,29 +45,29 @@
*/
int G_strcasecmp(const char *x, const char *y)
{
- int xx, yy;
+ return _strncasecmp(x, y, -1);
+}
- if (!x)
- return y ? -1 : 0;
- if (!y)
- return x ? 1 : 0;
- while (*x && *y) {
- xx = *x++;
- yy = *y++;
- if (xx >= 'A' && xx <= 'Z')
- xx = xx + 'a' - 'A';
- if (yy >= 'A' && yy <= 'Z')
- yy = yy + 'a' - 'A';
- if (xx < yy)
- return -1;
- if (xx > yy)
- return 1;
- }
- if (*x)
- return 1;
- if (*y)
- return -1;
- return 0;
+/*!
+ \brief String compare ignoring case (upper or lower) - limited
+ number of characters
+
+ Returning a value that has the same sign as the difference between
+ the first differing pair of characters.
+
+ Note: strcasecmp() is affected by the locale (LC_CTYPE), while
+ G_strcasecmp() isn't.
+
+ \param x first string to compare
+ \param y second string to compare
+ \param n number or characters to compare
+
+ \return 0 the two strings are equal
+ \return -1, 1
+*/
+int G_strncasecmp(const char *x, const char *y, int n)
+{
+ return _strncasecmp(x, y, n);
}
/*!
@@ -364,3 +366,38 @@
if (*(line + l) == '\n')
*(line + l) = '\0';
}
+
+int _strncasecmp(const char *x, const char *y, int n)
+{
+ int xx, yy, i;
+
+ if (!x)
+ return y ? -1 : 0;
+ if (!y)
+ return x ? 1 : 0;
+
+ i = 1;
+ while (*x && *y) {
+ xx = *x++;
+ yy = *y++;
+ if (xx >= 'A' && xx <= 'Z')
+ xx = xx + 'a' - 'A';
+ if (yy >= 'A' && yy <= 'Z')
+ yy = yy + 'a' - 'A';
+ if (xx < yy)
+ return -1;
+ if (xx > yy)
+ return 1;
+
+ if (n > -1 && i >= n)
+ return 0;
+
+ i++;
+ }
+
+ if (*x)
+ return 1;
+ if (*y)
+ return -1;
+ return 0;
+}
More information about the grass-commit
mailing list