<commit_before>﻿// This file contains substantial code from an MIT Licensed codebase (udpkit). The license for udpkit is as follows:

//The MIT License (MIT)

//Copyright (c) 2012-2014 <NAME> (<EMAIL>)

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

namespace MassiveNet {

    [StructLayout(LayoutKind.Explicit)]
    internal struct FpBytes {
        [FieldOffset(0)]
        public Single Float;
        [FieldOffset(0)]
        public Double Double;
        [FieldOffset(0)]
        public Byte B0;
        [FieldOffset(1)]
        public Byte B1;
        [FieldOffset(2)]
        public Byte B2;
        [FieldOffset(3)]
        public Byte B3;
        [FieldOffset(4)]
        public Byte B4;
        [FieldOffset(5)]
        public Byte B5;
        [FieldOffset(6)]
        public Byte B6;
        [FieldOffset(7)]
        public Byte B7;

        public static implicit operator FpBytes(Single value) {
            var bytes = default(FpBytes);
            bytes.Float = value;
            return bytes;
        }

        public static implicit operator FpBytes(Double value) {
            var bytes = default(FpBytes);
            bytes.Double = value;
            return bytes;
        }
    }

    public class NetStream {

        internal NetConnection Connection;
        internal NetSocket Socket;
        internal int Pos;
        internal int Length;
        internal byte[] Data;

        internal bool WriteLength = false;

        /// <summary>
        /// When true, all floats written will use half precision. This value must be the same for client and server. 
        /// </summary>
        public static bool HalfFloats = true;

        private NetStream() { }

        /// <summary> The current size of the stream (in bits). </summary>
        public int Size {
            get { return Length; }
            internal set { Length = Mathf.Clamp(value, 0, Data.Length << 3); }
        }

        /// <summary> There current read/write position of the stream (in bits). </summary>
        public int Position {
            get { return Pos; }
            set { Pos = Mathf.Clamp(value, 0, Length); }
        }

        internal byte[] ByteBuffer {
            get { return Data; }
        }

        /// <summary> Creates a new NetStream, recycling from the pool when possible. </summary>
        internal static NetStream Create() {
            return CreateFromPool();
        }

        /// <summary> Creates a new NetStream, recycling from the pool when possible. </summary>
        public static NetStream New() {
            var strm = CreateFromPool();
            strm.WriteLength = true;
            return strm;
        }

        private NetStream(byte[] arr) {
            Pos = 0;
            Data = arr;
            Length = arr.Length << 3;
        }

        internal bool CanWrite(int bits) {
            return Pos + bits <= Length;
        }

        internal bool CanRead(int bits) {
            return Pos + bits <= Length;
        }

        internal void Reset() {
            Reset(Data.Length);
        }

        internal void Reset(int size) {
            Pos = 0;
            Length = size << 3;
            Array.Clear(Data, 0, Data.Length);
        }

        public bool WriteBool(bool value) {
            InternalWriteByte(value ? (byte)1 : (byte)0, 1);
            return value;
        }

        public bool ReadBool() {
            return InternalReadByte(1) == 1;
        }

        internal void WriteByte(byte value, int bits) {
            InternalWriteByte(value, bits);
        }

        internal byte ReadByte(int bits) {
            return InternalReadByte(bits);
        }

        public void WriteByte(byte value) {
            WriteByte(value, 8);
        }

        public byte ReadByte() {
            return ReadByte(8);
        }

        internal void WriteSByte(sbyte value, int bits) {
            InternalWriteByte((byte)value, bits);
        }

        internal sbyte ReadSByte(int bits) {
            return (sbyte)InternalReadByte(bits);
        }

        public void WriteSByte(sbyte value) {
            WriteSByte(value, 8);
        }

        public sbyte ReadSByte() {
            return ReadSByte(8);
        }

        internal void WriteUShort(ushort value, int bits) {
            if (bits <= 8) InternalWriteByte((byte)(value & 0xFF), bits);
            InternalWriteByte((byte)(value & 0xFF), 8);
            InternalWriteByte((byte)(value >> 8), bits - 8);
        }

        internal ushort ReadUShort(int bits) {
            if (bits <= 8) return InternalReadByte(bits);
            return (ushort)(InternalReadByte(8) | (InternalReadByte(bits - 8) << 8));
        }

