diff options
Diffstat (limited to 'src/core/lib/slice')
-rw-r--r-- | src/core/lib/slice/slice.c | 46 | ||||
-rw-r--r-- | src/core/lib/slice/slice_intern.c | 2 | ||||
-rw-r--r-- | src/core/lib/slice/slice_internal.h | 2 | ||||
-rw-r--r-- | src/core/lib/slice/slice_string_helpers.c | 5 |
4 files changed, 51 insertions, 4 deletions
diff --git a/src/core/lib/slice/slice.c b/src/core/lib/slice/slice.c index f2c09d60e5..c377edd349 100644 --- a/src/core/lib/slice/slice.c +++ b/src/core/lib/slice/slice.c @@ -82,14 +82,18 @@ static const grpc_slice_refcount_vtable noop_refcount_vtable = { noop_ref, noop_unref, grpc_slice_default_hash_impl}; static grpc_slice_refcount noop_refcount = {&noop_refcount_vtable}; -grpc_slice grpc_slice_from_static_string(const char *s) { +grpc_slice grpc_slice_from_static_buffer(const void *s, size_t len) { grpc_slice slice; slice.refcount = &noop_refcount; slice.data.refcounted.bytes = (uint8_t *)s; - slice.data.refcounted.length = strlen(s); + slice.data.refcounted.length = len; return slice; } +grpc_slice grpc_slice_from_static_string(const char *s) { + return grpc_slice_from_static_buffer(s, strlen(s)); +} + /* grpc_slice_new support structures - we create a refcount object extended with the user provided data pointer & destroy function */ typedef struct new_slice_refcount { @@ -384,3 +388,41 @@ int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b) { return a.data.refcounted.length == b.data.refcounted.length && a.data.refcounted.bytes == b.data.refcounted.bytes; } + +int grpc_slice_buf_start_eq(grpc_slice a, const void *b, size_t len) { + if (GRPC_SLICE_LENGTH(a) < len) return 0; + return 0 == memcmp(GRPC_SLICE_START_PTR(a), b, len); +} + +int grpc_slice_rchr(grpc_slice s, char c) { + const char *b = (const char *)GRPC_SLICE_START_PTR(s); + int i; + for (i = (int)GRPC_SLICE_LENGTH(s) - 1; i != -1 && b[i] != c; i--) + ; + return i; +} + +int grpc_slice_chr(grpc_slice s, char c) { + const char *b = (const char *)GRPC_SLICE_START_PTR(s); + const char *p = memchr(b, c, GRPC_SLICE_LENGTH(s)); + return p == NULL ? -1 : (int)(p - b); +} + +int grpc_slice_slice(grpc_slice haystack, grpc_slice needle) { + size_t haystack_len = GRPC_SLICE_LENGTH(haystack); + const uint8_t *haystack_bytes = GRPC_SLICE_START_PTR(haystack); + size_t needle_len = GRPC_SLICE_LENGTH(needle); + const uint8_t *needle_bytes = GRPC_SLICE_START_PTR(needle); + + if (haystack_len == 0 || needle_len == 0) return -1; + if (haystack_len < needle_len) return -1; + if (needle_len == 1) return grpc_slice_chr(haystack, (char)*needle_bytes); + + const uint8_t *last = haystack_bytes + haystack_len - needle_len; + for (const uint8_t *cur = haystack_bytes; cur != last; ++cur) { + if (0 == memcmp(cur, needle_bytes, needle_len)) { + return (int)(cur - haystack_bytes); + } + } + return -1; +} diff --git a/src/core/lib/slice/slice_intern.c b/src/core/lib/slice/slice_intern.c index a7e17527c3..b5e00a38d7 100644 --- a/src/core/lib/slice/slice_intern.c +++ b/src/core/lib/slice/slice_intern.c @@ -206,7 +206,7 @@ grpc_slice grpc_slice_intern(grpc_slice slice) { return materialize(s); } -void grpc_test_only_set_slice_interning_hash_seed(uint32_t seed) { +void grpc_test_only_set_slice_hash_seed(uint32_t seed) { g_hash_seed = seed; g_forced_hash_seed = 1; } diff --git a/src/core/lib/slice/slice_internal.h b/src/core/lib/slice/slice_internal.h index 8bfe066fdb..bf9117c74e 100644 --- a/src/core/lib/slice/slice_internal.h +++ b/src/core/lib/slice/slice_internal.h @@ -48,6 +48,6 @@ void grpc_slice_buffer_destroy_internal(grpc_exec_ctx *exec_ctx, void grpc_slice_intern_init(void); void grpc_slice_intern_shutdown(void); -void grpc_test_only_set_slice_interning_hash_seed(uint32_t key); +void grpc_test_only_set_slice_hash_seed(uint32_t key); #endif /* GRPC_CORE_LIB_SLICE_SLICE_INTERNAL_H */ diff --git a/src/core/lib/slice/slice_string_helpers.c b/src/core/lib/slice/slice_string_helpers.c index 839c366b32..99695007cc 100644 --- a/src/core/lib/slice/slice_string_helpers.c +++ b/src/core/lib/slice/slice_string_helpers.c @@ -88,3 +88,8 @@ void grpc_slice_split(grpc_slice str, const char *sep, grpc_slice_buffer *dst) { grpc_slice_buffer_add_indexed(dst, grpc_slice_ref_internal(str)); } } + +bool grpc_parse_slice_to_uint32(grpc_slice str, uint32_t *result) { + return gpr_parse_bytes_to_uint32((const char *)GRPC_SLICE_START_PTR(str), + GRPC_SLICE_LENGTH(str), result) != 0; +} |