func AppendUndefinedElement(dst []byte, key string) []byte {
	return AppendHeader(dst, bsontype.Undefined, key)
}
AppendUndefinedElement will append a BSON undefined element using key 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
}