﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;

using Microsoft.RE2.Managed;

namespace Microsoft.CodeAnalysis.Sarif.PatternMatcher
{
    public static class DictionaryExtensions
    {
        public static Dictionary<string, string> ToStringDictionary(this IDictionary<string, FlexMatch> dictionary)
        {
            var stringDictionary = new Dictionary<string, string>(dictionary.Count);

            foreach (KeyValuePair<string, FlexMatch> kv in dictionary)
            {
                stringDictionary[kv.Key] = kv.Value.Value.String;
            }

            return stringDictionary;
        }

        public static Dictionary<string, FlexMatch> Copy(this IDictionary<string, FlexMatch> dictionary)
        {
            return new Dictionary<string, FlexMatch>(dictionary);
        }

        public static void AddProperties(this IDictionary<string, FlexMatch> dictionary, IDictionary<string, string> properties)
        {
            if (properties == null || properties.Count == 0)
            {
                return;
            }

            foreach (KeyValuePair<string, string> kv in properties)
            {
                if (!dictionary.ContainsKey(kv.Key))
                {
                    dictionary[kv.Key] = new FlexMatch() { Value = kv.Value };
                }
            }
        }
    }
}

{'avg_line_length': 32.130434782608695, 'max_line_length': 128, 'alphanum_fraction': 0.6041948579161028, 'licenses': ['MIT'], 'repository_name': 'marmegh/sarif-pattern-matcher', 'path': 'Src/Sarif.PatternMatcher/ExtensionMethods.cs', 'size': 1480, 'lang': 'C#'}