Skip to content

Instantly share code, notes, and snippets.

@kaidokert
Last active January 4, 2020 08:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kaidokert/dfc08be8e75a3fc650d3daf8e89c3fe9 to your computer and use it in GitHub Desktop.
C++11 recursive constexpr static string length and compare
constexpr bool is_str_end(const char *t) {
return !(t) || !(*t);
}
constexpr int recursive_cstr_len(const char * t) {
return is_str_end(t) ?
0 :
1 + recursive_cstr_len(t + 1);
}
constexpr int str_equal = 0;
constexpr int str_greater = 1;
constexpr int str_lesser = -1;
constexpr int recursive_cstr_cmp(const char *a, const char *b) {
return is_str_end(a) ?
( is_str_end(b) ? str_equal : str_lesser) :
(
(*a != *b) ?
( *a > *b ? str_greater : str_lesser) :
recursive_cstr_cmp(a+1,b+1)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment