📄 fmt.c
字号:
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, 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 += 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 += t - (char *)f->to; f->to = t; if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0) return -1; } return 0;}int__quotestrfmt(int runesin, Fmt *f){ int nin, outlen; Rune *r; char *s; Quoteinfo q; nin = -1; if(f->flags&FmtPrec) nin = f->prec; if(runesin){ r = va_arg(f->args, Rune *); s = nil; }else{ s = va_arg(f->args, char *); r = nil; } if(!s && !r) return __fmtcpy(f, (void*)"<nil>", 5, 5); if(f->flush) outlen = 0x7FFFFFFF; /* if we can flush, no output limit */ else if(f->runes) outlen = (Rune*)f->stop - (Rune*)f->to; else outlen = (char*)f->stop - (char*)f->to; __quotesetup(s, r, nin, outlen, &q, f->flags&FmtSharp, f->runes);/*print("bytes in %d bytes out %d runes in %d runesout %d\n", q.nbytesin, q.nbytesout, q.nrunesin, q.nrunesout); */ if(runesin){ if(!q.quoted) return __fmtrcpy(f, r, q.nrunesin); return qstrfmt(nil, r, &q, f); } if(!q.quoted) return __fmtcpy(f, s, q.nrunesin, q.nbytesin); return qstrfmt(s, nil, &q, f);}intquotestrfmt(Fmt *f){ return __quotestrfmt(0, f);}intquoterunestrfmt(Fmt *f){ return __quotestrfmt(1, f);}voidquotefmtinstall(void){ fmtinstall('q', quotestrfmt); fmtinstall('Q', quoterunestrfmt);}int__needsquotes(char *s, int *quotelenp){ Quoteinfo q; __quotesetup(s, nil, -1, 0x7FFFFFFF, &q, 0, 0); *quotelenp = q.nbytesout; return q.quoted;}int__runeneedsquotes(Rune *r, int *quotelenp){ Quoteinfo q; __quotesetup(nil, r, -1, 0x7FFFFFFF, &q, 0, 0); *quotelenp = q.nrunesout; return q.quoted;}/* -------------- fmtrune.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"intfmtrune(Fmt *f, int r){ Rune *rt; char *t; int n; if(f->runes){ rt = (Rune*)f->to; FMTRCHAR(f, rt, f->stop, r); f->to = rt; n = 1; }else{ t = (char*)f->to; FMTRUNE(f, t, f->stop, r); n = t - (char*)f->to; f->to = t; } f->nfmt += n; return 0;}/* -------------- fmtstr.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdlib.h>// #include <stdarg.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"char*fmtstrflush(Fmt *f){ if(f->start == nil) return nil; *(char*)f->to = '\0'; f->to = f->start; return (char*)f->start;}/* -------------- fmtvprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"/* * format a string into the output buffer * designed for formats which themselves call fmt, * but ignore any width flags */intfmtvprint(Fmt *f, char *fmt, va_list args){ va_list va; int n; f->flags = 0; f->width = 0; f->prec = 0; VA_COPY(va,f->args); VA_END(f->args); VA_COPY(f->args,args); n = dofmt(f, fmt); f->flags = 0; f->width = 0; f->prec = 0; VA_END(f->args); VA_COPY(f->args,va); VA_END(va); if(n >= 0) return 0; return n;}/* -------------- fprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"intfprint(int fd, char *fmt, ...){ int n; va_list args; va_start(args, fmt); n = vfprint(fd, fmt, args); va_end(args); return n;}/* -------------- nan64.c --------------- *//* * 64-bit IEEE not-a-number routines. * This is big/little-endian portable assuming that * the 64-bit doubles and 64-bit integers have the * same byte ordering. */// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"#if defined (__APPLE__) || (__powerpc__)#define _NEEDLL#endifstatic uvlong uvnan = ((uvlong)0x7FF00000<<32)|0x00000001;static uvlong uvinf = ((uvlong)0x7FF00000<<32)|0x00000000;static uvlong uvneginf = ((uvlong)0xFFF00000<<32)|0x00000000;double__NaN(void){ uvlong *p; /* gcc complains about "return *(double*)&uvnan;" */ p = &uvnan; return *(double*)p;}int__isNaN(double d){ uvlong x; double *p; p = &d; x = *(uvlong*)p; return (ulong)(x>>32)==0x7FF00000 && !__isInf(d, 0);}double__Inf(int sign){ uvlong *p; if(sign < 0) p = &uvinf; else p = &uvneginf; return *(double*)p;}int__isInf(double d, int sign){ uvlong x; double *p; p = &d; x = *(uvlong*)p; if(sign == 0) return x==uvinf || x==uvneginf; else if(sign > 0) return x==uvinf; else return x==uvneginf;}/* -------------- pow10.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"/* * this table might overflow 127-bit exponent representations. * in that case, truncate it after 1.0e38. * it is important to get all one can from this * routine since it is used in atof to scale numbers. * the presumption is that C converts fp numbers better * than multipication of lower powers of 10. */staticdouble tab[] ={ 1.0e0, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5, 1.0e6, 1.0e7, 1.0e8, 1.0e9, 1.0e10,1.0e11,1.0e12,1.0e13,1.0e14,1.0e15,1.0e16,1.0e17,1.0e18,1.0e19, 1.0e20,1.0e21,1.0e22,1.0e23,1.0e24,1.0e25,1.0e26,1.0e27,1.0e28,1.0e29, 1.0e30,1.0e31,1.0e32,1.0e33,1.0e34,1.0e35,1.0e36,1.0e37,1.0e38,1.0e39, 1.0e40,1.0e41,1.0e42,1.0e43,1.0e44,1.0e45,1.0e46,1.0e47,1.0e48,1.0e49, 1.0e50,1.0e51,1.0e52,1.0e53,1.0e54,1.0e55,1.0e56,1.0e57,1.0e58,1.0e59, 1.0e60,1.0e61,1.0e62,1.0e63,1.0e64,1.0e65,1.0e66,1.0e67,1.0e68,1.0e69,};double__fmtpow10(int n){ int m; if(n < 0) { n = -n; if(n < (int)(sizeof(tab)/sizeof(tab[0]))) return 1/tab[n]; m = n/2; return __fmtpow10(-m) * __fmtpow10(m-n); } if(n < (int)(sizeof(tab)/sizeof(tab[0]))) return tab[n]; m = n/2; return __fmtpow10(m) * __fmtpow10(n-m);}/* -------------- print.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"/*intprint(char *fmt, ...){ int n; va_list args; va_start(args, fmt); n = vfprint(1, fmt, args); va_end(args); return n;}*//* -------------- runefmtstr.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <stdlib.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"Rune*runefmtstrflush(Fmt *f){ if(f->start == nil) return nil; *(Rune*)f->to = '\0'; f->to = f->start; return f->start;}/* -------------- runeseprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"Rune*runeseprint(Rune *buf, Rune *e, char *fmt, ...){ Rune *p; va_list args; va_start(args, fmt); p = runevseprint(buf, e, fmt, args); va_end(args); return p;}/* -------------- runesmprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"Rune*runesmprint(char *fmt, ...){ va_list args; Rune *p; va_start(args, fmt); p = runevsmprint(fmt, args); va_end(args); return p;}/* -------------- runesnprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"intrunesnprint(Rune *buf, int len, char *fmt, ...){ int n; va_list args; va_start(args, fmt); n = runevsnprint(buf, len, fmt, args); va_end(args); return n;}/* -------------- runesprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"intrunesprint(Rune *buf, char *fmt, ...){ int n; va_list args; va_start(args, fmt); n = runevsnprint(buf, 256, fmt, args); va_end(args); return n;}/* -------------- runevseprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"Rune*runevseprint(Rune *buf, Rune *e, char *fmt, va_list args){ Fmt f; if(e <= buf) return nil; f.runes = 1; f.start = buf; f.to = buf; f.stop = e - 1; f.flush = nil; f.farg = nil; f.nfmt = 0; VA_COPY(f.args,args); fmtlocaleinit(&f, nil, nil, nil); dofmt(&f, fmt); VA_END(f.args); *(Rune*)f.to = '\0'; return (Rune*)f.to;}/* -------------- runevsmprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE *//* * Plan 9 port version must include libc.h in order to * get Plan 9 debugging malloc, which sometimes returns * different pointers than the standard malloc. */#ifdef PLAN9PORT// #include <u.h>// #include <libc.h>// #include "fmtdef.h"#else// #include <stdlib.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"#endifstatic intruneFmtStrFlush(Fmt *f){ Rune *s; int n; if(f->start == nil) return 0; n = (uintptr)f->farg; n *= 2; s = (Rune*)f->start; f->start = realloc(s, sizeof(Rune)*n); if(f->start == nil){ f->farg = nil; f->to = nil; f->stop = nil; free(s); return 0; } f->farg = (void*)(uintptr)n; f->to = (Rune*)f->start + ((Rune*)f->to - s); f->stop = (Rune*)f->start + n - 1; return 1;}intrunefmtstrinit(Fmt *f){ int n; memset(f, 0, sizeof *f); f->runes = 1; n = 32; f->start = malloc(sizeof(Rune)*n); if(f->start == nil) return -1; f->to = f->start; f->stop = (Rune*)f->start + n - 1; f->flush = runeFmtStrFlush; f->farg = (void*)(uintptr)n; f->nfmt = 0; fmtlocaleinit(f, nil, nil, nil); return 0;}/* * print into an allocated string buffer */Rune*runevsmprint(char *fmt, va_list args){ Fmt f; int n; if(runefmtstrinit(&f) < 0) return nil; VA_COPY(f.args,args); n = dofmt(&f, fmt); VA_END(f.args); if(f.start == nil) return nil; if(n < 0){ free(f.start); return nil; } *(Rune*)f.to = '\0'; return (Rune*)f.start;}/* -------------- runevsnprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"intrunevsnprint(Rune *buf, int len, char *fmt, va_list args){ Fmt f; if(len <= 0) return -1; f.runes = 1; f.start = buf; f.to = buf; f.stop = buf + len - 1; f.flush = nil; f.farg = nil; f.nfmt = 0; VA_COPY(f.args,args); fmtlocaleinit(&f, nil, nil, nil); dofmt(&f, fmt); VA_END(f.args); *(Rune*)f.to = '\0'; return (Rune*)f.to - buf;}/* -------------- seprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"char*seprint(char *buf, char *e, char *fmt, ...){ char *p; va_list args; va_start(args, fmt); p = vseprint(buf, e, fmt, args); va_end(args); return p;}/* -------------- smprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"char*smprint(char *fmt, ...){ va_list args; char *p; va_start(args, fmt); p = vsmprint(fmt, args); va_end(args); return p;}/* -------------- snprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"intsnprint(char *buf, int len, char *fmt, ...){ int n; va_list args; va_start(args, fmt); n = vsnprint(buf, len, fmt, args); va_end(args); return n;}/* -------------- sprint.c --------------- *//* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */// #include <stdarg.h>// #include <fmt.h>// #include "plan9.h"// #include "fmt.h"// #include "fmtdef.h"intsprint(char *buf, char *fmt, ...){ int n; uint len; va_list args; len = 1<<30; /* big number, but sprint is deprecated anyway */ /* * on PowerPC, the stack is near the top of memory, so * we must be sure not to overflow a 32-bit pointer. */ if(buf+len < buf) len = -(uintptr)buf-1; va_start(args, fmt); n = vsnprint(buf, len, fmt, args); va_end(args);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -