<commit_before>#include "http/json.hpp"

#include <stdlib.h>

#include <set>
#include <vector>

#include "http/http.hpp"
#include "stl_utils.hpp"
#include "utils.hpp"
#include "containers/archive/stl_types.hpp"

#ifndef NDEBUG
std::string (*cJSON_default_print)(cJSON *json) = cJSON_print_std_string;
#else
std::string (*cJSON_default_print)(cJSON *json) = cJSON_print_unformatted_std_string;
#endif
http_res_t http_json_res(cJSON *json) {
    return http_res_t(HTTP_OK, "application/json", cJSON_default_print(json));
}

cJSON *cJSON_merge(cJSON *lhs, cJSON *rhs) {
    rassert(lhs->type == cJSON_Object);
    rassert(rhs->type == cJSON_Object);
    cJSON *obj = cJSON_DeepCopy(lhs);

    for (int i = 0; i < cJSON_GetArraySize(rhs); ++i) {
        cJSON *item = cJSON_GetArrayItem(rhs, i);
        cJSON_DeleteItemFromObject(obj, item->string);
        cJSON_AddItemToObject(obj, item->string, cJSON_DeepCopy(item));
    }
    return obj;
}

scoped_cJSON_t::scoped_cJSON_t(cJSON *_val)
    : val(_val)
{ }

scoped_cJSON_t::~scoped_cJSON_t() {
    if (val) {
        cJSON_Delete(val);
    }
}


/* Render a cJSON entity to text for transfer/storage. */
std::string scoped_cJSON_t::Print() const THROWS_NOTHING {
    char *s = cJSON_Print(val);
    rassert(s);
    std::string res(s);
    free(s);

    return res;
}
/* Render a cJSON entity to text for transfer/storage without any formatting. */
std::string scoped_cJSON_t::PrintUnformatted() const THROWS_NOTHING {
    char *s = cJSON_PrintUnformatted(val);
    rassert(s);
    std::string res(s);
    free(s);

    return res;
}

std::string scoped_cJSON_t::PrintLexicographic() const THROWS_NOTHING {
    std::string acc;
    rassert(type() == cJSON_Number || type() == cJSON_String);
    if (type() == cJSON_Number) {
        acc += "N";

        union {double d; int64_t u;} packed;
        rassert(sizeof(packed.d) == sizeof(packed.u));
        rassert((void *)&packed.d == (void *)&packed.u);
        packed.d = val->valuedouble;
        acc += strprintf("%0.*lx", (int)(sizeof(double)*2), packed.u);
        return acc;<commit_msg>Fixed strprintf bad format strring in json.cc.<commit_after>#include "http/json.hpp"

#include <stdlib.h>

#include <set>
#include <vector>

#include "http/http.hpp"
#include "stl_utils.hpp"
#include "utils.hpp"
#include "containers/archive/stl_types.hpp"

#ifndef NDEBUG
std::string (*cJSON_default_print)(cJSON *json) = cJSON_print_std_string;
#else
std::string (*cJSON_default_print)(cJSON *json) = cJSON_print_unformatted_std_string;
#endif
http_res_t http_json_res(cJSON *json) {
    return http_res_t(HTTP_OK, "application/json", cJSON_default_print(json));
}

cJSON *cJSON_merge(cJSON *lhs, cJSON *rhs) {
    rassert(lhs->type == cJSON_Object);
    rassert(rhs->type == cJSON_Object);
    cJSON *obj = cJSON_DeepCopy(lhs);

    for (int i = 0; i < cJSON_GetArraySize(rhs); ++i) {
        cJSON *item = cJSON_GetArrayItem(rhs, i);
        cJSON_DeleteItemFromObject(obj, item->string);
        cJSON_AddItemToObject(obj, item->string, cJSON_DeepCopy(item));
    }
    return obj;
}

scoped_cJSON_t::scoped_cJSON_t(cJSON *_val)
    : val(_val)
{ }

scoped_cJSON_t::~scoped_cJSON_t() {
    if (val) {
        cJSON_Delete(val);
    }
}


/* Render a cJSON entity to text for transfer/storage. */
std::string scoped_cJSON_t::Print() const THROWS_NOTHING {
    char *s = cJSON_Print(val);
    rassert(s);
    std::string res(s);
    free(s);

    return res;
}
/* Render a cJSON entity to text for transfer/storage without any formatting. */
std::string scoped_cJSON_t::PrintUnformatted() const THROWS_NOTHING {
    char *s = cJSON_PrintUnformatted(val);
    rassert(s);
    std::string res(s);
    free(s);

    return res;
}

std::string scoped_cJSON_t::PrintLexicographic() const THROWS_NOTHING {
    std::string acc;
    rassert(type() == cJSON_Number || type() == cJSON_String);
    if (type() == cJSON_Number) {
        acc += "N";

        union {double d; int64_t u;} packed;
        rassert(sizeof(packed.d) == sizeof(packed.u));
        rassert((void *)&packed.d == (void *)&packed.u);
        packed.d = val->valuedouble;
        acc += strprintf("%.*lx", (int)(sizeof(double)*2), packed.u);
        return acc;
{'fraction_non_alphanumeric': 0.14943074003795068, 'fraction_numerical': 0.0021347248576850096, 'mean_word_length': 3.990532544378698, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '3900147', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1566, 'n_words': 309}