using System;
using System.Threading;
using JetBrains.Annotations;
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
} // TODO: move to netfx35.dll
}
namespace JetBrains.Util
{
///
/// String class extension methods.
///
public static class StringEx
{
#region Operations
///
/// Works like , but surrounds the space-containing arguments with quotes.
///
[NotNull]
[StringFormatMethod("format")]
public static string FormatQuoted([NotNull] string format, [NotNull] params object[] args)
{
if(format == null)
throw new ArgumentNullException("format");
if(args == null)
throw new ArgumentNullException("args");
for(int a = 0; a < args.Length; a++)
{
if(args[a] == null)
{
args[a] = "";
continue;
}
string s = args[a] is string ? (string)args[a] : args[a].ToString();
args[a] = s.QuoteIfNeeded();
}
return string.Format(format, args);
}
///
/// Tell if the string is either Null or an empty string.
///
public static bool IsEmpty([CanBeNull] this string s)
{
return string.IsNullOrEmpty(s);
}
///
/// If the string contains spaces, surrounds it with quotes.
///
[NotNull]
public static string QuoteIfNeeded([NotNull] this string s)
{
if(s == null)
throw new ArgumentNullException("s");
return s.Contains(" ") ? '“' + s + '”' : s;
}
///
/// Formats the identity of a thread.
///
public static string ToThreadString([CanBeNull] this Thread thread)
{
if(thread == null)
return "";
return FormatQuoted("{0}:{1}", thread.Name, thread.ManagedThreadId);
}
#endregion
}
}