From: Tommy Wu <wu.tommy@gmail.com>
To: cgit@lists.zx2c4.com
Subject: [PATCH] avoid to break UTF-8 character when wrap message
Date: Thu, 21 Oct 2021 16:42:44 +0800 [thread overview]
Message-ID: <CAGdb-8dQfUW4YULxkCgDinyK1Xm4k3CEqX-8szKK=4qaVDm+PQ@mail.gmail.com> (raw)
Wrap message with specified length might break the UTF-8 character.
We need to calculate the proper length when wrap message.
---
ui-log.c | 2 +-
ui-shared.c | 17 ++++++++++++++++-
ui-shared.h | 2 ++
3 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/ui-log.c b/ui-log.c
index 20774bf..cf66a84 100644
--- a/ui-log.c
+++ b/ui-log.c
@@ -227,7 +227,7 @@ static void print_commit(struct commit *commit,
struct rev_info *revs)
while (i > 0 && !isspace(info->subject[i]))
--i;
if (!i) /* Oops, zero spaces. Reset i */
- i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
+ i = cgit_utf8_wrap_len(info->subject, ctx.cfg.max_msg_len -
strlen(wrap_symbol));
/* add remainder starting at i to msgbuf */
strbuf_add(&msgbuf, info->subject + i, subject_len - i);
diff --git a/ui-shared.c b/ui-shared.c
index acd8ab5..ba0a968 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -29,6 +29,20 @@ static char *http_date(time_t t)
tm.tm_hour, tm.tm_min, tm.tm_sec);
}
+int cgit_utf8_wrap_len(const char *s, int maxlen)
+{
+ int i = 0, pos = 0;
+
+ while (s[i]) {
+ if ((s[i] & 0xC0) != 0x80)
+ pos = i;
+ i++;
+ if (i > maxlen)
+ break;
+ }
+ return pos;
+}
+
void cgit_print_error(const char *fmt, ...)
{
va_list ap;
@@ -436,7 +450,8 @@ void cgit_commit_link(const char *name, const char
*title, const char *class,
html("'>");
if (name[0] != '\0') {
if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
- html_ntxt(name, ctx.cfg.max_msg_len - 3);
+ int maxlen = cgit_utf8_wrap_len(name, ctx.cfg.max_msg_len - 3);
+ html_ntxt(name, maxlen);
html("...");
} else
html_txt(name);
diff --git a/ui-shared.h b/ui-shared.h
index 6964873..28ec841 100644
--- a/ui-shared.h
+++ b/ui-shared.h
@@ -1,6 +1,8 @@
#ifndef UI_SHARED_H
#define UI_SHARED_H
+extern int cgit_utf8_wrap_len(const char *s, int maxlen);
+
extern const char *cgit_httpscheme(void);
extern char *cgit_hosturl(void);
extern const char *cgit_rooturl(void);
--
2.30.2
reply other threads:[~2021-10-21 8:43 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAGdb-8dQfUW4YULxkCgDinyK1Xm4k3CEqX-8szKK=4qaVDm+PQ@mail.gmail.com' \
--to=wu.tommy@gmail.com \
--cc=cgit@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).