internal static void WriteLine(string format, params object[] args) { if (SupportAnsiColors) { format = AnsiColors.Aggregate($"{format}{{reset}}", (result, kvp) => result.Replace(kvp.Key, kvp.Value)); } else { format = AnsiColors.Aggregate(format, (result, kvp) => result.Replace(kvp.Key, "")); } lock (consoleLock) { if (readingInput) { var backspaces = new string('\b', inputBuffer.Length + 1); var wipe = new string(' ', inputBuffer.Length + 1); SConsole.Write(backspaces); SConsole.Write(wipe); SConsole.Write(backspaces); } SConsole.WriteLine(format, args); if (readingInput) { SConsole.Write($">{inputBuffer}"); } } }
public static void WriteLine(string format, bool includeTime, params object[] args) { if (includeTime) { format = $"{{logtime}}[{DateTime.Now.ToString("dd HH:mm:ss.fff")}]{{reset}} {format}"; } if (SupportAnsiColors) { format = AnsiColors.Aggregate($"{format}{{reset}}", (result, kvp) => result.Replace(kvp.Key, kvp.Value)); } else { format = AnsiColors.Aggregate(format, (result, kvp) => result.Replace(kvp.Key, "")); } lock (consoleLock) { if (readingInput) { var backspaces = new string('\b', inputBuffer.Length + 1); var wipe = new string(' ', inputBuffer.Length + 1); SConsole.Write(backspaces); SConsole.Write(wipe); SConsole.Write(backspaces); } SConsole.WriteLine(format, args); if (readingInput) { SConsole.Write($">{inputBuffer}"); } } }
private async Task ExecuteRawScriptAsync(string scriptContent) { scriptContent = escapeScriptTextReplacements.Aggregate(scriptContent, (r, pair) => r.Replace(pair.Key, pair.Value)); var blob = $"URL.createObjectURL(new Blob([\"{scriptContent}\"],{{ \"type\": \"text/javascript\"}}))"; var bootStrapScript = $"(function(){{var d = document; var s = d.createElement('script'); s.async=false; s.src={blob}; d.head.appendChild(s); d.head.removeChild(s);}})();"; await jsRuntime.InvokeVoidAsync("eval", bootStrapScript); }
public List <Setting> GetSettings(SettingPath path, IReadOnlyDictionary <string, object> namespaces) { var attributes = namespaces.Aggregate( $"@{nameof(Setting.Name)}='{path}'", (result, next) => $"{result} and @{next.Key}='{next.Value}'"); var xPath = $"//{RootElementName}/{SettingElementName}[{attributes}]"; var xSettings = XConfig.XPathSelectElements(xPath); var elements = xSettings.Select(x => { // set default key and value var element = new Setting { Name = x.Attribute(nameof(Setting.Name)).Value, Value = x.Value }; // set other keys foreach (var settingNamespace in namespaces) { element[settingNamespace.Key] = x.Attribute(settingNamespace.Key).Value; } return(element); }).ToList(); return(elements); }
public static string FillTemplate( string template, IReadOnlyCollection <string> definitions = null, IReadOnlyDictionary <string, string> replacements = null) { definitions = definitions ?? new List <string>(); replacements = replacements ?? new Dictionary <string, string>(); // TODO: checked build? // definitions.ForEach(x => ControlFlow.Assert(template.Contains(x), // $"Definition '{x}' is not contained in template.")); // replacements.Keys.ForEach(x => ControlFlow.Assert(template.Contains(x), // $"Replacement for '{x}' is not contained in template.")); var crCount = template.Count(x => x == '\r'); var lfCount = template.Count(x => x == '\n'); var lineEnding = crCount == lfCount ? "\r\n" : "\n"; var lines = template.Split(new[] { lineEnding }, StringSplitOptions.None) .Select(x => HandleLine(x, definitions)) .Where(x => x != null) .ToList(); for (var i = 0; i < lines.Count; i++) { if (i > 0 && string.IsNullOrWhiteSpace(lines[i - 1]) && string.IsNullOrWhiteSpace(lines[i])) { lines.RemoveAt(i); i--; } } return(replacements.Aggregate(lines.Join(lineEnding), (t, r) => t.Replace(r.Key, r.Value))); }
public static object ExpandAllForProperty(string propertyName, object property, IReadOnlyDictionary <string, string> varExpansions, Func <string, PropertyDefinition> propertyDefinitionGetter) { PropertyDefinition definition = propertyDefinitionGetter(propertyName); bool didExpand = false; property = varExpansions.Aggregate(property, (current, kvp) => { didExpand |= definition.ExpandVariable(current, kvp.Key, kvp.Value, out object copy); return(copy); }); if (didExpand) { Log.Debug("Expanded all variables in property '{0}' => '{1}'", propertyName, property); } if (definition.StripEscapedVariables(property, out property)) { Log.Debug("Stripped all escaped variables in property '{0}' => '{1}'", propertyName, property); } return(property); }
public List<Setting> GetSettings(SettingPath path, IReadOnlyDictionary<string, object> namespaces) { var attributes = namespaces.Aggregate( $"@{nameof(Setting.Name)}='{path}'", (result, next) => $"{result} and @{next.Key}='{next.Value}'"); var xPath = $"//{RootElementName}/{SettingElementName}[{attributes}]"; var xSettings = XConfig.XPathSelectElements(xPath); var elements = xSettings.Select(x => { // set default key and value var element = new Setting { Name = x.Attribute(nameof(Setting.Name)).Value, Value = x.Value }; // set other keys foreach (var settingNamespace in namespaces) { element[settingNamespace.Key] = x.Attribute(settingNamespace.Key).Value; } return element; }).ToList(); return elements; }
public int GetHashCode(IReadOnlyDictionary <TKey, TValue> obj) { IEqualityComparer <TKey> keyComparer; IEqualityComparer <TValue> valueComparer; switch (obj) { case ImmutableDictionary <TKey, TValue> id: valueComparer = id.ValueComparer; keyComparer = id.KeyComparer; break; case Dictionary <TKey, TValue> rod: keyComparer = rod.Comparer; valueComparer = EqualityComparer <TValue> .Default; break; default: valueComparer = EqualityComparer <TValue> .Default; keyComparer = EqualityComparer <TKey> .Default; break; } return(obj?.Aggregate(0, (a, p) => { unchecked { return (a * 397 * 397) ^ (keyComparer.GetHashCode(p.Key) * 397) ^ valueComparer.GetHashCode(p.Value); } }) ?? 0); }
public object ExpandAllInCopy(object obj, IReadOnlyDictionary <string, string> varExpansions) { obj = varExpansions.Aggregate(obj, (current, kvp) => { ExpandInCopy(current, kvp.Key, kvp.Value, out object copy); return(copy); }); return(obj); }
public override int GetHashCode() { unchecked { int HashPair(KeyValuePair <string, HerculesValue> pair) => ((pair.Key?.GetHashCode() ?? 0) * 397) ^ (pair.Value?.GetHashCode() ?? 0); return(tags.Aggregate(tags.Count, (current, element) => current ^ HashPair(element))); } }
public static string DictionaryToString(IReadOnlyDictionary <string, string> input) { if (input == null || !input.Any()) { return(string.Empty); } var result = input.Aggregate(string.Empty, (current, pair) => current + $"{pair.Key}={pair.Value}|"); return(result.Remove(result.Length - 1)); }
private int DictHashCode <TKey, TValue>(IReadOnlyDictionary <TKey, TValue> dict) { unchecked { // I would probably support KeyValuePair as another case in // BetterComparer if it were easy, but it's nontrivial // to check for a KeyValuePair of any type arguments. :( return(dict.Aggregate(0, (h, kvp) => h + Hasher.Start.With(GetHashCode(kvp.Key)).With(GetHashCode(kvp.Value)))); } }
private static string WriteSpecifiers(IReadOnlyDictionary <string, string> specifiers) { if (specifiers == null || !specifiers.Any()) { return(string.Empty); } var tmp = specifiers.Aggregate(string.Empty, (current, pair) => current + $"{pair.Key}={pair.Value}|"); return(tmp.Remove(tmp.Length - 1)); }
public override int GetHashCode() { unchecked { var hashCode = Guid.GetHashCode(); hashCode = (hashCode * 397) ^ (DriverName?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (TeamName?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ Skins.Aggregate(0, (current, skin) => current ^ skin.GetHashCode()).GetHashCode(); return(hashCode); } }
private static string WriteAdditionalInfo(IReadOnlyDictionary <string, string> additionalInfos) { if (additionalInfos == null || !additionalInfos.Any()) { return(string.Empty); } var tmp = additionalInfos.Aggregate(string.Empty, (current, pair) => current + $"{pair.Key}={pair.Value}|"); return(tmp.Remove(tmp.Length - 1)); }
private IEnumerable <XElement> GetXSettings(SettingPath name, IReadOnlyDictionary <string, object> namespaces) { var attributes = namespaces.Aggregate( $"@{nameof(Setting.Name)}='{name}'", (result, next) => $"{result} and {next.Key}='{next.Value}'"); var xPath = $"//{RootElementName}/{SettingElementName}[{attributes}]"; var xSettings = XConfig.XPathSelectElements(xPath); return(xSettings); }
private async Task ExecuteRawScriptAsync(string scriptContent) { scriptContent = escapeScriptTextReplacements.Aggregate(scriptContent, (r, pair) => r.Replace(pair.Key, pair.Value)); StringBuilder builder = new(); builder.Append("(function(){let d = document; var s = d.createElement('script'); s.async=false; s.src="); builder.Append("URL.createObjectURL(new Blob([\""); builder.Append(scriptContent); builder.Append("\"],{\"type\": \"text/javascript\"})); d.head.appendChild(s); d.head.removeChild(s);})();"); string script = builder.ToString(); await _jsRuntime.InvokeVoidAsync("eval", script); }
private static string AppendParametersIfNeeded( string requestUri, IReadOnlyDictionary <string, string> parameters) { if (parameters == null) { return(requestUri.RemoveSlashSuffix()); } var query = parameters.Aggregate( string.Empty, (current, parameter) => current + $"{parameter.Key}={parameter.Value}&"); query = query.TrimEnd('&'); return($"{requestUri.RemoveSlashSuffix()}?{query}"); }
private async Task Initialize() { string scriptContent; using (var stream = this.GetType().Assembly.GetManifestResourceStream("blazor:js:FileReaderComponent.js")) { using (var streamReader = new StreamReader(stream)) { scriptContent = await streamReader.ReadToEndAsync(); } } scriptContent = escapeScriptTextReplacements.Aggregate(scriptContent, (r, pair) => r.Replace(pair.Key, pair.Value)); var blob = $"URL.createObjectURL(new Blob([\"{scriptContent}\"],{{ \"type\": \"text/javascript\"}}))"; var bootStrapScript = $"(function(){{var d = document; var s = d.createElement('script'); s.src={blob}; d.head.appendChild(s); d.head.removeChild(s);}})();"; await CurrentJSRuntime.InvokeAsync <object>("eval", bootStrapScript); }
private static string Replace(string input, IReadOnlyDictionary<Regex, string> rules) { return rules.Aggregate(input, (current, pattern) => pattern.Key.Replace(current, pattern.Value)); }
private IEnumerable<XElement> GetXSettings(SettingPath name, IReadOnlyDictionary<string, object> namespaces) { var attributes = namespaces.Aggregate( $"@{nameof(Setting.Name)}='{name}'", (result, next) => $"{result} and {next.Key}='{next.Value}'"); var xPath = $"//{RootElementName}/{SettingElementName}[{attributes}]"; var xSettings = XConfig.XPathSelectElements(xPath); return xSettings; }
private static string Replace(this string str, [CanBeNull] IReadOnlyDictionary <string, string> tokens) { return(tokens?.Aggregate(str, (t, r) => t.Replace($"_{r.Key}_", r.Value))); }
public static string Fix(string s) { return(Mappings.Aggregate(s, (a, b) => b.Key.Replace(a, b.Value))); }
// ReSharper disable once ConditionIsAlwaysTrueOrFalse public int GetHashCode(IReadOnlyDictionary <TKey, TValue> dictionary) => dictionary == null ? 0 : dictionary.Aggregate(dictionary.Count, (current, element) => current ^ valueComparer.GetHashCode(element.Value));
private string GetTopic(IReadOnlyDictionary <string, string> properties) { return(properties .Aggregate(_config.Value.TopicPattern, (topic, kvp) => topic.Replace($"{{{kvp.Key}}}", kvp.Value)) .ToLower()); }
public static string Substitution(IDevice device, string pattern) { return(string.IsNullOrWhiteSpace(pattern) ? pattern : Substitutions.Aggregate(pattern, (current, kvp) => current.Replace(kvp.Key, kvp.Value(device)))); }
private static string Replace(string input, IReadOnlyDictionary <Regex, string> rules) { return(rules.Aggregate(input, (current, pattern) => pattern.Key.Replace(current, pattern.Value))); }
public int GetHashCode(IReadOnlyDictionary <TKey, TValue> obj) => obj.Aggregate(17, (acc, pair) => (acc * 31 + pair.Key.GetHashCode()) * 31 + pair.Value.GetHashCode());
private static string Evaluate(string unevaluatedValue, IReadOnlyDictionary <string, string> properties) { return(properties.Aggregate(unevaluatedValue, (value, prop) => value.Replace($"$({prop.Key})", prop.Value))); }
private static string Replace(this string str, [CanBeNull] IReadOnlyDictionary <string, string> replacements) { return(replacements?.Aggregate(str, (t, r) => t.Replace(r.Key, r.Value))); }
/// <summary> /// Decodes a Base64 string to replace URL-safe characters with regular ones. /// </summary> /// <param name="input">The (potentially URL-safe) Base64 string to decode.</param> /// <returns>A regular Base64 string.</returns> private static string UrlSafeDecode(string input) { var result = UrlSafeDecoders.Aggregate(input, (current, kvp) => current.Replace(kvp.Key, kvp.Value)); return(AddPadding(result)); }
/// <summary> /// Encodes a Base64 string to be URL-safe. /// </summary> /// <param name="input">The Base64 string to encode.</param> /// <returns>A URL-safe Base64 string.</returns> private static string UrlSafeEncode(string input) { return(UrlSafeEncoders.Aggregate(input, (current, kvp) => current.Replace(kvp.Key, kvp.Value))); }