comparison libpurple/protocols/yahoo/util.c @ 28103:8660d8bc467c

Minor optimization when sending and receiving messages on Yahoo: only call strlen() on the message once instead of twice. We already had a variable for it... now we just initialize it earlier. I also gave the variables more descriptive names
author Mark Doliner <mark@kingant.net>
date Fri, 31 Jul 2009 08:47:42 +0000
parents e23c7f6fb32d
children 70219821df98
comparison
equal deleted inserted replaced
28102:e23c7f6fb32d 28103:8660d8bc467c
336 } 336 }
337 } 337 }
338 338
339 char *yahoo_codes_to_html(const char *x) 339 char *yahoo_codes_to_html(const char *x)
340 { 340 {
341 size_t x_len;
341 GString *s, *tmp; 342 GString *s, *tmp;
342 int i, j, xs; 343 int i, j;
343 gboolean no_more_end_tags = FALSE; /* s/endtags/closinganglebrackets */ 344 gboolean no_more_end_tags = FALSE; /* s/endtags/closinganglebrackets */
344 char *match; 345 char *match;
345 346
346 s = g_string_sized_new(strlen(x)); 347 x_len = strlen(x);
347 348 s = g_string_sized_new(x_len);
348 for (i = 0, xs = strlen(x); i < xs; i++) { 349
350 for (i = 0; i < x_len; i++) {
349 if ((x[i] == 0x1b) && (x[i+1] == '[')) { 351 if ((x[i] == 0x1b) && (x[i+1] == '[')) {
350 j = i + 1; 352 j = i + 1;
351 353
352 while (j++ < xs) { 354 while (j++ < x_len) {
353 if (x[j] != 'm') 355 if (x[j] != 'm')
354 continue; 356 continue;
355 else { 357 else {
356 tmp = g_string_new_len(x + i + 2, j - i - 2); 358 tmp = g_string_new_len(x + i + 2, j - i - 2);
357 if (tmp->str[0] == '#') 359 if (tmp->str[0] == '#')
372 } 374 }
373 375
374 } else if (!no_more_end_tags && (x[i] == '<')) { 376 } else if (!no_more_end_tags && (x[i] == '<')) {
375 j = i; 377 j = i;
376 378
377 while (j++ < xs) { 379 while (j++ < x_len) {
378 if (x[j] != '>') 380 if (x[j] != '>')
379 if (j == xs) { 381 if (j == x_len) {
380 g_string_append(s, "&lt;"); 382 g_string_append(s, "&lt;");
381 no_more_end_tags = TRUE; 383 no_more_end_tags = TRUE;
382 } 384 }
383 else 385 else
384 continue; 386 continue;
632 634
633 } 635 }
634 636
635 char *yahoo_html_to_codes(const char *src) 637 char *yahoo_html_to_codes(const char *src)
636 { 638 {
637 int i, j, len; 639 GQueue *colors, *tags;
640 size_t src_len;
641 int i, j;
638 GString *dest; 642 GString *dest;
639 char *esc; 643 char *esc;
640 GQueue *colors, *tags;
641 GQueue *ftattr = NULL; 644 GQueue *ftattr = NULL;
642 gboolean no_more_specials = FALSE; 645 gboolean no_more_specials = FALSE;
643 646
644 colors = g_queue_new(); 647 colors = g_queue_new();
645 tags = g_queue_new(); 648 tags = g_queue_new();
646 dest = g_string_sized_new(strlen(src)); 649 src_len = strlen(src);
647 650 dest = g_string_sized_new(src_len);
648 for (i = 0, len = strlen(src); i < len; i++) { 651
652 for (i = 0; i < src_len; i++) {
649 653
650 if (!no_more_specials && src[i] == '<') { 654 if (!no_more_specials && src[i] == '<') {
651 j = i; 655 j = i;
652 656
653 while (1) { 657 while (1) {
654 j++; 658 j++;
655 659
656 if (j >= len) { /* no '>' */ 660 if (j >= src_len) { /* no '>' */
657 g_string_append_c(dest, src[i]); 661 g_string_append_c(dest, src[i]);
658 no_more_specials = TRUE; 662 no_more_specials = TRUE;
659 break; 663 break;
660 } 664 }
661 665
680 if (src[j] == ' ') { 684 if (src[j] == ' ') {
681 if (!g_ascii_strncasecmp(&src[i+1], "BODY", j - i - 1)) { 685 if (!g_ascii_strncasecmp(&src[i+1], "BODY", j - i - 1)) {
682 char *t = strchr(&src[j], '>'); 686 char *t = strchr(&src[j], '>');
683 if (!t) { 687 if (!t) {
684 g_string_append(dest, &src[i]); 688 g_string_append(dest, &src[i]);
685 i = len; 689 i = src_len;
686 break; 690 break;
687 } else { 691 } else {
688 i = t - src; 692 i = t - src;
689 break; 693 break;
690 } 694 }
691 } else if (!g_ascii_strncasecmp(&src[i+1], "A HREF=\"", j - i - 1)) { 695 } else if (!g_ascii_strncasecmp(&src[i+1], "A HREF=\"", j - i - 1)) {
692 j += 7; 696 j += 7;
693 g_string_append(dest, "\033[lm"); 697 g_string_append(dest, "\033[lm");
694 while (1) { 698 while (1) {
695 g_string_append_c(dest, src[j]); 699 g_string_append_c(dest, src[j]);
696 if (++j >= len) { 700 if (++j >= src_len) {
697 i = len; 701 i = src_len;
698 break; 702 break;
699 } 703 }
700 if (src[j] == '"') { 704 if (src[j] == '"') {
701 g_string_append(dest, "\033[xlm"); 705 g_string_append(dest, "\033[xlm");
702 while (1) { 706 while (1) {
703 if (++j >= len) { 707 if (++j >= src_len) {
704 i = len; 708 i = src_len;
705 break; 709 break;
706 } 710 }
707 if (!g_ascii_strncasecmp(&src[j], "</A>", 4)) { 711 if (!g_ascii_strncasecmp(&src[j], "</A>", 4)) {
708 j += 3; 712 j += 3;
709 break; 713 break;
713 break; 717 break;
714 } 718 }
715 } 719 }
716 } else if (!g_ascii_strncasecmp(&src[i+1], "SPAN", j - i - 1)) { /* drop span tags */ 720 } else if (!g_ascii_strncasecmp(&src[i+1], "SPAN", j - i - 1)) { /* drop span tags */
717 while (1) { 721 while (1) {
718 if (++j >= len) { 722 if (++j >= src_len) {
719 g_string_append(dest, &src[i]); 723 g_string_append(dest, &src[i]);
720 i = len; 724 i = src_len;
721 break; 725 break;
722 } 726 }
723 if (src[j] == '>') { 727 if (src[j] == '>') {
724 i = j; 728 i = j;
725 break; 729 break;
726 } 730 }
727 } 731 }
728 } else if (g_ascii_strncasecmp(&src[i+1], "FONT", j - i - 1)) { /* not interested! */ 732 } else if (g_ascii_strncasecmp(&src[i+1], "FONT", j - i - 1)) { /* not interested! */
729 while (1) { 733 while (1) {
730 if (++j >= len) { 734 if (++j >= src_len) {
731 g_string_append(dest, &src[i]); 735 g_string_append(dest, &src[i]);
732 i = len; 736 i = src_len;
733 break; 737 break;
734 } 738 }
735 if (src[j] == '>') { 739 if (src[j] == '>') {
736 g_string_append_len(dest, &src[i], j - i + 1); 740 g_string_append_len(dest, &src[i], j - i + 1);
737 i = j; 741 i = j;
738 break; 742 break;
739 } 743 }
740 } 744 }
741 } else { /* yay we have a font tag */ 745 } else { /* yay we have a font tag */
742 _parse_font_tag(src, dest, &i, &j, len, colors, tags, ftattr); 746 _parse_font_tag(src, dest, &i, &j, src_len, colors, tags, ftattr);
743 } 747 }
744 748
745 break; 749 break;
746 } 750 }
747 751
798 } 802 }
799 803
800 } 804 }
801 805
802 } else { 806 } else {
803 if (((len - i) >= 4) && !strncmp(&src[i], "&lt;", 4)) { 807 if (((src_len - i) >= 4) && !strncmp(&src[i], "&lt;", 4)) {
804 g_string_append_c(dest, '<'); 808 g_string_append_c(dest, '<');
805 i += 3; 809 i += 3;
806 } else if (((len - i) >= 4) && !strncmp(&src[i], "&gt;", 4)) { 810 } else if (((src_len - i) >= 4) && !strncmp(&src[i], "&gt;", 4)) {
807 g_string_append_c(dest, '>'); 811 g_string_append_c(dest, '>');
808 i += 3; 812 i += 3;
809 } else if (((len - i) >= 5) && !strncmp(&src[i], "&amp;", 5)) { 813 } else if (((src_len - i) >= 5) && !strncmp(&src[i], "&amp;", 5)) {
810 g_string_append_c(dest, '&'); 814 g_string_append_c(dest, '&');
811 i += 4; 815 i += 4;
812 } else if (((len - i) >= 6) && !strncmp(&src[i], "&quot;", 6)) { 816 } else if (((src_len - i) >= 6) && !strncmp(&src[i], "&quot;", 6)) {
813 g_string_append_c(dest, '"'); 817 g_string_append_c(dest, '"');
814 i += 5; 818 i += 5;
815 } else if (((len - i) >= 6) && !strncmp(&src[i], "&apos;", 6)) { 819 } else if (((src_len - i) >= 6) && !strncmp(&src[i], "&apos;", 6)) {
816 g_string_append_c(dest, '\''); 820 g_string_append_c(dest, '\'');
817 i += 5; 821 i += 5;
818 } else { 822 } else {
819 g_string_append_c(dest, src[i]); 823 g_string_append_c(dest, src[i]);
820 } 824 }