Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active October 28, 2024 14:05
Show Gist options
  • Save karenpayneoregon/b455b4702d4e5fda6b0c26e6e35eb078 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/b455b4702d4e5fda6b0c26e6e35eb078 to your computer and use it in GitHub Desktop.
Experimenting with multiple versions of code

Many times a developer will write code that works but never considers alternatives. The code here was written as ReplaceDiacritics then used ChatGPT to write ReplaceDiacritics1 from the first method to exclude using a StringBuilder than used ChatGPT to rewrite ReplaceDiacritics1 using Span in ReplaceDiacritics2.

No matter if alternatives are done with A.I. or developer rewrites its never a bad idea to look at alternatives.

public class DiacriticsConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() ?? string.Empty;
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ReplaceDiacritics2());
}
}
using System.Globalization;
using System.Text;
namespace yournamespace.Extensions;
public static class StringExtensions
{
/// <summary>
/// Replaces diacritic characters in the specified text with their non-diacritic equivalents.
/// </summary>
/// <param name="text">The input string containing diacritic characters.</param>
/// <returns>A new string with diacritic characters replaced by their non-diacritic equivalents.</returns>
public static string ReplaceDiacritics(this string text)
{
if (string.IsNullOrEmpty(text))
return text;
var normalizedString = text.Normalize(NormalizationForm.FormD);
var length = normalizedString.Length;
StringBuilder stringBuilder = new(length);
for (var index = 0; index < length; index++)
{
char c = normalizedString[index];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
stringBuilder.Append(c);
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
public static string ReplaceDiacritics1(this string text)
{
if (string.IsNullOrEmpty(text))
return text;
var normalizedString = text.Normalize(NormalizationForm.FormD);
string result = new(
normalizedString
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
.ToArray()
);
return result.Normalize(NormalizationForm.FormC);
}
public static string ReplaceDiacritics2(this string text)
{
if (string.IsNullOrEmpty(text))
return text;
ReadOnlySpan<char> normalizedSpan = text.Normalize(NormalizationForm.FormD).AsSpan();
var estimatedLength = normalizedSpan.Length;
char[] result = new char[estimatedLength];
var resultIndex = 0;
foreach (char c in normalizedSpan)
{
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
{
result[resultIndex++] = c;
}
}
return new string(result, 0, resultIndex).Normalize(NormalizationForm.FormC);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment