예제 #1
0
        public virtual void Write(string value)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
            }
            var v   = value.ToCharArray();
            int len = UTF8.GetByteCount(v);

            Write7BitEncodedInt(len);

            if (stringBuffer == null)
            {
                stringBuffer     = new byte[512];
                maxCharsPerRound = 128;
            }

            int chpos = 0;
            int chrem = value.Length;

            while (chrem > 0)
            {
                int cch  = (chrem > maxCharsPerRound) ? maxCharsPerRound : chrem;
                int blen = UTF8.Encode(v, chpos, cch, stringBuffer, 0);
                OutStream.Write(stringBuffer, 0, blen);

                chpos += cch;
                chrem -= cch;
            }
        }
예제 #2
0
        public uint GetSizeWithPadding()
        {
            int  keySpanLength = UTF8.GetByteCount(Key);
            uint totalSize     = (uint)(keySpanLength + 1 + Value.Length);
            int  paddingBytes  = (int)(3 - ((totalSize + 3) % 4));

            return((uint)(totalSize + paddingBytes));
        }
예제 #3
0
        public static byte[] StringToByteArray(string value)
        {
            if (value == null)
            {
                return(new byte[0]);
            }
            var size   = UTF8.GetByteCount(value);
            var buffer = new byte[size];

            UTF8.GetBytes(value, 0, value.Length, buffer, 0);
            return(buffer);
        }
예제 #4
0
        private static void WriteSingleRecord(SerializationState state, AbstractRecord record, Boolean forceWrite)
        {
            Int32 id;

            if (state.TryAddRecord(record, out id) || forceWrite)
            {
                // If record hasn't been previously processed, or if we are told to write contents no matter what
                switch (record.Kind)
                {
                case RecordKind.String:
                    var s   = ((StringRecord)record).StringValue;
                    var len = UTF8.GetByteCount(s);
                    state.EnsureCapacity(10 + len);
                    state.array
                    .WriteByteToBytes(ref state.idx, (Byte)RecordTypeEnumeration.BinaryObjectString)
                    .WriteInt32LEToBytes(ref state.idx, id)
                    .WriteInt32Encoded7Bit(ref state.idx, len)
                    .WriteStringToBytes(ref state.idx, UTF8, s);
                    state.WriteArrayToStream();
                    break;

                case RecordKind.Class:
                    WriteClassRecord(state, (ClassRecord)record, id);
                    break;

                case RecordKind.Array:
                    WriteArrayRecord(state, (ArrayRecord)record, id);
                    break;

                case RecordKind.PrimitiveWrapper:
                    // Write header
                    state.EnsureCapacity(2);
                    var p     = ((PrimitiveWrapperRecord)record).Value;
                    var pType = GetPrimitiveType(p);
                    state.array
                    .WriteByteToBytes(ref state.idx, (Byte)RecordTypeEnumeration.MemberPrimitiveTyped)
                    .WriteByteToBytes(ref state.idx, (Byte)pType);
                    state.WriteArrayToStream();
                    // Write primitive
                    WritePrimitive(state, p, pType);
                    break;
                }
            }
            else
            {
                // Record was already serialized, write member reference to it
                state.EnsureCapacity(5);
                state.array
                .WriteByteToBytes(ref state.idx, (Byte)RecordTypeEnumeration.MemberReference)
                .WriteInt32LEToBytes(ref state.idx, id);
                state.WriteArrayToStream();
            }
        }
예제 #5
0
        public static SafeGssNameHandle CreatePrincipal(string name)
        {
            Debug.Assert(!string.IsNullOrEmpty(name), "Invalid principal passed to SafeGssNameHandle create");
            Status status = ImportPrincipalName(
                out Status minorStatus, name, Encoding.UTF8.GetByteCount(name), out SafeGssNameHandle retHandle);

            if (status != Status.GSS_S_COMPLETE)
            {
                retHandle.Dispose();
                throw new GssApiException(status, minorStatus);
            }

            return(retHandle);
        }
#pragma warning disable IDE0060 // map isn't implemented yet, but we definitely want it
            private void WriteStringWithLengthPrefix(string value, StringMap map)
