private Delegate MakeGetterImageDataViewType(DataViewRow input, int iinfo, Func <int, bool> activeOutput, out Action disposer) { Contracts.AssertValue(input); Contracts.Assert(0 <= iinfo && iinfo < _parent.ColumnPairs.Length); var lastImage = default(Bitmap); disposer = () => { if (lastImage != null) { lastImage.Dispose(); lastImage = null; } }; var getSrc = input.GetGetter <ReadOnlyMemory <char> >(input.Schema[ColMapNewToOld[iinfo]]); ReadOnlyMemory <char> src = default; ValueGetter <Bitmap> del = (ref Bitmap dst) => { if (dst != null) { dst.Dispose(); dst = null; } getSrc(ref src); if (src.Length > 0) { string path = src.ToString(); if (!string.IsNullOrWhiteSpace(_parent.ImageFolder)) { path = Path.Combine(_parent.ImageFolder, path); } // to avoid locking file, use the construct below to load bitmap var bytes = File.ReadAllBytes(path); var ms = new MemoryStream(bytes); dst = (Bitmap)Image.FromStream(ms); dst.Tag = path; // Check for an incorrect pixel format which indicates the loading failed if (dst.PixelFormat == System.Drawing.Imaging.PixelFormat.DontCare) { throw Host.Except($"Failed to load image {src.ToString()}."); } } lastImage = dst; }; return(del); }
protected override Delegate MakeGetter(Row input, int iinfo, Func <int, bool> activeOutput, out Action disposer) { Contracts.AssertValue(input); Contracts.Assert(0 <= iinfo && iinfo < _parent.ColumnPairs.Length); disposer = null; var getSrc = input.GetGetter <ReadOnlyMemory <char> >(ColMapNewToOld[iinfo]); ReadOnlyMemory <char> src = default; ValueGetter <Bitmap> del = (ref Bitmap dst) => { if (dst != null) { dst.Dispose(); dst = null; } getSrc(ref src); if (src.Length > 0) { // Catch exceptions and pass null through. Should also log failures... try { string path = src.ToString(); if (!string.IsNullOrWhiteSpace(_parent.ImageFolder)) { path = Path.Combine(_parent.ImageFolder, path); } dst = new Bitmap(path); } catch (Exception) { // REVIEW: We catch everything since the documentation for new Bitmap(string) // appears to be incorrect. When the file isn't found, it throws an ArgumentException, // while the documentation says FileNotFoundException. Not sure what it will throw // in other cases, like corrupted file, etc. throw Host.Except($"Image {src.ToString()} was not found."); } // Check for an incorrect pixel format which indicates the loading failed if (dst.PixelFormat == System.Drawing.Imaging.PixelFormat.DontCare) { throw Host.Except($"Failed to load image {src.ToString()}."); } } }; return(del); }
private Delegate MakeGetterVectorDataViewByteType(DataViewRow input, int iinfo, Func <int, bool> activeOutput, out Action disposer) { Contracts.AssertValue(input); Contracts.Assert(0 <= iinfo && iinfo < _parent.ColumnPairs.Length); disposer = null; var getSrc = input.GetGetter <ReadOnlyMemory <char> >(input.Schema[ColMapNewToOld[iinfo]]); ReadOnlyMemory <char> src = default; ValueGetter <VBuffer <byte> > del = (ref VBuffer <byte> dst) => { getSrc(ref src); if (src.Length > 0) { string path = src.ToString(); if (!string.IsNullOrWhiteSpace(_parent.ImageFolder)) { path = Path.Combine(_parent.ImageFolder, path); } if (!TryLoadDataIntoBuffer(path, ref dst)) { var editor = VBufferEditor.Create(ref dst, 0); //Empty Image dst = editor.Commit(); } } else { var editor = VBufferEditor.Create(ref dst, 0); dst = editor.Commit(); } }; return(del); }
private async Task <bool> authenticateUserBasic(SessionEventArgsBase session, ReadOnlyMemory <char> authenticationType, ReadOnlyMemory <char> credentials, Func <SessionEventArgsBase, string, string, Task <bool> > proxyBasicAuthenticateFunc) { if (!KnownHeaders.ProxyAuthorizationBasic.Equals(authenticationType.Span)) { // Return not authorized session.HttpClient.Response = createAuthentication407Response("Proxy Authentication Invalid"); return(false); } string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(credentials.ToString())); int colonIndex = decoded.IndexOf(':'); if (colonIndex == -1) { // Return not authorized session.HttpClient.Response = createAuthentication407Response("Proxy Authentication Invalid"); return(false); } string username = decoded.Substring(0, colonIndex); string password = decoded.Substring(colonIndex + 1); bool authenticated = await proxyBasicAuthenticateFunc(session, username, password); if (!authenticated) { session.HttpClient.Response = createAuthentication407Response("Proxy Authentication Invalid"); } return(authenticated); }
public ProtocolReader(ILogger <ProtocolReader> logger, ReadOnlyMemory <char> memory) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger.LogTrace("{Source}", memory.ToString()); _data = memory; }
public string GetContent(int ifeat) { Contracts.Assert(0 <= ifeat && ifeat < Count); ReadOnlyMemory <char> content = _content.GetItemOrDefault(ifeat); return(!content.IsEmpty ? content.ToString() : DatasetUtils.GetDefaultTransform(GetName(ifeat))); }
public string GetName(int ifeat) { Contracts.Assert(0 <= ifeat && ifeat < Count); ReadOnlyMemory <char> name = _names.GetItemOrDefault(ifeat); return(!name.IsEmpty ? name.ToString() : string.Format("f{0}", ifeat)); }
private string ParseValues(ReadOnlyMemory <char> pathLookup) { pathLookup = TrimDataReference(pathLookup); foreach (ReadOnlyMemory <char> arithmeticPath in GetPartBySeperators(pathLookup, new char[] { '/', '*', '+', '-', '(', ')' })) { ReadOnlySpan <char> path = arithmeticPath.Span; if (!double.TryParse(path.Trim(), out double constant)) { if (!path.Trim().IsEmpty) { int position = pathLookup.Span.IndexOf(arithmeticPath.Span); if (position >= 0) { string value = GetValueFromPath(arithmeticPath.ToString(), out double?scalingValue); if (scalingValue.HasValue) { pathLookup = $"{pathLookup.Slice(0, position)}{value}~~{scalingValue}~~{pathLookup.Slice(position + arithmeticPath.Length)}".AsMemory(); } else { pathLookup = (pathLookup.Slice(0, position) + value + pathLookup.Slice(position + arithmeticPath.Length)).AsMemory(); } } } } } return(pathLookup.ToString()); }
/// <summary> /// Gets a normalized title derived from the file path. /// </summary> /// <returns>A normalized title.</returns> public string GetTitle() { // Get the filename, unless an index file, then get containing directory ReadOnlyMemory <char> titleMemory = Segments[Segments.Length - 1]; if (titleMemory.StartsWith(IndexFileName) && Segments.Length > 1) { titleMemory = Segments[Segments.Length - 2]; } // Strip the extension(s) int extensionIndex = titleMemory.Span.IndexOf('.'); if (extensionIndex > 0) { titleMemory = titleMemory.Slice(0, extensionIndex); } // Decode URL escapes string title = WebUtility.UrlDecode(titleMemory.ToString()); // Replace special characters with spaces title = title.Replace('-', ' ').Replace('_', ' '); // Join adjacent spaces while (title.IndexOf(" ", StringComparison.Ordinal) > 0) { title = title.Replace(" ", " "); } // Capitalize return(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(title)); }
public NormStr Get(ReadOnlyMemory <char> str, bool add = false, bool duplicateStr = true) { AssertValid(); var span = str.Span; uint hash = Hashing.HashString(span); int ins = GetIns(hash); while (ins >= 0) { ulong meta = _rgmeta[ins]; if ((int)Utils.GetLo(meta) == str.Length) { var ns = GetNs(ins); if (ns.Value.Span.SequenceEqual(span)) { return(ns); } } ins = (int)Utils.GetHi(meta); } Contracts.Assert(ins == -1); if (duplicateStr) { // To avoid the case where 'str' actually stores a string with the // content of a whole row in the dataset, a new 'str' is created // See issue https://github.com/dotnet/machinelearning/issues/4571 // and PR https://github.com/dotnet/machinelearning/pull/4576 return(add ? AddCore(str.ToString().AsMemory(), hash) : null); } return(add ? AddCore(str, hash) : null); }
protected LineChInfo GetLineCh(TextSpan currentSpan) { if (currentSpan.IsEmpty) { return(new LineChInfo { LineNumber = 0, ChNumber = 0, PrevLines = new string[0], Line = string.Empty, }); } var content = _textSpan.ToString(); var previewContent = content.Substring(0, currentSpan.Offset); var lines = previewContent.Split("\n").Select(x => x.Replace("\r", "")).ToArray(); var line = lines[lines.Length - 1]; var prevLines = lines.SkipLast(1).TakeLast(3).ToArray(); var maxLen = content.Length - (currentSpan.Offset + currentSpan.Length); maxLen = Math.Min(maxLen, 50); var backContent = content.Substring(currentSpan.Offset + currentSpan.Length, maxLen); var backLine = backContent.Split("\n").Select(x => x.Replace("\r", "")).First(); return(new LineChInfo { LineNumber = lines.Length, ChNumber = line.Length + 1, PrevLines = prevLines, Line = line, BackContent = backLine, }); }
public List <object> Deserialize(IFormatProvider provider, char delimiter, bool skipHeader, ReadOnlyMemory <char> csv) { bool firstRow = true; List <object> items = new List <object>(); while (csv.Length > 0) { List <ReadOnlyMemory <char> > columns = StringSplitter.ReadNextLine(ref csv, delimiter); if (firstRow && skipHeader) { firstRow = false; continue; } if (_properties.Length != columns.Count) { int endOfLine = csv.Span.IndexOf('\n'); string line = endOfLine == -1 ? csv.ToString() : csv.Slice(0, endOfLine).ToString(); throw new CsvFormatException(typeof(T), line, $"Row must consists of {_properties.Length} columns."); } T item = Activator.CreateInstance <T>(); for (int i = 0; i < _properties.Length; i++) { switch (_deserializeAs[i]) { case DeserializeAs.SByte: if (columns[i].Length >= 2 && columns[i].Span[0] == '"' && sbyte.TryParse(columns[i].Span[1..^ 1], NumberStyles.Integer, provider, out sbyte vSByte))
public void Execute(ITwitchClient client, ChatMessage message, ReadOnlyMemory <char> parsedText) { if (allCommands.Count == 0) { PopulateAllCommandsDictionary(service); } if (parsedText.IsEmpty) { var keys = allCommands.Keys; var output = string.Join(",", keys); client.SendMessage(message.Channel, $"Available commands: {output}"); } else { var parsedTextAsString = parsedText.ToString(); if (allCommands.ContainsKey(parsedTextAsString)) { client.SendMessage(message.Channel, $"{parsedText}: {allCommands[parsedTextAsString]}"); } else { client.SendMessage(message.Channel, $"{parsedText} command does not exist in bot"); } } }
public async Task Execute(IChatService chatService, string userName, bool isModerator, bool isBroadcaster, ReadOnlyMemory <char> rhs) { if (!(isModerator || isBroadcaster)) { return; } var rhsTest = rhs.ToString(); if (rhsTest.StartsWith("@")) { rhsTest = rhsTest.Substring(1); } if (rhsTest.StartsWith("http")) { return; } if (rhsTest.Contains(" ")) { return; } rhsTest = WebUtility.UrlEncode(rhsTest); var result = await _HttpClient.GetAsync(rhsTest); if (result.StatusCode != HttpStatusCode.OK) { return; } await chatService.SendMessageAsync($"Please follow @{rhsTest} at: https://twitch.tv/{rhsTest}"); }
public LineChInfo GetLineCh(TextSpan currentSpan) { if (currentSpan.IsEmpty) { return(new LineChInfo { LineNumber = 0, ChNumber = 0, PrevLines = new string[0], Line = String.Empty, }); } var content = _textSpan.ToString(); var previewContent = content.Substring(0, currentSpan.Offset); var lines = previewContent.Split("\r\n"); var line = lines[lines.Length - 1]; var prevLines = lines.SkipLast(1).TakeLast(3).ToArray(); return(new LineChInfo { LineNumber = lines.Length, ChNumber = line.Length + 1, PrevLines = prevLines, Line = line }); }
public void SequencePredictorSchemaTest() { int keyCount = 10; var scoreColumnType = new KeyType(DataKind.U4, 0, keyCount); VBuffer <ReadOnlyMemory <char> > keyNames = GenerateKeyNames(keyCount); var sequenceSchema = ScoreSchemaFactory.CreateSequencePredictionSchema(scoreColumnType, MetadataUtils.Const.ScoreColumnKind.SequenceClassification, keyNames); // Output schema should only contain one column, which is the predicted label. Assert.Single(sequenceSchema); var scoreColumn = sequenceSchema[0]; // Check score column name. Assert.Equal(MetadataUtils.Const.ScoreValueKind.PredictedLabel, scoreColumn.Name); // Check score column type. Assert.True(scoreColumn.Type.IsKey); Assert.Equal((scoreColumnType as KeyType).Min, (scoreColumn.Type as KeyType).Min); Assert.Equal((scoreColumnType as KeyType).Count, (scoreColumn.Type as KeyType).Count); Assert.Equal((scoreColumnType as KeyType).RawKind, (scoreColumn.Type as KeyType).RawKind); Assert.Equal((scoreColumnType as KeyType).Contiguous, (scoreColumn.Type as KeyType).Contiguous); // Check metadata. Because keyNames is not empty, there should be three metadata fields. var scoreMetadata = scoreColumn.Metadata; Assert.Equal(3, scoreMetadata.Schema.Count); // Check metadata columns' names. Assert.Equal(MetadataUtils.Kinds.KeyValues, scoreMetadata.Schema[0].Name); Assert.Equal(MetadataUtils.Kinds.ScoreColumnKind, scoreMetadata.Schema[1].Name); Assert.Equal(MetadataUtils.Kinds.ScoreValueKind, scoreMetadata.Schema[2].Name); // Check metadata columns' types. Assert.True(scoreMetadata.Schema[0].Type.IsVector); Assert.Equal(keyNames.Length, (scoreMetadata.Schema[0].Type as VectorType).VectorSize); Assert.Equal(TextType.Instance, (scoreMetadata.Schema[0].Type as VectorType).ItemType); Assert.Equal(TextType.Instance, scoreColumn.Metadata.Schema[1].Type); Assert.Equal(TextType.Instance, scoreColumn.Metadata.Schema[2].Type); // Check metadata columns' values. var keyNamesGetter = scoreMetadata.GetGetter <VBuffer <ReadOnlyMemory <char> > >(0); var actualKeyNames = new VBuffer <ReadOnlyMemory <char> >(); keyNamesGetter(ref actualKeyNames); Assert.Equal(keyNames.Length, actualKeyNames.Length); Assert.Equal(keyNames.DenseValues(), actualKeyNames.DenseValues()); var scoreColumnKindGetter = scoreMetadata.GetGetter <ReadOnlyMemory <char> >(1); ReadOnlyMemory <char> scoreColumnKindValue = null; scoreColumnKindGetter(ref scoreColumnKindValue); Assert.Equal(MetadataUtils.Const.ScoreColumnKind.SequenceClassification, scoreColumnKindValue.ToString()); var scoreValueKindGetter = scoreMetadata.GetGetter <ReadOnlyMemory <char> >(2); ReadOnlyMemory <char> scoreValueKindValue = null; scoreValueKindGetter(ref scoreValueKindValue); Assert.Equal(MetadataUtils.Const.ScoreValueKind.PredictedLabel, scoreValueKindValue.ToString()); }
public Task <ReadOnlyMemory <char> > ReplaceAsync(ReadOnlyMemory <char> raw) { if (raw.IsEmpty) { return(Task.FromResult(raw)); } var matchIndexes = GetMatches(raw).ToList(); if (!matchIndexes.Any()) { return(Task.FromResult(raw)); } var memoryList = new List <ReadOnlyMemory <char> >(); try { memoryList.AddRange(CreateEscapedTextList(raw, matchIndexes)); } catch (Exception e) { throw new EscapeRouteParseException($"Cannot parse the string '{raw.ToString()}'", e); } if (!memoryList.Any()) { return(Task.FromResult(raw)); } var combinedMemory = memoryList.CombineMemory(); return(Task.FromResult((ReadOnlyMemory <char>)combinedMemory)); }
public void SequencePredictorSchemaTest() { int keyCount = 10; var expectedScoreColumnType = new KeyDataViewType(typeof(uint), keyCount); VBuffer <ReadOnlyMemory <char> > keyNames = GenerateKeyNames(keyCount); var sequenceSchema = ScoreSchemaFactory.CreateSequencePredictionSchema(expectedScoreColumnType, AnnotationUtils.Const.ScoreColumnKind.SequenceClassification, keyNames); // Output schema should only contain one column, which is the predicted label. Assert.Single(sequenceSchema); var scoreColumn = sequenceSchema[0]; // Check score column name. Assert.Equal(AnnotationUtils.Const.ScoreValueKind.PredictedLabel, scoreColumn.Name); // Check score column type. var actualScoreColumnType = scoreColumn.Type as KeyDataViewType; Assert.NotNull(actualScoreColumnType); Assert.Equal(expectedScoreColumnType.Count, actualScoreColumnType.Count); Assert.Equal(expectedScoreColumnType.RawType, actualScoreColumnType.RawType); // Check metadata. Because keyNames is not empty, there should be three metadata fields. var scoreMetadata = scoreColumn.Annotations; Assert.Equal(3, scoreMetadata.Schema.Count); // Check metadata columns' names. Assert.Equal(AnnotationUtils.Kinds.KeyValues, scoreMetadata.Schema[0].Name); Assert.Equal(AnnotationUtils.Kinds.ScoreColumnKind, scoreMetadata.Schema[1].Name); Assert.Equal(AnnotationUtils.Kinds.ScoreValueKind, scoreMetadata.Schema[2].Name); // Check metadata columns' types. Assert.True(scoreMetadata.Schema[0].Type is VectorDataViewType); Assert.Equal(keyNames.Length, (scoreMetadata.Schema[0].Type as VectorDataViewType).Size); Assert.Equal(TextDataViewType.Instance, (scoreMetadata.Schema[0].Type as VectorDataViewType).ItemType); Assert.Equal(TextDataViewType.Instance, scoreColumn.Annotations.Schema[1].Type); Assert.Equal(TextDataViewType.Instance, scoreColumn.Annotations.Schema[2].Type); // Check metadata columns' values. var keyNamesGetter = scoreMetadata.GetGetter <VBuffer <ReadOnlyMemory <char> > >(scoreMetadata.Schema[0]); var actualKeyNames = new VBuffer <ReadOnlyMemory <char> >(); keyNamesGetter(ref actualKeyNames); Assert.Equal(keyNames.Length, actualKeyNames.Length); Assert.Equal(keyNames.DenseValues(), actualKeyNames.DenseValues()); var scoreColumnKindGetter = scoreMetadata.GetGetter <ReadOnlyMemory <char> >(scoreMetadata.Schema[1]); ReadOnlyMemory <char> scoreColumnKindValue = null; scoreColumnKindGetter(ref scoreColumnKindValue); Assert.Equal(AnnotationUtils.Const.ScoreColumnKind.SequenceClassification, scoreColumnKindValue.ToString()); var scoreValueKindGetter = scoreMetadata.GetGetter <ReadOnlyMemory <char> >(scoreMetadata.Schema[2]); ReadOnlyMemory <char> scoreValueKindValue = null; scoreValueKindGetter(ref scoreValueKindValue); Assert.Equal(AnnotationUtils.Const.ScoreValueKind.PredictedLabel, scoreValueKindValue.ToString()); }
public void TestSaveImages() { var env = new MLContext(); var dataFile = GetDataPath("images/images.tsv"); var imageFolder = Path.GetDirectoryName(dataFile); var data = TextLoader.Create(env, new TextLoader.Arguments() { Columns = new[] { new TextLoader.Column("ImagePath", DataKind.TX, 0), new TextLoader.Column("Name", DataKind.TX, 1), } }, new MultiFileSource(dataFile)); var images = new ImageLoaderTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data); var cropped = new ImageResizerTransformer(env, "ImageCropped", 100, 100, "ImageReal", ImageResizerTransformer.ResizingKind.IsoPad).Transform(images); cropped.Schema.TryGetColumnIndex("ImagePath", out int pathColumn); cropped.Schema.TryGetColumnIndex("ImageCropped", out int cropBitmapColumn); using (var cursor = cropped.GetRowCursorForAllColumns()) { var pathGetter = cursor.GetGetter <ReadOnlyMemory <char> >(pathColumn); ReadOnlyMemory <char> path = default; var bitmapCropGetter = cursor.GetGetter <Bitmap>(cropBitmapColumn); Bitmap bitmap = default; while (cursor.MoveNext()) { pathGetter(ref path); bitmapCropGetter(ref bitmap); Assert.NotNull(bitmap); var fileToSave = GetOutputPath(Path.GetFileNameWithoutExtension(path.ToString()) + ".cropped.jpg"); bitmap.Save(fileToSave, System.Drawing.Imaging.ImageFormat.Jpeg); } } Done(); }
public static void ToStringMemoryOverFullStringReturnsOriginal() { string original = TestHelpers.BuildString(10, 42); ReadOnlyMemory <char> memory = original.AsMemory(); string returnedString = memory.ToString(); string returnedStringUsingSlice = memory.Slice(0, original.Length).ToString(); string subString1 = memory.Slice(1).ToString(); string subString2 = memory.Slice(0, 2).ToString(); string subString3 = memory.Slice(1, 2).ToString(); Assert.Equal(original, returnedString); Assert.Equal(original, returnedStringUsingSlice); Assert.Equal(original.Substring(1), subString1); Assert.Equal(original.Substring(0, 2), subString2); Assert.Equal(original.Substring(1, 2), subString3); Assert.Same(original, returnedString); Assert.Same(original, returnedStringUsingSlice); Assert.NotSame(original, subString1); Assert.NotSame(original, subString2); Assert.NotSame(original, subString3); Assert.NotSame(subString1, subString2); Assert.NotSame(subString1, subString3); Assert.NotSame(subString2, subString3); }
/// <summary> /// A helper to throw a new exception from the given source location, indicating /// an invalid token in the document. /// </summary> /// <param name="location">The location in the source text.</param> /// <param name="expected">The expected value.</param> /// <param name="actual">The actual value received.</param> public static void ThrowFromExpectation( SourceLocation location, ReadOnlyMemory <char> expected, ReadOnlyMemory <char> actual) { ThrowFromExpectation(location, expected.ToString(), actual.ToString()); }
/// <summary> /// 接收到消息 /// </summary> /// <param name="channel">通道</param> /// <param name="body"></param> /// <param name="send">对象</param> /// <param name="e">参数</param> /// <param name="config">配置</param> /// <returns></returns> private void Received(IModel channel, ReadOnlyMemory <byte> body, object send, BasicDeliverEventArgs e, ConsumingConfig config) { var message = body.ToString(); Console.WriteLine($"[{DateTime.Now:HH:mm:ss}]:{message}"); channel.BasicAck(e.DeliveryTag, false); }
public static void ToStringChar_Empty() { var memory = new ReadOnlyMemory <char>(); Assert.Equal("", memory.ToString()); Assert.Equal("", ReadOnlyMemory <char> .Empty.ToString()); Assert.Equal("", memory.Span.ToString()); }
public static void ToStringInt_Empty() { var memory = new ReadOnlyMemory <int>(); Assert.Equal("System.ReadOnlyMemory<Int32>[0]", memory.ToString()); Assert.Equal("System.ReadOnlyMemory<Int32>[0]", ReadOnlyMemory <int> .Empty.ToString()); Assert.Equal("System.ReadOnlySpan<Int32>[0]", memory.Span.ToString()); }
public TEnum?Deserialize(ReadOnlyMemory <char> text, IFormatProvider provider, CsvColumnAttribute?attribute) { if (text.Length == 0) { return(null); } return(Enum.Parse <TEnum>(text.ToString())); }
private async Task HandleCommandsAsync(DiscordClient sender, MessageCreateEventArgs e) { if (e.Author.IsBot) // bad bot { return; } if (!this.Config.EnableDms && e.Channel.IsPrivate) { return; } var mpos = -1; if (this.Config.EnableMentionPrefix) { mpos = e.Message.GetMentionPrefixLength(this.Client.CurrentUser); } if (this.Config.StringPrefixes?.Any() == true) { foreach (var pfix in this.Config.StringPrefixes) { if (mpos == -1 && !string.IsNullOrWhiteSpace(pfix)) { mpos = e.Message.GetStringPrefixLength(pfix, this.Config.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase); } } } if (mpos == -1 && this.Config.PrefixResolver != null) { mpos = await this.Config.PrefixResolver(e.Message).ConfigureAwait(false); } if (mpos == -1) { return; } ReadOnlyMemory <char> pfx = e.Message.Content.AsMemory().Slice(0, mpos); ReadOnlyMemory <char> cnt = e.Message.Content.AsMemory().Slice(mpos); var __ = 0; var fname = cnt.ExtractNextArgument(ref __); var cmd = this.FindCommand(cnt, out string args); var ctx = this.CreateContext(e.Message, pfx.ToString(), cmd, args); if (cmd == null) { await this._error.InvokeAsync(this, new CommandErrorEventArgs { Context = ctx, Exception = new CommandNotFoundException(fname) }).ConfigureAwait(false); return; } await this.Config.CommandExecutor.ExecuteAsync(ctx).ConfigureAwait(false); }
static void addOptions( AnalyzerConfig.Section section, TreeOptions.Builder treeBuilder, AnalyzerOptions.Builder analyzerBuilder, ArrayBuilder <Diagnostic> diagnosticBuilder, string analyzerConfigPath, ConcurrentDictionary <ReadOnlyMemory <char>, string> diagIdCache) { const string DiagnosticOptionPrefix = "dotnet_diagnostic."; const string DiagnosticOptionSuffix = ".severity"; foreach (var(key, value) in section.Properties) { // Keys are lowercased in editorconfig parsing int diagIdLength = -1; if (key.StartsWith(DiagnosticOptionPrefix, StringComparison.Ordinal) && key.EndsWith(DiagnosticOptionSuffix, StringComparison.Ordinal)) { diagIdLength = key.Length - (DiagnosticOptionPrefix.Length + DiagnosticOptionSuffix.Length); } if (diagIdLength >= 0) { ReadOnlyMemory <char> idSlice = key.AsMemory().Slice(DiagnosticOptionPrefix.Length, diagIdLength); // PERF: this is similar to a double-checked locking pattern, and trying to fetch the ID first // lets us avoid an allocation if the id has already been added if (!diagIdCache.TryGetValue(idSlice, out var diagId)) { // We use ReadOnlyMemory<char> to allow allocation-free lookups in the // dictionary, but the actual keys stored in the dictionary are trimmed // to avoid holding GC references to larger strings than necessary. The // GetOrAdd APIs do not allow the key to be manipulated between lookup // and insertion, so we separate the operations here in code. diagId = idSlice.ToString(); diagId = diagIdCache.GetOrAdd(diagId.AsMemory(), diagId); } if (TryParseSeverity(value, out ReportDiagnostic severity)) { treeBuilder[diagId] = severity; } else { diagnosticBuilder.Add(Diagnostic.Create( InvalidAnalyzerConfigSeverityDescriptor, Location.None, diagId, value, analyzerConfigPath)); } } else { analyzerBuilder[key] = value; } } }
public static void ToStringForMemoryOfString() { string[] a = { "a", "b", "c" }; var memory = new ReadOnlyMemory <string>(a); Assert.Equal("System.ReadOnlyMemory<String>[3]", memory.ToString()); Assert.Equal("System.ReadOnlyMemory<String>[1]", memory.Slice(1, 1).ToString()); Assert.Equal("System.ReadOnlySpan<String>[3]", memory.Span.ToString()); }
public static void ToStringMemoryFromReadOnlyMemory() { string testString = "abcdefg"; ReadOnlyMemory <char> memory = testString.AsMemory(); Assert.Equal(testString, memory.ToString()); Assert.Equal(testString.Substring(1, 1), memory.Slice(1, 1).ToString()); Assert.Equal(testString, memory.Span.ToString()); }
public static void ToStringChar() { char[] a = { 'a', 'b', 'c' }; var memory = new ReadOnlyMemory <char>(a); Assert.Equal("abc", memory.ToString()); Assert.Equal("b", memory.Slice(1, 1).ToString()); Assert.Equal("abc", memory.Span.ToString()); }