Skip to content

Commit a7efa94

Browse files
committed
Fixes bracket parsing logic for inclusivity
Corrects the logic for determining inclusive and exclusive date ranges based on brackets. Improves the bracket parsing logic to handle null or empty content and bracket characters, ensuring accurate date range calculations. Also fixes the IsValidBracketPair function to improve bracket validation.
1 parent c467a12 commit a7efa94

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/TwoPartFormatParser.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,29 @@ public TwoPartFormatParser(IEnumerable<IPartParser> parsers, bool includeDefault
2828

2929
public DateTimeRange Parse(string content, DateTimeOffset relativeBaseTime)
3030
{
31-
int index = 0;
32-
var begin = _beginRegex.Match(content, index);
31+
if (String.IsNullOrEmpty(content))
32+
return null;
33+
34+
var begin = _beginRegex.Match(content);
3335
if (!begin.Success)
3436
return null;
3537

36-
string openingBracket = begin.Groups[1].Value;
38+
string openingValue = begin.Groups[1].Value;
39+
char? openingBracket = openingValue.Length > 0 ? openingValue[0] : (char?)null;
3740

3841
// Scan backwards from end of string to find closing bracket character.
3942
// This is cheaper than a regex and lets us determine max inclusivity upfront.
40-
string closingBracket = "";
41-
for (int i = content.Length - 1; i >= 0; i--)
43+
char? closingBracket = null;
44+
for (int pos = content.Length - 1; pos >= 0; pos--)
4245
{
43-
char c = content[i];
44-
if (c == ']' || c == '}')
46+
char ch = content[pos];
47+
if (ch is ']' or '}')
4548
{
46-
closingBracket = c.ToString();
49+
closingBracket = ch;
4750
break;
4851
}
4952

50-
if (!Char.IsWhiteSpace(c))
53+
if (!Char.IsWhiteSpace(ch))
5154
break;
5255
}
5356

@@ -56,13 +59,13 @@ public DateTimeRange Parse(string content, DateTimeOffset relativeBaseTime)
5659

5760
// Inclusive min ([): round down (start of period) — ">= start"
5861
// Exclusive min ({): round up (end of period) — "> end"
59-
bool minInclusive = !String.Equals(openingBracket, "{");
62+
bool minInclusive = openingBracket != '{';
6063

6164
// Inclusive max (]): round up (end of period) — "<= end"
6265
// Exclusive max (}): round down (start of period) — "< start"
63-
bool maxInclusive = !String.Equals(closingBracket, "}");
66+
bool maxInclusive = closingBracket != '}';
6467

65-
index += begin.Length;
68+
int index = begin.Length;
6669
DateTimeOffset? start = null;
6770
foreach (var parser in Parsers)
6871
{
@@ -123,22 +126,16 @@ public DateTimeRange Parse(string content, DateTimeOffset relativeBaseTime)
123126
/// Validates that opening and closing brackets form a valid pair.
124127
/// Both Elasticsearch bracket types can be mixed: [ with ], [ with }, { with ], { with }.
125128
/// </summary>
126-
/// <param name="opening">The opening bracket character</param>
127-
/// <param name="closing">The closing bracket character</param>
128-
/// <returns>True if brackets are properly paired, false otherwise</returns>
129-
private static bool IsValidBracketPair(string opening, string closing)
129+
private static bool IsValidBracketPair(char? opening, char? closing)
130130
{
131-
// Both empty - valid (no brackets)
132-
if (String.IsNullOrEmpty(opening) && String.IsNullOrEmpty(closing))
131+
if (opening == null && closing == null)
133132
return true;
134133

135-
// One empty, one not - invalid (unbalanced)
136-
if (String.IsNullOrEmpty(opening) || String.IsNullOrEmpty(closing))
134+
if (opening == null || closing == null)
137135
return false;
138136

139-
// Any valid opening bracket ([, {) can pair with any valid closing bracket (], })
140-
bool validOpening = String.Equals(opening, "[") || String.Equals(opening, "{");
141-
bool validClosing = String.Equals(closing, "]") || String.Equals(closing, "}");
137+
bool validOpening = opening is '[' or '{';
138+
bool validClosing = closing is ']' or '}';
142139
return validOpening && validClosing;
143140
}
144141
}

0 commit comments

Comments
 (0)