// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Globalization;
namespace Umbraco.Extensions;
///
/// Culture and invariant comparison extensions.
///
public static partial class StringExtensions
{
///
/// formats the string with invariant culture
///
/// The format.
/// The args.
///
public static string InvariantFormat(this string? format, params object?[] args) =>
string.Format(CultureInfo.InvariantCulture, format ?? string.Empty, args);
///
/// Converts an integer to an invariant formatted string
///
///
///
public static string ToInvariantString(this int str) => str.ToString(CultureInfo.InvariantCulture);
public static string ToInvariantString(this long str) => str.ToString(CultureInfo.InvariantCulture);
///
/// Compares 2 strings with invariant culture and case ignored
///
/// The compare.
/// The compare to.
///
public static bool InvariantEquals(this string? compare, string? compareTo) =>
string.Equals(compare, compareTo, StringComparison.InvariantCultureIgnoreCase);
public static bool InvariantStartsWith(this string compare, string compareTo) =>
compare.StartsWith(compareTo, StringComparison.InvariantCultureIgnoreCase);
public static bool InvariantEndsWith(this string compare, string compareTo) =>
compare.EndsWith(compareTo, StringComparison.InvariantCultureIgnoreCase);
public static bool InvariantContains(this string compare, string compareTo) =>
compare.Contains(compareTo, StringComparison.OrdinalIgnoreCase);
public static bool InvariantContains(this IEnumerable compare, string compareTo) =>
compare.Contains(compareTo, StringComparer.InvariantCultureIgnoreCase);
public static int InvariantIndexOf(this string s, string value) =>
s.IndexOf(value, StringComparison.OrdinalIgnoreCase);
public static int InvariantLastIndexOf(this string s, string value) =>
s.LastIndexOf(value, StringComparison.OrdinalIgnoreCase);
///
/// Verifies the provided string is a valid culture code and returns it in a consistent casing.
///
/// Culture code.
/// Culture code in standard casing.
public static string? EnsureCultureCode(this string? culture)
{
if (string.IsNullOrEmpty(culture) || culture == "*")
{
return culture;
}
// Create as CultureInfo instance from provided name so we can ensure consistent casing of culture code when persisting.
// This will accept mixed case but once created have a `Name` property that is consistently and correctly cased.
// Will throw in an invalid culture code is provided.
return new CultureInfo(culture).Name;
}
}