~begs/hru

c39a70e3f86489617eec93725757e0e91b706d05 — Maxim Karasev 11 months ago d11d316 master 1.1
*.[ch]: calculate grunt string size at program start

Saves a lot of CPU cycles.
3 files changed, 17 insertions(+), 4 deletions(-)

M grunt.c
M grunt.h
M main.c
M grunt.c => grunt.c +8 -4
@@ 1,7 1,6 @@
#include "grunt.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/* Returns true if given codepoint belongs to cyrillic characters range


@@ 29,20 28,25 @@ int convert_char(struct grunt_options options, utf8proc_int32_t *chptr,
	// Initlaize helper variable to avoid multiple dereferencing
	utf8proc_int32_t ch = *chptr;
	const char *s;
	int l;
	bool uppercase;

	// Determine correct substitution string and case
	if (islower(ch)) {
		s = options.grunt;
		l = options.grunt_size;
		uppercase = false;
	} else if (isupper(ch)) {
		s = options.grunt;
		l = options.grunt_size;
		uppercase = true;
	} else if (cyr_islower(ch)) {
		s = options.cyr_grunt;
		l = options.cyr_grunt_size;
		uppercase = false;
	} else if (cyr_isupper(ch)) {
		s = options.cyr_grunt;
		l = options.cyr_grunt_size;
		uppercase = true;
	} else {
		*chptr = ch;


@@ 50,9 54,8 @@ int convert_char(struct grunt_options options, utf8proc_int32_t *chptr,
	}

	// Loop on the last codepoint
	int len = strlen(s);
	if (grunt_pos >= len) {
		grunt_pos = len - char_size;
	if (grunt_pos >= l) {
		grunt_pos = l - char_size;
	}

	// Get substitution character


@@ 98,3 101,4 @@ bool convert_text(struct grunt_options options, char *string, int len) {

	return 1;
}


M grunt.h => grunt.h +3 -0
@@ 4,7 4,9 @@
// Holds substitution strings
struct grunt_options {
	const char *grunt;
	int grunt_size;
	const char *cyr_grunt;
	int cyr_grunt_size;
};

extern bool convert_text(struct grunt_options options, char *string, int len);


@@ 13,3 15,4 @@ extern int convert_char(struct grunt_options options, utf8proc_int32_t *chptr,
	int grunt_pos, int char_size);

extern bool cyr_isalpha(int c);


M main.c => main.c +6 -0
@@ 2,6 2,7 @@
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
	// Construct options structure


@@ 26,6 27,10 @@ int main(int argc, char *argv[]) {
		}
	}

	// Calculate sizes of substitution strings
	options.grunt_size = strlen(options.grunt);
	options.cyr_grunt_size = strlen(options.cyr_grunt);

	// Initialize required variables
	char *string = NULL;
	size_t len = 0;


@@ 48,3 53,4 @@ int main(int argc, char *argv[]) {

	return 0;
}