Skip to content

Commit

Permalink
Lib(fix): rewrite string transcoding function
Browse files Browse the repository at this point in the history
- For to_codeset, we only need UTF-8 is need, because GTK only supports
  UTF-8
- Deal with invalid UTF-8 sequence
- Prevent uncessary convertion when from_codeset is UTF-8 as well
  • Loading branch information
SilverRainZ committed Mar 23, 2020
1 parent 7aaddd1 commit 50e7757
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/inc/srain.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ typedef gboolean bool;
#define SRN_TRUE TRUE
#define SRN_FALSE FALSE

/* All strings in Srain should be utf-8 sequence */
#define SRN_ENCODING "utf-8"
#define SRN_FALLBACK_CHAR "�"
#define SRN_CODESET "UTF-8"

#endif /* __SRAIN_H */
2 changes: 1 addition & 1 deletion src/inc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ time_t get_current_time_s(void);
void time_to_str(time_t time, char *timestr, size_t size, const char *fmt);
void str_assign(char **left, const char *right);
bool str_is_empty(const char *str);
void str_transcoding(char **str, const char *to, const char *from, const char *fallback);
void str_transcoding(char **str, const char *from_codeset);

#endif /* __UTILS_H */
22 changes: 15 additions & 7 deletions src/lib/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,27 @@ bool str_is_empty(const char *str){
return TRUE;
}

void str_transcoding(char **str, const char *to, const char *from, const char *fallback){
char *tmp;
GError *err;

void str_transcoding(char **str, const char *from_codeset){
if (!*str) return;

err = NULL;
tmp = g_convert_with_fallback(*str, -1, to, from, fallback, NULL, NULL, &err);
if (g_ascii_strcasecmp(from_codeset, SRN_CODESET) == 0) {
// UTF-8 to UTF-8, just make sure it is valid
if (g_utf8_validate(*str, -1, NULL)) {
return;
}
// If invalid, make it valid
str_assign(str, g_utf8_make_valid(*str, -1));
return;
}

// To other codeset
GError *err = NULL;
char *tmp = g_convert_with_fallback(*str, -1, SRN_CODESET, from_codeset, "�", NULL, NULL, &err);
if (tmp){
str_assign(str, tmp);
}
if (err) {
WARN_FR("Failed to convert line from %s to %s: %s", from, to, err->message);
WARN_FR("Failed to convert line from %s to %s: %s", from_codeset, SRN_CODESET, err->message);
g_error_free(err);
}
}

0 comments on commit 50e7757

Please sign in to comment.