...

Source file src/github.com/golang/geo/s2/encode.go

Documentation: github.com/golang/geo/s2

     1  // Copyright 2017 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package s2
    16  
    17  import (
    18  	"bufio"
    19  	"encoding/binary"
    20  	"io"
    21  	"math"
    22  )
    23  
    24  const (
    25  	// encodingVersion is the current version of the encoding
    26  	// format that is compatible with C++ and other S2 libraries.
    27  	encodingVersion = int8(1)
    28  
    29  	// encodingCompressedVersion is the current version of the
    30  	// compressed format.
    31  	encodingCompressedVersion = int8(4)
    32  )
    33  
    34  // encoder handles the specifics of encoding for S2 types.
    35  type encoder struct {
    36  	w   io.Writer // the real writer passed to Encode
    37  	err error
    38  }
    39  
    40  func (e *encoder) writeUvarint(x uint64) {
    41  	if e.err != nil {
    42  		return
    43  	}
    44  	var buf [binary.MaxVarintLen64]byte
    45  	n := binary.PutUvarint(buf[:], x)
    46  	_, e.err = e.w.Write(buf[:n])
    47  }
    48  
    49  func (e *encoder) writeBool(x bool) {
    50  	if e.err != nil {
    51  		return
    52  	}
    53  	var val int8
    54  	if x {
    55  		val = 1
    56  	}
    57  	e.err = binary.Write(e.w, binary.LittleEndian, val)
    58  }
    59  
    60  func (e *encoder) writeInt8(x int8) {
    61  	if e.err != nil {
    62  		return
    63  	}
    64  	e.err = binary.Write(e.w, binary.LittleEndian, x)
    65  }
    66  
    67  func (e *encoder) writeInt16(x int16) {
    68  	if e.err != nil {
    69  		return
    70  	}
    71  	e.err = binary.Write(e.w, binary.LittleEndian, x)
    72  }
    73  
    74  func (e *encoder) writeInt32(x int32) {
    75  	if e.err != nil {
    76  		return
    77  	}
    78  	e.err = binary.Write(e.w, binary.LittleEndian, x)
    79  }
    80  
    81  func (e *encoder) writeInt64(x int64) {
    82  	if e.err != nil {
    83  		return
    84  	}
    85  	e.err = binary.Write(e.w, binary.LittleEndian, x)
    86  }
    87  
    88  func (e *encoder) writeUint8(x uint8) {
    89  	if e.err != nil {
    90  		return
    91  	}
    92  	_, e.err = e.w.Write([]byte{x})
    93  }
    94  
    95  func (e *encoder) writeUint32(x uint32) {
    96  	if e.err != nil {
    97  		return
    98  	}
    99  	e.err = binary.Write(e.w, binary.LittleEndian, x)
   100  }
   101  
   102  func (e *encoder) writeUint64(x uint64) {
   103  	if e.err != nil {
   104  		return
   105  	}
   106  	e.err = binary.Write(e.w, binary.LittleEndian, x)
   107  }
   108  
   109  func (e *encoder) writeFloat32(x float32) {
   110  	if e.err != nil {
   111  		return
   112  	}
   113  	e.err = binary.Write(e.w, binary.LittleEndian, x)
   114  }
   115  
   116  func (e *encoder) writeFloat64(x float64) {
   117  	if e.err != nil {
   118  		return
   119  	}
   120  	e.err = binary.Write(e.w, binary.LittleEndian, x)
   121  }
   122  
   123  type byteReader interface {
   124  	io.Reader
   125  	io.ByteReader
   126  }
   127  
   128  func asByteReader(r io.Reader) byteReader {
   129  	if br, ok := r.(byteReader); ok {
   130  		return br
   131  	}
   132  	return bufio.NewReader(r)
   133  }
   134  
   135  type decoder struct {
   136  	r   byteReader // the real reader passed to Decode
   137  	err error
   138  	buf []byte
   139  }
   140  
   141  // Get a buffer of size 8, to avoid allocating over and over.
   142  func (d *decoder) buffer() []byte {
   143  	if d.buf == nil {
   144  		d.buf = make([]byte, 8)
   145  	}
   146  	return d.buf
   147  }
   148  
   149  func (d *decoder) readBool() (x bool) {
   150  	if d.err != nil {
   151  		return
   152  	}
   153  	var val int8
   154  	d.err = binary.Read(d.r, binary.LittleEndian, &val)
   155  	return val == 1
   156  }
   157  
   158  func (d *decoder) readInt8() (x int8) {
   159  	if d.err != nil {
   160  		return
   161  	}
   162  	d.err = binary.Read(d.r, binary.LittleEndian, &x)
   163  	return
   164  }
   165  
   166  func (d *decoder) readInt64() (x int64) {
   167  	if d.err != nil {
   168  		return
   169  	}
   170  	d.err = binary.Read(d.r, binary.LittleEndian, &x)
   171  	return
   172  }
   173  
   174  func (d *decoder) readUint8() (x uint8) {
   175  	if d.err != nil {
   176  		return
   177  	}
   178  	x, d.err = d.r.ReadByte()
   179  	return
   180  }
   181  
   182  func (d *decoder) readUint32() (x uint32) {
   183  	if d.err != nil {
   184  		return
   185  	}
   186  	d.err = binary.Read(d.r, binary.LittleEndian, &x)
   187  	return
   188  }
   189  
   190  func (d *decoder) readUint64() (x uint64) {
   191  	if d.err != nil {
   192  		return
   193  	}
   194  	d.err = binary.Read(d.r, binary.LittleEndian, &x)
   195  	return
   196  }
   197  
   198  func (d *decoder) readFloat64() float64 {
   199  	if d.err != nil {
   200  		return 0
   201  	}
   202  	buf := d.buffer()
   203  	_, d.err = io.ReadFull(d.r, buf)
   204  	return math.Float64frombits(binary.LittleEndian.Uint64(buf))
   205  }
   206  
   207  func (d *decoder) readUvarint() (x uint64) {
   208  	if d.err != nil {
   209  		return
   210  	}
   211  	x, d.err = binary.ReadUvarint(d.r)
   212  	return
   213  }
   214  

View as plain text