// 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;
// ReSharper disable once CheckNamespace
///
/// Contains extension methods for .
///
///
/// Original from Cake build tool source:
/// https://github.com/cake-build/cake/blob/9828d7b246d332054896e52ba56983822feb3f05/src/Cake.Core/Extensions/StringExtensions.cs
///
public static class StringExtensions
{
///
/// Quotes the specified .
///
/// The string to quote.
/// A quoted string.
public static string Quote(this string value)
{
if (!IsQuoted(value))
{
value = string.Concat("\"", value, "\"");
}
return value;
}
///
/// Unquote the specified .
///
/// The string to unquote.
/// An unquoted string.
public static string UnQuote(this string value)
{
if (IsQuoted(value))
{
value = value.Trim('"');
}
return value;
}
///
/// Splits the into lines.
///
/// The string to split.
/// The lines making up the provided string.
public static string[] SplitLines(this string content)
{
content = NormalizeLineEndings(content);
return content.Split(new[] { "\r\n" }, StringSplitOptions.None);
}
///
/// Normalizes the line endings in a .
///
/// The string to normalize line endings in.
/// A with normalized line endings.
public static string NormalizeLineEndings(this string value)
{
if (value != null)
{
value = value.Replace("\r\n", "\n");
value = value.Replace("\r", string.Empty);
return value.Replace("\n", "\r\n");
}
return string.Empty;
}
private static bool IsQuoted(this string value)
{
return value.StartsWith("\"", StringComparison.OrdinalIgnoreCase)
&& value.EndsWith("\"", StringComparison.OrdinalIgnoreCase);
}
}