From 54d93cf2b4bc7bcef460109aeb7be0c77d910d12 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sun, 23 Aug 2020 18:44:15 +0300 Subject: [PATCH] code cleanup: move ssml reference handling logic to a new function ParseSsmlReference() It's unclear why c2 needs to be set after an entity reference. --- src/libespeak-ng/readclause.c | 29 +++-------------------------- src/libespeak-ng/ssml.c | 35 +++++++++++++++++++++++++++++++++++ src/libespeak-ng/ssml.h | 4 ++++ 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/libespeak-ng/readclause.c b/src/libespeak-ng/readclause.c index 9a7cb3a4..b45f4ff2 100644 --- a/src/libespeak-ng/readclause.c +++ b/src/libespeak-ng/readclause.c @@ -603,16 +603,6 @@ static void RemoveChar(char *p) memset(p, ' ', utf8_in(&c, p)); } -static MNEM_TAB xml_char_mnemonics[] = { - { "gt", '>' }, - { "lt", 0xe000 + '<' }, // private usage area, to avoid confusion with XML tag - { "amp", '&' }, - { "quot", '"' }, - { "nbsp", ' ' }, - { "apos", '\'' }, - { NULL, -1 } -}; - int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_buf, int *tone_type, char *voice_change) { /* Find the end of the current clause. @@ -641,7 +631,6 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ int phoneme_mode = 0; int n_xml_buf; int terminator; - int found; bool any_alnum = false; int punct_data = 0; bool is_end_clause; @@ -727,22 +716,10 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ c2 = GetC(); sprintf(ungot_string, "%s%c%c", &xml_buf2[0], c1, c2); + int found = -1; if (c1 == ';') { - if (xml_buf2[0] == '#') { - // character code number - if (xml_buf2[1] == 'x') - found = sscanf(&xml_buf2[2], "%x", (unsigned int *)(&c1)); - else - found = sscanf(&xml_buf2[1], "%d", &c1); - } else { - if ((found = LookupMnem(xml_char_mnemonics, xml_buf2)) != -1) { - c1 = found; - if (c2 == 0) - c2 = ' '; - } - } - } else - found = -1; + found = ParseSsmlReference(xml_buf2, &c1, &c2); + } if (found <= 0) { ungot_string_ix = 0; diff --git a/src/libespeak-ng/ssml.c b/src/libespeak-ng/ssml.c index de811d84..d4535f46 100644 --- a/src/libespeak-ng/ssml.c +++ b/src/libespeak-ng/ssml.c @@ -949,3 +949,38 @@ int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int *outix, int n_outbuf, con } return 0; } + +static MNEM_TAB xml_entity_mnemonics[] = { + { "gt", '>' }, + { "lt", 0xe000 + '<' }, // private usage area, to avoid confusion with XML tag + { "amp", '&' }, + { "quot", '"' }, + { "nbsp", ' ' }, + { "apos", '\'' }, + { NULL, -1 } +}; + +int ParseSsmlReference(char *ref, int *c1, int *c2) { + // Check if buffer *ref contains an XML character or entity reference + // if found, set *c1 to the replacement char + // change *c2 for entity references + // returns >= 0 on success + + if (ref[0] == '#') { + // character reference + if (ref[1] == 'x') + return sscanf(&ref[2], "%x", c1); + else + return sscanf(&ref[1], "%d", c1); + } else { + // entity reference + int found; + if ((found = LookupMnem(xml_entity_mnemonics, ref)) != -1) { + *c1 = found; + if (*c2 == 0) + *c2 = ' '; + return found; + } + } + return -1; +} diff --git a/src/libespeak-ng/ssml.h b/src/libespeak-ng/ssml.h index 6c6235e2..bcc8a8aa 100644 --- a/src/libespeak-ng/ssml.h +++ b/src/libespeak-ng/ssml.h @@ -80,6 +80,10 @@ int ProcessSsmlTag(wchar_t *xml_buf, int *n_param_stack, int *speech_parameters); +int ParseSsmlReference(char *ref, + int *c1, + int *c2); + #ifdef __cplusplus } #endif