String.c 4.7 KB

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