Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion Source/aweXpect.Core/Core/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,35 @@ private static bool IsNormalized(string value)
return null;
}

return string.Join("\n", value.RemoveNewlineStyle().Split('\n').Select(line => line.TrimStart()));
string normalized = value.RemoveNewlineStyle();
if (!HasIndentedLine(normalized))
{
return normalized;
}

return string.Join("\n", normalized.Split('\n').Select(line => line.TrimStart()));
}

private static bool HasIndentedLine(string value)
{
int lineStart = 0;
while (lineStart < value.Length)
{
if (char.IsWhiteSpace(value[lineStart]))
{
return true;
}

int newline = value.IndexOf('\n', lineStart);
if (newline < 0)
{
return false;
}

lineStart = newline + 1;
}

return false;
}

[return: NotNullIfNotNull(nameof(value))]
Expand All @@ -136,6 +164,11 @@ private static bool IsNormalized(string value)
return null;
}

if (value.IndexOf('\r') < 0)
{
return value;
}

return value.Replace("\r\n", "\n", StringComparison.Ordinal)
.Replace("\r", "\n", StringComparison.Ordinal);
}
Expand Down
12 changes: 9 additions & 3 deletions Source/aweXpect.Core/Core/Polyfills/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ internal static string Replace(this string @this,
return @this.Replace(oldValue, newValue);
}

int index = @this.IndexOf(oldValue, 0, comparisonType);
if (index < 0)
{
// Like the overload with a comparison type on newer frameworks, which returns the instance itself.
return @this;
}

StringBuilder sb = new();
int start = 0;
int index;
while ((index = @this.IndexOf(oldValue, start, comparisonType)) >= 0)
do
{
sb.Append(@this, start, index - start).Append(newValue);
start = index + oldValue.Length;
}
} while ((index = @this.IndexOf(oldValue, start, comparisonType)) >= 0);

return sb.Append(@this, start, @this.Length - start).ToString();
}
Expand Down
Loading