#pragma warning restore IDE0060
            {
                var writer = _writer;

                if (string.IsNullOrEmpty(value))
                {
                    writer.AdvanceAndReset(writer.ImplWriteVarint32(ref this, 0));
                }
                else
                {
                    var len = UTF8.GetByteCount(value);
                    writer.AdvanceAndReset(writer.ImplWriteVarint32(ref this, (uint)len) + len);
                    writer.ImplWriteString(ref this, value, len);
                }
            }
예제 #7
0
        public static uint WriteKeyValuePair(BinaryWriter bw, KtxKeyValuePair pair)
        {
            int         keySpanLength = UTF8.GetByteCount(pair.Key);
            Span <byte> keySpan       = stackalloc byte[keySpanLength];
            Span <byte> valueSpan     = pair.Value;

            uint totalSize    = (uint)(keySpan.Length + 1 + valueSpan.Length);
            int  paddingBytes = (int)(3 - ((totalSize + 3) % 4));

            bw.Write(totalSize);
            bw.Write(keySpan);
            bw.Write((byte)0);
            bw.Write(valueSpan);

            return((uint)(totalSize + paddingBytes));
        }
예제 #8
0
        public static void WriteLength(string s)
        {
            var codes = GetCodePoints(s).ToArray();

            Console.WriteLine("文字: " + s);
            Console.WriteLine("UTF32 len: " + codes.Length);
            Console.WriteLine("UTF16 len: " + s.Length);
            Console.WriteLine("UTF8  len: " + UTF8.GetByteCount(s));

            foreach (var c in codes)
            {
                Console.WriteLine($"{c.value.ToString("X"),6} / {Join(c.utf32)} / {Join(c.utf16)} / {Join(c.utf8)}");
            }

            Console.Read();
        }
        public static IntPtr CreateMarker(string name, ushort categoryId, MarkerFlags flags, int metadataCount)
        {
#if ENABLE_PROFILER
            int textBytes = UTF8.GetByteCount(name);
            unsafe
            {
                byte *bytes = stackalloc byte[textBytes];
                fixed(char *t = name)
                {
                    UTF8.GetBytes(t, name.Length, bytes, textBytes);
                }

                return((IntPtr)Development.Profiling.Profiler.MarkerGetOrCreate(categoryId, bytes, textBytes, (ushort)(flags | MarkerFlags.Script)));
            }
#else
            return(IntPtr.Zero);
#endif
        }
예제 #10
0
        /// <summary>
        /// Calculates the number of bytes produced by encoding the string.
        /// </summary>
        /// <param name="s">The string to encode</param>
        public override int GetByteCount(string s)
        {
            int numbytes       = 0;
            var textEnumerator = StringInfo.GetTextElementEnumerator(s);

            while (textEnumerator.MoveNext())
            {
                var elem = textEnumerator.GetTextElement();
                if (_currentMap.ContainsKey(elem))
                {
                    numbytes += ((long)_currentMap[elem]).Size();
                }
                else
                {
                    numbytes += UTF8.GetByteCount(elem);
                }
            }
            return(numbytes);
        }
예제 #11
0
 private static void WriteAssemblyName(SerializationState state, ElementWithTypeInfo element)
 {
     if (element != null)
     {
         var   assName = element.AssemblyName;
         Int32 id;
         if (state.TryAddAssemblyName(assName, out id))
         {
             var strByteCount = UTF8.GetByteCount(assName);
             state.EnsureCapacity(10 + strByteCount);
             state.array
             .WriteByteToBytes(ref state.idx, (Byte)RecordTypeEnumeration.BinaryLibrary)
             .WriteInt32LEToBytes(ref state.idx, id)
             .WriteInt32Encoded7Bit(ref state.idx, strByteCount)
             .WriteStringToBytes(ref state.idx, UTF8, assName);
             state.WriteArrayToStream();
         }
     }
 }
        public static void Log(string text, LogType type = LogType.Log)
        {
            if (text.Length == 0)
            {
                return;
            }

            int textBytes = UTF8.GetByteCount(text);

            unsafe
            {
                byte *textBuf = stackalloc byte[textBytes];

                fixed(char *t = text)
                {
                    UTF8.GetBytes(t, text.Length, textBuf, textBytes);
                    Log(textBuf, textBytes, type);
                }
            }
        }
