<commit_before>/*
 * Replace blanks with tabs and blanks as to keep the same spacing.
 */

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

#define MAXLINE 10000
#define TABSIZE 8

size_t getaline(char *, size_t);
void entab(char *, const char *);

/*
 * Replace blanks with tabs and blanks as to keep the same spacing.
 * Assume that the input file is free of tabs (run through detab).
 */
int
main(void)
{
	int len;
	char line[MAXLINE];	/* Current input line. */
	char tabbed[MAXLINE];	/* Cooked string with tabs. */

	while ((len = getaline(line, MAXLINE)) != -1) {
		entab(tabbed, line);
		printf("%s", tabbed);
	}

	return (0);
}

/* getaline:  read a line into `s'; return length, or -1 if EOF. */
size_t
getaline(char *s, size_t lim)
{
	int c;
	size_t i;

	c = 0;

	for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
		s[i] = c;
	if (c == EOF)
		return (-1);	/* POSIX */
	if (c == '\n')
		s[i++] = c;
	s[i++] = '\0';

	return (i);
}

/* entab:  replace blanks with tabs and blanks. */
void
entab(char *cooked, const char *raw)
{
	int mark, n, tabcolumn, tflag;
	const char *tok;
	const char delim = ' ';
	const char *src = raw;
	char *dst = cooked;

	n = tabcolumn = 0;

	while (*src != '\0') {
		/* Copy characters that are not blanks. */
		for (; *src != delim; n++)
			*dst++ = *src++;

		tok = src;	/* Save the location of the first blank. */
		mark = n;
		tflag = 0;
		tabcolumn += TABSIZE;

		while (*src++ == delim) {
			n++;
			if (n % tabcolumn == 0) {
				tflag = 1;
				break;
			}
		}
		if (tflag == 1) {
			tflag = 0;
			*dst++ = '\t';
		} else {
			src = tok;	/* Not enough blanks for a tab. */
			while (mark++ != n)
				*dst++ = *src++;
		}
	}
}<commit_msg>Let single blanks on a tabstop boundary stay blanks, and not tabs. Properly deal with when to place a tab (and fixes a potential divide by zero): if (n % TABSIZ == 0) instead of if (n % tabcolumn == 0) The program works with input files that either contain or don't contain tabs.<commit_after>/*
 * Replace blanks with tabs and blanks as to keep the same spacing.
 */

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

#define TABSIZ 8

size_t getaline(char *, size_t);
void entab(char *, const char *);

/*
 * Replace blanks with tabs and blanks as to keep the same spacing.
 * Assume that the input file is free of tabs (run through detab).
 */
int
main(void)
{
	int len;
	char line[BUFSIZ];	/* Current input line. */
	char tabbed[BUFSIZ];	/* Cooked string with tabs. */

	while ((len = getaline(line, BUFSIZ)) != -1) {
		entab(tabbed, line);
		printf("%s", tabbed);
	}

	return (0);
}

/* getaline:  read a line into `s'; return length, or -1 if EOF. */
size_t
getaline(char *s, size_t lim)
{
	int c;
	size_t i;

	c = 0;

	for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
		s[i] = c;
	if (c == EOF)
		return (-1);	/* POSIX */
	if (c == '\n')
		s[i++] = c;
	s[i++] = '\0';

	return (i);
}

/* entab:  replace blanks with tabs and blanks. */
void
entab(char *cooked, const char *raw)
{
	int blanks, mark, n, tflag;
	const char *tok;
	const char delim = ' ';
	const char *src = raw;
	char *dst = cooked;

	n = blanks = 0;

	while (*src != '\0') {
		/* Copy characters that are not blanks. */
		for (; *src != delim; n++)
			*dst++ = *src++;

		tok = src;	/* Save the location of the first blank. */
		mark = n;
		tflag = 0;

		while (*src++ == delim) {
			n++;
			blanks++;
			if ((n % TABSIZ == 0) && (blanks > 1)) {
				tflag = 1;
				break;
			}
		}
		blanks = 0;
		if (tflag == 1) {
			tflag = 0;
			*dst++ = '\t';
		} else {
			src = tok;	/* Not enough blanks for a tab. */
			while (mark++ != n)
				*dst++ = *src++;
		}
	}
}
{'fraction_non_alphanumeric': 0.17988980716253444, 'fraction_numerical': 0.011294765840220386, 'mean_word_length': 2.6713852376137512, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, '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': '2531544', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1406, 'n_words': 505}