<commit_before>﻿// 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.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.NamingStyles;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.Shared.Naming
{
    internal readonly struct IdentifierNameParts
    {
        public readonly string BaseName;
        public readonly ImmutableArray<string> BaseNameParts;

        public IdentifierNameParts(string baseName, ImmutableArray<string> baseNameParts)
        {
            BaseName = baseName;
            BaseNameParts = baseNameParts;
        }

        public static IdentifierNameParts CreateIdentifierNameParts(ISymbol symbol, ImmutableArray<NamingRule> rules)
        {
            var baseName = RemovePrefixesAndSuffixes(symbol, rules, symbol.Name);

            using var parts = TemporaryArray<TextSpan>.Empty;
            StringBreaker.AddWordParts(baseName, ref parts.AsRef());
            var words = CreateWords(ref parts.AsRef(), baseName);

            return new IdentifierNameParts(baseName, words);
        }

        private static string RemovePrefixesAndSuffixes(ISymbol symbol, ImmutableArray<NamingRule> rules, string baseName)
        {
            var newBaseName = baseName;

            foreach (var rule in rules)
            {
                if (rule.SymbolSpecification.AppliesTo(symbol))
                {
                    // remove specified prefix
                    var prefix = rule.NamingStyle.Prefix;
                    newBaseName = newBaseName.StartsWith(prefix)
                        ? newBaseName[prefix.Length..]
                        : newBaseName;

                    // remove specified suffix
                    var suffix = rule.NamingStyle.Suffix;
                    newBaseName = newBaseName.EndsWith(suffix)
                        ? newBaseName.Substring(0, newBaseName.Length - suffix.Length)
                        : newBaseName;

                    break;
                }
            }

            // remove any common prefixes
            newBaseName = NamingStyle.StripCommonPrefixes(newBaseName, out var _);

            // If no changes were made to the basename passed in, we're done
            if (newBaseName == baseName)
            {
                return baseName;
            }

            // otherwise, see if any other prefixes exist
            return RemovePrefixesAndSuffixes(symbol, rules, newBaseName);
        }

        private static ImmutableArray<string> CreateWords(ref TemporaryArray<TextSpan> parts, string name)
        {
            using var words = TemporaryArray<string>.Empty;
            foreach (var part in parts)
                words.Add(name.Substring(part.Start, part.Length));<commit_msg>Switch to 'in'<commit_after>﻿// 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.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.NamingStyles;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.Shared.Naming
{
    internal readonly struct IdentifierNameParts
    {
        public readonly string BaseName;
        public readonly ImmutableArray<string> BaseNameParts;

        public IdentifierNameParts(string baseName, ImmutableArray<string> baseNameParts)
        {
            BaseName = baseName;
            BaseNameParts = baseNameParts;
        }

        public static IdentifierNameParts CreateIdentifierNameParts(ISymbol symbol, ImmutableArray<NamingRule> rules)
        {
            var baseName = RemovePrefixesAndSuffixes(symbol, rules, symbol.Name);

            using var parts = TemporaryArray<TextSpan>.Empty;
            StringBreaker.AddWordParts(baseName, ref parts.AsRef());
            var words = CreateWords(parts, baseName);

            return new IdentifierNameParts(baseName, words);
        }

        private static string RemovePrefixesAndSuffixes(ISymbol symbol, ImmutableArray<NamingRule> rules, string baseName)
        {
            var newBaseName = baseName;

            foreach (var rule in rules)
            {
                if (rule.SymbolSpecification.AppliesTo(symbol))
                {
                    // remove specified prefix
                    var prefix = rule.NamingStyle.Prefix;
                    newBaseName = newBaseName.StartsWith(prefix)
                        ? newBaseName[prefix.Length..]
                        : newBaseName;

                    // remove specified suffix
                    var suffix = rule.NamingStyle.Suffix;
                    newBaseName = newBaseName.EndsWith(suffix)
                        ? newBaseName.Substring(0, newBaseName.Length - suffix.Length)
                        : newBaseName;

                    break;
                }
            }

            // remove any common prefixes
            newBaseName = NamingStyle.StripCommonPrefixes(newBaseName, out var _);

            // If no changes were made to the basename passed in, we're done
            if (newBaseName == baseName)
            {
                return baseName;
            }

            // otherwise, see if any other prefixes exist
            return RemovePrefixesAndSuffixes(symbol, rules, newBaseName);
        }

        private static ImmutableArray<string> CreateWords(in TemporaryArray<TextSpan> parts, string name)
        {
            using var words = TemporaryArray<string>.Empty;
            foreach (var part in parts)
                words.Add(name.Substring(part.Start, part.Length));
{'fraction_non_alphanumeric': 0.06846005154639176, 'fraction_numerical': 0.00032216494845360824, 'mean_word_length': 2.0998502246630055, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3435274', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1476, 'n_words': 396}