<commit_before>/*
 * Functions for editing URIs.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "uri-edit.h"

#include <assert.h>
#include <string.h>

const char *
uri_insert_query_string(pool_t pool, const char *uri,
                        const char *query_string)
{
    const char *qmark = strchr(uri, '?');

    if (qmark != NULL) {
        ++qmark;
        return p_strncat(pool, uri, qmark - uri,
                         query_string, strlen(query_string),
                         "&", (size_t)1,
                         qmark, strlen(qmark),
                         NULL);
    } else
        return p_strcat(pool, uri, "?", query_string, NULL);
}

const char *
uri_append_query_string_n(pool_t pool, const char *uri,
                          const char *query_string, size_t length)
{
    assert(uri != NULL);
    assert(query_string != NULL);
    assert(length > 0);

    return p_strncat(pool, uri, strlen(uri),
                     strchr(uri, '?') == NULL ? "?" : "&", (size_t)1,
                     query_string, length,
                     NULL);
}

static size_t
query_string_begins_with(const char *query_string, const char *needle,
                         size_t needle_length)
{
    if (memcmp(query_string, needle, needle_length) != 0)
        return 0;

    query_string += needle_length;
    if (*query_string == '&')
        return needle_length + 1;
    else if (*query_string == 0)
        return needle_length;
    else
        return 0;
}

const char *
uri_delete_query_string(pool_t pool, const char *uri,
                        const char *needle, size_t needle_length)
{
    const char *p;
    size_t delete_length;

    p = strchr(uri, '?');
    if (p == NULL)
        /* no query string, nothing to remove */
        return uri;

    ++p;
    delete_length = query_string_begins_with(p, needle, needle_length);<commit_msg>uri-edit: add assertions<commit_after>/*
 * Functions for editing URIs.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "uri-edit.h"

#include <assert.h>
#include <string.h>

const char *
uri_insert_query_string(pool_t pool, const char *uri,
                        const char *query_string)
{
    assert(pool != NULL);
    assert(uri != NULL);
    assert(query_string != NULL);

    const char *qmark = strchr(uri, '?');

    if (qmark != NULL) {
        ++qmark;
        return p_strncat(pool, uri, qmark - uri,
                         query_string, strlen(query_string),
                         "&", (size_t)1,
                         qmark, strlen(qmark),
                         NULL);
    } else
        return p_strcat(pool, uri, "?", query_string, NULL);
}

const char *
uri_append_query_string_n(pool_t pool, const char *uri,
                          const char *query_string, size_t length)
{
    assert(pool != NULL);
    assert(uri != NULL);
    assert(query_string != NULL);
    assert(length > 0);

    return p_strncat(pool, uri, strlen(uri),
                     strchr(uri, '?') == NULL ? "?" : "&", (size_t)1,
                     query_string, length,
                     NULL);
}

static size_t
query_string_begins_with(const char *query_string, const char *needle,
                         size_t needle_length)
{
    assert(query_string != NULL);
    assert(needle != NULL);

    if (memcmp(query_string, needle, needle_length) != 0)
        return 0;

    query_string += needle_length;
    if (*query_string == '&')
        return needle_length + 1;
    else if (*query_string == 0)
        return needle_length;
    else
        return 0;
}

const char *
uri_delete_query_string(pool_t pool, const char *uri,
                        const char *needle, size_t needle_length)
{
    assert(pool != NULL);
    assert(uri != NULL);
    assert(needle != NULL);

    const char *p;
    size_t delete_length;

    p = strchr(uri, '?');
    if (p == NULL)
        /* no query string, nothing to remove */
        return uri;

    ++p;
    delete_length = query_string_begins_with(p, needle, needle_length);
{'fraction_non_alphanumeric': 0.12217762167586553, 'fraction_numerical': 0.004014049172102358, 'mean_word_length': 2.052833078101072, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5166586', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1241, 'n_words': 309}