Skip to content

Commit 0cb8efa

Browse files
committed
Chore: A lot of tiny code fixes.
1 parent 4c033a1 commit 0cb8efa

File tree

5 files changed

+45
-71
lines changed

5 files changed

+45
-71
lines changed

PhpSerializerNET.Test/Serialize/DynamicSerialization.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public void SerializesPhpDynamicObjectWithClassname() {
2929
data.SetClassName("phpDynamicObject");
3030
data.Foo = "a";
3131
data.Bar = 3.1415;
32-
System.Console.WriteLine(data.Bar);
3332
Assert.Equal(
3433
"O:16:\"phpDynamicObject\":2:{s:3:\"Foo\";s:1:\"a\";s:3:\"Bar\";d:3.1415;}",
3534
PhpSerialization.Serialize(data)

PhpSerializerNET/Deserialization/PhpDeserializer.cs

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,7 @@ private object DeserializeInteger(Type targetType, in PhpToken token) {
127127
TypeCode.UInt32 => uint.Parse(token.Value.GetSlice(_input), CultureInfo.InvariantCulture),
128128
TypeCode.UInt64 => ulong.Parse(token.Value.GetSlice(_input), CultureInfo.InvariantCulture),
129129
TypeCode.SByte => sbyte.Parse(token.Value.GetSlice(_input), CultureInfo.InvariantCulture),
130-
_ => this.DeserializeTokenFromSimpleType(
131-
targetType,
132-
token.Type,
133-
this.GetString(token),
134-
token.Position
135-
),
130+
_ => this.DeserializeTokenFromSimpleType(targetType, token.Type, this.GetString(token), token.Position),
136131
};
137132
}
138133

@@ -143,7 +138,7 @@ private object DeserializeDouble(Type targetType, in PhpToken token) {
143138

144139
string value = this.GetString(token);
145140
if (value == "INF") {
146-
value = double.PositiveInfinity.ToString(CultureInfo.InvariantCulture);
141+
value = double.PositiveInfinity.ToString(CultureInfo.InvariantCulture);
147142
} else if (value == "-INF") {
148143
value = double.NegativeInfinity.ToString(CultureInfo.InvariantCulture);
149144
}
@@ -159,13 +154,12 @@ private object DeserializeBoolean(Type targetType, in PhpToken token) {
159154
underlyingType = targetType.GenericTypeArguments[0];
160155
}
161156

162-
if (underlyingType.IsIConvertible()) {
163-
return ((IConvertible)token.Value.GetBool(_input)).ToType(underlyingType, CultureInfo.InvariantCulture);
164-
} else {
157+
if (!underlyingType.IsIConvertible()) {
165158
throw new DeserializationException(
166159
$"Can not assign value \"{this.GetString(token)}\" (at position {token.Position}) to target type of {targetType.Name}."
167160
);
168161
}
162+
return ((IConvertible)token.Value.GetBool(_input)).ToType(underlyingType, CultureInfo.InvariantCulture);
169163
}
170164

171165
private object DeserializeTokenFromSimpleType(
@@ -351,7 +345,7 @@ private object MakeObject(Type targetType, in PhpToken token) {
351345
DeserializeToken(property.PropertyType)
352346
);
353347
} catch (Exception exception) {
354-
var valueToken = _tokens[_currentToken-1];
348+
var valueToken = _tokens[_currentToken - 1];
355349
throw new DeserializationException(
356350
$"Exception encountered while trying to assign '{this.GetString(valueToken)}' to {targetType.Name}.{property.Name}. See inner exception for details.",
357351
exception
@@ -368,24 +362,24 @@ private object MakeArray(Type targetType, in PhpToken token) {
368362
var elementType = targetType.GetElementType() ?? throw new InvalidOperationException("targetType.GetElementType() returned null");
369363
Array result = Array.CreateInstance(elementType, token.Length);
370364

371-
var arrayIndex = 0;
372-
for (int i = 0; i < token.Length; i++) {
373-
_currentToken++;
374-
result.SetValue(
375-
elementType == typeof(object)
376-
? DeserializeToken()
377-
: DeserializeToken(elementType),
378-
arrayIndex
379-
);
380-
arrayIndex++;
365+
if (elementType == typeof(object)) {
366+
for (int i = 0; i < token.Length; i++) {
367+
_currentToken++;
368+
result.SetValue(DeserializeToken(), i);
369+
}
370+
} else {
371+
for (int i = 0; i < token.Length; i++) {
372+
_currentToken++;
373+
result.SetValue(DeserializeToken(elementType), i);
374+
}
381375
}
382376
return result;
383377
}
384378

385379
private object MakeList(Type targetType, in PhpToken token) {
386-
for (int i = 0; i < token.Length * 2; i+=2) {
387-
if (this._tokens[_currentToken+i].Type != PhpDataType.Integer) {
388-
var badToken = this._tokens[_currentToken+i];
380+
for (int i = 0; i < token.Length * 2; i += 2) {
381+
if (this._tokens[_currentToken + i].Type != PhpDataType.Integer) {
382+
var badToken = this._tokens[_currentToken + i];
389383
throw new DeserializationException(
390384
$"Can not deserialize array at position {token.Position} to list: " +
391385
$"It has a non-integer key '{this.GetString(badToken)}' at element {i} (position {badToken.Position})."
@@ -396,24 +390,23 @@ private object MakeList(Type targetType, in PhpToken token) {
396390
if (targetType.IsArray) {
397391
return MakeArray(targetType, token);
398392
}
399-
var result = (IList)Activator.CreateInstance(targetType, token.Length);
400-
if (result == null) {
393+
if (Activator.CreateInstance(targetType, token.Length) is not IList result) {
401394
throw new NullReferenceException("Activator.CreateInstance(targetType) returned null");
402395
}
403-
Type itemType;
404-
if (targetType.GenericTypeArguments.Length >= 1) {
405-
itemType = targetType.GenericTypeArguments[0];
406-
} else {
407-
itemType = typeof(object);
408-
}
396+
Type itemType = targetType.GenericTypeArguments.Length >= 1
397+
? targetType.GenericTypeArguments[0]
398+
: typeof(object);
409399

410-
for (int i = 0; i < token.Length; i++) {
411-
_currentToken++;
412-
result.Add(
413-
itemType == typeof(object)
414-
? DeserializeToken()
415-
: DeserializeToken(itemType)
416-
);
400+
if (itemType == typeof(object)) {
401+
for (int i = 0; i < token.Length; i++) {
402+
_currentToken++;
403+
result.Add(DeserializeToken());
404+
}
405+
} else {
406+
for (int i = 0; i < token.Length; i++) {
407+
_currentToken++;
408+
result.Add(DeserializeToken(itemType));
409+
}
417410
}
418411
return result;
419412
}
@@ -425,10 +418,7 @@ private object MakeDictionary(Type targetType, in PhpToken token) {
425418
}
426419
if (!targetType.GenericTypeArguments.Any()) {
427420
for (int i = 0; i < token.Length; i++) {
428-
result.Add(
429-
DeserializeToken(),
430-
DeserializeToken()
431-
);
421+
result.Add(DeserializeToken(), DeserializeToken());
432422
}
433423
return result;
434424
}
@@ -455,12 +445,12 @@ private object MakeCollection(in PhpToken token) {
455445
long previousKey = -1;
456446
bool isList = true;
457447
bool consecutive = true;
458-
for (int i = 0; i < token.Length*2; i+=2) {
459-
if (this._tokens[_currentToken+i].Type != PhpDataType.Integer) {
448+
for (int i = 0; i < token.Length * 2; i += 2) {
449+
if (this._tokens[_currentToken + i].Type != PhpDataType.Integer) {
460450
isList = false;
461451
break;
462452
} else {
463-
var key = this._tokens[_currentToken+i].Value.GetLong(_input);
453+
var key = this._tokens[_currentToken + i].Value.GetLong(_input);
464454
if (i == 0 || key == previousKey + 1) {
465455
previousKey = key;
466456
} else {
@@ -471,10 +461,7 @@ private object MakeCollection(in PhpToken token) {
471461
if (!isList || (this._options.UseLists == ListOptions.Default && consecutive == false)) {
472462
var result = new Dictionary<object, object>(token.Length);
473463
for (int i = 0; i < token.Length; i++) {
474-
result.Add(
475-
this.DeserializeToken(),
476-
this.DeserializeToken()
477-
);
464+
result.Add(this.DeserializeToken(), this.DeserializeToken());
478465
}
479466
return result;
480467
} else {

PhpSerializerNET/Deserialization/PhpToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ internal readonly struct PhpToken {
1313
internal readonly PhpDataType Type;
1414
internal readonly int Position;
1515
internal readonly int Length;
16-
1716
internal readonly ValueSpan Value;
17+
1818
internal PhpToken(PhpDataType type, int position, in ValueSpan value, int length = 0) {
1919
this.Type = type;
2020
this.Position = position;

PhpSerializerNET/Deserialization/PhpTokenizer.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ private PhpTokenizer(ReadOnlySpan<byte> input, Span<PhpToken> array) {
2525
this._tokenPosition = 0;
2626
}
2727

28-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2928
private void Advance() {
3029
this._position++;
3130
}
3231

33-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3432
private void Advance(int positons) {
3533
this._position += positons;
3634
}
3735

3836
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3937
private ValueSpan GetNumbers() {
4038
int start = this._position;
41-
while (this._input[this._position] != (byte)';') {
42-
this._position++;
43-
}
44-
return new ValueSpan(start, this._position-start);
39+
while (this._input[++this._position] != (byte)';') { }
40+
return new ValueSpan(start, this._position - start);
4541
}
4642

4743
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -50,9 +46,7 @@ private int GetLength() {
5046
return _input[_position++] - 48;
5147
}
5248
int start = this._position;
53-
while (this._input[this._position] != (byte)':') {
54-
this._position++;
55-
}
49+
while (this._input[++this._position] != (byte)':') { }
5650
return int.Parse(this._input.Slice(start, this._position - start), CultureInfo.InvariantCulture);
5751
}
5852

@@ -84,12 +78,6 @@ internal void GetToken() {
8478
};
8579
}
8680

87-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
88-
private void GetNullToken() {
89-
this._tokens[this._tokenPosition++] = new PhpToken(PhpDataType.Null, _position - 1, ValueSpan.Empty);
90-
this.Advance();
91-
}
92-
9381
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9482
private void GetBooleanToken() {
9583
this.Advance();

PhpSerializerNET/Deserialization/ValueSpan.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ internal readonly struct ValueSpan {
1313
private static ValueSpan _empty = new ValueSpan(0,0);
1414
internal readonly int Start;
1515
internal readonly int Length;
16-
public ValueSpan(int start, int length) {
16+
17+
internal ValueSpan(int start, int length) {
1718
this.Start = start;
1819
this.Length = length;
1920
}
2021

21-
public static ValueSpan Empty => _empty;
22-
23-
public ReadOnlySpan<byte> GetSlice(ReadOnlySpan<byte> input) => input.Slice(this.Start, this.Length);
22+
internal static ValueSpan Empty => _empty;
2423

24+
internal ReadOnlySpan<byte> GetSlice(ReadOnlySpan<byte> input) => input.Slice(this.Start, this.Length);
2525

2626
internal double GetDouble(ReadOnlySpan<byte> input) {
2727
var value = input.Slice(Start, Length);

0 commit comments

Comments
 (0)