diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2012-06-04 14:45:09 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2012-06-04 14:45:09 +0200 |
commit | c35545a100c08d26d49fd1c2ca10e56e6650c5c1 (patch) | |
tree | 065ef3e229e3e9f46114d7f4088510d27b173467 | |
parent | 21f6fbf545e7fa58f0eaa444001a9d25bc37c4eb (diff) | |
download | busybox-w32-c35545a100c08d26d49fd1c2ca10e56e6650c5c1.tar.gz busybox-w32-c35545a100c08d26d49fd1c2ca10e56e6650c5c1.tar.bz2 busybox-w32-c35545a100c08d26d49fd1c2ca10e56e6650c5c1.zip |
sed: code shrink
function old new delta
process_files 2181 2173 -8
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/sed.c | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/editors/sed.c b/editors/sed.c index 87fc755eb..85c84665b 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -737,6 +737,8 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line_p) | |||
737 | 737 | ||
738 | /* Now loop through, substituting for matches */ | 738 | /* Now loop through, substituting for matches */ |
739 | do { | 739 | do { |
740 | int start = G.regmatch[0].rm_so; | ||
741 | int end = G.regmatch[0].rm_eo; | ||
740 | int i; | 742 | int i; |
741 | 743 | ||
742 | match_count++; | 744 | match_count++; |
@@ -746,16 +748,16 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line_p) | |||
746 | if (sed_cmd->which_match | 748 | if (sed_cmd->which_match |
747 | && (sed_cmd->which_match != match_count) | 749 | && (sed_cmd->which_match != match_count) |
748 | ) { | 750 | ) { |
749 | for (i = 0; i < G.regmatch[0].rm_eo; i++) | 751 | for (i = 0; i < end; i++) |
750 | pipe_putc(*line++); | 752 | pipe_putc(*line++); |
751 | /* Null match? Print one more char */ | 753 | /* Null match? Print one more char */ |
752 | if (G.regmatch[0].rm_so == i && *line) | 754 | if (start == end && *line) |
753 | pipe_putc(*line++); | 755 | pipe_putc(*line++); |
754 | goto next; | 756 | goto next; |
755 | } | 757 | } |
756 | 758 | ||
757 | /* Print everything before the match */ | 759 | /* Print everything before the match */ |
758 | for (i = 0; i < G.regmatch[0].rm_so; i++) | 760 | for (i = 0; i < start; i++) |
759 | pipe_putc(line[i]); | 761 | pipe_putc(line[i]); |
760 | 762 | ||
761 | /* Then print the substitution string, | 763 | /* Then print the substitution string, |
@@ -765,25 +767,25 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line_p) | |||
765 | * second is "" before "d", third is "" after "d". | 767 | * second is "" before "d", third is "" after "d". |
766 | * Second match is NOT replaced! | 768 | * Second match is NOT replaced! |
767 | */ | 769 | */ |
768 | if (prev_match_empty || i != 0) { | 770 | if (prev_match_empty || start != 0) { |
769 | dbg("inserting replacement at %d in '%s'", i, line); | 771 | dbg("inserting replacement at %d in '%s'", start, line); |
770 | do_subst_w_backrefs(line, sed_cmd->string); | 772 | do_subst_w_backrefs(line, sed_cmd->string); |
771 | } else { | 773 | } else { |
772 | dbg("NOT inserting replacement at %d in '%s'", i, line); | 774 | dbg("NOT inserting replacement at %d in '%s'", start, line); |
773 | } | 775 | } |
774 | 776 | ||
775 | /* If matched string is empty (f.e. "c*" pattern), | 777 | /* If matched string is empty (f.e. "c*" pattern), |
776 | * copy verbatim one char after it before attempting more matches | 778 | * copy verbatim one char after it before attempting more matches |
777 | */ | 779 | */ |
778 | prev_match_empty = (G.regmatch[0].rm_eo == i); | 780 | prev_match_empty = (start == end); |
779 | if (prev_match_empty && line[i]) { | 781 | if (prev_match_empty && line[end]) { |
780 | pipe_putc(line[i]); | 782 | pipe_putc(line[end]); |
781 | G.regmatch[0].rm_eo++; | 783 | end++; |
782 | } | 784 | } |
783 | 785 | ||
784 | /* Advance past the match */ | 786 | /* Advance past the match */ |
785 | dbg("line += %d", G.regmatch[0].rm_eo); | 787 | dbg("line += %d", end); |
786 | line += G.regmatch[0].rm_eo; | 788 | line += end; |
787 | /* Flag that something has changed */ | 789 | /* Flag that something has changed */ |
788 | altered = 1; | 790 | altered = 1; |
789 | 791 | ||
@@ -798,7 +800,7 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line_p) | |||
798 | tried_at_eol = 1; | 800 | tried_at_eol = 1; |
799 | } | 801 | } |
800 | 802 | ||
801 | //maybe (G.regmatch[0].rm_eo ? REG_NOTBOL : 0) instead of unconditional REG_NOTBOL? | 803 | //maybe (end ? REG_NOTBOL : 0) instead of unconditional REG_NOTBOL? |
802 | } while (regexec(current_regex, line, 10, G.regmatch, REG_NOTBOL) != REG_NOMATCH); | 804 | } while (regexec(current_regex, line, 10, G.regmatch, REG_NOTBOL) != REG_NOMATCH); |
803 | 805 | ||
804 | /* Copy rest of string into output pipeline */ | 806 | /* Copy rest of string into output pipeline */ |