mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-04 03:36:04 +00:00
Optimize JSONDetector with Span
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace QuickLook.Plugin.TextViewer.Detectors;
|
||||
@@ -29,24 +30,27 @@ public class JSONDetector : IFormatDetector
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text)) return false;
|
||||
|
||||
// TODO: Use AsSpan to improve
|
||||
var span = text.AsSpan();
|
||||
|
||||
var trimmedStart = text.TrimStart();
|
||||
// TrimStart
|
||||
int start = 0;
|
||||
while (start < span.Length && char.IsWhiteSpace(span[start]))
|
||||
start++;
|
||||
|
||||
if (trimmedStart.StartsWith("{") || trimmedStart.StartsWith("["))
|
||||
{
|
||||
var trimmedEnd = text.TrimEnd();
|
||||
if (start >= span.Length)
|
||||
return false;
|
||||
|
||||
if (trimmedEnd.EndsWith("}") || trimmedEnd.EndsWith("]"))
|
||||
{
|
||||
// At least one key exists
|
||||
if (Regex.IsMatch(text, @"""[^""]+""\s*:"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (span[start] != '{' && span[start] != '[')
|
||||
return false;
|
||||
|
||||
return false;
|
||||
// TrimEnd
|
||||
int end = span.Length - 1;
|
||||
while (end >= 0 && char.IsWhiteSpace(span[end]))
|
||||
end--;
|
||||
|
||||
if (end < 0 || (span[end] != '}' && span[end] != ']'))
|
||||
return false;
|
||||
|
||||
return Regex.IsMatch(text, @"""[^""]+""\s*:");
|
||||
}
|
||||
}
|
||||
|
@@ -63,6 +63,9 @@
|
||||
<PackageReference Include="UTF.Unknown" Version="2.5.1" />
|
||||
<Reference Include="WindowsBase" />
|
||||
<Reference Include="System.ComponentModel.Composition" />
|
||||
<PackageReference Include="System.Memory" Version="4.6.3" />
|
||||
<PackageReference Include="System.Buffers" Version="4.6.1" />
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
Reference in New Issue
Block a user