📄 out.c
字号:
if (outbuf[first] >= ' ') count++; first++; } return count;}Static void makeedit(pos, ch)int pos, ch;{ editpos[numedits] = pos; editold[numedits] = outbuf[pos]; editnew[numedits] = ch; outbuf[pos] = ch; numedits++;}Static void unedit(){ numedits--; outbuf[editpos[numedits]] = editold[numedits];}Static int parencount(par)Paren *par;{ int count = 0; while (par) { count++; par = par->next; } return count;}/* The following routine explores the tree of all possible line breaks, pruning according to the fact that "badness" and "extra" are increasing functions. The object is to find the set of breaks and indentation with the least total badness. (The basic idea was borrowed from Donald Knuth's "TeX".)*//* As an additional optimization, the concept of a "simple" line is used, i.e., a line with a structure such that the best break is sure to be the straightforward left-to-right fill used by a simple word processor. (For example, a long line with nothing but comma-breakpoints is simple.) Also, if the line is very long a few initial random passes are made just to scope out an estimate of the eventual badness of the line. This combined with the badness cull helps keep the breaker from using up its quota of tries before even considering a key break point! Note that when randombreaks==1, each call to trybreakline is fast since only one branch is taken at each decision point.*/#define randtest(lim) (!randombreaks ? -1 \ : randombreaks > 0 \ ? parencount(parens) < randombreaks-1 \ : randombreaks == -2 \ ? 0 \ : (rand() & 0xfff) < (lim))#define TB_BRKCOUNT 0x0ff#define TB_FORCEBRK 0x100#define TB_NOBREAK 0x200#define TB_ALREADYBRK 0x400#define TB_ALLORNONE 0x800#define TB_EXTRAIND 0x1000#define TB_EXTRAIND2 0x2000#define TBR_ABORT 0x1#define TBR_SIMPLE 0x2#define TBR_REACHED 0x4Static int trybreakline(pos, count, indent, badness, flags, parens)int pos, count, indent, flags;double badness;Paren *parens;{ int edited; int i, j, jmask, f, pos2, r; char ch, ch2, closing; double extra, penalty; Paren *pp;#if 0 { static double save = -1; if (showbadlimit != save) printf("Showbadlimit = %g\n", showbadlimit); save = showbadlimit; }#endif if (numalts >= maxalts) return TBR_ABORT; jmask = -1; for (;;) { if (numbreaks >= MAXBREAKS) { /* must leave rest of line alone */ count += measurechars(pos, outbufpos-1); pos = outbufpos; } i = count - breakcount[numbreaks-1] + breakindent[numbreaks-1] - linewidth; if (i <= 0) extra = 0; else { if (i + linewidth >= maxlinewidth || randombreaks == -2) return 0; /* absolutely too long! */ extra = overwidepenalty + ((long)i*i)*overwideextrapenalty; jmask &= ~TBR_SIMPLE; if (extra < 0) extra = 0; } if ((testinglinebreaker > 1 && showbadlimit > 0) ? (badness + extra >= showbadlimit) : (badness + extra >= bestbadness)) { numalts++; return 0; /* no point in going on, badness will only increase */ } if (pos >= outbufpos) break; if (parens && pos >= parens->pos) { indent = parens->indent; flags = parens->flags; parens = parens->next; } ch = outbuf[pos++]; if (ch >= ' ') count++; switch (ch) { case '(': case '[': case '\003': /* "invisible open paren" */ case '\010': /* "semi-invisible open paren" */ pos2 = pos - 1; if (!readparens(&pos2, 1)) break; i = measurechars(pos, pos2); if (count + i - breakcount[numbreaks-1] + breakindent[numbreaks-1] <= linewidth) { /* it fits, so leave it on one line */#if 0 /* I don't think this is necessary */ while (pos <= pos2) { if (outbuf[pos] == '\002') { jmask &= ~TBR_SIMPLE; pos = pos2 + 1; break; } pos++; }#else pos = pos2 + 1;#endif count += i; break; } pp = ALLOC(1, Paren, parens); /* doesn't fit, try poss breaks */ pp->next = parens; pp->pos = pos2; pp->indent = indent; pp->qmindent = indent; pp->flags = flags; parens = pp; flags = 0; if (ch == '\010' && /* change to real parens when broken */ numedits+1 < MAXEDITS) { /* (assume it will be broken!) */ makeedit(pos-1, '('); makeedit(pos2, ')'); count++; /* count the new open paren */ edited = 1; } else edited = 0; i = breakindent[numbreaks-1] + count - breakcount[numbreaks-1]; if (i <= thisindent) r = 0; /* e.g., don't break top-level assignments */ else if (i == indent + extraindent) r = 1; /* don't waste time on identical operations */ else r = randtest(0xc00); if (r != 0) { j = trybreakline(pos, count, i, badness + MAX(- extraindentpenalty,0), flags, parens); } else j = 0; if (r != 1) { j &= trybreakline(pos, count, indent + extraindent, badness + MAX(extraindentpenalty,0), flags | TB_EXTRAIND, parens); } if (!randombreaks && bumpindent != 0) { if (i == thisfutureindent) { j &= trybreakline(pos, count, i + bumpindent, badness + MAX(- extraindentpenalty,0) + bumpindentpenalty, flags, parens); } else if (indent + extraindent == thisfutureindent) { j &= trybreakline(pos, count, indent + extraindent + bumpindent, badness + MAX(extraindentpenalty,0) + bumpindentpenalty, flags | TB_EXTRAIND, parens); } } if (edited) { unedit(); unedit(); } FREE(pp); return j & jmask; case '\005': /* "set left margin" */ indent = breakindent[numbreaks-1] + count - breakcount[numbreaks-1]; break; case '\007': /* "all-or-none breaking" */ flags |= TB_ALLORNONE; break; case '\001': /* "possible break point" */ case '\002': /* "break point in parens" */ case '\006': /* "forced break point" */ case '\011': /* "break point after special args" */ case '\017': /* "break point for final : operator" */ /* first try the non-breaking case */ if (ch != '\001' && ch != '\006') jmask &= ~TBR_SIMPLE; if ((flags & TB_BRKCOUNT) != TB_BRKCOUNT) flags++; /* increment TB_BRKCOUNT field */ if (outbuf[pos] == '?' && parens) parens->qmindent = breakindent[numbreaks-1] + count - breakcount[numbreaks-1]; j = TBR_REACHED; if (ch == '\006' || (flags & TB_FORCEBRK)) { /* don't try the non-breaking case */ } else { if (ch == '\011') { i = breakindent[numbreaks-1] + count - breakcount[numbreaks-1] + 2; } else { i = indent; } f = flags; if (f & TB_ALLORNONE) f |= TB_NOBREAK; r = randtest(0x800); if (r != 1 || (flags & TB_NOBREAK)) { j = trybreakline(pos, count, i, badness, f, parens) & jmask; if (randombreaks == -2 && !(j & TBR_REACHED)) { r = -1; j |= TBR_REACHED; } if (r == 0 || (j & TBR_SIMPLE)) flags |= TB_NOBREAK; } } if (flags & TB_NOBREAK) return j; if (flags & TB_ALLORNONE) flags |= TB_FORCEBRK; if (flags & TB_EXTRAIND) { flags &= ~TB_EXTRAIND; flags |= TB_EXTRAIND2; } /* now try breaking here */ if (ch == '\017') indent = parens->qmindent; if (indent < 0) indent = 0; breakpos[numbreaks] = pos; breakcount[numbreaks] = count; breakindent[numbreaks] = indent; breakparen[numbreaks] = parens ? parens->pos : 0; numbreaks++; penalty = extra; if (indent == thisfutureindent) { i = pos; while (i < outbufpos-1 && outbuf[i] <= ' ') i++; ch2 = outbuf[i]; /* first character on next line */ if (ch2 != '(' && ch2 != '!' && ch2 != '~' && ch2 != '-') penalty += nobumpindentpenalty; } switch (ch) { case '\001': penalty += commabreakpenalty; if (flags & TB_ALREADYBRK) penalty += morebreakpenalty; break; case '\011': i = parencount(parens); penalty += specialargbreakpenalty + commabreakextrapenalty*i; break; case '\002': case '\017': i = parencount(parens); if (outbuf[pos-2] == '(') penalty += parenbreakpenalty + parenbreakextrapenalty*i; else if (outbuf[pos-2] == ',') penalty += commabreakpenalty + commabreakextrapenalty*i; else if (((outbuf[pos] == '&' || outbuf[pos] == '|') && outbuf[pos+1] == outbuf[pos]) || ((outbuf[pos-3] == '&' || outbuf[pos-3] == '|') && outbuf[pos-3] == outbuf[pos-2])) penalty += logbreakpenalty + logbreakextrapenalty*i; else if (((outbuf[pos] == '<' || outbuf[pos] == '>') && outbuf[pos+1] != outbuf[pos]) || ((outbuf[pos] == '=' || outbuf[pos] == '!') && outbuf[pos+1] == '=') || ((outbuf[pos-2] == '<' || outbuf[pos-2] == '>') && outbuf[pos-3] != outbuf[pos-2]) || ((outbuf[pos-3] == '<' || outbuf[pos-3] == '>' || outbuf[pos-3] == '=' || outbuf[pos-3] == '!') && outbuf[pos-2] == '=')) penalty += relbreakpenalty + relbreakextrapenalty*i; else if (outbuf[pos-2] == '=') penalty += assignbreakpenalty + assignbreakextrapenalty*i; else if (outbuf[pos] == '?') { penalty += qmarkbreakpenalty + qmarkbreakextrapenalty*i; if (parens) parens->qmindent = breakindent[numbreaks-1] + count - breakcount[numbreaks-1]; } else penalty += opbreakpenalty + opbreakextrapenalty*i; if (outbuf[pos-2] == '-') penalty += exhyphenpenalty; if (flags & TB_ALREADYBRK) penalty += morebreakpenalty + morebreakextrapenalty*i; break; default: break; } while (pos < outbufpos && outbuf[pos] == '\013') { penalty += wrongsidepenalty; pos++; } penalty -= earlybreakpenalty*(flags & TB_BRKCOUNT); /* the following test is not quite right, but it's not too bad. */ if (breakindent[numbreaks-2] == breakindent[numbreaks-1] && breakparen[numbreaks-2] != breakparen[numbreaks-1]) penalty += sameindentpenalty;#if 0 else if (ch == '\002' && parens && /*don't think this is needed*/ parens->indent == breakindent[numbreaks-1] && parens->pos != breakparen[numbreaks-1]) penalty += sameindentpenalty + 0.001; /***/#endif penalty += (breakindent[numbreaks-1] - thisindent) * indentamountpenalty; if (penalty < 1) penalty = 1; pos2 = pos; while (pos2 < outbufpos && outbuf[pos2] == ' ') pos2++; flags |= TB_ALREADYBRK; j = trybreakline(pos2, count, indent, badness + penalty, flags, parens) & jmask; numbreaks--; return j; case '\016': /* "hang-indent operator" */ if (count <= breakcount[numbreaks-1] + 2 && !(flags & TB_EXTRAIND2)) { breakindent[numbreaks-1] -= count - breakcount[numbreaks-1]; pos2 = pos; while (pos2 < outbufpos && outbuf[pos2] <= ' ') { if (outbuf[pos2] == ' ') breakindent[numbreaks-1]--; pos2++; } } break; case '"': case '\'': closing = ch; while (pos < outbufpos && outbuf[pos] != closing) { if (outbuf[pos] == '\\') pos++, count++; pos++; count++; } if (pos >= outbufpos) { intwarning("output", "Mismatched quotes [248]"); continue; } pos++; count++; break; case '/': if (pos < outbufpos && (outbuf[pos] == '*' || (outbuf[pos] == '/' && slashslash))) { count += measurechars(pos, outbufpos-1); pos = outbufpos; /* assume comment is at end of line */ } break; } } numalts++; badness += extra; if (testinglinebreaker > 1) { if (badness >= bestbadness && (badness < showbadlimit || showbadlimit == 0)) { fprintf(outf, "\n#if 0 /* rejected #%ld, badness = %g >= %g */\n", numalts, badness, bestbadness); flush_outbuf(numbreaks, breakpos, breakindent, numedits, editpos, editold, editnew); fprintf(outf, "#endif\n"); return TBR_SIMPLE & jmask; } else if ((bestbadness < showbadlimit || showbadlimit == 0) && bestnumalts > 0) { fprintf(outf, "\n#if 0 /* rejected #%ld, badness = %g > %g */\n", bestnumalts, bestbadness, badness); flush_outbuf(bestnumbreaks, bestbreakpos, bestbreakindent, bestnumedits, besteditpos, besteditold, besteditnew); fprintf(outf, "#endif\n"); } } bestbadness = badness; bestnumbreaks = numbreaks; bestnumalts = numalts; for (i = 0; i < numbreaks; i++) { bestbreakpos[i] = breakpos[i]; bestbreakindent[i] = breakindent[i]; } bestnumedits = numedits; for (i = 0; i < numedits; i++) { besteditpos[i] = editpos[i]; besteditold[i] = editold[i]; besteditnew[i] = editnew[i]; } return TBR_SIMPLE & jmask;}int parse_breakstr(cp)char *cp;{ short val = 0; if (isdigit(*cp)) return atoi(cp); while (*cp && !isspace(*cp) && *cp != '}') { switch (toupper(*cp++)) { case 'N': case '=': break; case 'L': val |= BRK_LEFT; break; case 'R': val |= BRK_RIGHT; break; case 'H': val |= BRK_HANG | BRK_LEFT; break; case '>': if (val & BRK_LEFT) val |= BRK_LPREF; else if (val & BRK_RIGHT) val |= BRK_RPREF; else return -1; break; case '<': if (val & BRK_LEFT) val |= BRK_RPREF; else if (val & BRK_RIGHT) val |= BRK_LPREF; else return -1; break; case 'A': val |= BRK_ALLNONE; break; default: return -1; } } return val;}long getcurtime(){#if USETIME static unsigned long starttime = 0; struct timeval t; struct timezone tz; gettimeofday(&t, &tz); if (starttime == 0) starttime = t.tv_sec; t.tv_sec -= starttime; return (t.tv_sec*1000 + t.tv_usec/1000);#else static unsigned long starttime = 0; if (!starttime) starttime = time(NULL); return (time(NULL) - starttime) * 1000;#endif}void output(msg)register char *msg;{ unsigned char ch; double savelimit; int i, savemaxlw, maxdp; long alts; long time0, time0a, time1; debughook(); if (outputmode) { end_source(); while ((ch = *msg++) != 0) { if (ch >= ' ') { putc_outf(ch); } else if (ch == '\n' || ch == '\r') { putc_outf(ch); flush_outfilebuf(); outf_lnum++; } } return; } while ((ch = *msg++) != 0) { if (ch == '\n' || ch == '\r') {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -