From 235f1e1a967cb070c7246617461f58f0413394b3 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 17 Dec 2009 18:29:37 -0500 Subject: Refactor out the 'find string at start of any line' logic. We do this in too many places throughout the code; it's time to start clamping down. Also, refactor Karsten's patch to use strchr-then-strndup, rather than malloc-then-strlcpy-then-strchr-then-clear. --- src/common/util.c | 23 +++++++++++++++++++++++ src/common/util.h | 2 ++ 2 files changed, 25 insertions(+) (limited to 'src/common') diff --git a/src/common/util.c b/src/common/util.c index e70a9ea5f3..6177a3e736 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -682,6 +682,29 @@ find_whitespace_eos(const char *s, const char *eos) return s; } +/** Return the the first occurrence of needle in haystack that + * occurs at the start of a line (that is, at the beginning of haystack + * or immediately after a newline). Return NULL if no such string is found. + */ +const char * +find_str_at_start_of_line(const char *haystack, const char *needle) +{ + size_t needle_len = strlen(needle); + + do { + if (!strncmp(haystack, needle, needle_len)) + return haystack; + + haystack = strchr(haystack, '\n'); + if (!haystack) + return NULL; + else + ++haystack; + } while (*haystack); + + return NULL; +} + /** Return true iff the 'len' bytes at 'mem' are all zero. */ int tor_mem_is_zero(const char *mem, size_t len) diff --git a/src/common/util.h b/src/common/util.h index 17cbb4a44f..b55bb91c51 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -195,6 +195,8 @@ const char *eat_whitespace_no_nl(const char *s) ATTR_PURE; const char *eat_whitespace_eos_no_nl(const char *s, const char *eos) ATTR_PURE; const char *find_whitespace(const char *s) ATTR_PURE; const char *find_whitespace_eos(const char *s, const char *eos) ATTR_PURE; +const char *find_str_at_start_of_line(const char *haystack, const char *needle) + ATTR_PURE; int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE; int tor_digest_is_zero(const char *digest) ATTR_PURE; int tor_digest256_is_zero(const char *digest) ATTR_PURE; -- cgit v1.2.3-54-g00ecf