Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 3323314

Browse files
committed
Replace all whitespace checks to use JsonUtils.IsWhiteSpace()
1 parent 4956d33 commit 3323314

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

src/ServiceStack.Text/Common/DeserializeTypeRefJson.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal static object StringToType(
6060
return null;
6161

6262
//if (!Serializer.EatMapStartChar(strType, ref index))
63-
for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) break; } //Whitespace inline
63+
for (; index < strType.Length; index++) { var c = strType[index]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
6464
if (strType[index++] != JsWriter.MapStartChar)
6565
throw DeserializeTypeRef.CreateSerializationError(type, strType);
6666

@@ -78,7 +78,7 @@ internal static object StringToType(
7878
var propertyName = JsonTypeSerializer.ParseJsonString(strType, ref index);
7979

8080
//Serializer.EatMapKeySeperator(strType, ref index);
81-
for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) break; } //Whitespace inline
81+
for (; index < strType.Length; index++) { var c = strType[index]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
8282
if (strType.Length != index) index++;
8383

8484
var propertyValueStr = Serializer.EatValue(strType, ref index);
@@ -152,13 +152,13 @@ internal static object StringToType(
152152
}
153153

154154
//Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
155-
for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) break; } //Whitespace inline
155+
for (; index < strType.Length; index++) { var c = strType[index]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
156156
if (index != strType.Length)
157157
{
158158
var success = strType[index] == JsWriter.ItemSeperator || strType[index] == JsWriter.MapEndChar;
159159
index++;
160160
if (success)
161-
for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) break; } //Whitespace inline
161+
for (; index < strType.Length; index++) { var c = strType[index]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
162162
}
163163

164164
continue;
@@ -195,13 +195,13 @@ internal static object StringToType(
195195
}
196196

197197
//Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
198-
for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) break; } //Whitespace inline
198+
for (; index < strType.Length; index++) { var c = strType[index]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
199199
if (index != strType.Length)
200200
{
201201
var success = strType[index] == JsWriter.ItemSeperator || strType[index] == JsWriter.MapEndChar;
202202
index++;
203203
if (success)
204-
for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) break; } //Whitespace inline
204+
for (; index < strType.Length; index++) { var c = strType[index]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
205205
}
206206

207207
}

src/ServiceStack.Text/Json/JsonTypeSerializer.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Globalization;
66
using System.IO;
7+
using System.Runtime.CompilerServices;
78
using System.Text;
89
using ServiceStack.Text.Common;
910
using ServiceStack.Text.Pools;
@@ -35,16 +36,6 @@ internal static string GetTypeAttrInObject(string typeAttr)
3536
return string.Format("{{\"{0}\":", typeAttr);
3637
}
3738

