home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 10 / string / striindx.cpp < prev    next >
C/C++ Source or Header  |  1991-07-30  |  1KB  |  48 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <string.hpp>
  5.  
  6. int String::index(const String &a, int pos) const
  7. {
  8.     if (a.length() > length()) return -1;
  9.     int al = a.length(), lim = length() - al--;
  10.     if (pos < 0 || pos > lim)
  11.         return -1;
  12.     if (!a.rp) return pos;
  13.     const char *p = body()+pos, *q = a.body(), *r = q+1;
  14.     for (; pos <= lim; ++pos) {
  15.         if (*p++ != *q) continue;
  16.         if (!strncmp(p,r,al)) return pos;
  17.     }
  18.     return -1;
  19. }
  20.  
  21. int String::index(const char *s, int pos) const
  22. {
  23.     int sl = strlen(s);
  24.     if (sl > length()) return -1;
  25.     if (!*s) return pos;
  26.     int lim = length() - sl--;
  27.     if (pos < 0 || pos > lim)
  28.         return -1;
  29.     const char *p = body()+pos, *r = s+1;
  30.     for (; pos <= lim; ++pos) {
  31.         if (*p++ != *s) continue;
  32.         if (!strncmp(p,r,sl)) return pos;
  33.     }
  34.     return -1;
  35. }
  36.  
  37. int String::index(char c, int pos) const
  38. {
  39.     if (!rp) return -1;
  40.     int lim = length() - 1;
  41.     if (pos < 0 || pos > lim)
  42.         return -1;
  43.     const char *p = body()+pos;
  44.     for (; pos <= lim; ++pos)
  45.         if (*p++ == c) return pos;
  46.     return -1;
  47. }
  48.