github.com/golang-haiku/go-1.4.3@v0.0.0-20190609233734-1f5ae41cc308/src/lib9/fmt/dorfmt.c (about)

     1  /*
     2   * The authors of this software are Rob Pike and Ken Thompson,
     3   * with contributions from Mike Burrows and Sean Dorward.
     4   *
     5   *     Copyright (c) 2002-2006 by Lucent Technologies.
     6   *     Portions Copyright (c) 2004 Google Inc.
     7   * 
     8   * Permission to use, copy, modify, and distribute this software for any
     9   * purpose without fee is hereby granted, provided that this entire notice
    10   * is included in all copies of any software which is or includes a copy
    11   * or modification of this software and in all copies of the supporting
    12   * documentation for such software.
    13   * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
    14   * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES 
    15   * NOR GOOGLE INC MAKE ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING 
    16   * THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
    17   */
    18  
    19  #include <u.h>
    20  #include <libc.h>
    21  #include "fmtdef.h"
    22  
    23  /* format the output into f->to and return the number of characters fmted  */
    24  
    25  /* BUG: THIS FILE IS NOT UPDATED TO THE  NEW SPEC */
    26  int
    27  dorfmt(Fmt *f, const Rune *fmt)
    28  {
    29  	Rune *rt, *rs;
    30  	Rune r;
    31  	char *t, *s;
    32  	int nfmt;
    33  
    34  	nfmt = f->nfmt;
    35  	for(;;){
    36  		if(f->runes){
    37  			rt = (Rune*)f->to;
    38  			rs = (Rune*)f->stop;
    39  			while((r = *fmt++) && r != '%'){
    40  				FMTRCHAR(f, rt, rs, r);
    41  			}
    42  			f->nfmt += (int)(rt - (Rune *)f->to);
    43  			f->to = rt;
    44  			if(!r)
    45  				return f->nfmt - nfmt;
    46  			f->stop = rs;
    47  		}else{
    48  			t = (char*)f->to;
    49  			s = (char*)f->stop;
    50  			while((r = *fmt++) && r != '%'){
    51  				FMTRUNE(f, t, f->stop, r);
    52  			}
    53  			f->nfmt += (int)(t - (char *)f->to);
    54  			f->to = t;
    55  			if(!r)
    56  				return f->nfmt - nfmt;
    57  			f->stop = s;
    58  		}
    59  
    60  		fmt = (Rune*)__fmtdispatch(f, (Rune*)fmt, 1);
    61  		if(fmt == nil)
    62  			return -1;
    63  	}
    64  }