List for cgit developers and users
 help / color / mirror / Atom feed
* [PATCH] avoid to break UTF-8 character when wrap message
@ 2021-10-21  8:42 Tommy Wu
  0 siblings, 0 replies; only message in thread
From: Tommy Wu @ 2021-10-21  8:42 UTC (permalink / raw)
  To: cgit

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-21  8:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-21  8:42 [PATCH] avoid to break UTF-8 character when wrap message Tommy Wu

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).