Esempio n. 1
0
 private static byte[] CompressUnsignedInteger(int value)
 {
     var stream = new MemoryStream();
     var writer = new BinaryWriter(stream);
     writer.WriteCompressedUInt((uint)value);
     return stream.ToArray();
 }
Esempio n. 2
0
        internal uint GetBlobIndex(ImmutableArray <byte> blob)
        {
            uint result = 0;

            if (blob.Length == 0 || _blobIndex.TryGetValue(blob, out result))
            {
                return(result);
            }

            Debug.Assert(!_streamsAreComplete);
            result = _blobWriter.BaseStream.Position + (uint)_blobIndexStartOffset;
            _blobIndex.Add(blob, result);
            _blobWriter.WriteCompressedUInt((uint)blob.Length);
            _blobWriter.WriteBytes(blob);
            return(result);
        }
Esempio n. 3
0
        public uint GetUserStringToken(string str)
        {
            uint index;

            if (!_userStringIndex.TryGetValue(str, out index))
            {
                Debug.Assert(!_streamsAreComplete);
                index = _userStringWriter.BaseStream.Position + (uint)_userStringIndexStartOffset;
                _userStringIndex.Add(str, index);
                _userStringWriter.WriteCompressedUInt((uint)str.Length * 2 + 1);
                _userStringWriter.WriteChars(str.ToCharArray());

                // Write out a trailing byte indicating if the string is really quite simple
                byte stringKind = 0;
                foreach (char ch in str)
                {
                    if (ch >= 0x7F)
                    {
                        stringKind = 1;
                    }
                    else
                    {
                        switch ((int)ch)
                        {
                        case 0x1:
                        case 0x2:
                        case 0x3:
                        case 0x4:
                        case 0x5:
                        case 0x6:
                        case 0x7:
                        case 0x8:
                        case 0xE:
                        case 0xF:
                        case 0x10:
                        case 0x11:
                        case 0x12:
                        case 0x13:
                        case 0x14:
                        case 0x15:
                        case 0x16:
                        case 0x17:
                        case 0x18:
                        case 0x19:
                        case 0x1A:
                        case 0x1B:
                        case 0x1C:
                        case 0x1D:
                        case 0x1E:
                        case 0x1F:
                        case 0x27:
                        case 0x2D:
                            stringKind = 1;
                            break;

                        default:
                            continue;
                        }
                    }

                    break;
                }

                _userStringWriter.WriteByte(stringKind);
            }

            return(0x70000000 | index);
        }