Beispiel #1
0
        private void Parse()
        {
            int           idx  = 0;
            StringBuilder text = new StringBuilder();

            while (idx < _messageFormat.Length)
            {
                char c = _messageFormat[idx];

                if (c == '{')
                {
                    if (PeekChar(idx) != '{')
                    {
                        // a hole starts -> save text before and parse hole
                        if (text.Length > 0)
                        {
                            _token.Add(new MessageTextToken(text.ToString()));
                            text.Clear();
                        }

                        MessageHoleToken hole = ParseHole(ref idx);
                        _token.Add(hole);
                    }
                    else
                    {
                        // escaped {
                        text.Append(c);
                        idx += 2;
                    }
                }
                else if (c == '}' && PeekChar(idx) == '}')
                {
                    // escaped }
                    text.Append(c);
                    idx += 2;
                }
                else
                {
                    text.Append(c);
                    idx++;
                }
            }

            if (text.Length > 0)
            {
                _token.Add(new MessageTextToken(text.ToString()));
            }
        }
Beispiel #2
0
        private string FormatPositional(List <object> values)
        {
            var format = new StringBuilder(_messageFormat.Length + 128);

            foreach (var token in _token)
            {
                var text = token switch
                {
                    MessageTextToken textToken => textToken.Text,
                    MessageHoleToken holeToken => FormatHole(holeToken, holeToken.Index.Filter(x => values.Count > x).Match(x => values[x], () => "?")),
                    _ => throw new NotImplementedException(),
                };

                format.Append(text);
            }

            return(format.ToString());
        }
Beispiel #3
0
        private string FormatNamed(IEnumerable <object> values)
        {
            var format = new StringBuilder(_messageFormat.Length + 128);

            IEnumerator <object> valuesEnumerator = values.GetEnumerator();

            foreach (var token in _token)
            {
                var text = token switch
                {
                    MessageTextToken textToken => textToken.Text,
                    MessageHoleToken holeToken => FormatHole(holeToken, valuesEnumerator.MoveNext() ? valuesEnumerator.Current : "?"),
                    _ => throw new NotImplementedException(),
                };

                format.Append(text);
            }

            return(format.ToString());
        }
Beispiel #4
0
 private string FormatHole(MessageHoleToken holeToken, object value)
 {
     return(string.Format($"{{0,{holeToken.Alignment}:{holeToken.Format}}}", value));
 }