ecuuclc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*+-----------------------------------------------------------------------
  2. ecuuclc.c - uuper/lower-case string functions
  3. wht@wht.net
  4. Defined functions:
  5. minunique(str1, str2, minquan)
  6. ulcmpb(str1, str2)
  7. ulindex(str1, str2)
  8. ulrindex(str1, str2)
  9. ------------------------------------------------------------------------*/
  10. /*+:EDITS:*/
  11. /*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
  12. /*:01-24-1997-02:37-wht@yuriatin-SOURCE RELEASE 4.00 */
  13. /*:09-11-1996-20:00-wht@yuriatin-3.48-major telnet,curses,structural overhaul */
  14. /*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
  15. /*:11-14-1995-10:23-wht@kepler-3.37.80-source control point: SOCKETS */
  16. /*:05-04-1994-04:39-wht@n4hgf-ECU release 3.30 */
  17. /*:09-10-1992-13:59-wht@n4hgf-ECU release 3.20 */
  18. /*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA */
  19. /*:07-25-1991-12:57-wht@n4hgf-ECU release 3.10 */
  20. /*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */
  21. char to_upper();
  22. char to_lower();
  23. /*+----------------------------------------------------------------------------
  24. ulcmpb(str1,str) -- Upper/Lower [case insensitive] Compare Bytes
  25. Returns -1 if strings are equal, else failing character position
  26. If the second strings terminates with a null and both strings have matched
  27. character for character until that point, then -1 is returned.
  28. NOTE: this is not a test for complete equality of two strings, but allows
  29. discovery of a string as a substring in a larger containing string.
  30. -----------------------------------------------------------------------------*/
  31. int
  32. ulcmpb(str1, str2)
  33. unsigned char *str1;
  34. unsigned char *str2;
  35. {
  36. int istr;
  37. for (istr = 0;; ++istr)
  38. {
  39. if (str2[istr] == '\0') /* if second string exhausts, match! */
  40. return (-1);
  41. if ((str1[istr] == '\0') ||
  42. (to_upper(str1[istr]) != to_upper(str2[istr])))
  43. return (istr);
  44. }
  45. /* NOTREACHED */
  46. } /* end of ulcmpb */
  47. /*+-------------------------------------------------------------------------
  48. ulindex: Upper/Lower [case insensitive] Index function
  49. Returns position of 'str2' in 'str1' if found
  50. If 'str2' is null, then 0 is returned (null matches anything)
  51. Returns -1 if not found
  52. uses 'ulcmpb'
  53. --------------------------------------------------------------------------*/
  54. int
  55. ulindex(str1, str2)
  56. char *str1; /* the (target) string to search */
  57. char *str2; /* the (comparand) string to search for */
  58. {
  59. int istr1 = 0; /* moving index into str1 */
  60. char *mstr = str1; /* moving string pointer */
  61. if (str2[0] == '\0') /* null string matches anything */
  62. return (0);
  63. if (strlen(str2) > strlen(str1))
  64. return (-1);
  65. while (1)
  66. {
  67. if (*mstr == '\0') /* if we exhaust target string, flunk */
  68. return (-1);
  69. /* Can we find either case of first comparand char in target? */
  70. if (to_upper(*mstr) == to_upper(str2[0]))
  71. {
  72. /* we have a first char match... does rest of string match? */
  73. if (ulcmpb(mstr, str2) == -1) /* if the rest matches, ... */
  74. break;
  75. }
  76. /*
  77. * we did not match this time... increment istr1, mstr and try
  78. * again
  79. */
  80. ++istr1;
  81. ++mstr;
  82. }
  83. return (istr1); /* return match position */
  84. } /* end of ulindex */
  85. /*+-------------------------------------------------------------------------
  86. ulrindex: Upper/Lower [case insensitive] Right Index function
  87. Returns position of 'str2' in 'str1' if found
  88. Returns -1 if not found
  89. If 'str2' is null, then -1 is returned
  90. uses 'ulcmpb'
  91. --------------------------------------------------------------------------*/
  92. int
  93. ulrindex(str1, str2)
  94. char *str1; /* the (target) string to search */
  95. char *str2; /* the (comparand) string to search for */
  96. {
  97. char *mstr;
  98. int istr1;
  99. if (!str2[0]) /* null string matches anything */
  100. return (-1);
  101. if (strlen(str2) > strlen(str1))
  102. return (-1);
  103. mstr = str1 + strlen(str1) - strlen(str2); /* moving string pointer */
  104. istr1 = mstr - str1; /* moving index into str1 */
  105. while (mstr >= str1)
  106. {
  107. /* Can we find either case of first comparand char in target? */
  108. if (to_upper(*mstr) == to_upper(str2[0]))
  109. {
  110. /* we have a first char match... does rest of string match? */
  111. if (ulcmpb(mstr, str2) == -1) /* if the rest matches, ... */
  112. return (istr1); /* ... return match position */
  113. }
  114. /*
  115. * we did not match this time... increment istr1, mstr and try
  116. * again
  117. */
  118. --istr1;
  119. --mstr;
  120. }
  121. return (-1);
  122. } /* end of ulrindex */
  123. /*+----------------------------------------------------------------
  124. minunique(str1,str2,minquan)
  125. Returns 1 if at least 'minquan' chars of str2 match
  126. str1 and there are no chars after the minimum unique
  127. chars which do not match str1. Returns 0 on failure.
  128. -----------------------------------------------------------------*/
  129. int
  130. minunique(str1, str2, minquan)
  131. char *str1;
  132. char *str2;
  133. int minquan;
  134. {
  135. int index;
  136. if (strlen(str2) < minquan)
  137. return (0);
  138. index = ulcmpb(str1, str2);
  139. if (index < 0)
  140. return (1);
  141. if (index < minquan)
  142. return (0);
  143. if (index < strlen(str2))
  144. return (0);
  145. return (1);
  146. } /* end of minunique */
  147. /* vi: set tabstop=4 shiftwidth=4: */