9fans.net/go@v0.0.7/cmd/sam/util.go (about)

     1  // #include "sam.h"
     2  
     3  package main
     4  
     5  import "unicode/utf8"
     6  
     7  // cvttorunes converts bytes in b to runes in r,
     8  // returning the number of bytes processed from b,
     9  // the number of runes written to r,
    10  // and whether any null bytes were elided.
    11  // If eof is true, then any partial runes at the end of b
    12  // should be processed, and nb == len(b) at return.
    13  // Otherwise, partial runes are left behind and
    14  // nb may be up to utf8.UTFMax-1 bytes short of len(b).
    15  func cvttorunes(b []byte, r []rune, eof bool) (nb, nr int, nulls bool) {
    16  	b0 := b
    17  	for len(b) > 0 && (eof || len(b) >= utf8.UTFMax || utf8.FullRune(b)) {
    18  		rr, w := utf8.DecodeRune(b)
    19  		if rr == 0 {
    20  			nulls = true
    21  		} else {
    22  			r[nr] = rr
    23  			nr++
    24  		}
    25  		b = b[w:]
    26  	}
    27  	nb = len(b0) - len(b)
    28  	return nb, nr, nulls
    29  }
    30  
    31  func fbufalloc() []rune {
    32  	return make([]rune, RBUFSIZE)
    33  }
    34  
    35  func fbuffree(f []rune) {
    36  	// free(f)
    37  }
    38  
    39  func min(a int, b int) int {
    40  	if a < b {
    41  		return a
    42  	}
    43  	return b
    44  }