<commit_before>/*
 * balde-utils: A general purpose utility library for C.
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file COPYING.
 */

#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include <balde-utils/string_utils.h>


char*
b_strdup(const char *s)
{
    if (!s)
        return NULL;
    size_t l = strlen(s);
    char *tmp = malloc(l + 1);
    if (!tmp)
        return NULL;
    memcpy(tmp, s, l + 1);
    return tmp;
}


char*
b_strndup(const char *s, size_t n)
{
    if (!s)
        return NULL;
    size_t l = strlen(s);
    if (l > n)
        l = n;
    char *tmp = malloc(l + 1);
    if (!tmp)
        return NULL;
    memcpy(tmp, s, l + 1);
    tmp[l] = '\0';
    return tmp;
}


char*
b_strdup_vprintf(const char *format, va_list ap)
{
    va_list ap2;
    va_copy(ap2, ap);
    int l = vsnprintf(NULL, 0, format, ap2);
    va_end(ap2);
    if (l < 0)
        return NULL;
    char *tmp = malloc(l + 1);
    if (!tmp)
        return NULL;
    int l2 = vsnprintf(tmp, l + 1, format, ap);
    if (l2 < 0)
        return NULL;
    return tmp;
}


char*
b_strdup_printf(const char *format, ...)
{
    va_list ap;<commit_msg>it makes no sense to call strlen in b_strndup<commit_after>/*
 * balde-utils: A general purpose utility library for C.
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file COPYING.
 */

#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include <balde-utils/string_utils.h>


char*
b_strdup(const char *s)
{
    if (!s)
        return NULL;
    size_t l = strlen(s);
    char *tmp = malloc(l + 1);
    if (!tmp)
        return NULL;
    memcpy(tmp, s, l + 1);
    return tmp;
}


char*
b_strndup(const char *s, size_t n)
{
    if (!s)
        return NULL;
    char *tmp = malloc(n + 1);
    if (!tmp)
        return NULL;
    strncpy(tmp, s, n + 1);
    tmp[n] = '\0';
    return tmp;
}


char*
b_strdup_vprintf(const char *format, va_list ap)
{
    va_list ap2;
    va_copy(ap2, ap);
    int l = vsnprintf(NULL, 0, format, ap2);
    va_end(ap2);
    if (l < 0)
        return NULL;
    char *tmp = malloc(l + 1);
    if (!tmp)
        return NULL;
    int l2 = vsnprintf(tmp, l + 1, format, ap);
    if (l2 < 0)
        return NULL;
    return tmp;
}


char*
b_strdup_printf(const char *format, ...)
{
    va_list ap;
{'fraction_non_alphanumeric': 0.14464915719325755, 'fraction_numerical': 0.01568012544100353, 'mean_word_length': 2.411764705882353, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 24, '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': '3904303', 'n_tokens_mistral': 1038, 'n_tokens_neox': 988, 'n_words': 305}