From 319ce7b09f9f14442e2f39a92487b76608ea783d Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sun, 23 Oct 2022 09:49:09 +0300 Subject: [PATCH] cleanup: refactor duplicate loops to CalcWordLength(). Note: This code has no tests. Tests would pass even without a call to this function. --- src/libespeak-ng/translate.c | 39 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/libespeak-ng/translate.c b/src/libespeak-ng/translate.c index caf9c3dc..5fe64005 100644 --- a/src/libespeak-ng/translate.c +++ b/src/libespeak-ng/translate.c @@ -45,6 +45,7 @@ #include "speech.h" // for MAKE_MEM_UNDEFINED #include "translateword.h" +static int CalcWordLength(int source_index, int charix_top, short int *charix, WORD_TAB *words, int word_count); static void CombineFlag(Translator *tr, WORD_TAB *wtab, char *word, int *flags, unsigned char *p, char *word_phonemes); static void SwitchLanguage(char *word, char *word_phonemes); @@ -1068,15 +1069,7 @@ void TranslateClause(Translator *tr, int *tone_out, char **voice_change) words[0].start = ix; words[0].flags = 0; - for (j = 0; charix[j] <= 0; j++) ; - words[0].sourceix = charix[j]; - k = 0; - while (charix[j] != 0) { - // count the number of characters (excluding multibyte continuation bytes) - if (charix[j++] != -1) - k++; - } - words[0].length = k; + words[0].length = CalcWordLength(source_index, charix_top, charix, words, 0); while (!finished && (ix < (int)sizeof(sbuf) - 1)) { prev_out2 = prev_out; @@ -1453,16 +1446,7 @@ void TranslateClause(Translator *tr, int *tone_out, char **voice_change) words[word_count].start = ix; words[word_count].flags = 0; - for (j = source_index; j < charix_top && charix[j] <= 0; j++) // skip blanks - ; - words[word_count].sourceix = charix[j]; - k = 0; - while (charix[j] != 0) { - // count the number of characters (excluding multibyte continuation bytes) - if (charix[j++] != -1) - k++; - } - words[word_count].length = k; + words[word_count].length = CalcWordLength(source_index, charix_top, charix, words, word_count); word_flags = next_word_flags; next_word_flags = 0; @@ -1697,6 +1681,21 @@ void TranslateClause(Translator *tr, int *tone_out, char **voice_change) } } +static int CalcWordLength(int source_index, int charix_top, short int *charix, WORD_TAB *words, int word_count) { + int j; + int k; + + for (j = source_index; j < charix_top && charix[j] <= 0; j++); // skip blanks + words[word_count].sourceix = charix[j]; + k = 0; + while (charix[j] != 0) { + // count the number of characters (excluding multibyte continuation bytes) + if (charix[j++] != -1) + k++; + } + return k; + } + static void CombineFlag(Translator *tr, WORD_TAB *wtab, char *word, int *flags, unsigned char *p, char *word_phonemes) { // combine a preposition with the following word @@ -1805,4 +1804,4 @@ void InitText(int control) if ((control & espeakKEEP_NAMEDATA) == 0) InitNamedata(); -} +} \ No newline at end of file