From d92091ed482a2c6b3fadfec805558cfdfeb0834e Mon Sep 17 00:00:00 2001 From: zapadi Date: Wed, 29 Jan 2025 23:33:44 +0200 Subject: [PATCH] [StringExtensions] Add ReplaceEndings --- .../Extensions/StringExtensions.cs | 20 +++++++++++++++++++ src/redmine-net-api/RedmineManager.cs | 2 ++ src/redmine-net-api/RedmineManagerAsync.cs | 5 ++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/redmine-net-api/Extensions/StringExtensions.cs b/src/redmine-net-api/Extensions/StringExtensions.cs index c02836e7..607c8547 100644 --- a/src/redmine-net-api/Extensions/StringExtensions.cs +++ b/src/redmine-net-api/Extensions/StringExtensions.cs @@ -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 { @@ -156,5 +157,24 @@ internal static string ToInvariantString(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; + } } } \ No newline at end of file diff --git a/src/redmine-net-api/RedmineManager.cs b/src/redmine-net-api/RedmineManager.cs index 49cf2ee0..0043919e 100644 --- a/src/redmine-net-api/RedmineManager.cs +++ b/src/redmine-net-api/RedmineManager.cs @@ -175,6 +175,8 @@ public void Update(string id, T entity, string projectId = null, RequestOptio var payload = Serializer.Serialize(entity); + payload = payload.ReplaceEndings(); + ApiClient.Update(url, payload, requestOptions); } diff --git a/src/redmine-net-api/RedmineManagerAsync.cs b/src/redmine-net-api/RedmineManagerAsync.cs index 4039f028..fb1bba7d 100644 --- a/src/redmine-net-api/RedmineManagerAsync.cs +++ b/src/redmine-net-api/RedmineManagerAsync.cs @@ -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; @@ -31,7 +30,7 @@ namespace Redmine.Net.Api; public partial class RedmineManager: IRedmineManagerAsync { - private const string CRLR = "\r\n"; + /// public async Task CountAsync(RequestOptions requestOptions, CancellationToken cancellationToken = default) @@ -208,7 +207,7 @@ public async Task UpdateAsync(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); }