Skip to content

[StringExtensions] Add ReplaceEndings #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025
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
20 changes: 20 additions & 0 deletions src/redmine-net-api/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Security;
using System.Text.RegularExpressions;

namespace Redmine.Net.Api.Extensions
{
Expand Down Expand Up @@ -156,5 +157,24 @@ internal static string ToInvariantString<T>(this T value) where T : struct
_ => value.ToString(),
};
}

private const string CRLR = "\r\n";
private const string CR = "\r";
private const string LR = "\n";

internal static string ReplaceEndings(this string input, string replacement = CRLR)
{
if (input.IsNullOrWhiteSpace())
{
return input;
}

#if NET6_0_OR_GREATER
input = input.ReplaceLineEndings(CRLR);
#else
input = Regex.Replace(input, $"{CRLR}|{CR}|{LR}", CRLR);
#endif
return input;
}
}
}
2 changes: 2 additions & 0 deletions src/redmine-net-api/RedmineManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public void Update<T>(string id, T entity, string projectId = null, RequestOptio

var payload = Serializer.Serialize(entity);

payload = payload.ReplaceEndings();

ApiClient.Update(url, payload, requestOptions);
}

Expand Down
5 changes: 2 additions & 3 deletions src/redmine-net-api/RedmineManagerAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Redmine.Net.Api.Extensions;
Expand All @@ -31,7 +30,7 @@ namespace Redmine.Net.Api;

public partial class RedmineManager: IRedmineManagerAsync
{
private const string CRLR = "\r\n";


/// <inheritdoc />
public async Task<int> CountAsync<T>(RequestOptions requestOptions, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -208,7 +207,7 @@ public async Task UpdateAsync<T>(string id, T entity, RequestOptions requestOpti

var payload = Serializer.Serialize(entity);

payload = Regex.Replace(payload, "\r\n|\r|\n",CRLR);
payload = payload.ReplaceEndings();

await ApiClient.UpdateAsync(url, payload, requestOptions, cancellationToken: cancellationToken).ConfigureAwait(false);
}
Expand Down
Loading