예제 #13
0
        /// <summary>
        /// Calculates the number of bytes produced by encoding the string.
        /// </summary>
        /// <param name="s">The string to encode</param>
        public override int GetByteCount(string s)
        {
            var numbytes = 0;
            TextElementEnumerator textEnumerator = StringInfo.GetTextElementEnumerator(s);

            while (textEnumerator.MoveNext())
            {
                var elem = textEnumerator.GetTextElement();
                var cu   = char.ConvertToUtf32(elem, 0);
                if (_currentMap.ContainsKey(cu))
                {
                    numbytes += _currentMap[cu].Size();
                }
                else
                {
                    numbytes += UTF8.GetByteCount(elem);
                }
            }
            return(numbytes);
        }
예제 #14
0
 static void w(this FileStream fs, string s)
 {
     fs.Write(UTF8.GetBytes(s + "\n"), 0, UTF8.GetByteCount(s) + 1);
 }
예제 #15
0
        /// <summary>
        /// Decodes a sequence of bytes from the specified byte array
        /// into the specified character array.
        /// </summary>
        /// <param name="bytes">The byte array containing the sequence of bytes to decode</param>
        /// <param name="byteIndex">The index of the first byte to decode</param>
        /// <param name="byteCount">The number of bytes to decode</param>
        /// <param name="chars">The character array to contain the resulting set of characters</param>
        /// <param name="charIndex">The index at which to start writing the resulting set of characters</param>
        /// <returns>The actual number of characters written into chars.</returns>
        /// <exception cref="T:System.ArgumentNullException">System.ArgumentNullException</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">System.ArgumentOutOfRangeException</exception>
        /// <exception cref="T:System.ArgumentException">System.ArgumentException</exception>
        /// <exception cref="T:System.IndexOutOfRangeException">System.IndexOutOfRangeException</exception>
        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            int j = charIndex;
            int i = 0;

            while (i < byteCount)
            {
                int displ    = 0;
                int encoding = 0;
                if (i + 3 + byteIndex < byteCount)
                {
                    encoding = bytes[i + byteIndex] |
                               (bytes[i + 1 + byteIndex] * 0x100) |
                               (bytes[i + 2 + byteIndex] * 0x10000) |
                               (bytes[i + 3 + byteIndex] * 0x1000000);
                    if (_currentMap.ContainsValue(encoding))
                    {
                        displ = 4;
                        goto SetChar;
                    }
                }
                if (i + 2 + byteIndex < byteCount)
                {
                    encoding = bytes[i + byteIndex] |
                               (bytes[i + 1 + byteIndex] * 0x100) |
                               (bytes[i + 2 + byteIndex] * 0x10000);
                    if (_currentMap.ContainsValue(encoding))
                    {
                        displ = 3;
                        goto SetChar;
                    }
                }
                if (i + 1 + byteIndex < byteCount)
                {
                    encoding = bytes[i + byteIndex] |
                               (bytes[i + 1 + byteIndex] * 0x100);
                    if (_currentMap.ContainsValue(encoding))
                    {
                        displ = 2;
                        goto SetChar;
                    }
                }
                encoding = bytes[i + byteIndex];
                if (_currentMap.ContainsValue(encoding))
                {
                    displ = 1;
                    goto SetChar;
                }

                var count    = 1;
                var utfChars = UTF8.GetChars(bytes.Skip(i).ToArray(), 0, byteCount - i);

                if (char.IsSurrogate(utfChars.First()))
                {
                    count++;
                }

                utfChars = utfChars.Take(count).ToArray();
                foreach (var utfChar in utfChars)
                {
                    chars[j++] = utfChar;
                }

                i += UTF8.GetByteCount(utfChars);
                continue;

SetChar:
                var key = _currentMap.First(e => e.Value.Equals(encoding)).Key;
                var utfchars = key.ToCharArray();
                foreach (var utfc in utfchars)
                {
                    chars[j++] = utfc;
                }
                i += displ;
            }
            return(j - charIndex);
        }
예제 #16
0
 public static int GetByteCount(string val) => string.IsNullOrEmpty(val) ? 0 : UTF8.GetByteCount(val);
예제 #17
0
 public static int GetByteCount(ReadOnlySpan <char> val) => val.IsEmpty ? 0 : UTF8.GetByteCount(val);
예제 #18
0
 //-------------------------------------------------------------------------
 //-------------------------------------------------------------------------
 public override int GetByteCount(char[] chars, int index, int count)
 {
     return(UTF8.GetByteCount(chars, index, count));
 }
