2
0

stripcaseeq.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <ctype.h>
  2. #include "bool.h"
  3. #include "girstring.h"
  4. bool
  5. stripcaseeq(const char * const comparand,
  6. const char * const comparator) {
  7. /*----------------------------------------------------------------------------
  8. Compare two strings, ignoring leading and trailing blanks and case.
  9. Return true if the strings are identical, false otherwise.
  10. -----------------------------------------------------------------------------*/
  11. const char *p, *q, *px, *qx;
  12. bool equal;
  13. /* Make p and q point to the first non-blank character in each string.
  14. If there are no non-blank characters, make them point to the terminating
  15. NULL.
  16. */
  17. p = &comparand[0];
  18. while (*p == ' ')
  19. ++p;
  20. q = &comparator[0];
  21. while (*q == ' ')
  22. ++q;
  23. /* Make px and qx point to the last non-blank character in each string.
  24. If there are no nonblank characters (which implies the string is
  25. null), make them point to the terminating NULL.
  26. */
  27. if (*p == '\0')
  28. px = p;
  29. else {
  30. px = p + strlen(p) - 1;
  31. while (*px == ' ')
  32. --px;
  33. }
  34. if (*q == '\0')
  35. qx = q;
  36. else {
  37. qx = q + strlen(q) - 1;
  38. while (*qx == ' ')
  39. --qx;
  40. }
  41. equal = true; /* initial assumption */
  42. /* If the stripped strings aren't the same length,
  43. we know they aren't equal
  44. */
  45. if (px - p != qx - q)
  46. equal = false;
  47. while (p <= px) {
  48. if (toupper(*p) != toupper(*q))
  49. equal = false;
  50. ++p; ++q;
  51. }
  52. return equal;
  53. }