1 /*============================================================================
2   KWSys - Kitware System Library
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4 
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7 
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #ifdef KWSYS_STRING_C
13 /*
14 All code in this source file is conditionally compiled to work-around
15 template definition auto-search on VMS.  Other source files in this
16 directory that use the stl string cause the compiler to load this
17 source to try to get the definition of the string template.  This
18 condition blocks the compiler from seeing the symbols defined here.
19 */
20 #include "kwsysPrivate.h"
21 #include KWSYS_HEADER(String.h)
22 
23 /* Work-around CMake dependency scanning limitation.  This must
24    duplicate the above list of headers.  */
25 #if 0
26 # include "String.h.in"
27 #endif
28 
29 /* Select an implementation for strcasecmp.  */
30 #if defined(_MSC_VER)
31 # define KWSYS_STRING_USE_STRICMP
32 # include <string.h>
33 #elif defined(__GNUC__)
34 # define KWSYS_STRING_USE_STRCASECMP
35 # include <strings.h>
36 #else
37 /* Table to convert upper case letters to lower case and leave all
38    other characters alone.  */
39 static char kwsysString_strcasecmp_tolower[] =
40 {
41   '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
42   '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
43   '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
44   '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
45   '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
46   '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
47   '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
48   '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
49   '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
50   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
51   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
52   '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
53   '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
54   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
55   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
56   '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
57   '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
58   '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
59   '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
60   '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
61   '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
62   '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
63   '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
64   '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
65   '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
66   '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
67   '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
68   '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
69   '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
70   '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
71   '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
72   '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377'
73 };
74 #endif
75 
76 /*--------------------------------------------------------------------------*/
kwsysString_strcasecmp(const char * lhs,const char * rhs)77 int kwsysString_strcasecmp(const char* lhs, const char* rhs)
78 {
79 #if defined(KWSYS_STRING_USE_STRICMP)
80   return _stricmp(lhs, rhs);
81 #elif defined(KWSYS_STRING_USE_STRCASECMP)
82   return strcasecmp(lhs, rhs);
83 #else
84   const char* const lower = kwsysString_strcasecmp_tolower;
85   unsigned char const* us1 = (unsigned char const*)lhs;
86   unsigned char const* us2 = (unsigned char const*)rhs;
87   int result;
88   while((result = lower[*us1] - lower[*us2++], result == 0) && *us1++)
89     {
90     }
91   return result;
92 #endif
93 }
94 
95 /*--------------------------------------------------------------------------*/
kwsysString_strncasecmp(const char * lhs,const char * rhs,size_t n)96 int kwsysString_strncasecmp(const char* lhs, const char* rhs, size_t n)
97 {
98 #if defined(KWSYS_STRING_USE_STRICMP)
99   return _strnicmp(lhs, rhs, n);
100 #elif defined(KWSYS_STRING_USE_STRCASECMP)
101   return strncasecmp(lhs, rhs, n);
102 #else
103   const char* const lower = kwsysString_strcasecmp_tolower;
104   unsigned char const* us1 = (unsigned char const*)lhs;
105   unsigned char const* us2 = (unsigned char const*)rhs;
106   int result = 0;
107   while(n && (result = lower[*us1] - lower[*us2++], result == 0) && *us1++)
108     {
109     --n;
110     }
111   return result;
112 #endif
113 }
114 
115 #endif /* KWSYS_STRING_C */
116