<commit_before>/*
Copyright (C) 2004-2007, The Perl Foundation.
$Id$

=head1 NAME

src/encoding.c - global encoding functions

=head1 DESCRIPTION

These are parrot's generic encoding handling functions

*/

#define PARROT_NO_EXTERN_ENCODING_PTRS
#include "parrot/parrot.h"

/* HEADER: include/parrot/encoding.h */

ENCODING *Parrot_default_encoding_ptr;
ENCODING *Parrot_fixed_8_encoding_ptr;
ENCODING *Parrot_utf8_encoding_ptr;
ENCODING *Parrot_ucs2_encoding_ptr;
ENCODING *Parrot_utf16_encoding_ptr;

typedef struct One_encoding {
    ENCODING *encoding;
    STRING  *name;
} One_encoding;

typedef struct All_encodings {
    int n_encodings;
    One_encoding *enc;
} All_encodings;

static All_encodings *all_encodings;

void
parrot_init_encodings_2(Interp *interp)
{
    int i, n;

    n = all_encodings->n_encodings;
    for (i = 0; i < n; ++i) {
        all_encodings->enc[i].name->charset = Parrot_default_charset_ptr;
    }
}

void
parrot_deinit_encodings(Interp *interp)
{
    const n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        mem_sys_free(all_encodings->enc[i].encoding);
    }
    mem_sys_free(all_encodings->enc);
    mem_sys_free(all_encodings);
    all_encodings = NULL;
}

PARROT_API
ENCODING *
Parrot_new_encoding(Interp *interp)
    /* MALLOC, WARN_UNUSED */
{
    return mem_allocate_typed(ENCODING);
}

PARROT_API
ENCODING *
Parrot_find_encoding(Interp *interp, const char *encodingname /*NN*/)
{
    const n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (strcmp(all_encodings->enc[i].encoding->name, encodingname) == 0) {
            return all_encodings->enc[i].encoding;
        }
    }
    return NULL;
}

/* Yep, this needs to be a char * parameter -- it's tough to load in
   encodings and such for strings if we can't be sure we've got enough
   info set up to actually build strings... */

PARROT_API
ENCODING *
Parrot_load_encoding(Interp *interp, const char *encodingname)
    /* WARN_UNUSED, NORETURN */
{
    internal_exception(UNIMPLEMENTED, "Can't load encodings yet");
    return NULL;
}

/*

FUNCDOC: INTVAL Parrot_encoding_number

Return the number of the encoding or -1 if not found.

*/

PARROT_API
INTVAL
Parrot_encoding_number(Interp *interp, STRING *encodingname /*NN*/)
    /* WARN_UNUSED */
{
    const int n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (!string_equal(interp, all_encodings->enc[i].name,
                          encodingname))
            return i;
    }
    return -1;
}

/*

FUNCDOC: Parrot_encoding_number_of_str

Return the number of the encoding of the given string or -1 if not found.

*/

PARROT_API
INTVAL
Parrot_encoding_number_of_str(Interp *interp, STRING *src /*NN*/)
    /* WARN_UNUSED */
{
    const int n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (src->encoding == all_encodings->enc[i].encoding)
            return i;
    }
    return -1;
}

PARROT_API
STRING*
Parrot_encoding_name(Interp *interp, INTVAL number_of_encoding)
    /* WARN_UNUSED */
{
    if (number_of_encoding >= all_encodings->n_encodings)
        return NULL;
    return all_encodings->enc[number_of_encoding].name;
}

PARROT_API
ENCODING*
Parrot_get_encoding(Interp *interp, INTVAL number_of_encoding)
    /* WARN_UNUSED */
{
    if (number_of_encoding >= all_encodings->n_encodings)
        return NULL;
    return all_encodings->enc[number_of_encoding].encoding;
}

PARROT_API
const char *
Parrot_encoding_c_name(Interp *interp, INTVAL number_of_encoding)
    /* WARN_UNUSED */
{
    if (number_of_encoding >= all_encodings->n_encodings)
        return NULL;
    return all_encodings->enc[number_of_encoding].encoding->name;
}

static INTVAL
register_encoding(Interp *interp, const char *encodingname,
        ENCODING *encoding)
{
    const n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (strcmp(all_encodings->enc[i].encoding->name, encodingname) == 0)
            return 0;
    }
    /*
     * TODO
     * this needs either a LOCK or we just forbid dynamic
     * loading of encodings from inside threads
     */
    if (!n)
        all_encodings->enc = mem_allocate_typed(One_encoding);
    else
        all_encodings->enc = (One_encoding*)mem_sys_realloc(all_encodings->enc,
                (n + 1) * sizeof (One_encoding));
    all_encodings->n_encodings++;
    all_encodings->enc[n].encoding = encoding;
    all_encodings->enc[n].name = const_string(interp, encodingname);

    return 1;<commit_msg>[src] Added back in type specifier ("const" is not always enough).<commit_after>/*
Copyright (C) 2004-2007, The Perl Foundation.
$Id$

=head1 NAME

src/encoding.c - global encoding functions

=head1 DESCRIPTION

These are parrot's generic encoding handling functions

*/

