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

=head1 NAME

src/charset.c - global charset functions

=head1 DESCRIPTION

These are Parrot's generic charset handling functions

*/

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

#include "encodings/fixed_8.h"
#include "encodings/utf8.h"
#include "encodings/utf16.h"
#include "encodings/ucs2.h"

#include "charset/ascii.h"
#include "charset/binary.h"
#include "charset/iso-8859-1.h"
#include "charset/unicode.h"

CHARSET *Parrot_iso_8859_1_charset_ptr;
CHARSET *Parrot_binary_charset_ptr;
CHARSET *Parrot_default_charset_ptr;
CHARSET *Parrot_unicode_charset_ptr;
CHARSET *Parrot_ascii_charset_ptr;

/*
 * all registered charsets are collected in one global structure
 */

typedef struct To_converter {
    NOTNULL(CHARSET *to);
    NOTNULL(charset_converter_t func);
} To_converter;

typedef struct One_charset {
    NOTNULL(CHARSET *charset);
    STRING  *name;
    int n_converters;
    To_converter *to_converters;
} One_charset;

typedef struct All_charsets {
    int n_charsets;
    One_charset *set;
} All_charsets;

static All_charsets *all_charsets;

/* HEADERIZER HFILE: include/parrot/charset.h */

/* HEADERIZER BEGIN: static */

static INTVAL register_charset( PARROT_INTERP,
    NOTNULL(const char *charsetname),
    NOTNULL(CHARSET *charset) )
        __attribute__nonnull__(1)
        __attribute__nonnull__(2)
        __attribute__nonnull__(3);

static void register_static_converters( PARROT_INTERP )
        __attribute__nonnull__(1);

/* HEADERIZER END: static */

PARROT_API
PARROT_CAN_RETURN_NULL
PARROT_MALLOC
CHARSET *
Parrot_new_charset(SHIM_INTERP)
{
    return mem_allocate_typed(CHARSET);
}

PARROT_API
void
Parrot_charsets_encodings_deinit(SHIM_INTERP)
{
    const int n = all_charsets->n_charsets;
    int i;

    for (i = 0; i < n; ++i) {
        if (all_charsets->set[i].n_converters)
            mem_sys_free(all_charsets->set[i].to_converters);
        mem_sys_free(all_charsets->set[i].charset);
    }
    mem_sys_free(all_charsets->set);
    mem_sys_free(all_charsets);
    all_charsets = NULL;
    parrot_deinit_encodings();
}

PARROT_API
PARROT_CAN_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
const CHARSET *
Parrot_find_charset(SHIM_INTERP, NOTNULL(const char *charsetname))
{
    int i;
    const int n = all_charsets->n_charsets;

    for (i = 0; i < n; ++i) {
        if (strcmp(all_charsets->set[i].charset->name, charsetname) == 0) {
            return all_charsets->set[i].charset;
        }
    }
    return NULL;
}

PARROT_API
PARROT_CAN_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
CHARSET *
Parrot_load_charset(PARROT_INTERP, NOTNULL(const char *charsetname))
{
    UNUSED(charsetname);

    real_exception(interp, NULL, UNIMPLEMENTED, "Can't load charsets yet");
    return NULL;  /* placeholder return until this is implemented */
}

/*

FUNCDC: Parrot_charset_number

Return the number of the charset or -1 if not found.
<commit_msg>[src] Removed (now) superfluous return statement.<commit_after>/*
Copyright (C) 2004-2007, The Perl Foundation.
$Id$

=head1 NAME

src/charset.c - global charset functions

=head1 DESCRIPTION

These are Parrot's generic charset handling functions

*/

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

#include "encodings/fixed_8.h"
#include "encodings/utf8.h"
#include "encodings/utf16.h"
#include "encodings/ucs2.h"

#include "charset/ascii.h"
#include "charset/binary.h"
#include "charset/iso-8859-1.h"
#include "charset/unicode.h"

CHARSET *Parrot_iso_8859_1_charset_ptr;
CHARSET *Parrot_binary_charset_ptr;
CHARSET *Parrot_default_charset_ptr;
CHARSET *Parrot_unicode_charset_ptr;
CHARSET *Parrot_ascii_charset_ptr;

/*
 * all registered charsets are collected in one global structure
 */

typedef struct To_converter {
    NOTNULL(CHARSET *to);
    NOTNULL(charset_converter_t func);
} To_converter;

typedef struct One_charset {
    NOTNULL(CHARSET *charset);
    STRING  *name;
    int n_converters;
    To_converter *to_converters;
} One_charset;

typedef struct All_charsets {
    int n_charsets;
    One_charset *set;
} All_charsets;

static All_charsets *all_charsets;

/* HEADERIZER HFILE: include/parrot/charset.h */

/* HEADERIZER BEGIN: static */

static INTVAL register_charset( PARROT_INTERP,
    NOTNULL(const char *charsetname),
    NOTNULL(CHARSET *charset) )
        __attribute__nonnull__(1)
        __attribute__nonnull__(2)
        __attribute__nonnull__(3);

static void register_static_converters( PARROT_INTERP )
        __attribute__nonnull__(1);

/* HEADERIZER END: static */

PARROT_API
PARROT_CAN_RETURN_NULL
PARROT_MALLOC
CHARSET *
Parrot_new_charset(SHIM_INTERP)
{
    return mem_allocate_typed(CHARSET);
}

PARROT_API
void
Parrot_charsets_encodings_deinit(SHIM_INTERP)
{
    const int n = all_charsets->n_charsets;
    int i;

    for (i = 0; i < n; ++i) {
        if (all_charsets->set[i].n_converters)
            mem_sys_free(all_charsets->set[i].to_converters);
        mem_sys_free(all_charsets->set[i].charset);
    }
    mem_sys_free(all_charsets->set);
    mem_sys_free(all_charsets);
    all_charsets = NULL;
    parrot_deinit_encodings();
}

PARROT_API
PARROT_CAN_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
const CHARSET *
Parrot_find_charset(SHIM_INTERP, NOTNULL(const char *charsetname))
{
    int i;
    const int n = all_charsets->n_charsets;

    for (i = 0; i < n; ++i) {
        if (strcmp(all_charsets->set[i].charset->name, charsetname) == 0) {
            return all_charsets->set[i].charset;
        }
    }
    return NULL;
}

PARROT_API
PARROT_CAN_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
CHARSET *
Parrot_load_charset(PARROT_INTERP, NOTNULL(const char *charsetname))
{
    UNUSED(charsetname);

    real_exception(interp, NULL, UNIMPLEMENTED, "Can't load charsets yet");
}

/*

FUNCDC: Parrot_charset_number

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

{'fraction_non_alphanumeric': 0.09981327448650484, 'fraction_numerical': 0.011203530809709726, 'mean_word_length': 4.415441176470588, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '1944925', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2167, 'n_words': 429}