/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/StringExtensions.cs
59 строк
2 KB
Cyrus Najmabadi
Move extensions
20 дек 2023, 04:35
20 дек 2023, 04:35
126745d
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Simplification; namespace Microsoft.CodeAnalysis.CSharp.Extensions; internal static class StringExtensions { public static string EscapeIdentifier( this string identifier, bool isQueryContext = false) { var nullIndex = identifier.IndexOf('\0'); if (nullIndex >= 0) { identifier = identifier[..nullIndex]; } var needsEscaping = SyntaxFacts.GetKeywordKind(identifier) != SyntaxKind.None; // Check if we need to escape this contextual keyword needsEscaping = needsEscaping || (isQueryContext && SyntaxFacts.IsQueryContextualKeyword(SyntaxFacts.GetContextualKeywordKind(identifier))); return needsEscaping ? "@" + identifier : identifier; } public static SyntaxToken ToIdentifierToken( this string identifier, bool isQueryContext = false) { var escaped = identifier.EscapeIdentifier(isQueryContext); if (escaped.Length == 0 || escaped[0] != '@') { return SyntaxFactory.Identifier(escaped); } var unescaped = identifier.StartsWith("@", StringComparison.Ordinal) ? identifier[1..] : identifier; var token = SyntaxFactory.Identifier( default, SyntaxKind.None, "@" + unescaped, unescaped, default); if (!identifier.StartsWith("@", StringComparison.Ordinal)) { token = token.WithAdditionalAnnotations(Simplifier.Annotation); } return token; } public static IdentifierNameSyntax ToIdentifierName(this string identifier) => SyntaxFactory.IdentifierName(identifier.ToIdentifierToken()); }