#define PARROT_NO_EXTERN_ENCODING_PTRS
#include "parrot/parrot.h"

/* HEADER: include/parrot/encoding.h */

ENCODING *Parrot_default_encoding_ptr;
ENCODING *Parrot_fixed_8_encoding_ptr;
ENCODING *Parrot_utf8_encoding_ptr;
ENCODING *Parrot_ucs2_encoding_ptr;
ENCODING *Parrot_utf16_encoding_ptr;

typedef struct One_encoding {
    ENCODING *encoding;
    STRING  *name;
} One_encoding;

typedef struct All_encodings {
    int n_encodings;
    One_encoding *enc;
} All_encodings;

static All_encodings *all_encodings;

void
parrot_init_encodings_2(Interp *interp)
{
    int i, n;

    n = all_encodings->n_encodings;
    for (i = 0; i < n; ++i) {
        all_encodings->enc[i].name->charset = Parrot_default_charset_ptr;
    }
}

void
parrot_deinit_encodings(Interp *interp)
{
    const int n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        mem_sys_free(all_encodings->enc[i].encoding);
    }
    mem_sys_free(all_encodings->enc);
    mem_sys_free(all_encodings);
    all_encodings = NULL;
}

PARROT_API
ENCODING *
Parrot_new_encoding(Interp *interp)
    /* MALLOC, WARN_UNUSED */
{
    return mem_allocate_typed(ENCODING);
}

PARROT_API
ENCODING *
Parrot_find_encoding(Interp *interp, const char *encodingname /*NN*/)
{
    const int n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (strcmp(all_encodings->enc[i].encoding->name, encodingname) == 0) {
            return all_encodings->enc[i].encoding;
        }
    }
    return NULL;
}

/* Yep, this needs to be a char * parameter -- it's tough to load in
   encodings and such for strings if we can't be sure we've got enough
   info set up to actually build strings... */

PARROT_API
ENCODING *
Parrot_load_encoding(Interp *interp, const char *encodingname)
    /* WARN_UNUSED, NORETURN */
{
    internal_exception(UNIMPLEMENTED, "Can't load encodings yet");
    return NULL;
}

/*

FUNCDOC: INTVAL Parrot_encoding_number

Return the number of the encoding or -1 if not found.

*/

PARROT_API
INTVAL
Parrot_encoding_number(Interp *interp, STRING *encodingname /*NN*/)
    /* WARN_UNUSED */
{
    const int n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (!string_equal(interp, all_encodings->enc[i].name,
                          encodingname))
            return i;
    }
    return -1;
}

/*

FUNCDOC: Parrot_encoding_number_of_str

Return the number of the encoding of the given string or -1 if not found.

*/

PARROT_API
INTVAL
Parrot_encoding_number_of_str(Interp *interp, STRING *src /*NN*/)
    /* WARN_UNUSED */
{
    const int n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (src->encoding == all_encodings->enc[i].encoding)
            return i;
    }
    return -1;
}

PARROT_API
STRING*
Parrot_encoding_name(Interp *interp, INTVAL number_of_encoding)
    /* WARN_UNUSED */
{
    if (number_of_encoding >= all_encodings->n_encodings)
        return NULL;
    return all_encodings->enc[number_of_encoding].name;
}

PARROT_API
ENCODING*
Parrot_get_encoding(Interp *interp, INTVAL number_of_encoding)
    /* WARN_UNUSED */
{
    if (number_of_encoding >= all_encodings->n_encodings)
        return NULL;
    return all_encodings->enc[number_of_encoding].encoding;
}

PARROT_API
const char *
Parrot_encoding_c_name(Interp *interp, INTVAL number_of_encoding)
    /* WARN_UNUSED */
{
    if (number_of_encoding >= all_encodings->n_encodings)
        return NULL;
    return all_encodings->enc[number_of_encoding].encoding->name;
}

static INTVAL
register_encoding(Interp *interp, const char *encodingname,
        ENCODING *encoding)
{
    const int n = all_encodings->n_encodings;
    int i;

    for (i = 0; i < n; ++i) {
        if (strcmp(all_encodings->enc[i].encoding->name, encodingname) == 0)
            return 0;
    }
    /*
     * TODO
     * this needs either a LOCK or we just forbid dynamic
     * loading of encodings from inside threads
     */
    if (!n)
        all_encodings->enc = mem_allocate_typed(One_encoding);
    else
        all_encodings->enc = (One_encoding*)mem_sys_realloc(all_encodings->enc,
                (n + 1) * sizeof (One_encoding));
    all_encodings->n_encodings++;
    all_encodings->enc[n].encoding = encoding;
    all_encodings->enc[n].name = const_string(interp, encodingname);

    return 1;
{'fraction_non_alphanumeric': 0.10950977180914948, 'fraction_numerical': 0.00676929795829239, 'mean_word_length': 3.306535025858016, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 71, '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': '7609356', 'n_tokens_mistral': 3467, 'n_tokens_neox': 3327, 'n_words': 748}