@@ -31,70 +31,72 @@ public struct GMODVariant
3131 [ FieldOffset ( 4 ) ] public Vector3 Vec ;
3232 [ FieldOffset ( 4 ) ] public QAngle Ang ;
3333 [ FieldOffset ( 4 ) ] public ushort StringLength ;
34- [ FieldOffset ( 16 ) ] public string ? String ;
34+ [ FieldOffset ( 16 ) ] public byte [ ] ? StringBytes ;
3535
3636 public readonly bool IsIntLike => Type is GMODVariantType . Int or GMODVariantType . Bool or GMODVariantType . Entity ;
3737 public readonly bool IsFloatLike => Type is GMODVariantType . Float or GMODVariantType . Vector or GMODVariantType . Angle ;
3838
3939 public void SetFloat ( float value ) {
4040 Type = GMODVariantType . Float ;
4141 Vec = new ( value , 0 , 0 ) ;
42- String = null ;
42+ StringBytes = null ;
4343 }
4444
4545 public void SetInt ( int value ) {
4646 Type = GMODVariantType . Int ;
4747 Vec = default ;
4848 Int = value ;
49- String = null ;
49+ StringBytes = null ;
5050 }
5151
5252 public void SetBool ( bool value ) {
5353 Type = GMODVariantType . Bool ;
5454 Vec = default ;
5555 Int = value ? 1 : 0 ;
56- String = null ;
56+ StringBytes = null ;
5757 }
5858
5959 public void SetVector ( in Vector3 value ) {
6060 Type = GMODVariantType . Vector ;
6161 Vec = value ;
62- String = null ;
62+ StringBytes = null ;
6363 }
6464
6565 public void SetAngle ( in QAngle value ) {
6666 Type = GMODVariantType . Angle ;
6767 Ang = value ;
68- String = null ;
68+ StringBytes = null ;
6969 }
7070
7171 public void SetEntity ( int handle ) {
7272 Type = GMODVariantType . Entity ;
7373 Vec = default ;
7474 Int = handle ;
75- String = null ;
75+ StringBytes = null ;
7676 }
7777
78- public void SetString ( ReadOnlySpan < char > value ) {
79- value = value . SliceNullTerminatedString ( ) ;
80- int bytes = Encoding . UTF8 . GetByteCount ( value ) ;
81- if ( bytes + 1 > MAX_STRING_LENGTH ) {
82- byte [ ] buffer = new byte [ bytes ] ;
83- Encoding . UTF8 . GetBytes ( value , buffer ) ;
84- String = Encoding . UTF8 . GetString ( buffer , 0 , MAX_STRING_LENGTH - 1 ) ;
85- StringLength = MAX_STRING_LENGTH ;
86- }
87- else {
88- String = new string ( value ) ;
89- StringLength = ( ushort ) ( bytes + 1 ) ;
90- }
78+ public void SetString ( ReadOnlySpan < byte > value ) {
79+ int nul = value . IndexOf ( ( byte ) 0 ) ;
80+ if ( nul >= 0 )
81+ value = value [ .. nul ] ;
82+ if ( value . Length > MAX_STRING_LENGTH - 1 )
83+ value = value [ .. ( MAX_STRING_LENGTH - 1 ) ] ;
9184 Type = GMODVariantType . String ;
85+ Vec = default ;
86+ StringBytes = value . ToArray ( ) ;
87+ StringLength = ( ushort ) ( value . Length + 1 ) ;
88+ }
89+
90+ readonly ReadOnlySpan < char > StringAsChars ( Span < char > buffer ) {
91+ ReadOnlySpan < byte > bytes = StringBytes ;
92+ Encoding . Latin1 . GetChars ( bytes , buffer ) ;
93+ return buffer [ .. bytes . Length ] ;
9294 }
9395
9496 public void Clear ( ) {
9597 Type = GMODVariantType . NIL ;
9698 Vec = default ;
97- String = null ;
99+ StringBytes = null ;
98100 }
99101
100102 public readonly int ToInt ( ) {
@@ -103,7 +105,7 @@ public readonly int ToInt() {
103105 if ( IsFloatLike )
104106 return ( int ) Float ;
105107 if ( Type == GMODVariantType . String )
106- return atoi ( String ) ;
108+ return atoi ( StringAsChars ( stackalloc char [ MAX_STRING_LENGTH ] ) ) ;
107109 return 0 ;
108110 }
109111
@@ -113,7 +115,7 @@ public readonly float ToFloat() {
113115 if ( IsFloatLike )
114116 return Float ;
115117 if ( Type == GMODVariantType . String )
116- return ( float ) atof ( String ) ;
118+ return ( float ) atof ( StringAsChars ( stackalloc char [ MAX_STRING_LENGTH ] ) ) ;
117119 return 0 ;
118120 }
119121
@@ -123,9 +125,10 @@ public readonly bool ToBool() {
123125 if ( IsFloatLike )
124126 return Float != 0 || float . IsNaN ( Float ) ;
125127 if ( Type == GMODVariantType . String ) {
126- if ( string . IsNullOrEmpty ( String ) || String . Equals ( "false" , StringComparison . OrdinalIgnoreCase ) )
128+ ReadOnlySpan < byte > bytes = StringBytes ;
129+ if ( bytes . IsEmpty || Ascii . EqualsIgnoreCase ( bytes , "false"u8 ) )
127130 return false ;
128- return atoi ( String ) != 0 ;
131+ return atoi ( StringAsChars ( stackalloc char [ MAX_STRING_LENGTH ] ) ) != 0 ;
129132 }
130133 return false ;
131134 }
@@ -139,7 +142,7 @@ public readonly Vector3 ToVector() {
139142 return Vec ;
140143 if ( Type == GMODVariantType . String ) {
141144 Span < float > values = stackalloc float [ 3 ] ;
142- if ( ScanFloats ( String , values ) == 3 )
145+ if ( ScanFloats ( StringAsChars ( stackalloc char [ MAX_STRING_LENGTH ] ) , values ) == 3 )
143146 return new ( values [ 0 ] , values [ 1 ] , values [ 2 ] ) ;
144147 }
145148 return default ;
@@ -156,7 +159,7 @@ public readonly int ToHandle() {
156159 if ( IsFloatLike )
157160 return ( int ) Float ;
158161 if ( Type == GMODVariantType . String )
159- return atoi ( String ) ;
162+ return atoi ( StringAsChars ( stackalloc char [ MAX_STRING_LENGTH ] ) ) ;
160163 return 0 ;
161164 }
162165
@@ -165,7 +168,7 @@ public readonly int ToHandle() {
165168 GMODVariantType . Int or GMODVariantType . Entity => Int . ToString ( ) ,
166169 GMODVariantType . Bool => Int != 0 ? "true" : "false" ,
167170 GMODVariantType . Vector or GMODVariantType . Angle => $ "{ FormatG ( Vec . X ) } { FormatG ( Vec . Y ) } { FormatG ( Vec . Z ) } ",
168- GMODVariantType . String => String ?? "" ,
171+ GMODVariantType . String => StringBytes == null ? "" : Encoding . UTF8 . GetString ( StringBytes ) ,
169172 _ => ""
170173 } ;
171174}
0 commit comments