// Bytes returns z's representation as a big-endian byte array.
func (z *Int) Bytes() []byte {
- b := make([]byte, (z.Len() + 7)/8);
+ b := make([]byte, (z.Len()+7)/8);
n := C.size_t(len(b));
C.mpz_export(unsafe.Pointer(&b[0]), &n, 1, 1, 1, 0, &z.i[0]);
return b[0:n];
machine,
"-Wall", // many warnings
"-Werror", // warnings are errors
- "-o"+tmp, // write object to tmp
+ "-o" + tmp, // write object to tmp
"-gdwarf-2", // generate DWARF v2 debugging symbols
"-c", // do not link
"-xc", // input language is C
s = ss
}
s = strings.Join(strings.Split(s, " ", 0), ""); // strip spaces
- name := c.Ident("_C_"+s);
+ name := c.Ident("_C_" + s);
c.typedef[name.Value] = t.Go;
t.Go = name;
}
// Struct conversion
func (c *typeConv) Struct(dt *dwarf.StructType) (expr *ast.StructType, csyntax string, align int64) {
csyntax = "struct { ";
- fld := make([]*ast.Field, 0, 2*len(dt.Field) + 1); // enough for padding around every field
+ fld := make([]*ast.Field, 0, 2*len(dt.Field)+1); // enough for padding around every field
off := int64(0);
for _, f := range dt.Field {
if f.ByteOffset > off {
- fld = c.pad(fld, f.ByteOffset - off);
+ fld = c.pad(fld, f.ByteOffset-off);
off = f.ByteOffset;
}
t := c.Type(f.Type);
}
}
if off < dt.ByteSize {
- fld = c.pad(fld, dt.ByteSize - off);
+ fld = c.pad(fld, dt.ByteSize-off);
off = dt.ByteSize;
}
if off != dt.ByteSize {
)
func creat(name string) *os.File {
- f, err := os.Open(name, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0666);
+ f, err := os.Open(name, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666);
if err != nil {
fatal("%s", err)
}
for name, def := range p.Funcdef {
// Go func declaration.
d := &ast.FuncDecl{
- Name: &ast.Ident{Value: "_C_"+name},
+ Name: &ast.Ident{Value: "_C_" + name},
Type: def.Go,
};
printer.Fprint(fgo2, d);
off := int64(0);
npad := 0;
for i, t := range def.Params {
- if off % t.Align != 0 {
- pad := t.Align - off % t.Align;
+ if off%t.Align != 0 {
+ pad := t.Align - off%t.Align;
structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad);
off += pad;
npad++;
structType += fmt.Sprintf("\t\t%s p%d;\n", t.C, i);
off += t.Size;
}
- if off % p.PtrSize != 0 {
- pad := p.PtrSize - off % p.PtrSize;
+ if off%p.PtrSize != 0 {
+ pad := p.PtrSize - off%p.PtrSize;
structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad);
off += pad;
npad++;
}
if t := def.Result; t != nil {
- if off % t.Align != 0 {
- pad := t.Align - off % t.Align;
+ if off%t.Align != 0 {
+ pad := t.Align - off%t.Align;
structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad);
off += pad;
npad++;
structType += fmt.Sprintf("\t\t%s r;\n", t.C);
off += t.Size;
}
- if off % p.PtrSize != 0 {
- pad := p.PtrSize - off % p.PtrSize;
+ if off%p.PtrSize != 0 {
+ pad := p.PtrSize - off%p.PtrSize;
structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad);
off += pad;
npad++;
// j = end of EBNF text (or end of source)
j := bytes.Index(src[i:len(src)], close); // close marker
if j < 0 {
- j = len(src)-i
+ j = len(src) - i
}
j += i;
j := -1; // index+1 of first period that is followed by white space
prev := 'A';
for k, ch := range s {
- k1 := k+1;
+ k1 := k + 1;
if ch == '.' {
if i < 0 {
i = k1 // first period
if text == "" {
// no package documentation yet; take the first found
file, err := parser.ParseFile(pathutil.Join(path, d.Name), nil,
- parser.ParseComments | parser.PackageClauseOnly);
+ parser.ParseComments|parser.PackageClauseOnly);
if err == nil &&
// Also accept fakePkgName, so we get synopses for commmands.
// Note: This may lead to incorrect results if there is a
// determine number of entries n and maximum height
n := 0;
- minDepth := 1<<30; // infinity
+ minDepth := 1 << 30; // infinity
maxDepth := 0;
for d := range root.iter(skipRoot) {
n++;
maxDepth = d.Depth
}
}
- maxHeight := maxDepth-minDepth+1;
+ maxHeight := maxDepth - minDepth + 1;
if n == 0 {
return nil
for i, r := range errors {
// Should always be true, but check for robustness.
if 0 <= r.Pos.Offset && r.Pos.Offset <= len(src) {
- errs[i].src = src[offs : r.Pos.Offset];
+ errs[i].src = src[offs:r.Pos.Offset];
offs = r.Pos.Offset;
}
errs[i].line = r.Pos.Line;
// Template formatter for "time" format.
func timeFmt(w io.Writer, x interface{}, format string) {
// note: os.Dir.Mtime_ns is in uint64 in ns!
- template.HTMLEscape(w, strings.Bytes(time.SecondsToLocalTime(int64(x.(uint64) / 1e9)).String()))
+ template.HTMLEscape(w, strings.Bytes(time.SecondsToLocalTime(int64(x.(uint64)/1e9)).String()))
}
_, ts := fsTree.get();
d := Data{
Title: title,
- Timestamp: uint64(ts)*1e9, // timestamp in ns
+ Timestamp: uint64(ts) * 1e9, // timestamp in ns
Query: query,
Content: content,
};
if err := parseerrorHTML.Execute(errors, &buf); err != nil {
log.Stderrf("parseerrorHTML.Execute: %s", err)
}
- servePage(c, "Parse errors in source file " + errors.filename, "", buf.Bytes());
+ servePage(c, "Parse errors in source file "+errors.filename, "", buf.Bytes());
}
writeNode(&buf, prog, true, styler);
fmt.Fprintln(&buf, "</pre>");
- servePage(c, "Source file " + r.URL.Path, "", buf.Bytes());
+ servePage(c, "Source file "+r.URL.Path, "", buf.Bytes());
}
template.HTMLEscape(&buf, src);
fmt.Fprintln(&buf, "</pre>");
- servePage(c, "Text file " + path, "", buf.Bytes());
+ servePage(c, "Text file "+path, "", buf.Bytes());
}
log.Stderrf("dirlistHTML.Execute: %s", err)
}
- servePage(c, "Directory " + path, "", buf.Bytes());
+ servePage(c, "Directory "+path, "", buf.Bytes());
}
stop := time.Nanoseconds();
searchIndex.set(index);
if *verbose {
- secs := float64((stop-start)/1e6)/1e3;
+ secs := float64((stop-start)/1e6) / 1e3;
nwords, nspots := index.Size();
log.Stderrf("index updated (%gs, %d unique words, %d spots)", secs, nwords, nspots);
}
}
- time.Sleep(1*60e9); // try once a minute
+ time.Sleep(1 * 60e9); // try once a minute
}
}
// makeSpotInfo makes a SpotInfo.
func makeSpotInfo(kind SpotKind, lori int, isIndex bool) SpotInfo {
// encode lori: bits [4..32)
- x := SpotInfo(lori)<<4;
+ x := SpotInfo(lori) << 4;
if int(x>>4) != lori {
// lori value doesn't fit - since snippet indices are
// most certainly always smaller then 1<<28, this can
x = 0
}
// encode kind: bits [1..4)
- x |= SpotInfo(kind)<<1;
+ x |= SpotInfo(kind) << 1;
// encode isIndex: bit 0
if isIndex {
x |= 1
}
-func (x SpotInfo) Kind() SpotKind { return SpotKind(x>>1&7) }
-func (x SpotInfo) Lori() int { return int(x>>4) }
+func (x SpotInfo) Kind() SpotKind { return SpotKind(x >> 1 & 7) }
+func (x SpotInfo) Lori() int { return int(x >> 4) }
func (x SpotInfo) IsIndex() bool { return x&1 != 0 }
// reduce the list of Spots into a list of KindRuns
var h1 RunList;
- h1.Vector.Init(j-i);
+ h1.Vector.Init(j - i);
k := 0;
for ; i < j; i++ {
h1.Set(k, h0.At(i).(Spot).Info);
syncDelay.set(*syncMin) // revert to regular sync schedule
default:
// sync failed because of an error - back off exponentially, but try at least once a day
- syncDelay.backoff(24*60)
+ syncDelay.backoff(24 * 60)
}
}
if *verbose {
log.Stderrf("next sync in %dmin", delay.(int))
}
- time.Sleep(int64(delay.(int))*60e9);
+ time.Sleep(int64(delay.(int)) * 60e9);
}
}();
}
func (p *ebnfParser) flush() {
- p.out.Write(p.src[p.prev : p.pos.Offset]);
+ p.out.Write(p.src[p.prev:p.pos.Offset]);
p.prev = p.pos.Offset;
}
// make the error message more specific
msg += ", found '" + p.tok.String() + "'";
if p.tok.IsLiteral() {
- msg += " "+string(p.lit)
+ msg += " " + string(p.lit)
}
}
p.Error(pos, msg);
func (p *ebnfParser) expect(tok token.Token) token.Position {
pos := p.pos;
if p.tok != tok {
- p.errorExpected(pos, "'" + tok.String() + "'")
+ p.errorExpected(pos, "'"+tok.String()+"'")
}
p.next(); // make progress in any case
return pos;
// i: beginning of EBNF text (or end of source)
i := bytes.Index(src, openTag);
if i < 0 {
- i = n-len(openTag)
+ i = n - len(openTag)
}
i += len(openTag);
// j: end of EBNF text (or end of source)
j := bytes.Index(src[i:n], closeTag); // close marker
if j < 0 {
- j = n-i
+ j = n - i
}
j += i;
// flags for a rule having an action, and being reduced
const (
- ACTFLAG = 1<<(iota+2);
+ ACTFLAG = 1 << (iota + 2);
REDFLAG;
)
// parse tokens
const (
- IDENTIFIER = PRIVATE+iota;
+ IDENTIFIER = PRIVATE + iota;
MARK;
TERM;
LEFT;
const NOMORE = -1000
// macros for getting associativity and precedence levels
-func ASSOC(i int) int { return i&3 }
+func ASSOC(i int) int { return i & 3 }
-func PLEVEL(i int) int { return (i>>4)&077 }
+func PLEVEL(i int) int { return (i >> 4) & 077 }
-func TYPE(i int) int { return (i>>10)&077 }
+func TYPE(i int) int { return (i >> 10) & 077 }
// macros for setting associativity and precedence levels
-func SETASC(i, j int) int { return i|j }
+func SETASC(i, j int) int { return i | j }
-func SETPLEV(i, j int) int { return i|(j<<4) }
+func SETPLEV(i, j int) int { return i | (j << 4) }
-func SETTYPE(i, j int) int { return i|(j<<10) }
+func SETTYPE(i, j int) int { return i | (j << 10) }
// I/O descriptors
var finput *bufio.Reader // input file
setup(); // initialize and read productions
- tbitset = (ntokens+32)/32;
+ tbitset = (ntokens + 32) / 32;
cpres(); // make table of which productions yield a given nonterminal
cempty(); // make a table of which nonterminals can match the empty string
cpfir(); // make a table of firsts of nonterminals
case LEFT, BINARY, RIGHT, TERM:
// nonzero means new prec. and assoc.
- lev := t-TERM;
+ lev := t - TERM;
if lev != 0 {
i++
}
func moreprod() {
n := len(prdptr);
if nprod >= n {
- nn := n+PRODINC;
+ nn := n + PRODINC;
aprod := make([][]int, nn);
alevprd := make([]int, nn);
arlines := make([]int, nn);
nontrst = anontrst;
}
nontrst[nnonter] = Symb{s, 0};
- return NTBASE+nnonter;
+ return NTBASE + nnonter;
}
// must be a token
ntokens++;
if ntokens >= len(tokset) {
- nn := ntokens+SYMINC;
+ nn := ntokens + SYMINC;
atokset := make([]Symb, nn);
atoklev := make([]int, nn);
case c >= '0' && c <= '9':
c -= '0'
case c >= 'a' && c <= 'f':
- c -= 'a'-10
+ c -= 'a' - 10
case c >= 'A' && c <= 'F':
- c -= 'A'-10
+ c -= 'A' - 10
default:
error("illegal \\unnnn construction")
}
if tokname == resrv[c].name {
if tokflag {
fmt.Printf(">>> %%%v %v %v\n", tokname,
- resrv[c].value - PRIVATE, lineno)
+ resrv[c].value-PRIVATE, lineno)
}
return resrv[c].value;
}
error("invalid escape, or illegal reserved word: %v", tokname);
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
- numbval = c-'0';
+ numbval = c - '0';
for {
c = getrune(finput);
if !isdigit(c) {
}
for i := 0; i <= nnonter; i++ {
if s == nontrst[i].name {
- return NTBASE+i
+ return NTBASE + i
}
}
c = getrune(finput);
}
ungetrune(finput, c);
- j = j*s;
+ j = j * s;
if j >= max {
error("Illegal use of $%v", j)
}
fatfl = 0; // make undefined symbols nonfatal
for i := 0; i <= nnonter; i++ {
n := 0;
- c := i+NTBASE;
+ c := i + NTBASE;
for j := 0; j < nprod; j++ {
if prdptr[j][0] == c {
curres[n] = prdptr[j][1:len(prdptr[j])];
if pempty[prd[0]-NTBASE] != 0 {
continue
}
- np = len(prd)-1;
+ np = len(prd) - 1;
for p = 1; p < np; p++ {
if prd[p] >= NTBASE && pempty[prd[p]-NTBASE] == WHOKNOWS {
break
if pempty[prd[0]-NTBASE] != WHOKNOWS {
continue
}
- np = len(prd)-1;
+ np = len(prd) - 1;
for p = 1; p < np; p++ {
if prd[p] < NTBASE || pempty[prd[p]-NTBASE] != EMPTY {
continue next
// initially fill the sets
for s = 0; s < n; s++ {
prd = curres[s];
- np = len(prd)-1;
+ np = len(prd) - 1;
for p = 0; p < np; p++ {
ch = prd[p];
if ch < NTBASE {
n = len(curres);
for s = 0; s < n; s++ {
prd = curres[s];
- np = len(prd)-1;
+ np = len(prd) - 1;
for p = 0; p < np; p++ {
- ch = prd[p]-NTBASE;
+ ch = prd[p] - NTBASE;
if ch < 0 {
break
}
// do a goto on c
putitem(wsets[p].pitem, wsets[p].ws);
- for q := p+1; q < cwp; q++ {
+ for q := p + 1; q < cwp; q++ {
// this item contributes to the goto
if c == wsets[q].pitem.first {
putitem(wsets[q].pitem, wsets[q].ws);
// sort the items
var k, l int;
- for k = p1+1; k < p2; k++ { // make k the biggest
+ for k = p1 + 1; k < p2; k++ { // make k the biggest
for l = k; l > p1; l-- {
if statemem[l].pitem.prodno < statemem[l-1].pitem.prodno ||
statemem[l].pitem.prodno == statemem[l-1].pitem.prodno &&
}
}
- size1 := p2-p1; // size of state
+ size1 := p2 - p1; // size of state
var i int;
if c >= NTBASE {
// get ith state
q1 := pstate[i];
q2 := pstate[i+1];
- size2 := q2-q1;
+ size2 := q2 - q1;
if size1 != size2 {
continue
}
}
tystate[nstate] = MUSTDO;
nstate++;
- return nstate-1;
+ return nstate - 1;
}
func putitem(p Pitem, set Lkset) {
var i int;
p := pp.prod;
- q := chcopy(nontrst[prdptr[pp.prodno][0]-NTBASE].name)+": ";
+ q := chcopy(nontrst[prdptr[pp.prodno][0]-NTBASE].name) + ": ";
npi := pp.off;
pi := aryeq(p, prdptr[pp.prodno]);
p = p[pp : n+1];
// now, find a place for the elements from p to q, inclusive
- r := len(amem)-len(p);
+ r := len(amem) - len(p);
nextk:
for rr := 0; rr <= r; rr++ {
fmt.Fprintf(foutput, "\n");
}
}
- return off+rr;
+ return off + rr;
}
error("no space in action table");
return 0;
}
if tystate[i] == MUSTLOOKAHEAD {
// print out empty productions in closure
- for u = pstate[i+1]-pstate[i]; u < cwp; u++ {
+ for u = pstate[i+1] - pstate[i]; u < cwp; u++ {
if wsets[u].pitem.first < 0 {
fmt.Fprintf(foutput, "\t%v\n", writem(wsets[u].pitem))
}
}
// best is now the default entry
- zzgobest += times-1;
+ zzgobest += times - 1;
n := 0;
for j := 0; j < nstate; j++ {
if tystate[j] != 0 && tystate[j] != best {
n++
}
}
- goent := make([]int, 2*n + 1);
+ goent := make([]int, 2*n+1);
n = 0;
for j := 0; j < nstate; j++ {
if tystate[j] != 0 && tystate[j] != best {
work = 0;
for i = 0; i < nprod; i++ {
// cc is a nonterminal with a goto on c
- cc = prdptr[i][1]-NTBASE;
+ cc = prdptr[i][1] - NTBASE;
if cc >= 0 && temp1[cc] != 0 {
// thus, the left side of production i does too
- cc = prdptr[i][0]-NTBASE;
+ cc = prdptr[i][0] - NTBASE;
if temp1[cc] == 0 {
work = 1;
temp1[cc] = 1;
nred := 0;
levprd[0] = 0;
for i := 1; i < nprod; i++ {
- if (levprd[i]&REDFLAG) == 0 {
+ if (levprd[i] & REDFLAG) == 0 {
if foutput != nil {
fmt.Fprintf(foutput, "Rule not reduced: %v\n",
writem(Pitem{prdptr[i], 0, 0, i}))
fmt.Printf("rule %v never reduced\n", writem(Pitem{prdptr[i], 0, 0, i}));
nred++;
}
- levprd[i] = prdptr[i][0]-NTBASE;
+ levprd[i] = prdptr[i][0] - NTBASE;
}
if nred != 0 {
fmt.Printf("%v rules never reduced\n", nred)
// minimum entry index is always 0
v = yypgo[i];
- q = len(v)-1;
+ q = len(v) - 1;
for p = 0; p < q; p += 2 {
ggreed[i] += 2;
if v[p] > j {
ggreed[i] = 0;
q := yypgo[i];
- nq := len(q)-1;
+ nq := len(q) - 1;
// now, find amem place for it
nextgp:
continue
}
for r := 0; r < nq; r += 2 {
- s = p+q[r]+1;
+ s = p + q[r] + 1;
if s > maxa {
maxa = s;
if maxa >= ACTSIZE {
maxa = p
}
for r := 0; r < nq; r += 2 {
- s = p+q[r]+1;
+ s = p + q[r] + 1;
amem[s] = q[r+1];
}
pgo[i] = p;
for n := -maxoff; n < ACTSIZE; n++ {
flag := 0;
for r := 0; r < nq; r += 2 {
- s = q[r]+n;
+ s = q[r] + n;
if s < 0 || s > ACTSIZE {
continue nextn
}
}
for r := 0; r < nq; r += 2 {
- s = q[r]+n;
+ s = q[r] + n;
if s > maxa {
maxa = s
}
//yyr2 is the number of rules for each production
//
for i = 1; i < nprod; i++ {
- temp1[i] = len(prdptr[i])-2
+ temp1[i] = len(prdptr[i]) - 2
}
arout("YYR2", temp1, nprod);
fmt.Fprintf(foutput, "%v shift/reduce, %v reduce/reduce conflicts reported\n", zzsrconf, zzrrconf);
fmt.Fprintf(foutput, "%v working sets used\n", len(wsets));
fmt.Fprintf(foutput, "memory: parser %v/%v\n", memp, ACTSIZE);
- fmt.Fprintf(foutput, "%v extra closures\n", zzclose - 2*nstate);
+ fmt.Fprintf(foutput, "%v extra closures\n", zzclose-2*nstate);
fmt.Fprintf(foutput, "%v shift entries, %v exceptions\n", zzacent, zzexcp);
fmt.Fprintf(foutput, "%v goto entries\n", zzgoent);
fmt.Fprintf(foutput, "%v entries saved by goto default\n", zzgobest);
j := 0;
for i = 0; i < len(q); i++ {
if q[i] == '"' {
- s += q[j:i]+"\\";
+ s += q[j:i] + "\\";
j = i;
}
}
- return s+q[j:i];
+ return s + q[j:i];
}
func usage() {
exit(1);
}
-func bitset(set Lkset, bit int) int { return set[bit>>5]&(1<<uint(bit&31)) }
+func bitset(set Lkset, bit int) int { return set[bit>>5] & (1 << uint(bit&31)) }
-func setbit(set Lkset, bit int) { set[bit>>5] |= (1<<uint(bit&31)) }
+func setbit(set Lkset, bit int) { set[bit>>5] |= (1 << uint(bit & 31)) }
func mkset() Lkset { return make([]int, tbitset) }
sub := 0;
for i := 0; i < tbitset; i++ {
x := a[i];
- y := x|b[i];
+ y := x | b[i];
a[i] = y;
if y != x {
sub = 1
}
func create(s string, m int) *bufio.Writer {
- fo, err := os.Open(s, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, m);
+ fo, err := os.Open(s, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, m);
if err != nil {
error("error opening %v: %v", s, err)
}
changed[o.Dst] = 1;
}
if o.Mode != 0 {
- chk(os.Chmod(o.Dst, o.Mode & 0755));
+ chk(os.Chmod(o.Dst, o.Mode&0755));
undoRevert(o.Dst);
changed[o.Dst] = 1;
}
}
if j > 0 {
- err = mkdirAll(path[0 : j-1], perm);
+ err = mkdirAll(path[0:j-1], perm);
if err != nil {
return err
}
for i := 0; i < len(header); i++ {
if i == 148 {
// The chksum field (header[148:156]) is special: it should be treated as space bytes.
- unsigned += ' '*8;
- signed += ' '*8;
+ unsigned += ' ' * 8;
+ signed += ' ' * 8;
i += 7;
continue;
}
}
// Two blocks of zero bytes marks the end of the archive.
- if bytes.Equal(header, zeroBlock[0 : blockSize]) {
+ if bytes.Equal(header, zeroBlock[0:blockSize]) {
if _, tr.err = io.ReadFull(tr.r, header); tr.err != nil {
return nil
}
- if !bytes.Equal(header, zeroBlock[0 : blockSize]) {
+ if !bytes.Equal(header, zeroBlock[0:blockSize]) {
tr.err = HeaderError
}
return nil;
// until Next is called to advance to the next entry.
func (tr *Reader) Read(b []uint8) (n int, err os.Error) {
if int64(len(b)) > tr.nb {
- b = b[0 : tr.nb]
+ b = b[0:tr.nb]
}
n, err = tr.r.Read(b);
tr.nb -= int64(n);
s := strconv.Itob64(x, 8);
// leading zeros, but leave room for a NUL.
for len(s)+1 < len(b) {
- s = "0"+s
+ s = "0" + s
}
tw.cString(b, s);
}
}
// Too big: use binary (big-endian).
tw.usedBinary = true;
- for i := len(b)-1; x > 0 && i >= 0; i-- {
+ for i := len(b) - 1; x > 0 && i >= 0; i-- {
b[i] = byte(x);
x >>= 8;
}
func (tw *Writer) Write(b []uint8) (n int, err os.Error) {
overwrite := false;
if int64(len(b)) > tw.nb {
- b = b[0 : tw.nb];
+ b = b[0:tw.nb];
overwrite = true;
}
n, err = tw.w.Write(b);
Mode: 0640,
Uid: 73025,
Gid: 5000,
- Size: 16<<30,
+ Size: 16 << 30,
Mtime: 1254699560,
Typeflag: '0',
Uname: "dsymonds",
if i < 0 || i >= b.BitLength {
return 0
}
- x := i/8;
- y := 7-uint(i%8);
- return int(b.Bytes[x] >> y)&1;
+ x := i / 8;
+ y := 7 - uint(i%8);
+ return int(b.Bytes[x]>>y) & 1;
}
// parseBitString parses an ASN.1 bit string from the given byte array and returns it.
s = make([]int, len(bytes)+1);
// The first byte is 40*value1 + value2:
- s[0] = int(bytes[0])/40;
- s[1] = int(bytes[0])%40;
+ s[0] = int(bytes[0]) / 40;
+ s[1] = int(bytes[0]) % 40;
i := 2;
for offset := 1; offset < len(bytes); i++ {
var v int;
}
ret <<= 7;
b := bytes[offset];
- ret |= int(b&0x7f);
+ ret |= int(b & 0x7f);
offset++;
if b&0x80 == 0 {
return
// RFC 5280, section 5.1.2.4 says that years 2050 or later use another date
// scheme.
if year > 50 {
- ret.Year = 1900+int64(year)
+ ret.Year = 1900 + int64(year)
} else {
- ret.Year = 2000+int64(year)
+ ret.Year = 2000 + int64(year)
}
ret.Month, ok2 = twoDigits(bytes[2:4], 12);
ret.Day, ok3 = twoDigits(bytes[4:6], 31);
if bytes[0] == '-' {
sign = -1
}
- ret.ZoneOffset = sign*(60*(hours*60 + minutes));
+ ret.ZoneOffset = sign * (60 * (hours*60 + minutes));
default:
goto Error
}
offset = initOffset;
b := bytes[offset];
offset++;
- ret.class = int(b>>6);
+ ret.class = int(b >> 6);
ret.isCompound = b&0x20 == 0x20;
- ret.tag = int(b&0x1f);
+ ret.tag = int(b & 0x1f);
// If the bottom five bits are set, then the tag number is actually base 128
// encoded afterwards
offset++;
if b&0x80 == 0 {
// The length is encoded in the bottom 7 bits.
- ret.length = int(b&0x7f)
+ ret.length = int(b & 0x7f)
} else {
// Bottom 7 bits give the number of length bytes to follow.
- numBytes := int(b&0x7f);
+ numBytes := int(b & 0x7f);
// We risk overflowing a signed 32-bit number if we accept more than 3 bytes.
if numBytes > 3 {
err = StructuralError{"length too large"};
err = SyntaxError{"data truncated"};
return;
}
- result := RawValue{t.class, t.tag, t.isCompound, bytes[offset : offset + t.length]};
+ result := RawValue{t.class, t.tag, t.isCompound, bytes[offset : offset+t.length]};
offset += t.length;
v.(*reflect.StructValue).Set(reflect.NewValue(result).(*reflect.StructValue));
return;
}
var result interface{}
if !t.isCompound && t.class == classUniversal {
- innerBytes := bytes[offset : offset + t.length];
+ innerBytes := bytes[offset : offset+t.length];
switch t.tag {
case tagPrintableString:
result, err = parsePrintableString(innerBytes)
err = SyntaxError{"data truncated"};
return;
}
- innerBytes := bytes[offset : offset + t.length];
+ innerBytes := bytes[offset : offset+t.length];
// We deal with the structures defined in this package first.
switch fieldType {
const (
_S = uintptr(unsafe.Sizeof(Word(0))); // TODO(gri) should Sizeof return a uintptr?
- _W = _S*8;
- _B = 1<<_W;
- _M = _B-1;
- _W2 = _W/2;
- _B2 = 1<<_W2;
- _M2 = _B2-1;
+ _W = _S * 8;
+ _B = 1 << _W;
+ _M = _B - 1;
+ _W2 = _W / 2;
+ _B2 = 1 << _W2;
+ _M2 = _B2 - 1;
)
// z1<<_W + z0 = x+y+c, with c == 0 or 1
func addWW_g(x, y, c Word) (z1, z0 Word) {
- yc := y+c;
- z0 = x+yc;
+ yc := y + c;
+ z0 = x + yc;
if z0 < x || yc < y {
z1 = 1
}
// z1<<_W + z0 = x-y-c, with c == 0 or 1
func subWW_g(x, y, c Word) (z1, z0 Word) {
- yc := y+c;
- z0 = x-yc;
+ yc := y + c;
+ z0 = x - yc;
if z0 > x || yc < y {
z1 = 1
}
// y < _B2 because y <= x
// sub-digits of x and y are (0, x) and (0, y)
// z = z[0] = x*y
- z0 = x*y;
+ z0 = x * y;
return;
}
x1, x0 := x>>_W2, x&_M2;
// x*y = t2*_B2*_B2 + t1*_B2 + t0
- t0 := x0*y;
- t1 := x1*y;
+ t0 := x0 * y;
+ t1 := x1 * y;
// compute result digits but avoid overflow
// z = z[1]*_B + z[0] = x*y
z0 = t1<<_W2 + t0;
- z1 = (t1 + t0>>_W2)>>_W2;
+ z1 = (t1 + t0>>_W2) >> _W2;
return;
}
y1, y0 := y>>_W2, y&_M2;
// x*y = t2*_B2*_B2 + t1*_B2 + t0
- t0 := x0*y0;
+ t0 := x0 * y0;
// t1 := x1*y0 + x0*y1;
var c Word;
- t1 := x1*y0;
+ t1 := x1 * y0;
t1a := t1;
- t1 += x0*y1;
+ t1 += x0 * y1;
if t1 < t1a {
c++
}
c3++
}
z1 >>= _W2;
- z1 += c3*_B2;
+ z1 += c3 * _B2;
z1 += t2;
return;
}
var c2 Word; // extra carry
t1 := x1*y0 + c1;
t1a := t1;
- t1 += x0*y1;
+ t1 += x0 * y1;
if t1 < t1a { // If the number got smaller then we overflowed.
c2++
}
func divStep(x1, x0, y Word) (q, r Word) {
d1, d0 := y>>_W2, y&_M2;
q1, r1 := x1/d1, x1%d1;
- m := q1*d0;
+ m := q1 * d0;
r1 = r1*_B2 | x0>>_W2;
if r1 < m {
q1--;
}
r1 -= m;
- r0 := r1%d1;
- q0 := r1/d1;
- m = q0*d0;
+ r0 := r1 % d1;
+ q0 := r1 / d1;
+ m = q0 * d0;
r0 = r0*_B2 | x0&_M2;
if r0 < m {
q0--;
if y > x1 {
if z != 0 {
y <<= z;
- x1 = (x1<<z)|(x0>>(uint(_W)-z));
+ x1 = (x1 << z) | (x0 >> (uint(_W) - z));
x0 <<= z;
}
q0, x0 = divStep(x1, x0, y);
x1 -= y;
q1 = 1;
} else {
- z1 := uint(_W)-z;
+ z1 := uint(_W) - z;
y <<= z;
- x2 := x1>>z1;
- x1 = (x1<<z)|(x0>>z1);
+ x2 := x1 >> z1;
+ x1 = (x1 << z) | (x0 >> z1);
x0 <<= z;
q1, x1 = divStep(x2, x1, y);
}
q0, x0 = divStep(x1, x0, y);
}
- r = x0>>z;
+ r = x0 >> z;
if q1 != 0 {
panic("div out of range")
func divWVW_s(z *Word, xn Word, x *Word, y Word, n int) (r Word)
func divWVW_g(z *Word, xn Word, x *Word, y Word, n int) (r Word) {
r = xn;
- for i := n-1; i >= 0; i-- {
+ for i := n - 1; i >= 0; i-- {
*z.at(i), r = divWW_g(r, *x.at(i), y)
}
return;
argWW{_M, 1, 0, 1, 0},
argWW{_M, 0, 1, 1, 0},
argWW{_M, 1, 1, 1, 1},
- argWW{_M, _M, 0, 1, _M-1},
+ argWW{_M, _M, 0, 1, _M - 1},
argWW{_M, _M, 1, 1, _M},
}
argVV{[]Word{1}, []Word{1}, []Word{0}, 0},
argVV{[]Word{0}, []Word{_M}, []Word{1}, 1},
argVV{[]Word{80235}, []Word{12345}, []Word{67890}, 0},
- argVV{[]Word{_M-1}, []Word{_M}, []Word{_M}, 1},
+ argVV{[]Word{_M - 1}, []Word{_M}, []Word{_M}, 1},
argVV{[]Word{0, 0, 0, 0}, []Word{_M, _M, _M, _M}, []Word{1, 0, 0, 0}, 1},
- argVV{[]Word{0, 0, 0, _M}, []Word{_M, _M, _M, _M-1}, []Word{1, 0, 0, 0}, 0},
+ argVV{[]Word{0, 0, 0, _M}, []Word{_M, _M, _M, _M - 1}, []Word{1, 0, 0, 0}, 0},
argVV{[]Word{0, 0, 0, 0}, []Word{_M, 0, _M, 0}, []Word{1, _M, 0, _M}, 1},
}
argVW{[]Word{0, 0, 0, 22793}, []Word{0, 0, 0, 991}, 23, 0},
argVW{[]Word{0, 0, 0, 0}, []Word{7893475, 7395495, 798547395, 68943}, 0, 0},
argVW{[]Word{0, 0, 0, 0}, []Word{0, 0, 0, 0}, 894375984, 0},
- argVW{[]Word{_M<<1&_M}, []Word{_M}, 1<<1, _M>>(_W-1)},
- argVW{[]Word{_M<<7&_M}, []Word{_M}, 1<<7, _M>>(_W-7)},
- argVW{[]Word{_M<<7&_M, _M, _M, _M}, []Word{_M, _M, _M, _M}, 1<<7, _M>>(_W-7)},
+ argVW{[]Word{_M << 1 & _M}, []Word{_M}, 1 << 1, _M >> (_W - 1)},
+ argVW{[]Word{_M << 7 & _M}, []Word{_M}, 1 << 7, _M >> (_W - 7)},
+ argVW{[]Word{_M << 7 & _M, _M, _M, _M}, []Word{_M, _M, _M, _M}, 1 << 7, _M >> (_W - 7)},
}
argVWW{[]Word{991, 0, 0, 0}, []Word{7893475, 7395495, 798547395, 68943}, 0, 991, 0},
argVWW{[]Word{0, 0, 0, 0}, []Word{0, 0, 0, 0}, 894375984, 0, 0},
argVWW{[]Word{991, 0, 0, 0}, []Word{0, 0, 0, 0}, 894375984, 991, 0},
- argVWW{[]Word{_M<<1&_M}, []Word{_M}, 1<<1, 0, _M>>(_W-1)},
- argVWW{[]Word{_M<<1&_M + 1}, []Word{_M}, 1<<1, 1, _M>>(_W-1)},
- argVWW{[]Word{_M<<7&_M}, []Word{_M}, 1<<7, 0, _M>>(_W-7)},
- argVWW{[]Word{_M<<7&_M + 1<<6}, []Word{_M}, 1<<7, 1<<6, _M>>(_W-7)},
- argVWW{[]Word{_M<<7&_M, _M, _M, _M}, []Word{_M, _M, _M, _M}, 1<<7, 0, _M>>(_W-7)},
- argVWW{[]Word{_M<<7&_M + 1<<6, _M, _M, _M}, []Word{_M, _M, _M, _M}, 1<<7, 1<<6, _M>>(_W-7)},
+ argVWW{[]Word{_M << 1 & _M}, []Word{_M}, 1 << 1, 0, _M >> (_W - 1)},
+ argVWW{[]Word{_M<<1&_M + 1}, []Word{_M}, 1 << 1, 1, _M >> (_W - 1)},
+ argVWW{[]Word{_M << 7 & _M}, []Word{_M}, 1 << 7, 0, _M >> (_W - 7)},
+ argVWW{[]Word{_M<<7&_M + 1<<6}, []Word{_M}, 1 << 7, 1 << 6, _M >> (_W - 7)},
+ argVWW{[]Word{_M << 7 & _M, _M, _M, _M}, []Word{_M, _M, _M, _M}, 1 << 7, 0, _M >> (_W - 7)},
+ argVWW{[]Word{_M<<7&_M + 1<<6, _M, _M, _M}, []Word{_M, _M, _M, _M}, 1 << 7, 1 << 6, _M >> (_W - 7)},
}
var mulWWTests = []mulWWTest{
- mulWWTest{_M, _M, _M-1, 1},
+ mulWWTest{_M, _M, _M - 1, 1},
// 32 bit only: mulWWTest{0xc47dfa8c, 50911, 0x98a4, 0x998587f4},
}
// TODO(agl): These will only work on 64-bit platforms.
// mulAddWWWTest{15064310297182388543, 0xe7df04d2d35d5d80, 13537600649892366549, 13644450054494335067, 10832252001440893781},
// mulAddWWWTest{15064310297182388543, 0xdab2f18048baa68d, 13644450054494335067, 12869334219691522700, 14233854684711418382},
- mulAddWWWTest{_M, _M, 0, _M-1, 1},
+ mulAddWWWTest{_M, _M, 0, _M - 1, 1},
mulAddWWWTest{_M, _M, _M, _M, 0},
}
for i, w := range z.abs {
wordBytes := b[(len(z.abs)-i-1)*s : (len(z.abs)-i)*s];
- for j := s-1; j >= 0; j-- {
+ for j := s - 1; j >= 0; j-- {
wordBytes[j] = byte(w);
w >>= 8;
}
shift := leadingZeros(v) + 1;
v <<= shift;
- const mask = 1<<(_W-1);
+ const mask = 1 << (_W - 1);
// We walk through the bits of the exponent one by one. Each time we see
// a bit, we square, thus doubling the power. If the bit is a one, we
// also multiply by x, thus adding one to the power.
- w := int(_W)-int(shift);
+ w := int(_W) - int(shift);
for j := 0; j < w; j++ {
z.Mul(z, z);
v <<= 1;
}
- for i := len(y.abs)-2; i >= 0; i-- {
+ for i := len(y.abs) - 2; i >= 0; i-- {
v = y.abs[i];
for j := 0; j < int(_W); j++ {
}
}
- z.neg = x.neg && y.abs[0] & 1 == 1;
+ z.neg = x.neg && y.abs[0]&1 == 1;
return z;
}
hex2 := hex.EncodeToString(b);
for len(hex1) < len(hex2) {
- hex1 = "0"+hex1
+ hex1 = "0" + hex1
}
for len(hex1) > len(hex2) {
- hex2 = "0"+hex2
+ hex2 = "0" + hex2
}
return hex1 == hex2;
// See Knuth, Volume 2, section 4.3.1, exercise 21. This code exercises
// a code path which only triggers 1 in 10^{-19} cases.
- u := &Int{false, []Word{0, 0, 1 + 1<<(_W-1), _M^(1<<(_W-1))}};
- v := &Int{false, []Word{5, 2 + 1<<(_W-1), 1<<(_W-1)}};
+ u := &Int{false, []Word{0, 0, 1 + 1<<(_W-1), _M ^ (1 << (_W - 1))}};
+ v := &Int{false, []Word{5, 2 + 1<<(_W-1), 1 << (_W - 1)}};
q, r := new(Int).Div(u, v);
const expectedQ64 = "18446744073709551613";
// split x into n words
z = makeN(z, n, false);
for i := 0; i < n; i++ {
- z[i] = Word(x&_M);
+ z[i] = Word(x & _M);
x >>= _W;
}
return;
}
- i := m-1;
+ i := m - 1;
for i > 0 && x[i] == y[i] {
i--
}
// len(uIn) >= 1 + len(vIn)
func divNN(z, z2, uIn, v []Word) (q, r []Word) {
n := len(v);
- m := len(uIn)-len(v);
+ m := len(uIn) - len(v);
u := makeN(z2, len(uIn)+1, false);
qhatv := make([]Word, len(v)+1);
shift := leadingZeroBits(v[n-1]);
shiftLeft(v, v, shift);
shiftLeft(u, uIn, shift);
- u[len(uIn)] = uIn[len(uIn)-1]>>(uint(_W)-uint(shift));
+ u[len(uIn)] = uIn[len(uIn)-1] >> (uint(_W) - uint(shift));
// D2.
for j := m; j >= 0; j-- {
for ; x > 0; x >>= 1 {
n++
}
- return n-1;
+ return n - 1;
}
var d byte;
switch {
case '0' <= ch && ch <= '9':
- d = ch-'0'
+ d = ch - '0'
case 'a' <= ch && ch <= 'f':
- d = ch-'a'+10
+ d = ch - 'a' + 10
case 'A' <= ch && ch <= 'F':
- d = ch-'A'+10
+ d = ch - 'A' + 10
default:
return -1
}
func leadingZeroBits(x Word) int {
c := 0;
if x < 1<<(_W/2) {
- x <<= _W/2;
- c = int(_W/2);
+ x <<= _W / 2;
+ c = int(_W / 2);
}
for i := 0; x != 0; i++ {
if x&(1<<(_W-1)) != 0 {
- return i+c
+ return i + c
}
x <<= 1;
}
return
}
- ñ := uint(_W)-uint(n);
- for i := len(src)-1; i >= 1; i-- {
- dst[i] = src[i]<<uint(n);
- dst[i] |= src[i-1]>>ñ;
+ ñ := uint(_W) - uint(n);
+ for i := len(src) - 1; i >= 1; i-- {
+ dst[i] = src[i] << uint(n);
+ dst[i] |= src[i-1] >> ñ;
}
- dst[0] = src[0]<<uint(n);
+ dst[0] = src[0] << uint(n);
}
return
}
- ñ := uint(_W)-uint(n);
+ ñ := uint(_W) - uint(n);
for i := 0; i < len(src)-1; i++ {
- dst[i] = src[i]>>uint(n);
- dst[i] |= src[i+1]<<ñ;
+ dst[i] = src[i] >> uint(n);
+ dst[i] |= src[i+1] << ñ;
}
- dst[len(src)-1] = src[len(src)-1]>>uint(n);
+ dst[len(src)-1] = src[len(src)-1] >> uint(n);
}
argNN{nil, nil, nil},
argNN{nil, []Word{991}, nil},
argNN{[]Word{991}, []Word{991}, []Word{1}},
- argNN{[]Word{991*991}, []Word{991}, []Word{991}},
- argNN{[]Word{0, 0, 991*991}, []Word{0, 991}, []Word{0, 991}},
- argNN{[]Word{1*991, 2*991, 3*991, 4*991}, []Word{1, 2, 3, 4}, []Word{991}},
+ argNN{[]Word{991 * 991}, []Word{991}, []Word{991}},
+ argNN{[]Word{0, 0, 991 * 991}, []Word{0, 991}, []Word{0, 991}},
+ argNN{[]Word{1 * 991, 2 * 991, 3 * 991, 4 * 991}, []Word{1, 2, 3, 4}, []Word{991}},
argNN{[]Word{4, 11, 20, 30, 20, 11, 4}, []Word{1, 2, 3, 4}, []Word{4, 3, 2, 1}},
}
func TestLeadingZeroBits(t *testing.T) {
- var x Word = 1<<(_W-1);
+ var x Word = 1 << (_W - 1);
for i := 0; i <= int(_W); i++ {
if leadingZeroBits(x) != i {
t.Errorf("failed at %x: got %d want %d", x, leadingZeroBits(x), i)
shiftTest{[]Word{0}, 0, []Word{0}},
shiftTest{[]Word{1}, 0, []Word{1}},
shiftTest{[]Word{1}, 1, []Word{2}},
- shiftTest{[]Word{1<<(_W-1)}, 1, []Word{0}},
- shiftTest{[]Word{1<<(_W-1), 0}, 1, []Word{0, 1}},
+ shiftTest{[]Word{1 << (_W - 1)}, 1, []Word{0}},
+ shiftTest{[]Word{1 << (_W - 1), 0}, 1, []Word{0, 1}},
}
shiftTest{[]Word{1}, 0, []Word{1}},
shiftTest{[]Word{1}, 1, []Word{0}},
shiftTest{[]Word{2}, 1, []Word{1}},
- shiftTest{[]Word{0, 1}, 1, []Word{1<<(_W-1), 0}},
- shiftTest{[]Word{2, 1, 1}, 1, []Word{1<<(_W-1) + 1, 1<<(_W-1), 0}},
+ shiftTest{[]Word{0, 1}, 1, []Word{1 << (_W - 1), 0}},
+ shiftTest{[]Word{2, 1, 1}, 1, []Word{1<<(_W-1) + 1, 1 << (_W - 1), 0}},
}
// and return the product as 2 words.
const (
- W = uint(unsafe.Sizeof(x))*8;
- W2 = W/2;
- B2 = 1<<W2;
- M2 = B2-1;
+ W = uint(unsafe.Sizeof(x)) * 8;
+ W2 = W / 2;
+ B2 = 1 << W2;
+ M2 = B2 - 1;
)
if x < y {
// y < B2 because y <= x
// sub-digits of x and y are (0, x) and (0, y)
// z = z[0] = x*y
- z0 = x*y;
+ z0 = x * y;
return;
}
x1, x0 := x>>W2, x&M2;
// x*y = t2*B2*B2 + t1*B2 + t0
- t0 := x0*y;
- t1 := x1*y;
+ t0 := x0 * y;
+ t1 := x1 * y;
// compute result digits but avoid overflow
// z = z[1]*B + z[0] = x*y
z0 = t1<<W2 + t0;
- z1 = (t1 + t0>>W2)>>W2;
+ z1 = (t1 + t0>>W2) >> W2;
return;
}
y1, y0 := y>>W2, y&M2;
// x*y = t2*B2*B2 + t1*B2 + t0
- t0 := x0*y0;
+ t0 := x0 * y0;
t1 := x1*y0 + x0*y1;
- t2 := x1*y1;
+ t2 := x1 * y1;
// compute result digits but avoid overflow
// z = z[1]*B + z[0] = x*y
z0 = t1<<W2 + t0;
- z1 = t2 + (t1 + t0>>W2)>>W2;
+ z1 = t2 + (t1+t0>>W2)>>W2;
return;
}
// and return the product as 2 words.
const (
- W = uint(unsafe.Sizeof(x))*8;
- W2 = W/2;
- B2 = 1<<W2;
- M2 = B2-1;
+ W = uint(unsafe.Sizeof(x)) * 8;
+ W2 = W / 2;
+ B2 = 1 << W2;
+ M2 = B2 - 1;
)
// TODO(gri) Should implement special cases for faster execution.
// x*y + c = t2*B2*B2 + t1*B2 + t0
t0 := x0*y0 + c0;
t1 := x1*y0 + x0*y1 + c1;
- t2 := x1*y1;
+ t2 := x1 * y1;
// compute result digits but avoid overflow
// z = z[1]*B + z[0] = x*y
z0 = t1<<W2 + t0;
- z1 = t2 + (t1 + t0>>W2)>>W2;
+ z1 = t2 + (t1+t0>>W2)>>W2;
return;
}
const (
logW = 64; // word width
logH = 4; // bits for a hex digit (= small number)
- logB = logW-logH; // largest bit-width available
+ logB = logW - logH; // largest bit-width available
// half-digits
- _W2 = logB/2; // width
- _B2 = 1<<_W2; // base
- _M2 = _B2-1; // mask
+ _W2 = logB / 2; // width
+ _B2 = 1 << _W2; // base
+ _M2 = _B2 - 1; // mask
// full digits
- _W = _W2*2; // width
- _B = 1<<_W; // base
- _M = _B-1; // mask
+ _W = _W2 * 2; // width
+ _B = 1 << _W; // base
+ _M = _B - 1; // mask
)
// split x into digits
z := make(Natural, n);
for i := 0; i < n; i++ {
- z[i] = digit(x&_M);
+ z[i] = digit(x & _M);
x >>= _W;
}
z := uint64(0);
s := uint(0);
for i := 0; i < n && s < 64; i++ {
- z += uint64(x[i])<<s;
+ z += uint64(x[i]) << s;
s += _W;
}
c := digit(0);
i := 0;
for i < m {
- t := c+x[i]+y[i];
+ t := c + x[i] + y[i];
c, z[i] = t>>_W, t&_M;
i++;
}
for i < n {
- t := c+x[i];
+ t := c + x[i];
c, z[i] = t>>_W, t&_M;
i++;
}
c := digit(0);
i := 0;
for i < m {
- t := c+x[i]-y[i];
+ t := c + x[i] - y[i];
c, z[i] = digit(int64(t)>>_W), t&_M; // requires arithmetic shift!
i++;
}
for i < n {
- t := c+x[i];
+ t := c + x[i];
c, z[i] = digit(int64(t)>>_W), t&_M; // requires arithmetic shift!
i++;
}
//
func muladd11(x, y, c digit) (digit, digit) {
z1, z0 := MulAdd128(uint64(x), uint64(y), uint64(c));
- return digit(z1<<(64-logB) | z0>>logB), digit(z0&_M);
+ return digit(z1<<(64-logB) | z0>>logB), digit(z0 & _M);
}
func unpack(x Natural) []digit2 {
n := len(x);
- z := make([]digit2, n*2 + 1); // add space for extra digit (used by DivMod)
+ z := make([]digit2, n*2+1); // add space for extra digit (used by DivMod)
for i := 0; i < n; i++ {
t := x[i];
- z[i*2] = digit2(t&_M2);
- z[i*2 + 1] = digit2(t>>_W2&_M2);
+ z[i*2] = digit2(t & _M2);
+ z[i*2+1] = digit2(t >> _W2 & _M2);
}
// normalize result
- k := 2*n;
+ k := 2 * n;
for k > 0 && z[k-1] == 0 {
k--
}
func pack(x []digit2) Natural {
- n := (len(x)+1)/2;
+ n := (len(x) + 1) / 2;
z := make(Natural, n);
if len(x)&1 == 1 {
// handle odd len(x)
z[n] = digit(x[n*2]);
}
for i := 0; i < n; i++ {
- z[i] = digit(x[i*2 + 1])<<_W2 | digit(x[i*2])
+ z[i] = digit(x[i*2+1])<<_W2 | digit(x[i*2])
}
return normalize(z);
}
func div21(z, x []digit2, y digit2) digit2 {
c := digit(0);
d := digit(y);
- for i := len(x)-1; i >= 0; i-- {
+ for i := len(x) - 1; i >= 0; i-- {
t := c<<_W2 + digit(x[i]);
c, z[i] = t%d, digit2(t/d);
}
if m == 1 {
// division by single digit
// result is shifted left by 1 in place!
- x[0] = div21(x[1 : n+1], x[0:n], y[0])
+ x[0] = div21(x[1:n+1], x[0:n], y[0])
} else if m > n {
// y > x => quotient = 0, remainder = x
// TODO Instead of multiplying, it would be sufficient to
// shift y such that the normalization condition is
// satisfied (as done in Hacker's Delight).
- f := _B2/(digit(y[m-1])+1);
+ f := _B2 / (digit(y[m-1]) + 1);
if f != 1 {
mul21(x, x, digit2(f));
mul21(y, y, digit2(f));
assert(_B2/2 <= y[m-1] && y[m-1] < _B2); // incorrect scaling
y1, y2 := digit(y[m-1]), digit(y[m-2]);
- for i := n-m; i >= 0; i-- {
- k := i+m;
+ for i := n - m; i >= 0; i-- {
+ k := i + m;
// compute trial digit (Knuth)
var q digit;
{
x0, x1, x2 := digit(x[k]), digit(x[k-1]), digit(x[k-2]);
if x0 != y1 {
- q = (x0<<_W2 + x1)/y1
+ q = (x0<<_W2 + x1) / y1
} else {
- q = _B2-1
+ q = _B2 - 1
}
- for y2*q > (x0<<_W2 + x1 - y1*q)<<_W2 + x2 {
+ for y2*q > (x0<<_W2+x1-y1*q)<<_W2+x2 {
q--
}
}
// add y
c := digit(0);
for j := 0; j < m; j++ {
- t := c+digit(x[i+j])+digit(y[j]);
+ t := c + digit(x[i+j]) + digit(y[j]);
c, x[i+j] = t>>_W2, digit2(t&_M2);
}
assert(c+digit(x[k]) == 0);
n := len(x);
c := digit(0);
for i := 0; i < n; i++ {
- c, z[i] = x[i]>>(_W-s), x[i]<<s&_M | c
+ c, z[i] = x[i]>>(_W-s), x[i]<<s&_M|c
}
return c;
}
m := n + s/_W;
z := make(Natural, m+1);
- z[m] = shl(z[m-n : m], x, s%_W);
+ z[m] = shl(z[m-n:m], x, s%_W);
return normalize(z);
}
assert(s <= _W);
n := len(x);
c := digit(0);
- for i := n-1; i >= 0; i-- {
- c, z[i] = x[i]<<(_W-s)&_M, x[i]>>s | c
+ for i := n - 1; i >= 0; i-- {
+ c, z[i] = x[i]<<(_W-s)&_M, x[i]>>s|c
}
return c;
}
}
z := make(Natural, m);
- shr(z, x[n-m : n], s%_W);
+ shr(z, x[n-m:n], s%_W);
return normalize(z);
}
z := make(Natural, m);
for i := 0; i < m; i++ {
- z[i] = x[i]&y[i]
+ z[i] = x[i] & y[i]
}
// upper bits are 0
z := make(Natural, n);
for i := 0; i < m; i++ {
- z[i] = x[i]&^y[i]
+ z[i] = x[i] &^ y[i]
}
copy(z[m:n], x[m:n]);
z := make(Natural, n);
for i := 0; i < m; i++ {
- z[i] = x[i]|y[i]
+ z[i] = x[i] | y[i]
}
copy(z[m:n], x[m:n]);
z := make(Natural, n);
for i := 0; i < m; i++ {
- z[i] = x[i]^y[i]
+ z[i] = x[i] ^ y[i]
}
copy(z[m:n], x[m:n]);
m := len(y);
if n != m || n == 0 {
- return n-m
+ return n - m
}
- i := n-1;
+ i := n - 1;
for i > 0 && x[i] == y[i] {
i--
}
x >>= 1;
n++;
}
- return n-1;
+ return n - 1;
}
assert(0 < d && isSmall(d-1));
c := digit(0);
- for i := len(x)-1; i >= 0; i-- {
+ for i := len(x) - 1; i >= 0; i-- {
t := c<<_W + x[i];
c, x[i] = t%d, t/d;
}
// allocate buffer for conversion
assert(2 <= base && base <= 16);
- n := (x.Log2() + 1)/log2(uint64(base)) + 1; // +1: round up
+ n := (x.Log2()+1)/log2(uint64(base)) + 1; // +1: round up
s := make([]byte, n);
// don't destroy x
func hexvalue(ch byte) uint {
- d := uint(1<<logH);
+ d := uint(1 << logH);
switch {
case '0' <= ch && ch <= '9':
- d = uint(ch-'0')
+ d = uint(ch - '0')
case 'a' <= ch && ch <= 'f':
- d = uint(ch-'a')+10
+ d = uint(ch-'a') + 10
case 'A' <= ch && ch <= 'F':
- d = uint(ch-'A')+10
+ d = uint(ch-'A') + 10
}
return d;
}
func pop1(x digit) uint {
n := uint(0);
for x != 0 {
- x &= x-1;
+ x &= x - 1;
n++;
}
return n;
//
func (x Natural) Pop() uint {
n := uint(0);
- for i := len(x)-1; i >= 0; i-- {
+ for i := len(x) - 1; i >= 0; i-- {
n += pop1(x[i])
}
return n;
case a+1 == b:
return Nat(uint64(a)).Mul(Nat(uint64(b)))
}
- m := (a+b)>>1;
+ m := (a + b) >> 1;
assert(a <= m && m < b);
return MulRange(a, m).Mul(MulRange(m+1, b));
}
var slen int;
nat_eq(10, natFromString("0", 0, nil), nat_zero);
nat_eq(11, natFromString("123", 0, nil), Nat(123));
- nat_eq(12, natFromString("077", 0, nil), Nat(7*8 + 7));
- nat_eq(13, natFromString("0x1f", 0, nil), Nat(1*16 + 15));
- nat_eq(14, natFromString("0x1fg", 0, &slen), Nat(1*16 + 15));
+ nat_eq(12, natFromString("077", 0, nil), Nat(7*8+7));
+ nat_eq(13, natFromString("0x1f", 0, nil), Nat(1*16+15));
+ nat_eq(14, natFromString("0x1fg", 0, &slen), Nat(1*16+15));
test(4, slen == 4);
test_msg = "NatConvF";
int_eq(1, intFromString("-0", 0, nil), int_zero);
int_eq(2, intFromString("123", 0, nil), Int(123));
int_eq(3, intFromString("-123", 0, nil), Int(-123));
- int_eq(4, intFromString("077", 0, nil), Int(7*8 + 7));
+ int_eq(4, intFromString("077", 0, nil), Int(7*8+7));
int_eq(5, intFromString("-077", 0, nil), Int(-(7*8 + 7)));
- int_eq(6, intFromString("0x1f", 0, nil), Int(1*16 + 15));
+ int_eq(6, intFromString("0x1f", 0, nil), Int(1*16+15));
int_eq(7, intFromString("-0x1f", 0, &slen), Int(-(1*16 + 15)));
test(7, slen == 5);
int_eq(8, intFromString("+0x1f", 0, &slen), Int(+(1*16 + 15)));
test(8, slen == 5);
- int_eq(9, intFromString("0x1fg", 0, &slen), Int(1*16 + 15));
+ int_eq(9, intFromString("0x1fg", 0, &slen), Int(1*16+15));
test(9, slen == 4);
int_eq(10, intFromString("-0x1fg", 0, &slen), Int(-(1*16 + 15)));
test(10, slen == 5);
x, y := Int(e.x).Mul(ip), Int(e.y).Mul(ip);
q, r := Int(e.q), Int(e.r).Mul(ip);
qq, rr := x.QuoRem(y);
- int_eq(4*i + 0, x.Quo(y), q);
- int_eq(4*i + 1, x.Rem(y), r);
- int_eq(4*i + 2, qq, q);
- int_eq(4*i + 3, rr, r);
+ int_eq(4*i+0, x.Quo(y), q);
+ int_eq(4*i+1, x.Rem(y), r);
+ int_eq(4*i+2, qq, q);
+ int_eq(4*i+3, rr, r);
}
}
x, y := Int(e.x).Mul(ip), Int(e.y).Mul(ip);
q, r := Int(e.q), Int(e.r).Mul(ip);
qq, rr := x.DivMod(y);
- int_eq(4*i + 0, x.Div(y), q);
- int_eq(4*i + 1, x.Mod(y), r);
- int_eq(4*i + 2, qq, q);
- int_eq(4*i + 3, rr, r);
+ int_eq(4*i+0, x.Div(y), q);
+ int_eq(4*i+1, x.Mod(y), r);
+ int_eq(4*i+2, qq, q);
+ int_eq(4*i+3, rr, r);
}
}
{
const m = 3;
p := b;
- f := Nat(1<<m);
+ f := Nat(1 << m);
for i := uint(0); i < 100; i++ {
nat_eq(i, b.Shl(i*m), p);
p = mul(p, f);
{
const m = 3;
p := ip;
- f := Int(1<<m);
+ f := Int(1 << m);
for i := uint(0); i < 100; i++ {
int_eq(i, ip.Shl(i*m), p);
p = p.Mul(f);
}
test_msg = "IntShift4R";
- int_eq(0, Int(-43).Shr(1), Int(-43 >> 1));
+ int_eq(0, Int(-43).Shr(1), Int(-43>>1));
int_eq(0, Int(-1024).Shr(100), Int(-1));
int_eq(1, ip.Neg().Shr(10), ip.Neg().Div(Int(1).Shl(10)));
}
by := Nat(y);
test_msg = "NatAnd";
- bz := Nat(x&y);
+ bz := Nat(x & y);
for i := uint(0); i < 100; i++ {
nat_eq(i, bx.Shl(i).And(by.Shl(i)), bz.Shl(i))
}
test_msg = "NatAndNot";
- bz = Nat(x&^y);
+ bz = Nat(x &^ y);
for i := uint(0); i < 100; i++ {
nat_eq(i, bx.Shl(i).AndNot(by.Shl(i)), bz.Shl(i))
}
test_msg = "NatOr";
- bz = Nat(x|y);
+ bz = Nat(x | y);
for i := uint(0); i < 100; i++ {
nat_eq(i, bx.Shl(i).Or(by.Shl(i)), bz.Shl(i))
}
test_msg = "NatXor";
- bz = Nat(x^y);
+ bz = Nat(x ^ y);
for i := uint(0); i < 100; i++ {
nat_eq(i, bx.Shl(i).Xor(by.Shl(i)), bz.Shl(i))
}
};
for i := uint(0); i < uint(len(a)); i++ {
e := &a[i];
- int_eq(4*i + 0, Int(e.x).And(Int(e.y)), Int(e.x & e.y));
- int_eq(4*i + 1, Int(e.x).AndNot(Int(e.y)), Int(e.x &^ e.y));
- int_eq(4*i + 2, Int(e.x).Or(Int(e.y)), Int(e.x | e.y));
- int_eq(4*i + 3, Int(e.x).Xor(Int(e.y)), Int(e.x ^ e.y));
+ int_eq(4*i+0, Int(e.x).And(Int(e.y)), Int(e.x&e.y));
+ int_eq(4*i+1, Int(e.x).AndNot(Int(e.y)), Int(e.x&^e.y));
+ int_eq(4*i+2, Int(e.x).Or(Int(e.y)), Int(e.x|e.y));
+ int_eq(4*i+3, Int(e.x).Xor(Int(e.y)), Int(e.x^e.y));
}
}
for y := int64(-5); y < 15; y++ {
by := Int(y);
for i := uint(50); i < 70; i++ { // shift across 64bit boundary
- int_eq(2*i + 0, bx.Shl(i).AndNot(by.Shl(i)), Int(x&^y).Shl(i));
- int_eq(2*i + 1, bx.Shl(i).And(by.Shl(i).Not()), Int(x&^y).Shl(i));
+ int_eq(2*i+0, bx.Shl(i).AndNot(by.Shl(i)), Int(x&^y).Shl(i));
+ int_eq(2*i+1, bx.Shl(i).And(by.Shl(i).Not()), Int(x&^y).Shl(i));
}
}
}
mant, base, slen := NatFromString(s[i0:len(s)], base);
- return MakeInt(i0 > 0 && s[0] == '-', mant), base, i0+slen;
+ return MakeInt(i0 > 0 && s[0] == '-', mant), base, i0 + slen;
}
// Determine a scale factor f = 2^e such that
// 0.5 <= y/f == y*(2^-e) < 1.0
// and scale y accordingly.
- e := int(y.m.Log2())+1;
+ e := int(y.m.Log2()) + 1;
y.e -= e;
// t1
var c = 2.9142;
const n = 14;
- t1 := fpNat{Nat(uint64(c*(1<<n))), -n};
+ t1 := fpNat{Nat(uint64(c * (1 << n))), -n};
// Compute initial value r0 for the reciprocal of y/f.
// r0 = t1 - 2*y
// reduce mantissa size
// TODO: Find smaller bound as it will reduce
// computation time massively.
- d := int(r.m.Log2() + 1)-maxLen;
+ d := int(r.m.Log2()+1) - maxLen;
if d > 0 {
r = fpNat{r.m.Shr(uint(d)), r.e + d}
}
}
// read exponent, if any
- rlen := alen+blen;
+ rlen := alen + blen;
if rlen < len(s) {
ch := s[rlen];
if ch == 'e' || ch == 'E' {
func (b *Reader) fill() {
// Slide existing data to beginning.
if b.w > b.r {
- copySlice(b.buf[0 : b.w - b.r], b.buf[b.r : b.w]);
+ copySlice(b.buf[0:b.w-b.r], b.buf[b.r:b.w]);
b.w -= b.r;
} else {
b.w = 0
b.r = 0;
// Read new data.
- n, e := b.rd.Read(b.buf[b.w : len(b.buf)]);
+ n, e := b.rd.Read(b.buf[b.w:len(b.buf)]);
b.w += n;
if e != nil {
b.err = e
b.fill();
continue;
}
- if n > b.w - b.r {
+ if n > b.w-b.r {
n = b.w - b.r
}
- copySlice(p[0:n], b.buf[b.r : b.r + n]);
+ copySlice(p[0:n], b.buf[b.r:b.r+n]);
p = p[n:len(p)];
b.r += n;
- b.lastbyte = int(b.buf[b.r - 1]);
+ b.lastbyte = int(b.buf[b.r-1]);
nn += n;
}
return nn, nil;
// ReadRune reads a single UTF-8 encoded Unicode character and returns the
// rune and its size in bytes.
func (b *Reader) ReadRune() (rune int, size int, err os.Error) {
- for b.r + utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r : b.w]) && b.err == nil {
+ for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil {
b.fill()
}
if b.r == b.w {
}
rune, size = int(b.buf[b.r]), 1;
if rune >= 0x80 {
- rune, size = utf8.DecodeRune(b.buf[b.r : b.w])
+ rune, size = utf8.DecodeRune(b.buf[b.r:b.w])
}
b.r += size;
- b.lastbyte = int(b.buf[b.r - 1]);
+ b.lastbyte = int(b.buf[b.r-1]);
return rune, size, nil;
}
// ReadSlice returns err != nil if and only if line does not end in delim.
func (b *Reader) ReadSlice(delim byte) (line []byte, err os.Error) {
// Look in buffer.
- if i := findByte(b.buf[b.r : b.w], delim); i >= 0 {
- line1 := b.buf[b.r : b.r + i + 1];
- b.r += i+1;
+ if i := findByte(b.buf[b.r:b.w], delim); i >= 0 {
+ line1 := b.buf[b.r : b.r+i+1];
+ b.r += i + 1;
return line1, nil;
}
// Read more into buffer, until buffer fills or we find delim.
for {
if b.err != nil {
- line := b.buf[b.r : b.w];
+ line := b.buf[b.r:b.w];
b.r = b.w;
return line, b.err;
}
b.fill();
// Search new part of buffer
- if i := findByte(b.buf[n : b.w], delim); i >= 0 {
+ if i := findByte(b.buf[n:b.w], delim); i >= 0 {
line := b.buf[0 : n+i+1];
- b.r = n+i+1;
+ b.r = n + i + 1;
return line, nil;
}
buf := make([]byte, n);
n = 0;
for i := 0; i < nfull; i++ {
- copySlice(buf[n : n+len(full[i])], full[i]);
+ copySlice(buf[n:n+len(full[i])], full[i]);
n += len(full[i]);
}
- copySlice(buf[n : n+len(frag)], frag);
+ copySlice(buf[n:n+len(frag)], frag);
return buf, err;
}
if b.err != nil {
return b.err
}
- n, e := b.wr.Write(b.buf[0 : b.n]);
+ n, e := b.wr.Write(b.buf[0:b.n]);
if n < b.n && e == nil {
e = io.ErrShortWrite
}
if e != nil {
if n > 0 && n < b.n {
- copySlice(b.buf[0 : b.n - n], b.buf[n : b.n])
+ copySlice(b.buf[0:b.n-n], b.buf[n:b.n])
}
b.n -= n;
b.err = e;
if n > len(p) {
n = len(p)
}
- copySlice(b.buf[b.n : b.n + n], p[0:n]);
+ copySlice(b.buf[b.n:b.n+n], p[0:n]);
b.n += n;
nn += n;
p = p[n:len(p)];
return n, e
}
for i := 0; i < n; i++ {
- c := p[i]|0x20; // lowercase byte
+ c := p[i] | 0x20; // lowercase byte
if 'a' <= c && c <= 'm' {
p[i] += 13
} else if 'n' <= c && c <= 'z' {
str := "";
all := "";
for i := 0; i < len(texts)-1; i++ {
- texts[i] = str+"\n";
+ texts[i] = str + "\n";
all += texts[i];
str += string(i%26 + 'a');
}
// Bytes returns the contents of the unread portion of the buffer;
// len(b.Bytes()) == b.Len().
-func (b *Buffer) Bytes() []byte { return b.buf[b.off : len(b.buf)] }
+func (b *Buffer) Bytes() []byte { return b.buf[b.off:len(b.buf)] }
// String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
// Special case, useful in debugging.
return "<nil>"
}
- return string(b.buf[b.off : len(b.buf)]);
+ return string(b.buf[b.off:len(b.buf)]);
}
// Len returns the number of bytes of the unread portion of the buffer;
// Reuse buffer space.
b.off = 0
}
- b.buf = b.buf[0 : b.off + n];
+ b.buf = b.buf[0 : b.off+n];
}
// Reset resets the buffer so it has no content.
buf := b.buf;
if m+n > cap(b.buf) {
// not enough space anywhere
- buf = make([]byte, 2*cap(b.buf) + n)
+ buf = make([]byte, 2*cap(b.buf)+n)
}
- copyBytes(buf, 0, b.buf[b.off : b.off + m]);
+ copyBytes(buf, 0, b.buf[b.off:b.off+m]);
b.buf = buf;
b.off = 0;
}
- b.buf = b.buf[0 : b.off + m + n];
- copyBytes(b.buf, b.off + m, p);
+ b.buf = b.buf[0 : b.off+m+n];
+ copyBytes(b.buf, b.off+m, p);
return n, nil;
}
buf := b.buf;
if m+n > cap(b.buf) {
// not enough space anywhere
- buf = make([]byte, 2*cap(b.buf) + n)
+ buf = make([]byte, 2*cap(b.buf)+n)
}
- copyBytes(buf, 0, b.buf[b.off : b.off + m]);
+ copyBytes(buf, 0, b.buf[b.off:b.off+m]);
b.buf = buf;
b.off = 0;
}
- b.buf = b.buf[0 : b.off + m + n];
- copyString(b.buf, b.off + m, s);
+ b.buf = b.buf[0 : b.off+m+n];
+ copyString(b.buf, b.off+m, s);
return n, nil;
}
n = m
}
- copyBytes(p, 0, b.buf[b.off : b.off + n]);
+ copyBytes(p, 0, b.buf[b.off:b.off+n]);
b.off += n;
return n, err;
}
func init() {
bytes = make([]byte, N);
for i := 0; i < N; i++ {
- bytes[i] = 'a'+byte(i%26)
+ bytes[i] = 'a' + byte(i%26)
}
data = string(bytes);
}
// The initial contents of buf corresponds to the string s;
// the result is the final contents of buf returned as a string.
func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string {
- check(t, testname + " (fill 1)", buf, s);
+ check(t, testname+" (fill 1)", buf, s);
for ; n > 0; n-- {
m, err := buf.WriteString(fus);
if m != len(fus) {
- t.Errorf(testname + " (fill 2): m == %d, expected %d\n", m, len(fus))
+ t.Errorf(testname+" (fill 2): m == %d, expected %d\n", m, len(fus))
}
if err != nil {
- t.Errorf(testname + " (fill 3): err should always be nil, found err == %s\n", err)
+ t.Errorf(testname+" (fill 3): err should always be nil, found err == %s\n", err)
}
s += fus;
- check(t, testname + " (fill 4)", buf, s);
+ check(t, testname+" (fill 4)", buf, s);
}
return s;
}
// The initial contents of buf corresponds to the string s;
// the result is the final contents of buf returned as a string.
func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
- check(t, testname + " (fill 1)", buf, s);
+ check(t, testname+" (fill 1)", buf, s);
for ; n > 0; n-- {
m, err := buf.Write(fub);
if m != len(fub) {
- t.Errorf(testname + " (fill 2): m == %d, expected %d\n", m, len(fub))
+ t.Errorf(testname+" (fill 2): m == %d, expected %d\n", m, len(fub))
}
if err != nil {
- t.Errorf(testname + " (fill 3): err should always be nil, found err == %s\n", err)
+ t.Errorf(testname+" (fill 3): err should always be nil, found err == %s\n", err)
}
s += string(fub);
- check(t, testname + " (fill 4)", buf, s);
+ check(t, testname+" (fill 4)", buf, s);
}
return s;
}
// Empty buf through repeated reads into fub.
// The initial contents of buf corresponds to the string s.
func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) {
- check(t, testname + " (empty 1)", buf, s);
+ check(t, testname+" (empty 1)", buf, s);
for {
n, err := buf.Read(fub);
break
}
if err != nil {
- t.Errorf(testname + " (empty 2): err should always be nil, found err == %s\n", err)
+ t.Errorf(testname+" (empty 2): err should always be nil, found err == %s\n", err)
}
s = s[n:len(s)];
- check(t, testname + " (empty 3)", buf, s);
+ check(t, testname+" (empty 3)", buf, s);
}
- check(t, testname + " (empty 4)", buf, "");
+ check(t, testname+" (empty 4)", buf, "");
}
func TestLargeStringReads(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
- s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0 : len(data)/i]);
+ s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0:len(data)/i]);
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)));
}
check(t, "TestLargeStringReads (3)", &buf, "");
func TestLargeByteReads(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
- s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, bytes[0 : len(bytes)/i]);
+ s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, bytes[0:len(bytes)/i]);
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)));
}
check(t, "TestLargeByteReads (3)", &buf, "");
c := sep[0];
n := 0;
for i := 0; i+len(sep) <= len(s); i++ {
- if s[i] == c && (len(sep) == 1 || Equal(s[i : i+len(sep)], sep)) {
+ if s[i] == c && (len(sep) == 1 || Equal(s[i:i+len(sep)], sep)) {
n++;
- i += len(sep)-1;
+ i += len(sep) - 1;
}
}
return n;
}
c := sep[0];
for i := 0; i+n <= len(s); i++ {
- if s[i] == c && (n == 1 || Equal(s[i : i+n], sep)) {
+ if s[i] == c && (n == 1 || Equal(s[i:i+n], sep)) {
return i
}
}
return len(s)
}
c := sep[0];
- for i := len(s)-n; i >= 0; i-- {
- if s[i] == c && (n == 1 || Equal(s[i : i+n], sep)) {
+ for i := len(s) - n; i >= 0; i-- {
+ if s[i] == c && (n == 1 || Equal(s[i:i+n], sep)) {
return i
}
}
a := make([][]byte, n);
na := 0;
for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ {
- if s[i] == c && (len(sep) == 1 || Equal(s[i : i+len(sep)], sep)) {
+ if s[i] == c && (len(sep) == 1 || Equal(s[i:i+len(sep)], sep)) {
a[na] = s[start : i+sepSave];
na++;
- start = i+len(sep);
- i += len(sep)-1;
+ start = i + len(sep);
+ i += len(sep) - 1;
}
}
a[na] = s[start:len(s)];
if len(a) == 1 {
return a[0]
}
- n := len(sep)*(len(a)-1);
+ n := len(sep) * (len(a) - 1);
for i := 0; i < len(a); i++ {
n += len(a[i])
}
// HasSuffix tests whether the byte array s ends with suffix.
func HasSuffix(s, suffix []byte) bool {
- return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix) : len(s)], suffix)
+ return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix):len(s)], suffix)
}
// Map returns a copy of the byte array s with all its characters modified
rune, wid = utf8.DecodeRune(s[i:len(s)])
}
rune = mapping(rune);
- if nbytes + utf8.RuneLen(rune) > maxbytes {
+ if nbytes+utf8.RuneLen(rune) > maxbytes {
// Grow the buffer.
maxbytes = maxbytes*2 + utf8.UTFMax;
nb := make([]byte, maxbytes);
Copy(news, s);
s = news;
}
- Copy(s[lens : lens+lent], t);
+ Copy(s[lens:lens+lent], t);
return s;
}
var trimSpaceTests = []StringTest{
StringTest{"", ""},
StringTest{"abc", "abc"},
- StringTest{space+"abc"+space, "abc"},
+ StringTest{space + "abc" + space, "abc"},
StringTest{" ", ""},
StringTest{" \t\r\n \t\t\r\r\n\n ", ""},
StringTest{" \t\r\n x\t\t\r\r\n\n ", "x"},
b[i] = test.s[i]
}
b = Add(b, strings.Bytes(test.t));
- if string(b) != test.s + test.t {
+ if string(b) != test.s+test.t {
t.Errorf("Add(%q,%q) = %q", test.s, test.t, string(b))
}
}