• Avalanche.Core
Search Results for

    Show / Hide Table of Contents
    • Avalanche.Accessor
      • Introduction
      • IAccessor
        • IAccessor
        • IListAccessor
        • IMapAccessor
        • IRecordAccessor
        • IContentAccessor
        • IOneOfAccessor
        • IAnyAccessor
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • Dependency Injection
        • AccessorMessages
    • Avalanche.Binding
      • Introduction
    • Avalanche.Core
      • License
    • Avalanche.DataType
      • Introduction
      • DataType
        • IDataType
        • IListType
        • IMapType
        • IRecordType
        • IFieldType
        • IOneOfType
        • IAnyType
        • IStringType
        • IValueType
        • IIntegerType
        • IEnumerationType
        • IRealType
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • Enum
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • DataTypeRequest
        • PrintTree
        • DataTypeMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystem
          • IFileSystemBrowse
          • IFileSystemCreateDirectory
          • IFileSystemDelete
          • IFileSystemFileAttribute
          • IFileSystemMount
          • IFileSystemMove
          • IFileSystemObserve
          • IFileSystemOpen
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Events
      • Utilities
        • Dispose
        • File Scanner
        • Visit Tree
        • File Operation
    • Avalanche.Identity
      • Introduction
      • Identity
      • IdentityParts
      • IdentityInterner
      • IdentityComparer
      • Print Tree
      • IdentityAccessors
        • Introduction
        • TypeName
    • Avalanche.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
      • Asp.Net
        • Introduction
        • Supplying localization
        • Inject to pages
        • Culture Assigned
        • Minimalistic Api
        • Diagnostics
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR40
        • Unicode.CLDR41
        • Unicode.CLDR42
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
        • Printing templates
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Microsoft.Extensions
        • DependencyInjection
      • Articles
        • Aggregate Messages
        • Localization
        • Logging
        • Validation
    • Avalanche.Service
      • Introduction
      • Service
        • Introduction
        • IService
        • IServiceDisposable
        • IServiceDecoration
        • IServiceCast
        • IServiceObservable
        • IServiceContainer
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Scope
      • Handler
        • Introduction
        • IHandler
        • IHandlerCast
        • IHandlerDecoration
        • IHandlerWithOrder
        • CancellationToken
        • Cyclicity
        • Delegates
        • Invokable
        • ExportAttribute
        • OrderAttribute
        • PrintTree
        • Recursion
      • Query
        • Introduction
        • IQuery
        • IQueryCast
        • IQueryDecoration
      • Entry
        • Introduction
        • IEntry
        • IEntryCast
        • IEntryDecoration
        • IEntryObservable
        • IEntryVisitable
        • EntryState
      • Request
        • Introduction
        • IRequest
        • IRequestFor
        • IRequestToBeCached
        • IRequestToBeDisposed
        • RequestAttribute
        • ContextParameterAttribute
        • Print Tree
      • Dependency Injection
        • Introduction
        • Asp.Net
        • ServiceRequest<T>
        • Decorating a service
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
      • Examples
        • NodeCount
        • Expression
        • Mapper
      • Articles
        • Benchmarks
        • Error Handling
        • ServiceMessages
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • ItfMessages
        • Win32Messages
        • ClrMessages
      • System
        • Introduction
        • AccessControlMessages
        • AggregateMessages
        • AppDomainMessages
        • ArgumentMessages
        • ArgumentNullMessages
        • ArgumentOutOfRangeMessages
        • ArithmeticMessages
        • ArrayMessages
        • AssemblyMessages
        • BadImageFormatMessages
        • CodeContractMessages
        • CodePageMessages
        • CollectionsMessages
        • CompilerServiceMessages
        • CryptographyMessages
        • CultureMessages
        • DiagnosticsMessages
        • EventSourceMessages
        • ExecutionEngineMessages
        • FormatMessages
        • HostProtectionMessages
        • IOMessages
        • IndexOutOfRangeMessages
        • InteropServiceMessages
        • InvalidCastMessages
        • InvalidOperationMessages
        • IsolatedStorageMessages
        • LazyMessages
        • MarshalerMessages
        • MemoryMessages
        • MiscellaneousMessages
        • NotImplementedMessages
        • NotSupportedMessages
        • ObjectDisposedMessages
        • OperationCanceledMessages
        • OverflowMessages
        • PlatformMessages
        • PolicyMessages
        • PrincipalMessages
        • ProgramMessages
        • ReferenceMessages
        • ReflectionMessages
        • RegionMessages
        • RemotingMessages
        • ResourcesMessages
        • SecurityMessages
        • SerializationMessages
        • StackMessages
        • TaskMessages
        • TextMessages
        • ThreadingMessages
        • TimeZoneMessages
        • TypeMessages
        • XmlMessages
      • HttpStatusCode
      • OpcUaStatusCode
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • Avalanche.Utilities
      • Introduction
      • Collections
        • Tuples
        • StructList
        • ArrayList
        • BijectionMap
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • EnumerableExtensions
        • TupleUtilities
        • ArrayUtilities
      • Comparers
        • IGraphComparer
        • IGraphComparable
        • AlphaNumericComparer
        • EnumerableComparer
        • EnumerableGraphComparer
        • ReferenceComparer
        • KeyValuePairComparer
        • DefaultComparerProvider
        • RecordComparer
      • Cloners
        • ICloner
        • IGraphCloner
        • IGraphCloneable
        • ListCloner
        • DictionaryCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Pipe
        • Cache
        • ResultCapture
        • AsReadOnly
        • AsService
        • IProviderEvent
      • Record
        • IRecordDescription
        • IFieldDescription
        • IConstructorDescription
        • IConstructionDescription
        • IParameterDescription
        • IRecordProviders
        • RecordDelegates
          • RecordCreate
          • RecordClone
          • RecordCopy
          • IRecordDelegates
        • FieldDelegates
          • FieldRead
          • FieldWrite
          • RecreateWith
          • IFieldDelegates
      • String
        • IEscaper
        • UnicodeString
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • Void
    • Avalanche.Writer
      • Introduction
      • ConstantWriter
      • Context
      • ConvertWriter
      • DefaultConstructor
      • DelegateWriter
      • PassthroughWriter
      • Referer
      • TypeCast
      • Writer
      • WriterPipe
      • WriterMessages

    ITemplateBreakdown

    ITemplateBreakdown is a breakdown of template string into text and placeholder parts.

    /// <summary>Template string breakdown into parts, e.g. "Welcome, {0}!", into text and placeholder parts. </summary>
    public interface ITemplateBreakdown : ITemplateText
    {
        /// <summary>Breakdown into sequence of text and placeholder parts.</summary>
        ITemplatePart[] Parts { get; set; }
        /// <summary>Placeholders in order of occurance in <see cref="ITemplateText.Text"/>.</summary>
        ITemplatePlaceholderPart[] Placeholders { get; set; }
        /// <summary>Parameters in order of arguments and <see cref="ITemplateParameterPart.ParameterIndex"/>.</summary>
        ITemplateParameterPart?[] Parameters { get; set; }
    }
    


    TemplateBreakdown is the default implementation. Breakdown is composed of parts that describe string segments.

    // Create format string
    string text = "Welcome, {user}";
    ReadOnlyMemory<char> mem = text.AsMemory();
    TemplateBreakdown breakdown = new TemplateBreakdown { Text = text };
    var textPart = new TemplateTextPart().SetBreakdown(breakdown).SetTexts(mem[..9], Escaper.Brace);
    var parameter = new TemplateParameterPart().SetBreakdown(breakdown).SetTexts(mem[10..14]);
    var placeholder = new TemplatePlaceholderPart().SetBreakdown(breakdown).SetTexts(mem[9..15]).SetParameter(parameter);
    breakdown.Parts = new ITemplatePart[] { textPart, placeholder };
    breakdown.SetReadOnly();
    // Print
    WriteLine(breakdown.Print(CultureInfo.InvariantCulture, new object?[] { "Donald Duck" }));
    

    ITemplateFormat.Breakdown[string] parses string into breakdown using the rules of the template format.

    ITemplateBreakdown breakdown1 = TemplateFormat.BraceNumeric.Breakdown["Welcome, {0}"];
    ITemplateBreakdown breakdown2 = TemplateFormat.BraceAlphaNumeric.Breakdown["Welcome, {user}"];
    

    .Print(cultureInfo, arguments) puts arguments in placeholder locations.

    ITemplateBreakdown breakdown = TemplateFormat.BraceAlphaNumeric.Breakdown["Welcome, {user}"];
    WriteLine(breakdown.Print(CultureInfo.InvariantCulture, new object?[] { "Donald Duck" })); // "Welcome, Donald Duck"
    

    Parse errors are readable from ITemplateMalformedPart.

    ITemplateBreakdown breakdown = TemplateFormat.BraceAlphaNumeric.Breakdown["Welcome, {}"];
    // Get malformed parts
    foreach ((int index, int length, ReadOnlyMemory<char>? malformed) in breakdown.MalformedParts())
    {
        // Write
        WriteLine($"Malformed '{malformed}' at: {index}:{index + length}"); // Malformed at: 9:11
    }
    

    Parameter index can be used multiple times in different placeholder locations.

    // Create breakdown
    ITemplateBreakdown breakdown = TemplateFormat.Brace.Breakdown["Error code: {0} (0x{0:X4})."];
    // "Error code: 256 (0x0100)."
    WriteLine(breakdown.Print(null, new object?[] { 0x100 }));
    
    // Placeholder: "{0}", Placeholder: "{0:X8}"
    WriteLine(string.Join(", ", (IEnumerable<ITemplatePlaceholderPart>)breakdown.Placeholders));
    // Parameter: "0"
    WriteLine(string.Join(", ", (IEnumerable<ITemplateParameterPart>)breakdown.Parameters));
    

    As can parameter name.

    // Create breakdown
    ITemplateBreakdown breakdown = TemplateFormat.Brace.Breakdown["Error code: {code} (0x{code:X4})."];
    // "Error code: 256 (0x0100)."
    WriteLine(breakdown.Print(null, new object?[] { 0x100 }));
    
    // Placeholder: "{code}", Placeholder: "{code:X4}"
    WriteLine(string.Join(", ", (IEnumerable<ITemplatePlaceholderPart>)breakdown.Placeholders));
    // Parameter: "code"
    WriteLine(string.Join(", ", (IEnumerable<ITemplateParameterPart>)breakdown.Parameters));
    

    ITemplatePart

    ITemplatePart is a set of interfaces that describe template parts. These can be extended in 3rd party libraries.

    /// <summary>A part of a <see cref="ITemplateBreakdown"/>.</summary>
    public interface ITemplatePart
    {
        /// <summary>Reference to breakdown root</summary>
        ITemplateBreakdown? TemplateBreakdown { get; set; }
        /// <summary>Escaped text that is a slice of <see cref="ITemplateText.Text"/>.</summary>
        ReadOnlyMemory<char> Escaped { get; set; }
        /// <summary>Unescaped text that is either exact same slice than <see cref="Escaped"/>, or its derived unescape.</summary>
        ReadOnlyMemory<char> Unescaped { get; set; }
    }
    
    /// <summary>Text part.</summary>
    public interface ITemplateTextPart : ITemplatePart { }
    
    /// <summary>A placeholder part of <see cref="ITemplateBreakdown"/>.</summary>
    public interface ITemplatePlaceholderPart : ITemplatePart
    {
        /// <summary>Parameter name or number, e.g. "object" or "0"</summary>
        ITemplateParameterPart Parameter { get; set; }
        /// <summary>Formatting descriptions, e.g. ",10:N0" or ":X8".</summary>
        ITemplateFormattingPart? Formatting { get; set; }
        /// <summary>Alignment describes padding.</summary>
        ITemplateAlignmentPart? Alignment { get; set; }
    }
    
    /// <summary>Parameter name or index number part</summary>
    public interface ITemplateParameterPart : ITemplatePart
    {
        /// <summary>Parameter index in order of arguments and <see cref="ITemplateBreakdown.Parameters"/>.</summary>
        int ParameterIndex { get; set; }
    }
    
    /// <summary>Alignment describes padding.</summary>
    public interface ITemplateAlignmentPart : ITemplatePart
    {
        /// <summary>Alignment, with positive value, padding goes on left side, with negative to right side.</summary>
        int Alignment { get; set; }
    }
    
    /// <summary>Formatting description, e.g. ",10:N0" or ":X8".</summary>
    public interface ITemplateFormattingPart : ITemplatePart { }
    
    /// <summary>Malformed part</summary>
    public interface ITemplateMalformedPart : ITemplatePart { }
    

    Full Example

    Full example
    using System.Globalization;
    using Avalanche.Template;
    using Avalanche.Utilities;
    using static System.Console;
    
    public class templatebreakdown
    {
        public static void Run()
        {
            {
                // <01>
                // Create format string
                string text = "Welcome, {user}";
                ReadOnlyMemory<char> mem = text.AsMemory();
                TemplateBreakdown breakdown = new TemplateBreakdown { Text = text };
                var textPart = new TemplateTextPart().SetBreakdown(breakdown).SetTexts(mem[..9], Escaper.Brace);
                var parameter = new TemplateParameterPart().SetBreakdown(breakdown).SetTexts(mem[10..14]);
                var placeholder = new TemplatePlaceholderPart().SetBreakdown(breakdown).SetTexts(mem[9..15]).SetParameter(parameter);
                breakdown.Parts = new ITemplatePart[] { textPart, placeholder };
                breakdown.SetReadOnly();
                // Print
                WriteLine(breakdown.Print(CultureInfo.InvariantCulture, new object?[] { "Donald Duck" }));
                // </01>
                WriteLine(textPart.Unescaped);
                WriteLine(TemplateFormat.BraceNumeric.Assemble[breakdown]);
            }
            {
                // <04>
                ITemplateBreakdown breakdown1 = TemplateFormat.BraceNumeric.Breakdown["Welcome, {0}"];
                ITemplateBreakdown breakdown2 = TemplateFormat.BraceAlphaNumeric.Breakdown["Welcome, {user}"];
                // </04>
            }
            {
                // <08>
                ITemplateBreakdown breakdown = TemplateFormat.BraceAlphaNumeric.Breakdown["Welcome, {user}"];
                WriteLine(breakdown.Print(CultureInfo.InvariantCulture, new object?[] { "Donald Duck" })); // "Welcome, Donald Duck"
                // </08>
            }
            {
                // <10>
                ITemplateBreakdown breakdown = TemplateFormat.BraceAlphaNumeric.Breakdown["Welcome, {}"];
                // Get malformed parts
                foreach ((int index, int length, ReadOnlyMemory<char>? malformed) in breakdown.MalformedParts())
                {
                    // Write
                    WriteLine($"Malformed '{malformed}' at: {index}:{index + length}"); // Malformed at: 9:11
                }
                // </10>
            }
            {
                // <13>
                // Create breakdown
                ITemplateBreakdown breakdown = TemplateFormat.Brace.Breakdown["Error code: {0} (0x{0:X4})."];
                // "Error code: 256 (0x0100)."
                WriteLine(breakdown.Print(null, new object?[] { 0x100 }));
    
                // Placeholder: "{0}", Placeholder: "{0:X8}"
                WriteLine(string.Join(", ", (IEnumerable<ITemplatePlaceholderPart>)breakdown.Placeholders));
                // Parameter: "0"
                WriteLine(string.Join(", ", (IEnumerable<ITemplateParameterPart>)breakdown.Parameters));
                // </13>
                WriteLine(TemplateFormat.BraceAlphaNumeric.Assemble[breakdown]);
                WriteLine(TemplateFormat.BraceNumeric.Assemble[breakdown]);
            }
            {
                // <14>
                // Create breakdown
                ITemplateBreakdown breakdown = TemplateFormat.Brace.Breakdown["Error code: {code} (0x{code:X4})."];
                // "Error code: 256 (0x0100)."
                WriteLine(breakdown.Print(null, new object?[] { 0x100 }));
    
                // Placeholder: "{code}", Placeholder: "{code:X4}"
                WriteLine(string.Join(", ", (IEnumerable<ITemplatePlaceholderPart>)breakdown.Placeholders));
                // Parameter: "code"
                WriteLine(string.Join(", ", (IEnumerable<ITemplateParameterPart>)breakdown.Parameters));
                // </14>
                WriteLine(TemplateFormat.BraceAlphaNumeric.Assemble[breakdown]);
                WriteLine(TemplateFormat.BraceNumeric.Assemble[breakdown]);
            }
        }
    }
    
    
    In This Article
    Back to top Copyright © Toni Kalajainen