Skip to content

Instantly share code, notes, and snippets.

@ngbrown
Created July 3, 2012 17:52
Show Gist options
  • Save ngbrown/3041364 to your computer and use it in GitHub Desktop.
Save ngbrown/3041364 to your computer and use it in GitHub Desktop.
string starts with
#ifndef STRSTARTSWITH_H_
#define STRSTARTSWITH_H_
// From http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx
#ifndef __GNUC__
template <typename T, size_t N>
char ( &_ArraySizeHelper( T (&array)[N] ))[N];
#define countof(array) (sizeof(_ArraySizeHelper(array)))
#else
template <class T>
class _ArraySizeHelper {};
template <class T, size_t N>
class _ArraySizeHelper<T[N]> {
public:
static const size_t value = N;
};
#define countof(x) (_ArraySizeHelper<typeof(x)>::value)
#endif // __GNUC__
#define strstartswith(str1, str2) strncmp(str1, str2, countof(str2) - 1)
#endif // STRSTARTSWITH_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment