func AppendDBPointerElement(dst []byte, key, ns string, oid primitive.ObjectID) []byte {
	return AppendDBPointer(AppendHeader(dst, bsontype.DBPointer, key), ns, oid)
}
AppendDBPointerElement will append a BSON DBPointer element using key, ns,
and oid to dst and return the extended buffer.
package bsoncore  
import (
	"bytes"
	"fmt"
	"math"
	"strconv"
	"strings"
	"time"
	"go.mongodb.org/mongo-driver/bson/bsontype"
	"go.mongodb.org/mongo-driver/bson/primitive"
)
const (
	EmptyDocumentLength = 5
	nullTerminator       = string(byte(0))
	invalidKeyPanicMsg   = "BSON element keys cannot contain null bytes"
	invalidRegexPanicMsg = "BSON regex values cannot contain null bytes"
)
func AppendType(dst []byte, t bsontype.Type) []byte { return append(dst, byte(t)) }
func AppendKey(dst []byte, key string) []byte { return append(dst, key+nullTerminator...) }
func AppendHeader(dst []byte, t bsontype.Type, key string) []byte {
	if !isValidCString(key) {
		panic(invalidKeyPanicMsg)
	}
	dst = AppendType(dst, t)
	dst = append(dst, key...)
	return append(dst, 0x00)
}
func ReadType(src []byte) (bsontype.Type, []byte, bool) {
	if len(src) < 1 {
		return 0, src, false
	}
	return bsontype.Type(src[0]), src[1:], true
}
func ReadKey(src []byte) (string, []byte, bool) { return readcstring(src) }
func ReadKeyBytes(src []byte) ([]byte, []byte, bool) { return readcstringbytes(src) }
func ReadHeader(src []byte) (t bsontype.Type, key string, rem []byte, ok bool) {
	t, rem, ok = ReadType(src)
	if !ok {
		return 0, "", src, false
	}
	key, rem, ok = ReadKey(rem)
	if !ok {
		return 0, "", src, false
	}
	return t, key, rem, true
}
func ReadHeaderBytes(src []byte) (header []byte, rem []byte, ok bool) {
	if len(src) < 1 {
		return nil, src, false
	}
	idx := bytes.IndexByte(src[1:], 0x00)
	if idx == -1 {
		return nil, src, false
	}
	return src[:idx], src[idx+1:], true
}
func ReadElement(src []byte) (Element, []byte, bool) {
	if len(src) < 1 {
		return nil, src, false
	}
	t := bsontype.Type(src[0])
	idx := bytes.IndexByte(src[1:], 0x00)
	if idx == -1 {
		return nil, src, false
	}
	length, ok := valueLength(src[idx+2:], t)  
	if !ok {
		return nil, src, false
	}
	elemLength := 1 + idx + 1 + int(length)
	if elemLength > len(src) {
		return nil, src, false
	}
	if elemLength < 0 {
		return nil, src, false
	}
	return src[:elemLength], src[elemLength:], true
}
func AppendValueElement(dst []byte, key string, value Value) []byte {
	dst = AppendHeader(dst, value.Type, key)
	dst = append(dst, value.Data...)
	return dst
}
func ReadValue(src []byte, t bsontype.Type) (Value, []byte, bool) {
	data, rem, ok := readValue(src, t)
	if !ok {
		return Value{}, src, false
	}
	return Value{Type: t, Data: data}, rem, true
}
func AppendDouble(dst []byte, f float64) []byte {
	return appendu64(dst, math.Float64bits(f))
}
func AppendDoubleElement(dst []byte, key string, f float64) []byte {
	return AppendDouble(AppendHeader(dst, bsontype.Double, key), f)
}
func ReadDouble(src []byte) (float64, []byte, bool) {
	bits, src, ok := readu64(src)
	if !ok {
		return 0, src, false
	}
	return math.Float64frombits(bits), src, true
}
func AppendString(dst []byte, s string) []byte {
	return appendstring(dst, s)
}
func AppendStringElement(dst []byte, key, val string) []byte {
	return AppendString(AppendHeader(dst, bsontype.String, key), val)
}
func ReadString(src []byte) (string, []byte, bool) {
	return readstring(src)
}
func AppendDocumentStart(dst []byte) (index int32, b []byte) { return ReserveLength(dst) }
func AppendDocumentStartInline(dst []byte, index *int32) []byte {
	idx, doc := AppendDocumentStart(dst)
	*index = idx
	return doc
}
func AppendDocumentElementStart(dst []byte, key string) (index int32, b []byte) {
	return AppendDocumentStart(AppendHeader(dst, bsontype.EmbeddedDocument, key))
}
func AppendDocumentEnd(dst []byte, index int32) ([]byte, error) {
	if int(index) > len(dst)-4 {
		return dst, fmt.Errorf("not enough bytes available after index to write length")
	}
	dst = append(dst, 0x00)
	dst = UpdateLength(dst, index, int32(len(dst[index:])))
	return dst, nil
}
func AppendDocument(dst []byte, doc []byte) []byte { return append(dst, doc...) }
func AppendDocumentElement(dst []byte, key string, doc []byte) []byte {
	return AppendDocument(AppendHeader(dst, bsontype.EmbeddedDocument, key), doc)
}
func BuildDocument(dst []byte, elems ...[]byte) []byte {
	idx, dst := ReserveLength(dst)
	for _, elem := range elems {
		dst = append(dst, elem...)
	}
	dst = append(dst, 0x00)
	dst = UpdateLength(dst, idx, int32(len(dst[idx:])))
	return dst
}
func BuildDocumentValue(elems ...[]byte) Value {
	return Value{Type: bsontype.EmbeddedDocument, Data: BuildDocument(nil, elems...)}
}
func BuildDocumentElement(dst []byte, key string, elems ...[]byte) []byte {
	return BuildDocument(AppendHeader(dst, bsontype.EmbeddedDocument, key), elems...)
}
var BuildDocumentFromElements = BuildDocument
func ReadDocument(src []byte) (doc Document, rem []byte, ok bool) { return readLengthBytes(src) }
func AppendArrayStart(dst []byte) (index int32, b []byte) { return ReserveLength(dst) }
func AppendArrayElementStart(dst []byte, key string) (index int32, b []byte) {
	return AppendArrayStart(AppendHeader(dst, bsontype.Array, key))
}
func AppendArrayEnd(dst []byte, index int32) ([]byte, error) { return AppendDocumentEnd(dst, index) }
func AppendArray(dst []byte, arr []byte) []byte { return append(dst, arr...) }
func AppendArrayElement(dst []byte, key string, arr []byte) []byte {
	return AppendArray(AppendHeader(dst, bsontype.Array, key), arr)
}
func BuildArray(dst []byte, values ...Value) []byte {
	idx, dst := ReserveLength(dst)
	for pos, val := range values {
		dst = AppendValueElement(dst, strconv.Itoa(pos), val)
	}
	dst = append(dst, 0x00)
	dst = UpdateLength(dst, idx, int32(len(dst[idx:])))
	return dst
}
func BuildArrayElement(dst []byte, key string, values ...Value) []byte {
	return BuildArray(AppendHeader(dst, bsontype.Array, key), values...)
}
func ReadArray(src []byte) (arr Document, rem []byte, ok bool) { return readLengthBytes(src) }
func AppendBinary(dst []byte, subtype byte, b []byte) []byte {
	if subtype == 0x02 {
		return appendBinarySubtype2(dst, subtype, b)
	}
	dst = append(appendLength(dst, int32(len(b))), subtype)
	return append(dst, b...)
}
func AppendBinaryElement(dst []byte, key string, subtype byte, b []byte) []byte {
	return AppendBinary(AppendHeader(dst, bsontype.Binary, key), subtype, b)
}
func ReadBinary(src []byte) (subtype byte, bin []byte, rem []byte, ok bool) {
	length, rem, ok := ReadLength(src)
	if !ok {
		return 0x00, nil, src, false
	}
	if len(rem) < 1 {  
		return 0x00, nil, src, false
	}
	subtype, rem = rem[0], rem[1:]
	if len(rem) < int(length) {
		return 0x00, nil, src, false
	}
	if subtype == 0x02 {
		length, rem, ok = ReadLength(rem)
		if !ok || len(rem) < int(length) {
			return 0x00, nil, src, false
		}
	}
	return subtype, rem[:length], rem[length:], true
}
func AppendUndefinedElement(dst []byte, key string) []byte {
	return AppendHeader(dst, bsontype.Undefined, key)
}
func AppendObjectID(dst []byte, oid primitive.ObjectID) []byte { return append(dst, oid[:]...) }
func AppendObjectIDElement(dst []byte, key string, oid primitive.ObjectID) []byte {
	return AppendObjectID(AppendHeader(dst, bsontype.ObjectID, key), oid)
}
func ReadObjectID(src []byte) (primitive.ObjectID, []byte, bool) {
	if len(src) < 12 {
		return primitive.ObjectID{}, src, false
	}
	var oid primitive.ObjectID
	copy(oid[:], src[0:12])
	return oid, src[12:], true
}
func AppendBoolean(dst []byte, b bool) []byte {
	if b {
		return append(dst, 0x01)
	}
	return append(dst, 0x00)
}
func AppendBooleanElement(dst []byte, key string, b bool) []byte {
	return AppendBoolean(AppendHeader(dst, bsontype.Boolean, key), b)
}
func ReadBoolean(src []byte) (bool, []byte, bool) {
	if len(src) < 1 {
		return false, src, false
	}
	return src[0] == 0x01, src[1:], true
}
func AppendDateTime(dst []byte, dt int64) []byte { return appendi64(dst, dt) }
func AppendDateTimeElement(dst []byte, key string, dt int64) []byte {
	return AppendDateTime(AppendHeader(dst, bsontype.DateTime, key), dt)
}
func ReadDateTime(src []byte) (int64, []byte, bool) { return readi64(src) }
func AppendTime(dst []byte, t time.Time) []byte {
	return AppendDateTime(dst, t.Unix()*1000+int64(t.Nanosecond()/1e6))
}
func AppendTimeElement(dst []byte, key string, t time.Time) []byte {
	return AppendTime(AppendHeader(dst, bsontype.DateTime, key), t)
}
func ReadTime(src []byte) (time.Time, []byte, bool) {
	dt, rem, ok := readi64(src)
	return time.Unix(dt/1e3, dt%1e3*1e6), rem, ok
}
func AppendNullElement(dst []byte, key string) []byte { return AppendHeader(dst, bsontype.Null, key) }
func AppendRegex(dst []byte, pattern, options string) []byte {
	if !isValidCString(pattern) || !isValidCString(options) {
		panic(invalidRegexPanicMsg)
	}
	return append(dst, pattern+nullTerminator+options+nullTerminator...)
}
func AppendRegexElement(dst []byte, key, pattern, options string) []byte {
	return AppendRegex(AppendHeader(dst, bsontype.Regex, key), pattern, options)
}
func ReadRegex(src []byte) (pattern, options string, rem []byte, ok bool) {
	pattern, rem, ok = readcstring(src)
	if !ok {
		return "", "", src, false
	}
	options, rem, ok = readcstring(rem)
	if !ok {
		return "", "", src, false
	}
	return pattern, options, rem, true
}
func AppendDBPointer(dst []byte, ns string, oid primitive.ObjectID) []byte {
	return append(appendstring(dst, ns), oid[:]...)
}