File: str_start.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (42 lines) | stat: -rw-r--r-- 1,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "str.h"

/* str_start returns 1 if the b is a prefix of a, 0 otherwise */
int str_start(const char* a, const char* b) {
#if 1
  size_t i;
  for (i=0; ; ++i) {
    if (!b[i]) return 1;
    if (a[i]!=b[i]) break;
  }
  return 0;
#else
  register const char* s=a;
  register const char* t=b;
  for (;;) {
    if (!*t) return 1;
                       if (*s!=*t) break;
                                          ++s; ++t;
    if (!*t) return 1;
                       if (*s!=*t) break;
                                          ++s; ++t;
    if (!*t) return 1;
                       if (*s!=*t) break;
                                          ++s; ++t;
    if (!*t) return 1;
                       if (*s!=*t) break;
                                          ++s; ++t;
  }
  return 0;
#endif
}

#ifdef UNITTEST
#include <assert.h>
int main() {
  assert(str_start("fnord","no")==0);
  assert(str_start("fnord","fno")==1);
  assert(str_start("fnord","fnord")==1);
  assert(str_start("fnord","fnord1")==0);
  return 0;
}
#endif