<commit_before>﻿using System.Collections.Generic;
using Microsoft.Win32;
using System.Linq;

namespace xClient.Core.Extensions
{
    public static class RegistryKeyExtensions
    {
        /// <summary>
        /// Determines if the registry key by the name provided is null or has the value of null.
        /// </summary>
        /// <param name="keyName">The name associated with the registry key.</param>
        /// <param name="key">The actual registry key.</param>
        /// <param name="keyValue">The string value of the registry key determined by the key's name.</param>
        /// <returns>True if the provided name is null or empty, or the key is null; False if otherwise.</returns>
        public static bool IsNameOrValueNull(this string keyName, RegistryKey key)
        {
            return (string.IsNullOrEmpty(keyName) || (key == null));
        }

        /// <summary>
        /// Attempts to get the value of the key using the specified key name. This method assumes
        /// correct input.
        /// </summary>
        /// <param name="key">The key of which we obtain the value of.</param>
        /// <param name="keyName">The name of the key.</param>
        /// <returns>Returns the value of the key using the specified key name. If unable to do so,
        /// string.Empty will be returned instead.</returns>
        public static string GetValueSafe(this RegistryKey key, string keyName)
        {
            // Before calling this, use something such as "IsNameOrValueNull" to make sure
            // that the input used for this method is usable. The responsibility for this
            // method is to take these valid parameters and try to get the value of them,
            // allowing exceptions if any are generated.
            try
            {
                return key.GetValue(keyName).ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Attempts to obtain a readonly (non-writable) sub key from the key provided using the
        /// specified name. Exceptions thrown will be caught and will only return a null key.
        /// This method assumes the caller will dispose of the key when done using it.
        /// </summary>
        /// <param name="key">The key of which the sub key is obtained from.</param>
        /// <param name="name">The name of the sub-key.</param>
        /// <returns>Returns the sub-key obtained from the key and name provided; Returns null if
        /// unable to obtain a sub-key.</returns>
        public static RegistryKey OpenReadonlySubKeySafe(this RegistryKey key, string name)
        {
            try
            {
                return Registry.LocalMachine.OpenSubKey(name, false);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Attempts to obtain a writable sub key from the key provided using the specified
        /// name. This method assumes the caller will dispose of the key when done using it.
        /// </summary>
        /// <param name="key">The key of which the sub key is obtained from.</param>
        /// <param name="name">The name of the sub-key.</param>
        /// <returns>Returns the sub-key obtained from the key and name provided; Returns null if
        /// unable to obtain a sub-key.</returns>
        public static RegistryKey OpenWritableSubKey(this RegistryKey key, string name)
        {
            return Registry.LocalMachine.OpenSubKey(name, true);
        }

        /// <summary>
        /// Gets all of the value names associated with the registry key and returns
        /// formatted strings of the filtered values.
        /// </summary>
        /// <param name="key">The registry key of which the values are obtained.</param>
        /// <returns>Yield returns formatted strings of the key and the key value.</returns>
        public static IEnumerable<string> GetFormattedKeyValues(this RegistryKey key)
        {
            if (key != null)
            {
                foreach (var k in key.GetValueNames().Where(keyVal => !keyVal.IsNameOrValueNull(key))
                                                     .Select(keyVal => key.GetValueSafe(keyVal)))
                {
                    // Less-likely, but this will ensure no empty items if an exception was thrown
                    // when obtaining the value.
                    if (string.IsNullOrEmpty(k))
                    {
                        yield return string.Format("{0}||{1}", k, key.GetValue(k));
                    }
                }
            }<commit_msg>Correctly get entries<commit_after>﻿using System.Collections.Generic;
using Microsoft.Win32;
using System.Linq;

namespace xClient.Core.Extensions
{
    public static class RegistryKeyExtensions
    {
        /// <summary>
        /// Determines if the registry key by the name provided is null or has the value of null.
        /// </summary>
        /// <param name="keyName">The name associated with the registry key.</param>
        /// <param name="key">The actual registry key.</param>
        /// <param name="keyValue">The string value of the registry key determined by the key's name.</param>
        /// <returns>True if the provided name is null or empty, or the key is null; False if otherwise.</returns>
        public static bool IsNameOrValueNull(this string keyName, RegistryKey key)
        {
            return (string.IsNullOrEmpty(keyName) || (key == null));
        }

        /// <summary>
        /// Attempts to get the value of the key using the specified key name. This method assumes
        /// correct input.
        /// </summary>
        /// <param name="key">The key of which we obtain the value of.</param>
        /// <param name="keyName">The name of the key.</param>
        /// <returns>Returns the value of the key using the specified key name. If unable to do so,
        /// string.Empty will be returned instead.</returns>
        public static string GetValueSafe(this RegistryKey key, string keyName)
        {
            // Before calling this, use something such as "IsNameOrValueNull" to make sure
            // that the input used for this method is usable. The responsibility for this
            // method is to take these valid parameters and try to get the value of them,
            // allowing exceptions if any are generated.
            try
            {
                return key.GetValue(keyName).ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Attempts to obtain a readonly (non-writable) sub key from the key provided using the
        /// specified name. Exceptions thrown will be caught and will only return a null key.
        /// This method assumes the caller will dispose of the key when done using it.
        /// </summary>
        /// <param name="key">The key of which the sub key is obtained from.</param>
        /// <param name="name">The name of the sub-key.</param>
        /// <returns>Returns the sub-key obtained from the key and name provided; Returns null if
        /// unable to obtain a sub-key.</returns>
        public static RegistryKey OpenReadonlySubKeySafe(this RegistryKey key, string name)
        {
            try
            {
                return Registry.LocalMachine.OpenSubKey(name, false);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Attempts to obtain a writable sub key from the key provided using the specified
        /// name. This method assumes the caller will dispose of the key when done using it.
        /// </summary>
        /// <param name="key">The key of which the sub key is obtained from.</param>
        /// <param name="name">The name of the sub-key.</param>
        /// <returns>Returns the sub-key obtained from the key and name provided; Returns null if
        /// unable to obtain a sub-key.</returns>
        public static RegistryKey OpenWritableSubKey(this RegistryKey key, string name)
        {
            return Registry.LocalMachine.OpenSubKey(name, true);
        }

        /// <summary>
        /// Gets all of the value names associated with the registry key and returns
        /// formatted strings of the filtered values.
        /// </summary>
        /// <param name="key">The registry key of which the values are obtained.</param>
        /// <returns>Yield returns formatted strings of the key and the key value.</returns>
        public static IEnumerable<string> GetFormattedKeyValues(this RegistryKey key)
        {
            if (key != null)
            {
                foreach (var k in key.GetValueNames().Where(keyVal => !keyVal.IsNameOrValueNull(key)))
                {
                    // Less-likely, but this will ensure no empty items if an exception was thrown
                    // when obtaining the value.
                    if (string.IsNullOrEmpty(k))
                    {
                        yield return string.Format("{0}||{1}", k, key.GetValueSafe(k));
                    }
                }
            }
{'fraction_non_alphanumeric': 0.09463108998595657, 'fraction_numerical': 0.0008642108674516582, 'mean_word_length': 1.7611094542201013, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757875', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2165, 'n_words': 912}