static int fmtzdotpad(Fmt *f, int n, int pt) { char *t, *s; int i; Rune *rt, *rs; if(f->runes){ rt = (Rune*)f->to; rs = (Rune*)f->stop; for(i = 0; i < n; i++){ if(i == pt){ FMTRCHAR(f, rt, rs, '.'); } FMTRCHAR(f, rt, rs, '0'); } f->nfmt += rt - (Rune*)f->to; f->to = rt; }else{ t = (char*)f->to; s = (char*)f->stop; for(i = 0; i < n; i++){ if(i == pt){ FMTCHAR(f, t, s, '.'); } FMTCHAR(f, t, s, '0'); } f->nfmt += t - (char *)f->to; f->to = t; } return 0; }
int _fmtcpy(Fmt *f, void *vm, int n, int sz) { Rune *rt, *rs, r; char *t, *s, *m, *me; ulong fl; int nc, w; m = vm; me = m + sz; w = f->width; fl = f->flags; if((fl & FmtPrec) && n > f->prec) n = f->prec; if(f->runes){ if(!(fl & FmtLeft) && _rfmtpad(f, w - n) < 0) return -1; rt = f->to; rs = f->stop; for(nc = n; nc > 0; nc--){ r = *(uchar*)m; if(r < Runeself) m++; else if((me - m) >= UTFmax || fullrune(m, me-m)) m += chartorune(&r, m); else break; FMTRCHAR(f, rt, rs, r); } f->nfmt += rt - (Rune *)f->to; f->to = rt; if(m < me) return -1; if(fl & FmtLeft && _rfmtpad(f, w - n) < 0) return -1; }else{ if(!(fl & FmtLeft) && _fmtpad(f, w - n) < 0) return -1; t = f->to; s = f->stop; for(nc = n; nc > 0; nc--){ r = *(uchar*)m; if(r < Runeself) m++; else if((me - m) >= UTFmax || fullrune(m, me-m)) m += chartorune(&r, m); else break; FMTRUNE(f, t, s, r); } f->nfmt += t - (char *)f->to; f->to = t; if(fl & FmtLeft && _fmtpad(f, w - n) < 0) return -1; } return 0; }
int _rfmtpad(Fmt *f, int n) { Rune *t, *s; int i; t = f->to; s = f->stop; for(i = 0; i < n; i++) FMTRCHAR(f, t, s, ' '); f->nfmt += t - (Rune *)f->to; f->to = t; return 0; }
int __fmtrcpy(Fmt *f, const void *vm, int n) { Rune r, *m, *me, *rt, *rs; char *t, *s; ulong fl; int w; m = (Rune*)vm; fl = f->flags; w = 0; if(fl & FmtWidth) w = f->width; if((fl & FmtPrec) && n > f->prec) n = f->prec; if(f->runes){ if(!(fl & FmtLeft) && __rfmtpad(f, w - n) < 0) return -1; rt = (Rune*)f->to; rs = (Rune*)f->stop; for(me = m + n; m < me; m++) FMTRCHAR(f, rt, rs, *m); f->nfmt += rt - (Rune *)f->to; f->to = rt; if(fl & FmtLeft && __rfmtpad(f, w - n) < 0) return -1; }else{ if(!(fl & FmtLeft) && __fmtpad(f, w - n) < 0) return -1; t = (char*)f->to; s = (char*)f->stop; for(me = m + n; m < me; m++){ r = *m; FMTRUNE(f, t, s, r); } f->nfmt += t - (char *)f->to; f->to = t; if(fl & FmtLeft && __fmtpad(f, w - n) < 0) return -1; } return 0; }
int fmtrune(Fmt *f, int r) { Rune *rt; char *t; int n; if(f->runes){ rt = f->to; FMTRCHAR(f, rt, f->stop, r); f->to = rt; n = 1; }else{ t = f->to; FMTRUNE(f, t, f->stop, r); n = t - (char*)f->to; f->to = t; } f->nfmt += n; return 0; }
int dorfmt(Fmt *f, const Rune *fmt) { Rune *rt, *rs; int r; char *t, *s; int nfmt; nfmt = f->nfmt; for(;;) { if(f->runes) { rt = f->to; rs = f->stop; while((r = *fmt++) && r != '%') { FMTRCHAR(f, rt, rs, r); } f->nfmt += rt - (Rune *)f->to; f->to = rt; if(!r) return f->nfmt - nfmt; f->stop = rs; } else { t = f->to; s = f->stop; while((r = *fmt++) && r != '%') { FMTRUNE(f, t, f->stop, r); } f->nfmt += t - (char *)f->to; f->to = t; if(!r) return f->nfmt - nfmt; f->stop = s; } fmt = __fmtdispatch(f, (Rune*)fmt, 1); if(fmt == nil) return -1; } return 0; /* not reached */ }
/* format the output into f->to and return the number of characters fmted */ int dofmt(Fmt *f, char *fmt) { Rune rune, *rt, *rs; int r; char *t, *s; int n, nfmt; nfmt = f->nfmt; for(;;){ if(f->runes){ rt = f->to; rs = f->stop; while((r = *(uchar*)fmt) && r != '%'){ if(r < Runeself) fmt++; else{ fmt += chartorune(&rune, fmt); r = rune; } FMTRCHAR(f, rt, rs, r); } fmt++; f->nfmt += rt - (Rune *)f->to; f->to = rt; if(!r) return f->nfmt - nfmt; f->stop = rs; }else{ t = f->to; s = f->stop; while((r = *(uchar*)fmt) && r != '%'){ if(r < Runeself){ FMTCHAR(f, t, s, r); fmt++; }else{ n = chartorune(&rune, fmt); if(t + n > s){ t = _fmtflush(f, t, n); if(t != nil) s = f->stop; else return -1; } while(n--) *t++ = *fmt++; } } fmt++; f->nfmt += t - (char *)f->to; f->to = t; if(!r) return f->nfmt - nfmt; f->stop = s; } fmt = _fmtdispatch(f, fmt, 0); if(fmt == nil) return -1; } }
static int qstrfmt(char *sin, Rune *rin, Quoteinfo *q, Fmt *f) { Rune r, *rm, *rme; char *t, *s, *m, *me; Rune *rt, *rs; ulong fl; int nc, w; m = sin; me = m + q->nbytesin; rm = rin; rme = rm + q->nrunesin; fl = f->flags; w = 0; if(fl & FmtWidth) w = f->width; if(f->runes){ if(!(fl & FmtLeft) && __rfmtpad(f, w - q->nrunesout) < 0) return -1; }else{ if(!(fl & FmtLeft) && __fmtpad(f, w - q->nbytesout) < 0) return -1; } t = (char*)f->to; s = (char*)f->stop; rt = (Rune*)f->to; rs = (Rune*)f->stop; if(f->runes) FMTRCHAR(f, rt, rs, '\''); else FMTRUNE(f, t, s, '\''); for(nc = q->nrunesin; nc > 0; nc--){ if(sin){ r = *(uchar*)m; if(r < Runeself) m++; else if((me - m) >= UTFmax || fullrune(m, (int)(me-m))) m += chartorune(&r, m); else break; }else{ if(rm >= rme) break; r = *(uchar*)rm++; } if(f->runes){ FMTRCHAR(f, rt, rs, r); if(r == '\'') FMTRCHAR(f, rt, rs, r); }else{ FMTRUNE(f, t, s, r); if(r == '\'') FMTRUNE(f, t, s, r); } } if(f->runes){ FMTRCHAR(f, rt, rs, '\''); USED(rs); f->nfmt += (int)(rt - (Rune *)f->to); f->to = rt; if(fl & FmtLeft && __rfmtpad(f, w - q->nrunesout) < 0) return -1; }else{ FMTRUNE(f, t, s, '\''); USED(s); f->nfmt += (int)(t - (char *)f->to); f->to = t; if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0) return -1; } return 0; }