@@ -5,10 +5,12 @@ This Source Code Form is subject to the terms of the Mozilla Public
5
5
**/
6
6
7
7
using System ;
8
- using System . Diagnostics ;
8
+ using System . Globalization ;
9
9
using System . Runtime . CompilerServices ;
10
10
using System . Text ;
11
11
12
+ #nullable enable
13
+
12
14
namespace PhpSerializerNET ;
13
15
14
16
public ref struct PhpTokenizer {
@@ -26,24 +28,11 @@ private PhpTokenizer(ReadOnlySpan<byte> input, Encoding inputEncoding, Span<PhpT
26
28
this . _tokenPosition = 0 ;
27
29
}
28
30
29
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
30
- private PhpDataType GetDataType ( ) {
31
- return this . _input [ this . _position ++ ] switch {
32
- ( byte ) 'N' => PhpDataType . Null ,
33
- ( byte ) 'b' => PhpDataType . Boolean ,
34
- ( byte ) 's' => PhpDataType . String ,
35
- ( byte ) 'i' => PhpDataType . Integer ,
36
- ( byte ) 'd' => PhpDataType . Floating ,
37
- ( byte ) 'a' => PhpDataType . Array ,
38
- ( byte ) 'O' => PhpDataType . Object ,
39
- _ => throw new UnreachableException ( ) ,
40
- } ;
41
- }
42
-
43
31
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
44
32
private void Advance ( ) {
45
33
this . _position ++ ;
46
34
}
35
+
47
36
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
48
37
private void Advance ( int positons ) {
49
38
this . _position += positons ;
@@ -55,50 +44,52 @@ private string GetNumbers() {
55
44
while ( this . _input [ this . _position ] != ( byte ) ';' ) {
56
45
this . _position ++ ;
57
46
}
58
- return this . _inputEncoding . GetString ( this . _input . Slice ( start , this . _position - start ) ) ;
47
+ return this . _inputEncoding . GetString ( this . _input . Slice ( start , this . _position - start ) ) ;
59
48
}
60
49
61
50
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
62
51
private int GetLength ( ) {
63
- if ( this . _input [ this . _position + 1 ] == ':' ) {
52
+ if ( this . _input [ this . _position + 1 ] == ':' ) {
64
53
return _input [ _position ++ ] - 48 ;
65
54
}
66
55
int start = this . _position ;
67
56
while ( this . _input [ this . _position ] != ( byte ) ':' ) {
68
57
this . _position ++ ;
69
58
}
70
- return int . Parse ( this . _input . Slice ( start , this . _position - start ) ) ;
59
+ return int . Parse ( this . _input . Slice ( start , this . _position - start ) , CultureInfo . InvariantCulture ) ;
71
60
}
72
61
62
+ [ MethodImpl ( MethodImplOptions . AggressiveOptimization ) ]
73
63
internal void GetToken ( ) {
74
- switch ( this . GetDataType ( ) ) {
75
- case PhpDataType . Boolean :
64
+ switch ( this . _input [ this . _position ++ ] ) {
65
+ case ( byte ) 'b' :
76
66
this . GetBooleanToken ( ) ;
77
67
break ;
78
- case PhpDataType . Null :
79
- this . GetNullToken ( ) ;
68
+ case ( byte ) 'N' :
69
+ this . _tokens [ this . _tokenPosition ++ ] = new PhpToken ( PhpDataType . Null , _position - 1 ) ;
70
+ this . Advance ( ) ;
80
71
break ;
81
- case PhpDataType . String :
72
+ case ( byte ) 's' :
82
73
this . GetStringToken ( ) ;
83
74
break ;
84
- case PhpDataType . Integer :
75
+ case ( byte ) 'i' :
85
76
this . GetIntegerToken ( ) ;
86
77
break ;
87
- case PhpDataType . Floating :
78
+ case ( byte ) 'd' :
88
79
this . GetFloatingToken ( ) ;
89
80
break ;
90
- case PhpDataType . Array :
81
+ case ( byte ) 'a' :
91
82
this . GetArrayToken ( ) ;
92
83
break ;
93
- case PhpDataType . Object :
84
+ case ( byte ) 'O' :
94
85
this . GetObjectToken ( ) ;
95
86
break ;
96
87
} ;
97
88
}
98
89
99
90
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
100
91
private void GetNullToken ( ) {
101
- this . _tokens [ this . _tokenPosition ++ ] = new PhpToken ( PhpDataType . Null , _position - 1 ) ;
92
+ this . _tokens [ this . _tokenPosition ++ ] = new PhpToken ( PhpDataType . Null , _position - 1 ) ;
102
93
this . Advance ( ) ;
103
94
}
104
95
@@ -110,15 +101,14 @@ private void GetBooleanToken() {
110
101
_position - 2 ,
111
102
this . _input [ this . _position ++ ] == ( byte ) '1'
112
103
? "1"
113
- : "0" ,
114
- 0
104
+ : "0"
115
105
) ;
116
106
this . Advance ( ) ;
117
107
}
118
108
119
109
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
120
110
private void GetStringToken ( ) {
121
- int position = _position - 1 ;
111
+ int position = _position - 1 ;
122
112
this . Advance ( ) ;
123
113
int length = this . GetLength ( ) ;
124
114
this . Advance ( 2 ) ;
@@ -127,15 +117,15 @@ private void GetStringToken() {
127
117
position ,
128
118
_inputEncoding . GetString ( this . _input . Slice ( this . _position , length ) )
129
119
) ;
130
- this . Advance ( 2 + length ) ;
120
+ this . Advance ( 2 + length ) ;
131
121
}
132
122
133
123
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
134
124
private void GetIntegerToken ( ) {
135
125
this . Advance ( ) ;
136
126
this . _tokens [ this . _tokenPosition ++ ] = new PhpToken (
137
127
PhpDataType . Integer ,
138
- this . _position - 2 ,
128
+ this . _position - 2 ,
139
129
this . GetNumbers ( )
140
130
) ;
141
131
this . Advance ( ) ;
@@ -160,7 +150,7 @@ private void GetArrayToken() {
160
150
this . _tokens [ this . _tokenPosition ++ ] = new PhpToken (
161
151
PhpDataType . Array ,
162
152
position ,
163
- "" ,
153
+ null ,
164
154
length
165
155
) ;
166
156
this . Advance ( 2 ) ;
@@ -172,12 +162,12 @@ private void GetArrayToken() {
172
162
173
163
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
174
164
private void GetObjectToken ( ) {
175
- int position = _position - 1 ;
165
+ int position = _position - 1 ;
176
166
this . Advance ( ) ;
177
167
int classNameLength = this . GetLength ( ) ;
178
168
this . Advance ( 2 ) ;
179
169
string className = _inputEncoding . GetString ( this . _input . Slice ( this . _position , classNameLength ) ) ;
180
- this . Advance ( 2 + classNameLength ) ;
170
+ this . Advance ( 2 + classNameLength ) ;
181
171
int propertyCount = this . GetLength ( ) ;
182
172
this . _tokens [ this . _tokenPosition ++ ] = new PhpToken (
183
173
PhpDataType . Object ,
0 commit comments