/// <summary> /// Converts format to shift shards. Like convert "Its name is {name}" to <seealso cref="StringShiftShard"/> instance. /// </summary> /// <param name="format">The format.</param> /// <returns>StringShiftShard.</returns> public static StringShiftShard ConvertFormatToShiftShard(this string format) { if (string.IsNullOrWhiteSpace(format)) { return(null); } var matches = placeHolderRegex.Matches(format); StringShiftShard result = new StringShiftShard(); var lastStart = 0; foreach (Match one in matches) { var staticPart = format.Substring(lastStart, one.Index - lastStart); result.staticShards.Add(staticPart); var holderKey = one.Result(holderKeyResult); result.dynamicShards.Add(one.Result(holderKeyResult)); lastStart = lastStart + 2 + staticPart.Length + holderKey.Length; } result.staticShards.Add(format.Substring(lastStart, format.Length - lastStart)); return(result); }
/// <summary> /// Gets the string builder default capacity. /// </summary> /// <param name="shard">The shard.</param> /// <returns>System.Int32.</returns> internal static int GetStringBuilderDefaultCapacity(this StringShiftShard shard) { return(shard == null ? 0 : (shard.staticShards.Sum(x => x.Length) * 2)); }
/// <summary> /// Creates the string builder. /// </summary> /// <param name="shard">The shard.</param> /// <returns>StringBuilder.</returns> internal static StringBuilder CreateStringBuilder(this StringShiftShard shard) { return(shard == null ? null : new StringBuilder(GetStringBuilderDefaultCapacity(shard))); }
/// <summary> /// Initializes a new instance of the <see cref="StringShifter" /> class. /// </summary> /// <param name="sourceFormat">The source format.</param> /// <param name="destinationFormat">The destination format.</param> /// <param name="sourceFormatConstraints">The source format constraints.</param> public StringShifter(string sourceFormat, string destinationFormat, Dictionary <string, string> sourceFormatConstraints = null) { _sourceRegex = sourceFormat.ConvertFormatToRegex(sourceFormatConstraints, RegexOptions.Compiled | RegexOptions.IgnoreCase); _destinationShiftShard = destinationFormat.ConvertFormatToShiftShard(); }
/// <summary> /// Initializes a new instance of the <see cref="StringShifter" /> class. /// </summary> /// <param name="sourceFormat">The source format.</param> /// <param name="destinationFormat">The destination format.</param> /// <param name="sourceFormatConstraints">The source format constraints.</param> public StringShifter(string sourceFormat, string destinationFormat, Dictionary<string, string> sourceFormatConstraints = null) { _sourceRegex = sourceFormat.ConvertFormatToRegex(sourceFormatConstraints, RegexOptions.Compiled | RegexOptions.IgnoreCase); _destinationShiftShard = destinationFormat.ConvertFormatToShiftShard(); }