@@ -38,15 +38,16 @@ public enum Level
3838
3939
4040 // Constants.
41- const uint FLAGS_COMPRESSED_MASK = 0x80000000 ;
42- const int FLAGS_COMPRESSED_SHIFT_COUNT = 31 ;
43- const uint FLAGS_UTF8_ENCODING_SIZE_MASK = 0x7fffffff ;
41+ const uint FLAGS_DEFLATE_COMPRESSED_MASK = 0x80000000 ;
42+ const uint FLAGS_GZIP_COMPRESSED_MASK = 0x40000000 ;
43+ const uint FLAGS_COMPRESSED_MASK = FLAGS_DEFLATE_COMPRESSED_MASK | FLAGS_GZIP_COMPRESSED_MASK ;
44+ const uint FLAGS_UTF8_ENCODING_SIZE_MASK = 0x3fffffff ;
4445
4546
4647 // Static fields.
4748 static readonly long BaseSize = Memory . EstimateInstanceSize < CompressedString > ( ) ;
4849 [ ThreadStatic ]
49- static MemoryStream ? CompressionMemoryStream ;
50+ static MemoryStream ? DeflateCompressionMemoryStream ;
5051 [ ThreadStatic ]
5152 static MemoryStream ? DecompressionMemoryStream ;
5253
@@ -60,7 +61,7 @@ public enum Level
6061 // Constructor.
6162 CompressedString ( string value , Level level )
6263 {
63- if ( level == Level . None || value . Length < 32 )
64+ if ( level == Level . None || value . Length < 32 || value . Length > FLAGS_UTF8_ENCODING_SIZE_MASK )
6465 this . data = value ;
6566 else
6667 {
@@ -69,16 +70,16 @@ public enum Level
6970 this . flags = ( ( uint ) utf8Bytes . Length & FLAGS_UTF8_ENCODING_SIZE_MASK ) ;
7071 if ( level == Level . Optimal && value . Length >= 64 )
7172 {
72- CompressionMemoryStream ??= new ( ) ;
73- using ( var stream = new DeflateStream ( CompressionMemoryStream , CompressionMode . Compress , true ) )
73+ DeflateCompressionMemoryStream ??= new ( ) ;
74+ using ( var stream = new DeflateStream ( DeflateCompressionMemoryStream , CompressionLevel . SmallestSize , true ) )
7475 stream . Write ( utf8Bytes , 0 , utf8Bytes . Length ) ;
75- if ( CompressionMemoryStream . Position < utf8Bytes . Length )
76+ if ( DeflateCompressionMemoryStream . Position < utf8Bytes . Length )
7677 {
77- var compressedData = CompressionMemoryStream . ToArray ( ) ;
78+ var compressedData = DeflateCompressionMemoryStream . ToArray ( ) ;
7879 this . data = compressedData ;
79- this . flags |= ( 1u << FLAGS_COMPRESSED_SHIFT_COUNT ) ;
80+ this . flags |= FLAGS_DEFLATE_COMPRESSED_MASK ;
8081 }
81- CompressionMemoryStream . SetLength ( 0 ) ;
82+ DeflateCompressionMemoryStream . SetLength ( 0 ) ;
8283 }
8384 }
8485 this . length = value . Length ;
@@ -108,8 +109,16 @@ byte[] Decompress(byte[] bytes)
108109 DecompressionMemoryStream . Write ( bytes , 0 , bytes . Length ) ;
109110 DecompressionMemoryStream . Position = 0 ;
110111 var utf8Bytes = new byte [ ( int ) ( this . flags & FLAGS_UTF8_ENCODING_SIZE_MASK ) ] ;
111- using ( var stream = new DeflateStream ( DecompressionMemoryStream , CompressionMode . Decompress , true ) )
112- stream . Read ( utf8Bytes , 0 , utf8Bytes . Length ) ;
112+ if ( ( this . flags & FLAGS_DEFLATE_COMPRESSED_MASK ) != 0 )
113+ {
114+ using var stream = new DeflateStream ( DecompressionMemoryStream , CompressionMode . Decompress , true ) ;
115+ _ = stream . Read ( utf8Bytes , 0 , utf8Bytes . Length ) ;
116+ }
117+ else
118+ {
119+ using var stream = new GZipStream ( DecompressionMemoryStream , CompressionMode . Decompress , true ) ;
120+ _ = stream . Read ( utf8Bytes , 0 , utf8Bytes . Length ) ;
121+ }
113122 DecompressionMemoryStream . SetLength ( 0 ) ;
114123 return utf8Bytes ;
115124 }
@@ -148,21 +157,18 @@ public int GetString(Span<char> buffer, int offset = 0)
148157 /// <summary>
149158 /// Get number of characters of original string.
150159 /// </summary>
151- public int Length { get => this . length ; }
160+ public int Length => this . length ;
152161
153162
154163 /// <summary>
155164 /// Get size of compressed string in bytes.
156165 /// </summary>
157- public long Size
158- {
159- get => this . data switch
160- {
161- string str => Memory . EstimateInstanceSize ( typeof ( string ) , str . Length ) ,
162- byte [ ] bytes => Memory . EstimateArrayInstanceSize ( sizeof ( byte ) , bytes . Length ) ,
163- _ => 0 ,
164- } + BaseSize ;
165- }
166+ public long Size => this . data switch
167+ {
168+ string str => Memory . EstimateInstanceSize ( typeof ( string ) , str . Length ) ,
169+ byte [ ] bytes => Memory . EstimateArrayInstanceSize ( sizeof ( byte ) , bytes . Length ) ,
170+ _ => 0 ,
171+ } + BaseSize ;
166172
167173
168174 // Decompress to string.
0 commit comments