1
1
using MemoryPack . Internal ;
2
+ using System . Runtime . CompilerServices ;
2
3
3
4
namespace MemoryPack . Formatters ;
4
5
@@ -26,12 +27,14 @@ public sealed class Utf8StringFormatter : MemoryPackFormatter<string>
26
27
public static readonly Utf8StringFormatter Default = new Utf8StringFormatter ( ) ;
27
28
28
29
[ Preserve ]
30
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
29
31
public override void Serialize < TBufferWriter > ( ref MemoryPackWriter < TBufferWriter > writer , scoped ref string ? value )
30
32
{
31
33
writer . WriteUtf8 ( value ) ;
32
34
}
33
35
34
36
[ Preserve ]
37
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
35
38
public override void Deserialize ( ref MemoryPackReader reader , scoped ref string ? value )
36
39
{
37
40
value = reader . ReadString ( ) ;
@@ -44,14 +47,43 @@ public sealed class Utf16StringFormatter : MemoryPackFormatter<string>
44
47
public static readonly Utf16StringFormatter Default = new Utf16StringFormatter ( ) ;
45
48
46
49
[ Preserve ]
50
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
47
51
public override void Serialize < TBufferWriter > ( ref MemoryPackWriter < TBufferWriter > writer , scoped ref string ? value )
48
52
{
49
53
writer . WriteUtf16 ( value ) ;
50
54
}
51
55
52
56
[ Preserve ]
57
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
53
58
public override void Deserialize ( ref MemoryPackReader reader , scoped ref string ? value )
54
59
{
55
60
value = reader . ReadString ( ) ;
56
61
}
57
62
}
63
+
64
+ [ Preserve ]
65
+ public sealed class InternStringFormatter : MemoryPackFormatter < string >
66
+ {
67
+ public static readonly InternStringFormatter Default = new InternStringFormatter ( ) ;
68
+
69
+ [ Preserve ]
70
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
71
+ public override void Serialize < TBufferWriter > ( ref MemoryPackWriter < TBufferWriter > writer , scoped ref string ? value )
72
+ {
73
+ writer . WriteString ( value ) ;
74
+ }
75
+
76
+ [ Preserve ]
77
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
78
+ public override void Deserialize ( ref MemoryPackReader reader , scoped ref string ? value )
79
+ {
80
+ var str = reader . ReadString ( ) ;
81
+ if ( str == null )
82
+ {
83
+ value = null ;
84
+ return ;
85
+ }
86
+
87
+ value = string . Intern ( str ) ;
88
+ }
89
+ }
0 commit comments