Skip to content

Commit b59363b

Browse files
committed
Fix issue where urls were parsed as offsets
Closes #45
1 parent 6ac96e5 commit b59363b

File tree

3 files changed

+14
-102
lines changed

3 files changed

+14
-102
lines changed

src/Jeffijoe.MessageFormat.Tests/MessageFormatterCachingTests.cs

+2-97
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,17 @@
44
// Author: Jeff Hansen <[email protected]>
55
// Copyright (C) Jeff Hansen 2015. All rights reserved.
66

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Text;
10-
117
using Jeffijoe.MessageFormat.Formatting;
12-
using Jeffijoe.MessageFormat.Helpers;
138
using Jeffijoe.MessageFormat.Tests.TestHelpers;
14-
159
using Xunit;
16-
using Xunit.Abstractions;
1710

1811
namespace Jeffijoe.MessageFormat.Tests
1912
{
2013
/// <summary>
2114
/// The message formatter_caching_tests.
2215
/// </summary>
23-
public class MessageFormatterCachingTests
16+
public class MessageFormatterCachingTests()
2417
{
25-
#region Fields
26-
27-
/// <summary>
28-
/// The output helper.
29-
/// </summary>
30-
private readonly ITestOutputHelper outputHelper;
31-
32-
#endregion
33-
34-
#region Constructors and Destructors
35-
36-
/// <summary>
37-
/// Initializes a new instance of the <see cref="MessageFormatterCachingTests"/> class.
38-
/// </summary>
39-
/// <param name="outputHelper">
40-
/// The output helper.
41-
/// </param>
42-
public MessageFormatterCachingTests(ITestOutputHelper outputHelper)
43-
{
44-
this.outputHelper = outputHelper;
45-
}
46-
47-
#endregion
48-
4918
#region Public Methods and Operators
5019

5120
/// <summary>
@@ -75,71 +44,7 @@ public void FormatMessage_caches_reused_pattern()
7544
Assert.Equal("Hi Ma'am!", actual);
7645
Assert.Equal(3, parser.ParseCount);
7746
}
78-
79-
/// <summary>
80-
/// The format message_with_cache_benchmark.
81-
/// </summary>
82-
[Fact]
83-
public void FormatMessage_with_cache_benchmark()
84-
{
85-
var subject = new MessageFormatter(useCache: true);
86-
this.Benchmark(subject);
87-
}
88-
89-
/// <summary>
90-
/// The format message_without_cache_benchmark.
91-
/// </summary>
92-
[Fact]
93-
public void FormatMessage_without_cache_benchmark()
94-
{
95-
var subject = new MessageFormatter(false);
96-
this.Benchmark(subject);
97-
}
98-
99-
#endregion
100-
101-
#region Methods
102-
103-
/// <summary>
104-
/// The benchmark.
105-
/// </summary>
106-
/// <param name="subject">
107-
/// The subject.
108-
/// </param>
109-
private void Benchmark(MessageFormatter subject)
110-
{
111-
var pattern = "\r\n----\r\nOh {name}? And if we were " + "to surround {gender, select, " + "male {his} "
112-
+ "female {her}" + "} name with '{' and '}', it would look "
113-
+ "like '{'{name}'}'? Yeah, I know {gender, select, " + "male {him} " + "female {her}"
114-
+ "}. {gender, select, " + "male {He's}" + "female {She's}" + "} got {messageCount, plural, "
115-
+ "zero {no messages}" + "one {just one message}" + "=42 {a universal amount of messages}"
116-
+ "other {uuhm... let's see.. Oh yeah, # messages - and here's a pound: '#'}" + "}!";
117-
int iterations = 100000;
118-
var args = new Dictionary<string, object?>[iterations];
119-
var rnd = new Random();
120-
for (int i = 0; i < iterations; i++)
121-
{
122-
var val = rnd.Next(50);
123-
args[i] =
124-
new
125-
{
126-
gender = val % 2 == 0 ? "male" : "female",
127-
name = val % 2 == 0 ? "Jeff" : "Marcela",
128-
messageCount = val
129-
}.ToDictionary();
130-
}
131-
132-
TestHelpers.Benchmark.Start("Formatting message " + iterations + " times, no warm-up.", this.outputHelper);
133-
var output = new StringBuilder();
134-
for (int i = 0; i < iterations; i++)
135-
{
136-
output.AppendLine(subject.FormatMessage(pattern, args[i]));
137-
}
138-
139-
TestHelpers.Benchmark.End(this.outputHelper);
140-
this.outputHelper.WriteLine(output.ToString());
141-
}
142-
47+
14348
#endregion
14449
}
14550
}

src/Jeffijoe.MessageFormat/Formatting/BaseFormatter.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,22 @@ protected internal IEnumerable<FormatterExtension> ParseExtensions(FormatterRequ
5757
return Enumerable.Empty<FormatterExtension>();
5858
}
5959

60-
int length = request.FormatterArguments.Length;
60+
var length = request.FormatterArguments.Length;
6161
index = 0;
6262
const char Colon = ':';
63-
bool foundExtension = false;
63+
const char OpenBrace = '{';
64+
var foundExtension = false;
6465

6566
var extension = StringBuilderPool.Get();
6667
var value = StringBuilderPool.Get();
6768
try
6869
{
69-
for (int i = 0; i < length; i++)
70+
for (var i = 0; i < length; i++)
7071
{
7172
var c = request.FormatterArguments[i];
7273

7374
// Whitespace is tolerated at the beginning.
74-
bool isWhiteSpace = char.IsWhiteSpace(c);
75+
var isWhiteSpace = char.IsWhiteSpace(c);
7576
if (isWhiteSpace)
7677
{
7778
// We've reached the end
@@ -106,6 +107,12 @@ protected internal IEnumerable<FormatterExtension> ParseExtensions(FormatterRequ
106107
continue;
107108
}
108109

110+
if (c == OpenBrace)
111+
{
112+
// It's not an extension.
113+
break;
114+
}
115+
109116
extension.Append(c);
110117
}
111118

src/Jeffijoe.MessageFormat/Formatting/Formatters/SelectFormatter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public string Format(string locale,
6565
object? value,
6666
IMessageFormatter messageFormatter)
6767
{
68+
var str = Convert.ToString(value);
6869
var parsed = this.ParseArguments(request);
6970
KeyedBlock? other = null;
7071
foreach (var keyedBlock in parsed.KeyedBlocks)
7172
{
72-
var str = Convert.ToString(value);
7373
if (str == keyedBlock.Key)
7474
{
7575
return messageFormatter.FormatMessage(keyedBlock.BlockText, args);

0 commit comments

Comments
 (0)