Fossil SCM
Properly truncate a forum thread's title when is UTF-8 encoded.
Commit
4b12ad0eb2ee0300cf1b9ba4f0f27080afba0d92ae1b9e7ae7a9f6bac95ccde4
Parent
9559bdb69cea3e1…
1 file changed
+17
-4
+17
-4
| --- src/forum.c | ||
| +++ src/forum.c | ||
| @@ -549,14 +549,27 @@ | ||
| 549 | 549 | " WHERE event.objid=forumpost.fpid" |
| 550 | 550 | " AND forumpost.fpid=%d;", |
| 551 | 551 | fpid |
| 552 | 552 | ); |
| 553 | 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); | |
| 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); | |
| 558 | 571 | } |
| 559 | 572 | style_header("%s%s", blob_str(&title), blob_size(&title) ? " - Forum" : "Forum"); |
| 560 | 573 | blob_reset(&title); |
| 561 | 574 | fossil_free(zThreadTitle); |
| 562 | 575 | return 0; |
| 563 | 576 |
| --- 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 |