mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.1 KiB
82 lines
2.1 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Text;
|
|
|
|
namespace Squidex.Domain.Apps.Entities.Contents.Text
|
|
{
|
|
public sealed class QueryParser
|
|
{
|
|
private readonly Func<string, string> fieldProvider;
|
|
|
|
public QueryParser(Func<string, string> fieldProvider)
|
|
{
|
|
this.fieldProvider = fieldProvider;
|
|
}
|
|
|
|
public Query? Parse(string text)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
text = text.Trim();
|
|
text = ConvertFieldNames(text);
|
|
|
|
return new Query
|
|
{
|
|
Text = text
|
|
};
|
|
}
|
|
|
|
private string ConvertFieldNames(string query)
|
|
{
|
|
var indexOfColon = query.IndexOf(':', StringComparison.Ordinal);
|
|
|
|
if (indexOfColon < 0)
|
|
{
|
|
return query;
|
|
}
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
int position = 0, lastIndexOfColon = 0;
|
|
|
|
while (indexOfColon >= 0)
|
|
{
|
|
lastIndexOfColon = indexOfColon;
|
|
|
|
var i = 0;
|
|
|
|
for (i = indexOfColon - 1; i >= position; i--)
|
|
{
|
|
var c = query[i];
|
|
|
|
if (!char.IsLetter(c) && c != '-' && c != '_')
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
i++;
|
|
|
|
sb.Append(query[position..i]);
|
|
sb.Append(fieldProvider(query[i..indexOfColon]));
|
|
|
|
position = indexOfColon + 1;
|
|
|
|
indexOfColon = query.IndexOf(':', position);
|
|
}
|
|
|
|
sb.Append(query[lastIndexOfColon..]);
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|
|
|