1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
//---------------------------------------------------------------------
// <copyright file="StringUtil.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Diagnostics;
namespace System.Data.Common.Utils {
// This class provides some useful string utilities, e.g., converting a
// list to string.
internal static class StringUtil {
private const string s_defaultDelimiter = ", ";
#region String Conversion - Unsorted
/// <summary>
/// Converts an enumeration of values to a delimited string list.
/// </summary>
/// <typeparam name="T">Type of elements to convert.</typeparam>
/// <param name="values">Values. If null, returns empty string.</param>
/// <param name="converter">Converter. If null, uses default invariant culture converter.</param>
/// <param name="delimiter">Delimiter. If null, uses default (', ')</param>
/// <returns>Delimited list of values in string.</returns>
internal static string BuildDelimitedList<T>(IEnumerable<T> values, ToStringConverter<T> converter, string delimiter) {
if (null == values) { return String.Empty; }
if (null == converter) { converter = new ToStringConverter<T>(InvariantConvertToString<T>); }
if (null == delimiter) { delimiter = s_defaultDelimiter; }
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (T value in values) {
if (first) { first = false; }
else { sb.Append(delimiter); }
sb.Append(converter(value));
}
return sb.ToString();
}
// effects: Converts list to a string separated by a comma with
// string.Empty used for null values
internal static string ToCommaSeparatedString(IEnumerable list) {
return ToSeparatedString(list, s_defaultDelimiter, string.Empty);
}
// effects: Converts list to a string separated by "separator" with
// "nullValue" used for null values
internal static string ToSeparatedString(IEnumerable list, string separator, string nullValue) {
StringBuilder builder = new StringBuilder();
ToSeparatedString(builder, list, separator, nullValue);
return builder.ToString();
}
#endregion
#region String Conversion - Sorted
// effects: Converts the list to a list of strings, sorts its
// and then converts to a string separated by a comma with
// string.Empty used for null values
internal static string ToCommaSeparatedStringSorted(IEnumerable list) {
return ToSeparatedStringSorted(list, s_defaultDelimiter, string.Empty);
}
// effects: Converts the list to a list of strings, sorts its using
// StringComparer.Ordinal
// and then converts to a string separated by "separator" with
// with "nullValue" used for null values
internal static string ToSeparatedStringSorted(IEnumerable list, string separator, string nullValue) {
StringBuilder builder = new StringBuilder();
ToSeparatedStringPrivate(builder, list, separator, nullValue, true);
return builder.ToString();
}
#endregion
#region StringBuilder routines
internal static string MembersToCommaSeparatedString(IEnumerable members)
{
StringBuilder builder = new StringBuilder();
builder.Append("{");
StringUtil.ToCommaSeparatedString(builder, members);
builder.Append("}");
return builder.ToString();
}
internal static void ToCommaSeparatedString(StringBuilder builder, IEnumerable list) {
ToSeparatedStringPrivate(builder, list, s_defaultDelimiter, string.Empty, false);
}
internal static void ToCommaSeparatedStringSorted(StringBuilder builder, IEnumerable list) {
ToSeparatedStringPrivate(builder, list, s_defaultDelimiter, string.Empty, true);
}
internal static void ToSeparatedString(StringBuilder builder, IEnumerable list, string separator) {
ToSeparatedStringPrivate(builder, list, separator, string.Empty, false);
}
internal static void ToSeparatedStringSorted(StringBuilder builder, IEnumerable list, string separator) {
ToSeparatedStringPrivate(builder, list, separator, string.Empty, true);
}
// effects: Modifies stringBuilder to contain a string of values from list
// separated by "separator" with "nullValue" used for null values
internal static void ToSeparatedString(StringBuilder stringBuilder, IEnumerable list, string separator,
string nullValue) {
ToSeparatedStringPrivate(stringBuilder, list, separator, nullValue, false);
}
// effects: Converts the list to a list of strings, sorts its (if
// toSort is true) and then converts to a string separated by
// "separator" with "nullValue" used for null values.
private static void ToSeparatedStringPrivate(StringBuilder stringBuilder, IEnumerable list, string separator,
string nullValue, bool toSort) {
if (null == list) {
return;
}
bool isFirst = true;
// Get the list of strings first
List<string> elementStrings = new List<string>();
foreach (object element in list) {
string str;
// Get the element or its default null value
if (element == null) {
str = nullValue;
} else {
str = FormatInvariant("{0}", element);
}
elementStrings.Add(str);
}
if (toSort == true) {
// Sort the list
elementStrings.Sort(StringComparer.Ordinal);
}
// Now add the strings to the stringBuilder
foreach (string str in elementStrings) {
if (false == isFirst) {
stringBuilder.Append(separator);
}
stringBuilder.Append(str);
isFirst = false;
}
}
#endregion
#region Some Helper routines
/// <summary>
/// This private static method checks a string to make sure that it is not empty.
/// Comparing with String.Empty is not sufficient since a string with nothing
/// but white space isn't considered "empty" by that rationale.
/// </summary>
internal static bool IsNullOrEmptyOrWhiteSpace(string value)
{
return IsNullOrEmptyOrWhiteSpace(value, 0);
}
internal static bool IsNullOrEmptyOrWhiteSpace(string value, int offset)
{
// don't use Trim(), which will copy the string, which may be large, just to test for emptyness
//return String.IsNullOrEmpty(value) || String.IsNullOrEmpty(value.Trim());
if (null != value)
{
for(int i = offset; i < value.Length; ++i)
{
if (!Char.IsWhiteSpace(value[i]))
{
return false;
}
}
}
return true;
}
// separate implementation from IsNullOrEmptyOrWhiteSpace(string, int) because that one will
// pick up the jit optimization to avoid boundary checks and the this won't is unknown (most likely not)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] // referenced by System.Data.Entity.Design.dll
internal static bool IsNullOrEmptyOrWhiteSpace(string value, int offset, int length)
{
// don't use Trim(), which will copy the string, which may be large, just to test for emptyness
//return String.IsNullOrEmpty(value) || String.IsNullOrEmpty(value.Trim());
if (null != value)
{
length = Math.Min(value.Length, length);
for(int i = offset; i < length; ++i)
{
if (!Char.IsWhiteSpace(value[i]))
{
return false;
}
}
}
return true;
}
internal static string FormatInvariant(string format, params object[] args) {
Debug.Assert(args.Length > 0, "Formatting utilities must be called with at least one argument");
return String.Format(CultureInfo.InvariantCulture, format, args);
}
// effects: Formats args according to the format string and adds it
// to builder. Returns the modified builder
internal static StringBuilder FormatStringBuilder(StringBuilder builder, string format, params object[] args) {
Debug.Assert(args.Length > 0, "Formatting utilities must be called with at least one argument");
builder.AppendFormat(CultureInfo.InvariantCulture, format, args);
return builder;
}
// effects: Generates a new line and then indents the new line by
// indent steps in builder -- indent steps are determined internally
// by this method. Returns the modified builder
internal static StringBuilder IndentNewLine(StringBuilder builder, int indent) {
builder.AppendLine();
for (int i = 0; i < indent; i++) {
builder.Append(" ");
}
return builder;
}
// effects: returns a string of the form 'arrayVarName[index]'
internal static string FormatIndex(string arrayVarName, int index) {
System.Text.StringBuilder builder = new System.Text.StringBuilder(arrayVarName.Length + 10 + 2);
return builder.Append(arrayVarName).Append('[').Append(index).Append(']').ToString();
}
private static string InvariantConvertToString<T>(T value) {
return String.Format(CultureInfo.InvariantCulture, "{0}", value);
}
#endregion
#region Delegates
internal delegate string ToStringConverter<T>(T value);
#endregion
}
}
|