        public void WriteUShort(ushort value) {
            WriteUShort(value, 16);<commit_msg>Fix bug in NetStream.WriteUShort for bits <= 8<commit_after>﻿// This file contains substantial code from an MIT Licensed codebase (udpkit). The license for udpkit is as follows:

//The MIT License (MIT)

//Copyright (c) 2012-2014 <NAME> (<EMAIL>)

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

namespace MassiveNet {

    [StructLayout(LayoutKind.Explicit)]
    internal struct FpBytes {
        [FieldOffset(0)]
        public Single Float;
        [FieldOffset(0)]
        public Double Double;
        [FieldOffset(0)]
        public Byte B0;
        [FieldOffset(1)]
        public Byte B1;
        [FieldOffset(2)]
        public Byte B2;
        [FieldOffset(3)]
        public Byte B3;
        [FieldOffset(4)]
        public Byte B4;
        [FieldOffset(5)]
        public Byte B5;
        [FieldOffset(6)]
        public Byte B6;
        [FieldOffset(7)]
        public Byte B7;

        public static implicit operator FpBytes(Single value) {
            var bytes = default(FpBytes);
            bytes.Float = value;
            return bytes;
        }

        public static implicit operator FpBytes(Double value) {
            var bytes = default(FpBytes);
            bytes.Double = value;
            return bytes;
        }
    }

    public class NetStream {

        internal NetConnection Connection;
        internal NetSocket Socket;
        internal int Pos;
        internal int Length;
        internal byte[] Data;

        internal bool WriteLength = false;

        /// <summary>
        /// When true, all floats written will use half precision. This value must be the same for client and server. 
        /// </summary>
        public static bool HalfFloats = true;

        private NetStream() { }

        /// <summary> The current size of the stream (in bits). </summary>
        public int Size {
            get { return Length; }
            internal set { Length = Mathf.Clamp(value, 0, Data.Length << 3); }
        }

        /// <summary> There current read/write position of the stream (in bits). </summary>
        public int Position {
            get { return Pos; }
            set { Pos = Mathf.Clamp(value, 0, Length); }
        }

        internal byte[] ByteBuffer {
            get { return Data; }
        }

        /// <summary> Creates a new NetStream, recycling from the pool when possible. </summary>
        internal static NetStream Create() {
            return CreateFromPool();
        }

        /// <summary> Creates a new NetStream, recycling from the pool when possible. </summary>
        public static NetStream New() {
            var strm = CreateFromPool();
            strm.WriteLength = true;
            return strm;
        }

        private NetStream(byte[] arr) {
            Pos = 0;
            Data = arr;
            Length = arr.Length << 3;
        }

        internal bool CanWrite(int bits) {
            return Pos + bits <= Length;
        }

        internal bool CanRead(int bits) {
            return Pos + bits <= Length;
        }

        internal void Reset() {
            Reset(Data.Length);
        }

        internal void Reset(int size) {
            Pos = 0;
            Length = size << 3;
            Array.Clear(Data, 0, Data.Length);
        }

        public bool WriteBool(bool value) {
            InternalWriteByte(value ? (byte)1 : (byte)0, 1);
            return value;
        }

        public bool ReadBool() {
            return InternalReadByte(1) == 1;
        }

        internal void WriteByte(byte value, int bits) {
            InternalWriteByte(value, bits);
        }

        internal byte ReadByte(int bits) {
            return InternalReadByte(bits);
        }

        public void WriteByte(byte value) {
            WriteByte(value, 8);
        }

        public byte ReadByte() {
            return ReadByte(8);
        }

        internal void WriteSByte(sbyte value, int bits) {
            InternalWriteByte((byte)value, bits);
        }

        internal sbyte ReadSByte(int bits) {
            return (sbyte)InternalReadByte(bits);
        }

        public void WriteSByte(sbyte value) {
            WriteSByte(value, 8);
        }

        public sbyte ReadSByte() {
            return ReadSByte(8);
        }

        internal void WriteUShort(ushort value, int bits) {
            if (bits <= 8) InternalWriteByte((byte)(value & 0xFF), bits);
            else {
                InternalWriteByte((byte)(value & 0xFF), 8);
                InternalWriteByte((byte)(value >> 8), bits - 8);
            }
        }

        internal ushort ReadUShort(int bits) {
            if (bits <= 8) return InternalReadByte(bits);
            return (ushort)(InternalReadByte(8) | (InternalReadByte(bits - 8) << 8));
        }

        public void WriteUShort(ushort value) {
            WriteUShort(value, 16);
{'fraction_non_alphanumeric': 0.08976736345246122, 'fraction_numerical': 0.00935603506405934, 'mean_word_length': 1.96625, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4084239', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3013, 'n_words': 1128}