Fossil SCM

Properly truncate a forum thread's title when is UTF-8 encoded.

ashepilko 2020-02-29 19:32 forumthread-title
Commit 4b12ad0eb2ee0300cf1b9ba4f0f27080afba0d92ae1b9e7ae7a9f6bac95ccde4
1 file changed +17 -4
+17 -4
--- src/forum.c
+++ src/forum.c
@@ -549,14 +549,27 @@
549549
" WHERE event.objid=forumpost.fpid"
550550
" AND forumpost.fpid=%d;",
551551
fpid
552552
);
553553
blob_set(&title, zThreadTitle);
554
- /* truncate title when longer than max allowed */
555
- if ( mxForumPostTitleLen>0 && blob_size(&title)>mxForumPostTitleLen ) {
556
- blob_resize(&title, mxForumPostTitleLen);
557
- blob_append(&title, "...", 3);
554
+ /* truncate the title when longer than max allowed;
555
+ * in case of UTF-8 make sure the truncated string remains valid,
556
+ * otherwise (different encoding?) pass as-is
557
+ */
558
+ if( mxForumPostTitleLen>0 && blob_size(&title)>mxForumPostTitleLen ){
559
+ Blob truncated;
560
+ int len;
561
+ blob_copy(&truncated, &title);
562
+ for( len = mxForumPostTitleLen; len; --len ){
563
+ blob_truncate(&truncated, len);
564
+ if( !invalid_utf8(&truncated) ) break;
565
+ }
566
+ if( len ){
567
+ blob_append(&truncated, "...", 3);
568
+ blob_copy(&title, &truncated);
569
+ }
570
+ blob_reset(&truncated);
558571
}
559572
style_header("%s%s", blob_str(&title), blob_size(&title) ? " - Forum" : "Forum");
560573
blob_reset(&title);
561574
fossil_free(zThreadTitle);
562575
return 0;
563576
--- src/forum.c
+++ src/forum.c
@@ -549,14 +549,27 @@
549 " WHERE event.objid=forumpost.fpid"
550 " AND forumpost.fpid=%d;",
551 fpid
552 );
553 blob_set(&title, zThreadTitle);
554 /* truncate title when longer than max allowed */
555 if ( mxForumPostTitleLen>0 && blob_size(&title)>mxForumPostTitleLen ) {
556 blob_resize(&title, mxForumPostTitleLen);
557 blob_append(&title, "...", 3);
 
 
 
 
 
 
 
 
 
 
 
 
 
558 }
559 style_header("%s%s", blob_str(&title), blob_size(&title) ? " - Forum" : "Forum");
560 blob_reset(&title);
561 fossil_free(zThreadTitle);
562 return 0;
563
--- src/forum.c
+++ src/forum.c
@@ -549,14 +549,27 @@
549 " WHERE event.objid=forumpost.fpid"
550 " AND forumpost.fpid=%d;",
551 fpid
552 );
553 blob_set(&title, zThreadTitle);
554 /* truncate the title when longer than max allowed;
555 * in case of UTF-8 make sure the truncated string remains valid,
556 * otherwise (different encoding?) pass as-is
557 */
558 if( mxForumPostTitleLen>0 && blob_size(&title)>mxForumPostTitleLen ){
559 Blob truncated;
560 int len;
561 blob_copy(&truncated, &title);
562 for( len = mxForumPostTitleLen; len; --len ){
563 blob_truncate(&truncated, len);
564 if( !invalid_utf8(&truncated) ) break;
565 }
566 if( len ){
567 blob_append(&truncated, "...", 3);
568 blob_copy(&title, &truncated);
569 }
570 blob_reset(&truncated);
571 }
572 style_header("%s%s", blob_str(&title), blob_size(&title) ? " - Forum" : "Forum");
573 blob_reset(&title);
574 fossil_free(zThreadTitle);
575 return 0;
576

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button