aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--badroff.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/badroff.c b/badroff.c
index 5324aae..ab6fb69 100644
--- a/badroff.c
+++ b/badroff.c
@@ -279,17 +279,14 @@ char* cmd(void){
// normal typesetting
void wsclean(char* txt){
- size_t disp = 0;
- size_t firstws = 0;
- for (size_t i = 0; i == 0 || txt[i-1] != 0; i++){
- if ( (txt[i] == ' ' || txt[i] == '\n') && txt[i-1] != ' '){
- firstws = i;
- }
- if (txt[i] == '\n'){
- disp += i-firstws;
- }
- txt[i-disp] = txt[i];
+ int nl = 0; // count of newlines
+ size_t i;
+ for (i = strlen(txt); i > 0; i--) { // start at end [i-1] and dec
+ if (txt[i-1] == '\n') nl++; // count up newlines
+ else if (txt[i-1] != ' ') break; // and break on unpadded text
}
+ memset(txt+i, '\n', nl); // add #nl newlines at [i, i+nl)
+ txt[i+nl] = 0; // finish string
}
#define nbspchr (char) 255
void nbspclean(char* txt){