diff options
author | Alexander Færøy <ahf@0x90.dk> | 2015-01-05 01:03:58 +0100 |
---|---|---|
committer | Alexander Færøy <ahf@0x90.dk> | 2015-01-05 01:03:58 +0100 |
commit | 9abdeb8611977e0ab56ce3e30ee9561a7e8cb204 (patch) | |
tree | b977f35dad66c7288845851c198aff4f0471614d /src/core/misc.c | |
parent | 1136ea915a3ba4345d59c2863c83f12e41f72069 (diff) | |
parent | f81a54b937b16ebabc05a01d3053a49b3341ac8c (diff) | |
download | irssi-9abdeb8611977e0ab56ce3e30ee9561a7e8cb204.zip |
Merge pull request #152 from sebth/master
Try to split long lines on spaces
Diffstat (limited to 'src/core/misc.c')
-rw-r--r-- | src/core/misc.c | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/core/misc.c b/src/core/misc.c index 5e6087cb..586e4f7c 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -967,19 +967,30 @@ char *ascii_strdown(char *str) return str; } -char **strsplit_len(const char *str, int len) -{ - char **ret; - size_t total_len = strlen(str); - int n = total_len / len; - int i; - - if (total_len % len) - n++; - - ret = g_new(char *, n + 1); - for (i = 0; i < n; i++, str += len) - ret[i] = g_strndup(str, len); +char **strsplit_len(const char *str, int len, gboolean onspace) +{ + char **ret = g_new(char *, 1); + int n; + int offset; + + for (n = 0; *str != '\0'; n++, str += MIN(len - offset, strlen(str))) { + offset = 0; + if (onspace) { + /* + * Try to find a space to split on and leave + * the space on the previous line. + */ + int i; + for (i = 0; i < len; i++) { + if (str[len-1-i] == ' ') { + offset = i; + break; + } + } + } + ret[n] = g_strndup(str, len - offset); + ret = g_renew(char *, ret, n + 2); + } ret[n] = NULL; return ret; |