Add gpr_is_true

pull/12294/head
Yuchen Zeng 8 years ago
parent ba23e799dc
commit 6514a0df72
  1. 10
      src/core/lib/iomgr/iomgr.c
  2. 13
      src/core/lib/support/string.c
  3. 3
      src/core/lib/support/string.h
  4. 16
      test/core/support/string_test.c

@ -164,13 +164,7 @@ void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
bool grpc_iomgr_abort_on_leaks(void) {
char *env = gpr_getenv("GRPC_ABORT_ON_LEAKS");
if (env == NULL) return false;
static const char *truthy[] = {"yes", "Yes", "YES", "true",
"True", "TRUE", "1"};
bool should_we = false;
for (size_t i = 0; i < GPR_ARRAY_SIZE(truthy); i++) {
if (0 == strcmp(env, truthy[i])) should_we = true;
}
gpr_free(env);
bool should_we = gpr_is_true(env);
grp_free(env);
return should_we;
}

@ -298,3 +298,16 @@ void *gpr_memrchr(const void *s, int c, size_t n) {
}
return NULL;
}
bool gpr_is_true(const char *s) {
if (s == NULL) {
return false;
}
static const char *truthy[] = {"yes", "true", "1"};
for (size_t i = 0; i < GPR_ARRAY_SIZE(truthy); i++) {
if (0 == gpr_stricmp(s, truthy[i])) {
return true;
}
}
return false;
}

@ -19,6 +19,7 @@
#ifndef GRPC_CORE_LIB_SUPPORT_STRING_H
#define GRPC_CORE_LIB_SUPPORT_STRING_H
#include <stdbool.h>
#include <stddef.h>
#include <grpc/support/port_platform.h>
@ -106,6 +107,8 @@ int gpr_stricmp(const char *a, const char *b);
void *gpr_memrchr(const void *s, int c, size_t n);
/** Return true if lower(s) equals "true", "yes" or "1", otherwise false. */
bool gpr_is_true(const char *s);
#ifdef __cplusplus
}
#endif

@ -279,6 +279,21 @@ static void test_memrchr(void) {
GPR_ASSERT(0 == strcmp((const char *)gpr_memrchr("hello", 'l', 5), "lo"));
}
static void test_is_true(void) {
LOG_TEST_NAME("test_is_true");
GPR_ASSERT(true == gpr_is_true("True"));
GPR_ASSERT(true == gpr_is_true("true"));
GPR_ASSERT(true == gpr_is_true("TRUE"));
GPR_ASSERT(true == gpr_is_true("Yes"));
GPR_ASSERT(true == gpr_is_true("yes"));
GPR_ASSERT(true == gpr_is_true("YES"));
GPR_ASSERT(true == gpr_is_true("1"));
GPR_ASSERT(false == gpr_is_true(NULL));
GPR_ASSERT(false == gpr_is_true(""));
GPR_ASSERT(false == gpr_is_true("0"));
}
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
test_strdup();
@ -292,5 +307,6 @@ int main(int argc, char **argv) {
test_leftpad();
test_stricmp();
test_memrchr();
test_is_true();
return 0;
}

Loading…
Cancel
Save