<commit_before>/* $Id$ */

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

#include <fsm/fsm.h>
#include <fsm/graph.h>

#include "out/out.h"
#include "libfsm/set.h"
#include "libfsm/internal.h"

static void escputc(char c, FILE *f) {
	assert(f != NULL);

	switch (c) {
	case '\"':
		fprintf(f, "\\\"");
		return;

		/* TODO: others */

	default:
		/* TODO: if (isprint(c)) { putc(c, f); } else { hex } */
		putc(c, f);
	}
}

/* TODO: refactor for when FSM_EDGE_ANY goes; it is an "any" transition if all
 * labels transition to the same state. centralise that, perhaps */
static const struct fsm_state *findany(const struct fsm_state *state) {
	int i;

	assert(state != NULL);

	for (i = 0; i <= UCHAR_MAX; i++) {
		if (state->edges[0] != state->edges[i]) {
			return NULL;
		}
	}

	return state->edges[0];
}

static void singlecase(FILE *f, const struct fsm_state *state) {
	const struct fsm_state *to;
	int i;

	assert(f != NULL);
	assert(state != NULL);

	for (i = 0; i <= FSM_EDGE_MAX; i++) {
		if (state->edges[i] != NULL) {
			break;
		}
	}

	/* no edges */
	if (i > FSM_EDGE_MAX) {
		fprintf(f, "\t\t\treturn 0;\n");
		return;
	}

	fprintf(f, "\t\t\tswitch (c) {\n");

	/* "any" edge */
	to = findany(state);

	/* usual case */
	if (to == NULL) {
		for (i = 0; i <= FSM_EDGE_MAX; i++) {
			if (state->edges[i] == NULL) {
				continue;
			}

			/* TODO: pass S%u out to maximum state width */
			fprintf(f, "\t\t\tcase '");
			escputc(i, f);
			fprintf(f, "': state = S%u; continue;\n", state->edges[i]->id);
		}
	}

	if (to != NULL) {
		fprintf(f, "\t\t\tdefault:  state = S%u; continue;\n", to->id);
	} else {
		fprintf(f, "\t\t\tdefault:  return 0;\n");	/* invalid edge */
	}

	fprintf(f, "\t\t\t}\n");
}

static void stateenum(FILE *f, struct fsm_state *sl) {
	struct fsm_state *s;<commit_msg>Aesthetic optimisation to fall-through for cases with multiple adjacent edges.<commit_after>/* $Id$ */

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

#include <fsm/fsm.h>
#include <fsm/graph.h>

#include "out/out.h"
#include "libfsm/set.h"
#include "libfsm/internal.h"

static void escputc(char c, FILE *f) {
	assert(f != NULL);

	switch (c) {
	case '\"':
		fprintf(f, "\\\"");
		return;

		/* TODO: others */

	default:
		/* TODO: if (isprint(c)) { putc(c, f); } else { hex } */
		putc(c, f);
	}
}

/* TODO: refactor for when FSM_EDGE_ANY goes; it is an "any" transition if all
 * labels transition to the same state. centralise that, perhaps */
static const struct fsm_state *findany(const struct fsm_state *state) {
	int i;

	assert(state != NULL);

	for (i = 0; i <= UCHAR_MAX; i++) {
		if (state->edges[0] != state->edges[i]) {
			return NULL;
		}
	}

	return state->edges[0];
}

/* Return true if the edges after o contains state */
/* TODO: centralise */
static int contains(struct fsm_state *edges[], int o, struct fsm_state *state) {
	int i;

	assert(edges != NULL);
	assert(state != NULL);

	for (i = o; i <= FSM_EDGE_MAX; i++) {
		if (edges[i] == state) {
			return 1;
		}
	}

	return 0;
}

static void singlecase(FILE *f, const struct fsm_state *state) {
	const struct fsm_state *to;
	int i;

	assert(f != NULL);
	assert(state != NULL);

	for (i = 0; i <= FSM_EDGE_MAX; i++) {
		if (state->edges[i] != NULL) {
			break;
		}
	}

	/* no edges */
	if (i > FSM_EDGE_MAX) {
		fprintf(f, "\t\t\treturn 0;\n");
		return;
	}

	fprintf(f, "\t\t\tswitch (c) {\n");

	/* "any" edge */
	to = findany(state);

	/* usual case */
	if (to == NULL) {
		for (i = 0; i <= FSM_EDGE_MAX; i++) {
			if (state->edges[i] == NULL) {
				continue;
			}

			fprintf(f, "\t\t\tcase '");
			escputc(i, f);
			fprintf(f, "':");

			/* non-unique states fall through */
			if (contains(state->edges, i + 1, state->edges[i])) {
				fprintf(f, "\n");
				continue;
			}

			/* TODO: pass S%u out to maximum state width */
			if (state->edges[i]->id != state->id) {
				fprintf(f, " state = S%u; continue;\n", state->edges[i]->id);
			} else {
				fprintf(f, "             continue;\n");
			}
		}
	}

	if (to != NULL) {
		fprintf(f, "\t\t\tdefault:  state = S%u; continue;\n", to->id);
	} else {
		fprintf(f, "\t\t\tdefault:  return 0;\n");	/* invalid edge */
	}

	fprintf(f, "\t\t\t}\n");
}

static void stateenum(FILE *f, struct fsm_state *sl) {
	struct fsm_state *s;
{'fraction_non_alphanumeric': 0.20229186155285314, 'fraction_numerical': 0.003975678203928906, 'mean_word_length': 2.9786046511627906, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 36, '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': '1486412', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1762, 'n_words': 457}