Skip to content

Commit

Permalink
generic: Make stripping punctuation use locale charset
Browse files Browse the repository at this point in the history
So that notably «» can work

Fixes #872
  • Loading branch information
sthibaul committed Apr 19, 2024
1 parent 6c16359 commit 268706e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/modules/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <locale.h>

#include <speechd_types.h>

Expand Down Expand Up @@ -181,6 +182,9 @@ int module_init(char **status_info)
generic_msg_language->charset = g_strdup("iso-8859-1");
generic_msg_language->name = g_strdup("english");

/* For mbtowc to work in locale charset */
setlocale(LC_CTYPE, "");

generic_message = NULL;

char name[64];
Expand Down
26 changes: 19 additions & 7 deletions src/modules/module_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#endif

#include <fdsetconv.h>
#include <wchar.h>
#include "module_utils.h"
#include "module_main.h"

Expand Down Expand Up @@ -331,22 +332,33 @@ module_get_message_part(const char *message, char *part, unsigned int *pos,

void module_strip_punctuation_some(char *message, char *punct_chars)
{
int len;
int len, inc;
int lenp;
char *p = message;
int i;
assert(message != NULL);
mbstate_t state;

if (punct_chars == NULL)
return;

memset(&state, 0, sizeof(state));

len = strlen(message);
for (i = 0; i <= len - 1; i++) {
if (strchr(punct_chars, *p)) {
DBG("Substitution %d: char -%c- for whitespace\n", i,
*p);
*p = ' ';
lenp = strlen(punct_chars);
for (i = 0; i < len; i += inc) {
wchar_t wc;
inc = mbrtowc(&wc, p, len - i, &state);
if (inc < 0) {
DBG("Oops, at %d invalid char -%c- (%d)?\n", i, *p, inc);
return;
}

if (memmem(punct_chars, lenp, p, inc)) {
DBG("Substitution %d: char -%.*s- (%d) for whitespace\n", i, inc, p, inc);
memset(p, ' ', inc);
}
p++;
p += inc;
}
}

Expand Down

0 comments on commit 268706e

Please sign in to comment.