38-
public static readonly bool[] WhiteSpaceFlags = new bool[' ' + 1];
39-
40-
static JsonTypeSerializer()
41-
{
42-
foreach (var c in JsonUtils.WhiteSpaceChars)
43-
{
44-
WhiteSpaceFlags[c] = true;
45-
}
46-
}
47-
4839
public WriteObjectDelegate GetWriteFn<T>()
4940
{
5041
return JsonWriter<T>.WriteFn();
@@ -343,7 +334,7 @@ public string ParseString(string value)
343334

344335
public static bool IsEmptyMap(string value, int i = 1)
345336
{
346-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
337+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
347338
if (value.Length == i) return true;
348339
return value[i++] == JsWriter.MapEndChar;
349340
}
@@ -393,7 +384,7 @@ public string UnescapeSafeString(string value)
393384

394385
internal static string ParseJsonString(string json, ref int index)
395386
{
396-
for (; index < json.Length; index++) { var ch = json[index]; if (ch >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[ch]) break; } //Whitespace inline
387+
for (; index < json.Length; index++) { var ch = json[index]; if (!JsonUtils.IsWhiteSpace(ch)) break; } //Whitespace inline
397388

398389
return UnEscapeJsonString(json, ref index);
399390
}
@@ -558,14 +549,14 @@ public string EatTypeValue(string value, ref int i)
558549

559550
public bool EatMapStartChar(string value, ref int i)
560551
{
561-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
552+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
562553
return value[i++] == JsWriter.MapStartChar;
563554
}
564555

565556
public string EatMapKey(string value, ref int i)
566557
{
567558
var valueLength = value.Length;
568-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
559+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
569560

570561
var tokenStartPos = i;
571562
var valueChar = value[i];
@@ -589,7 +580,7 @@ public string EatMapKey(string value, ref int i)
589580

590581
if (valueChar == JsWriter.ItemSeperator
591582
//If it doesn't have quotes it's either a keyword or number so also has a ws boundary
592-
|| (valueChar < WhiteSpaceFlags.Length && WhiteSpaceFlags[valueChar])
583+
|| (JsonUtils.IsWhiteSpace(valueChar))
593584
)
594585
{
595586
break;
@@ -601,14 +592,14 @@ public string EatMapKey(string value, ref int i)
601592

602593
public bool EatMapKeySeperator(string value, ref int i)
603594
{
604-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
595+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
605596
if (value.Length == i) return false;
606597
return value[i++] == JsWriter.MapKeySeperator;
607598
}
608599

609600
public bool EatItemSeperatorOrMapEndChar(string value, ref int i)
610601
{
611-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
602+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
612603

613604
if (i == value.Length) return false;
614605

@@ -619,23 +610,23 @@ public bool EatItemSeperatorOrMapEndChar(string value, ref int i)
619610

620611
if (success)
621612
{
622-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
613+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
623614
}
624615

625616
return success;
626617
}
627618

628619
public void EatWhitespace(string value, ref int i)
629620
{
630-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
621+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
631622
}
632623

633624
public string EatValue(string value, ref int i)
634625
{
635626
var valueLength = value.Length;
636627
if (i == valueLength) return null;
637628

638-
for (; i < value.Length; i++) { var c = value[i]; if (c >= WhiteSpaceFlags.Length || !WhiteSpaceFlags[c]) break; } //Whitespace inline
629+
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
639630
if (i == valueLength) return null;
640631

641632
var tokenStartPos = i;
@@ -715,7 +706,7 @@ public string EatValue(string value, ref int i)
715706
if (valueChar == JsWriter.ItemSeperator
716707
|| valueChar == JsWriter.MapEndChar
717708
//If it doesn't have quotes it's either a keyword or number so also has a ws boundary
718-
|| (valueChar < WhiteSpaceFlags.Length && WhiteSpaceFlags[valueChar])
709+
|| JsonUtils.IsWhiteSpace(valueChar)
719710
)
720711
{
721712
break;

src/ServiceStack.Text/Json/JsonUtils.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//Copyright (c) Service Stack LLC. All Rights Reserved.
22
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
33

4+
using System;
45
using System.IO;
56
using System.Runtime.CompilerServices;
67

@@ -18,11 +19,12 @@ public static class JsonUtils
1819
public const string True = "true";
1920
public const string False = "false";
2021

21-
private const char TabChar = '\t';
22-
private const char CarriageReturnChar = '\r';
23-
private const char LineFeedChar = '\n';
24-
private const char FormFeedChar = '\f';
25-
private const char BackspaceChar = '\b';
22+
public const char SpaceChar = ' ';
23+
public const char TabChar = '\t';
24+
public const char CarriageReturnChar = '\r';
25+
public const char LineFeedChar = '\n';
26+
public const char FormFeedChar = '\f';
27+
public const char BackspaceChar = '\b';
2628

2729
/// <summary>
2830
/// Micro-optimization keep pre-built char arrays saving a .ToCharArray() + function call (see .net implementation of .Write(string))
@@ -37,6 +39,12 @@ public static class JsonUtils
3739

3840
public static readonly char[] WhiteSpaceChars = { ' ', TabChar, CarriageReturnChar, LineFeedChar };
3941

42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public static bool IsWhiteSpace(char c)
44+
{
45+
return c == ' ' || (c >= '\x0009' && c <= '\x000d') || c == '\x00a0' || c == '\x0085';
46+
}
47+
4048
public static void WriteString(TextWriter writer, string value)
4149
{
4250
if (value == null)

0 commit comments

Comments
 (0)