예제 #19
0
        private static void WritePrimitive(SerializationState state, Object primitive, PrimitiveTypeEnumeration pType)
        {
            String s;
            Int32  len;

            switch (pType)
            {
            case PrimitiveTypeEnumeration.Boolean:
                state.EnsureCapacity(1);
                state.array.WriteByteToBytes(ref state.idx, ((Boolean)primitive) == true ? (Byte)1 : (Byte)0);
                break;

            case PrimitiveTypeEnumeration.Byte:
                state.EnsureCapacity(1);
                state.array.WriteByteToBytes(ref state.idx, (Byte)primitive);
                break;

            case PrimitiveTypeEnumeration.Char:
                state.EnsureCapacity(4);
                state.idx = UTF8.GetBytes(new[] { (Char)primitive }, 0, 1, state.array, 0);
                break;

            case PrimitiveTypeEnumeration.Decimal:
                var d = (Decimal)primitive;
                s   = d.ToString();
                len = UTF8.GetByteCount(s);
                var ints = Decimal.GetBits(d);
                state.EnsureCapacity(5 + len + 16);
                state.array
                .WriteInt32Encoded7Bit(ref state.idx, len)
                .WriteStringToBytes(ref state.idx, UTF8, s)
                .WriteInt32LEToBytes(ref state.idx, ints[0])
                .WriteInt32LEToBytes(ref state.idx, ints[1])
                .WriteInt32LEToBytes(ref state.idx, ints[2])
                .WriteInt32LEToBytes(ref state.idx, ints[3]);
                break;

            case PrimitiveTypeEnumeration.Double:
                state.EnsureCapacity(8);
                state.array.WriteDoubleLEToBytes(ref state.idx, (Double)primitive);
                break;

            case PrimitiveTypeEnumeration.Int16:
                state.EnsureCapacity(2);
                state.array.WriteInt16LEToBytes(ref state.idx, (Int16)primitive);
                break;

            case PrimitiveTypeEnumeration.Int32:
                state.EnsureCapacity(4);
                state.array.WriteInt32LEToBytes(ref state.idx, (Int32)primitive);
                break;

            case PrimitiveTypeEnumeration.Int64:
                state.EnsureCapacity(8);
                state.array.WriteInt64LEToBytes(ref state.idx, (Int64)primitive);
                break;

            case PrimitiveTypeEnumeration.SByte:
                state.EnsureCapacity(1);
                state.array.WriteSByteToBytes(ref state.idx, (SByte)primitive);
                break;

            case PrimitiveTypeEnumeration.Single:
                state.EnsureCapacity(4);
                state.array.WriteSingleLEToBytes(ref state.idx, (Single)primitive);
                break;

            case PrimitiveTypeEnumeration.TimeSpan:
                state.EnsureCapacity(8);
                state.array.WriteInt64LEToBytes(ref state.idx, ((TimeSpan)primitive).Ticks);
                break;

            case PrimitiveTypeEnumeration.DateTime:
                state.EnsureCapacity(8);
                state.array.WriteInt64LEToBytes(ref state.idx, ((DateTime)primitive).Ticks);
                break;

            case PrimitiveTypeEnumeration.UInt16:
                state.EnsureCapacity(2);
                state.array.WriteUInt16LEToBytes(ref state.idx, (UInt16)primitive);
                break;

            case PrimitiveTypeEnumeration.UInt32:
                state.EnsureCapacity(4);
                state.array.WriteUInt32LEToBytes(ref state.idx, (UInt32)primitive);
                break;

            case PrimitiveTypeEnumeration.UInt64:
                state.EnsureCapacity(8);
                state.array.WriteUInt64LEToBytes(ref state.idx, (UInt64)primitive);
                break;

            case PrimitiveTypeEnumeration.Null:
                state.EnsureCapacity(0);
                break;

            case PrimitiveTypeEnumeration.String:
                s   = (String)primitive;
                len = UTF8.GetByteCount(s);
                state.EnsureCapacity(5 + len);
                state.array
                .WriteInt32Encoded7Bit(ref state.idx, len)
                .WriteStringToBytes(ref state.idx, UTF8, s);
                break;

            default:
                state.EnsureCapacity(0);
                break;
            }
            state.WriteArrayToStream();
        }