示例#1
0
/*!
 * \brief Replace string in string
 *
 * Replace \p from with \p to in \p *str, up to \p n times if \p n \> 0. If the
 * function succeeds, \p *str will be passed to `free()` and \p *str will be
 * updated to point to the newly allocated string. If \p n_replaced is not NULL,
 * the number of replacements done will be stored at the value pointed by
 * \p n_replaced. If the function fails, \p *str will be left unchanged.
 *
 * \param[in,out] str Pointer to string to modify
 * \param[in] from String to replace
 * \param[in] to Replacement string
 * \param[in] n Number of replacements to attempt (0 to disable limit)
 * \param[out] n_replaced Pointer to store number of replacements made
 *
 * \return Nothing if successful. Otherwise, the error code.
 */
oc::result<void> str_replace(char **str, const char *from, const char *to,
                             size_t n, size_t *n_replaced)
{
    size_t str_size = strlen(*str) + 1;

    return mem_replace(reinterpret_cast<void **>(str), &str_size,
                       from, strlen(from), to, strlen(to), n, n_replaced);
}
示例#2
0
void test_replace( const struct mem_server *server )
{
	int ret = 0;
	char data[] = "some string data";	
	struct store_item item;
	item._exp_time = 0;
	NEW_ITEM_DATA( item._item, "TestReplace", 0 );
	
	evbuffer_add( STORE_ITEM_BUF( item ), data, strlen( data ) );
	
	ret = mem_replace( server, &item, 1 );
	if( ret == MEM_STORED )
	{
		printf( "test_replace : stored.\n" );
	}
	if( ret == MEM_NOT_STORED )
	{
		/* add this */
		mem_add( server, &item, 1 );
	}

	DEL_ITEM_DATA( item._item );
}