zeroshade commented on code in PR #344:
URL: https://github.com/apache/arrow-go/pull/344#discussion_r2064842449


##########
parquet/variants/util.go:
##########
@@ -0,0 +1,154 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package variants
+
+import (
+       "fmt"
+       "io"
+       "reflect"
+       "time"
+)
+
+// Reads a little-endian encoded uint (betwen 1 and 8 bytes wide) from a raw 
buffer at a specified
+// offset and returns its value. If any part of the read would be out of 
bounds, this returns an error.
+func readUint(raw []byte, offset, size int) (uint64, error) {
+       if size < 1 || size > 8 {
+               return 0, fmt.Errorf("invalid size, must be in range [1,8]: 
%d", size)
+       }
+       if maxPos := offset + size; maxPos > len(raw) {
+               return 0, fmt.Errorf("out of bounds: trying to access position 
%d, max position is %d", maxPos, len(raw))
+       }
+       var ret uint64
+       for i := range size {
+               ret |= uint64(raw[i+offset]) << (8 * i)
+       }
+       return ret, nil
+}
+
+// Reads a little-endian encoded integer (between 1 and 8 bytes wide) from a 
raw buffer at a specified offset.
+func readInt(raw []byte, offset, size int) (int64, error) {
+       u, err := readUint(raw, offset, size)
+       if err != nil {
+               return -1, err
+       }
+       return int64(u), nil
+}
+
+func fieldOffsetSize(maxSize int32) int {
+       if maxSize < 0xFF {
+               return 1
+       } else if maxSize < 0xFFFF {
+               return 2
+       } else if maxSize < 0xFFFFFF {
+               return 3
+       }
+       return 4
+}
+
+// Checks that a given range is in the provided raw buffer.
+func checkBounds(raw []byte, low, high int) error {
+       maxPos := len(raw)
+       if low >= maxPos {
+               return fmt.Errorf("out of bounds: trying to access position %d, 
max is %d", low, maxPos)
+       }
+       if high > maxPos {
+               return fmt.Errorf("out of bounds: trying to access position %d, 
max is %d", high, maxPos)
+       }
+       return nil
+}
+
+// Encodes a number of a specified width in little-endian format and writes to 
a writer.
+func encodeNumber(val int64, size int, w io.Writer) {
+       buf := make([]byte, size)
+       for i := range size {
+               buf[i] = byte(val)
+               val >>= 8
+       }
+       w.Write(buf)

Review Comment:
   The problem with rolling our own here is that we're going to need to manage 
the big/little endian logic ourselves then so that this runs properly on 
big-endian systems.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to