File: byte_starts.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 (24 lines) | stat: -rw-r--r-- 558 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
#include <byte.h>
#undef byte_starts
#include <string.h>

int byte_starts(const void* a,size_t alen,const char* s) {
  size_t i;
  for (i=0; i<alen; ++i) {
    if (s[i]==0) return 1;
    if (((const char*)a)[i] != s[i]) return 0;
  }
  return s[i]==0;
}

#ifdef UNITTEST
#include <assert.h>

int main() {
  static char buf[]="The quick brown fox jumps over the lazy dog";
  assert(byte_starts(buf,sizeof(buf)-1,"The "));
  assert(!byte_starts(buf,sizeof(buf)-1,"the "));
  assert(!byte_starts(buf,2,"The "));
  assert(byte_starts("The ",4,"The "));
}
#endif