void RunTestImpl(int line, const char *haystack, size_t haystack_len, const char *needle, size_t needle_len, const char *expected) { const char *result = ib_strstr_ex(haystack, haystack_len, needle, needle_len); const int blen = 256; char b1[blen]; char b2[blen]; char b3[blen]; EXPECT_STREQ(expected, result) << "Line " << line << ": " << Stringize("strstr", haystack, haystack_len, needle, needle_len, b1, blen) << " expected " << Stringize(expected, b3, blen) << " returned " << Stringize(result, b2, blen); }
int ib_bytestr_index_of_c( const ib_bytestr_t *haystack, const char *needle ) { IB_FTRACE_INIT(); const uint8_t* haystack_data = ib_bytestr_const_ptr(haystack); const char *found; /* Let ib_strstr_ex() do the heavy lifting */ found = ib_strstr_ex( (const char *)haystack_data, ib_bytestr_length(haystack), needle, strlen(needle)); /* Return the offset (or -1) */ if (found != NULL) { IB_FTRACE_RET_INT(found - (const char *)haystack_data); } else { IB_FTRACE_RET_INT(-1); } }
/* * Expand a string from the given hash-like object, ex version. See expand.h. */ ib_status_t ib_expand_str_gen_ex(ib_mpool_t *mp, const char *str, size_t str_len, const char *prefix, const char *suffix, bool nul, bool recurse, ib_expand_lookup_fn_t lookup_fn, const void *lookup_data, char **result, size_t *result_len) { IB_FTRACE_INIT(); ib_status_t rc; size_t pre_len; /* Prefix string length */ size_t suf_len = SIZE_MAX; /* Suffix string length */ const char *buf = str; /* Current buffer */ size_t buflen = str_len; /* Length of the buffer */ /* Sanity checks */ assert(mp != NULL); assert(str != NULL); assert(prefix != NULL); assert(suffix != NULL); assert(lookup_fn != NULL); assert(result != NULL); assert(result_len != NULL); /* Initialize the result to NULL */ *result = NULL; *result_len = 0; /* Validate prefix and suffix */ if ( (*prefix == '\0') || (*suffix == '\0') ) { IB_FTRACE_RET_STATUS(IB_EINVAL); } /* Compute prefix length */ pre_len = strlen(prefix); assert(pre_len != 0); /* Check for minimum string length */ if (str_len < (pre_len+1) ) { *result = (char *)ib_mpool_memdup(mp, str, str_len); *result_len = str_len; IB_FTRACE_RET_STATUS(IB_OK); } /* Loop until there is nothing more to find. */ while (1) { const char *pre = NULL; /* Pointer to found prefix string */ size_t pre_off = 0;/* Offset of prefix in the string */ const char *suf = NULL; /* Pointer to found suffix string */ const char *name; /* Pointer to name between pre and suffix */ size_t namelen; /* Length of the name */ char *new; /* New buffer */ size_t newlen; /* Length of new buffer */ const char *iptr; /* Initial block (up to the prefix) */ size_t ilen; /* Length of the initial block */ const char *fptr; /* Final block (after the suffix) */ size_t flen; /* Length of the final block */ ib_field_t *f; /* Field */ size_t slen; /* Length of the buffer to search */ /* Look for the last prefix in the string with a matching suffix */ slen = buflen; while ( (pre == NULL) && (slen >= pre_len) ) { if (recurse) { pre = ib_strrstr_ex(buf, slen, prefix, pre_len); } else { pre = ib_strstr_ex(buf, slen, prefix, pre_len); } if (pre == NULL) { break; } /* Lazy compute suffix length */ if (suf_len == SIZE_MAX) { suf_len = strlen(suffix); assert (suf_len != 0); } /* And the next matching suffix */ pre_off = pre - buf; suf = ib_strstr_ex(pre+pre_len, buflen - (pre_off + pre_len), suffix, suf_len); if ( recurse && (suf == NULL) ) { slen = (pre - buf); pre = NULL; } } /* Did we find a matching pair? */ if ( (pre == NULL) || (suf == NULL) ) { break; } /* The name is the block between the two */ name = (pre + pre_len); namelen = (suf - pre) - pre_len; /* Length of the initial block */ iptr = buf; ilen = (pre - buf); /* The final block */ fptr = (suf + suf_len); flen = (buf + buflen ) - fptr; /* Zero length name? Expand it to "" */ if (namelen == 0) { rc = join2(mp, iptr, ilen, fptr, flen, true, &new, &newlen); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } buf = new; buflen = newlen; continue; } /* Search the hash */ rc = lookup_fn(lookup_data, name, namelen, &f); if (rc == IB_ENOENT) { /* Not in the hash; replace with "" */ rc = join2(mp, iptr, ilen, fptr, flen, true, &new, &newlen); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } buf = new; buflen = newlen; continue; }