Changeset d444c09
- Timestamp:
- 2007-10-12T00:06:50Z (17 years ago)
- Branches:
- master
- Children:
- 3933853, c78c032, eda54e4
- Parents:
- 285b55d
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/misc.c
r285b55d rd444c09 545 545 return reply; 546 546 } 547 548 /* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */ 549 char *word_wrap( char *msg, int line_len ) 550 { 551 GString *ret = g_string_sized_new( strlen( msg ) + 16 ); 552 553 while( strlen( msg ) > line_len ) 554 { 555 int i; 556 557 /* First try to find out if there's a newline already. Don't 558 want to add more splits than necessary. */ 559 for( i = line_len; i > 0 && msg[i] != '\n'; i -- ); 560 if( msg[i] == '\n' ) 561 { 562 g_string_append_len( ret, msg, i + 1 ); 563 msg += i + 1; 564 continue; 565 } 566 567 for( i = line_len; i > 0; i -- ) 568 { 569 if( msg[i] == '-' ) 570 { 571 g_string_append_len( ret, msg, i + 1 ); 572 g_string_append_c( ret, '\n' ); 573 msg += i + 1; 574 break; 575 } 576 else if( msg[i] == ' ' ) 577 { 578 g_string_append_len( ret, msg, i ); 579 g_string_append_c( ret, '\n' ); 580 msg += i + 1; 581 break; 582 } 583 } 584 if( i == 0 ) 585 { 586 g_string_append_len( ret, msg, line_len ); 587 g_string_append_c( ret, '\n' ); 588 msg += line_len; 589 } 590 } 591 g_string_append( ret, msg ); 592 593 return g_string_free( ret, FALSE ); 594 } -
lib/misc.h
r285b55d rd444c09 64 64 G_MODULE_EXPORT struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain ); 65 65 66 G_MODULE_EXPORT char *word_wrap( char *msg, int line_len ); 67 66 68 #endif -
protocols/nogaim.c
r285b55d rd444c09 606 606 { 607 607 irc_t *irc = ic->irc; 608 char *wrapped; 608 609 user_t *u; 609 610 … … 648 649 strip_html( msg ); 649 650 650 while( strlen( msg ) > 425 ) 651 { 652 char tmp, *nl; 653 654 tmp = msg[425]; 655 msg[425] = 0; 656 657 /* If there's a newline/space in this string, split up there, 658 looks a bit prettier. */ 659 if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) ) 660 { 661 msg[425] = tmp; 662 tmp = *nl; 663 *nl = 0; 664 } 665 666 irc_msgfrom( irc, u->nick, msg ); 667 668 /* Move on. */ 669 if( nl ) 670 { 671 *nl = tmp; 672 msg = nl + 1; 673 } 674 else 675 { 676 msg[425] = tmp; 677 msg += 425; 678 } 679 } 680 irc_msgfrom( irc, u->nick, msg ); 651 wrapped = word_wrap( msg, 425 ); 652 irc_msgfrom( irc, u->nick, wrapped ); 653 g_free( wrapped ); 681 654 } 682 655 … … 737 710 { 738 711 struct im_connection *ic = c->ic; 712 char *wrapped; 739 713 user_t *u; 740 714 … … 749 723 strip_html( msg ); 750 724 725 wrapped = word_wrap( msg, 425 ); 751 726 if( c && u ) 752 irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", msg ); 727 { 728 irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped ); 729 } 753 730 else 754 imcb_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, msg ); 731 { 732 imcb_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, wrapped ); 733 } 734 g_free( wrapped ); 755 735 } 756 736 -
tests/check_util.c
r285b55d rd444c09 104 104 END_TEST 105 105 106 struct 107 { 108 char *orig; 109 int line_len; 110 char *wrapped; 111 } word_wrap_tests[] = { 112 { 113 "Line-wrapping is not as easy as it seems?", 114 16, 115 "Line-wrapping is\nnot as easy as\nit seems?" 116 }, 117 { 118 "Line-wrapping is not as easy as it seems?", 119 8, 120 "Line-\nwrapping\nis not\nas easy\nas it\nseems?" 121 }, 122 { 123 "Line-wrapping is\nnot as easy as it seems?", 124 8, 125 "Line-\nwrapping\nis\nnot as\neasy as\nit\nseems?" 126 }, 127 { 128 "a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa", 129 5, 130 "a aa\naaa\naaaa\naaaaa\naaaaa\na\naaaaa\naa\naaaaa\naaa", 131 }, 132 { 133 "aaaaaaaa aaaaaaa aaaaaa aaaaa aaaa aaa aa a", 134 5, 135 "aaaaa\naaa\naaaaa\naa\naaaaa\na\naaaaa\naaaa\naaa\naa a", 136 }, 137 { 138 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 139 5, 140 "aaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\na", 141 }, 142 { 143 NULL 144 } 145 }; 146 147 START_TEST(test_word_wrap) 148 int i; 149 150 for( i = 0; word_wrap_tests[i].orig && *word_wrap_tests[i].orig; i ++ ) 151 { 152 char *wrapped = word_wrap( word_wrap_tests[i].orig, word_wrap_tests[i].line_len ); 153 154 fail_unless( strcmp( word_wrap_tests[i].wrapped, wrapped ) == 0, 155 "%s (line_len = %d) should wrap to `%s', not to `%s'", 156 word_wrap_tests[i].orig, word_wrap_tests[i].line_len, 157 word_wrap_tests[i].wrapped, wrapped ); 158 159 g_free( wrapped ); 160 } 161 END_TEST 162 106 163 Suite *util_suite (void) 107 164 { … … 116 173 tcase_add_test (tc_core, test_set_url_username); 117 174 tcase_add_test (tc_core, test_set_url_username_pwd); 175 tcase_add_test (tc_core, test_word_wrap); 118 176 return s; 119 177 }
Note: See TracChangeset
for help on using the changeset viewer.