Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
StringOps.cs109 linesDownload Raw Back to Operations
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Globalization;5using System.Management.Automation.Internal;6using System.Management.Automation.Language;7using System.Management.Automation.Runspaces;8 9// ReSharper disable UnusedMember.Global10 11namespace System.Management.Automation12{13    using Dbg = Diagnostics;14 15    internal static class StringOps16    {17        internal static string Add(string lhs, string rhs)18        {19            return string.Concat(lhs, rhs);20        }21 22        internal static string Add(string lhs, char rhs)23        {24            return string.Concat(lhs, rhs);25        }26 27        internal static string Multiply(string s, int times)28        {29            Diagnostics.Assert(s != null, "caller to verify argument is not null");30 31            // TODO: this should be a runtime error.32            ArgumentOutOfRangeException.ThrowIfNegative(times);33 34            if (times == 0 || s.Length == 0)35            {36                return string.Empty;37            }38 39            var context = LocalPipeline.GetExecutionContextFromTLS();40            if (context != null &&41                context.LanguageMode == PSLanguageMode.RestrictedLanguage && (s.Length * times) > 1024)42            {43                throw InterpreterError.NewInterpreterException(times, typeof(RuntimeException),44                    null, "StringMultiplyToolongInDataSection", ParserStrings.StringMultiplyToolongInDataSection, 1024);45            }46 47            if (s.Length == 1)48            {49                // A string of length 1 has special support in the BCL, so just use it.50                return new string(s[0], times);51            }52 53            return string.Create(s.Length * times, (s, times), (dst, args) =>54                {55                    ReadOnlySpan<char> src = args.s.AsSpan();56                    int length = src.Length;57                    for (int i = 0; i < args.times; i++)58                    {59                        src.CopyTo(dst);60                        dst = dst.Slice(length);61                    }62                });63        }64 65        internal static string FormatOperator(string formatString, object formatArgs)66        {67            try68            {69                object[] formatArgsArray = formatArgs as object[];70                return formatArgsArray != null71                           ? StringUtil.Format(formatString, formatArgsArray)72                           : StringUtil.Format(formatString, formatArgs);73            }74            catch (FormatException sfe)75            {76                // "error formatting a string: " + sfe.Message77                throw InterpreterError.NewInterpreterException(formatString, typeof(RuntimeException),78                    PositionUtilities.EmptyExtent, "FormatError", ParserStrings.FormatError, sfe.Message);79            }80        }81 82        // The following methods are used for the compatibility purpose between regular PowerShell and PowerShell on CSS83 84        /// <summary>85        /// StringComparison.InvariantCulture is not in CoreCLR, so we need to use86        ///    CultureInfo.InvariantCulture.CompareInfo.Compare(string, string, CompareOptions)87        /// to substitute88        ///    string.Compare(string, string, StringComparison)89        /// </summary>90        internal static int Compare(string strA, string strB, CultureInfo culture, CompareOptions option)91        {92            Diagnostics.Assert(culture != null, "Caller makes sure that 'culture' is not null.");93            return culture.CompareInfo.Compare(strA, strB, option);94        }95 96        /// <summary>97        /// StringComparison.InvariantCulture is not in CoreCLR, so we need to use98        ///    CultureInfo.InvariantCulture.CompareInfo.Compare(string, string, CompareOptions) == 099        /// to substitute100        ///    string.Equals(string, string, StringComparison)101        /// </summary>102        internal static bool Equals(string strA, string strB, CultureInfo culture, CompareOptions option)103        {104            Diagnostics.Assert(culture != null, "Caller makes sure that 'culture' is not null.");105            return culture.CompareInfo.Compare(strA, strB, option) == 0;106        }107    }108}109