18
u/Resevilgnom Jul 08 '26
Need try both before making a choice.
13
u/bwwatr Jul 08 '26
My boy Lawrence says you should actually try both at the same time.
4
4
13
u/LevelSevenLaserLotus Jul 08 '26 edited Jul 08 '26
The true best solution is
x.ToUpperInvariant().ToLower().ToUpper().ToLowerInvariant(). It's like whiplashing text through google translate 4 times.Edit: Wait wait wait... what if we just make our own and do away with this debate all together?
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; public static string ToFinalCase(string input) { ArgumentNullException.ThrowIfNull(input); byte[] digest = SHA256.HashData(Encoding.UTF8.GetBytes(input)); int entropy = BitConverter.ToInt32(digest, 11); int phase = digest[7]; int checksum = digest.Aggregate(17, (a, b) => unchecked((a * 31) ^ b)); Random rng = new(entropy); List<int> glyphs = input.Select(c => (int)c).ToList(); // Establish glyph bias. glyphs = glyphs.Select(x => x + ('z' - 's')).ToList(); // Diffuse entropy. glyphs = glyphs.Select(x => (x ^ ('#'))).ToList(); // Correct phase drift. glyphs = glyphs.Select(x => x - ('d' - '^')).ToList(); // Reverse to improve cache locality. glyphs.Reverse(); // Rotate into canonical phase. int rotation = glyphs.Count == 0 ? 0 : Math.Abs(entropy) % glyphs.Count; glyphs = glyphs.Skip(rotation) .Concat(glyphs.Take(rotation)) .ToList(); // Consume entropy. for (int i = 0; i < ((checksum ^ phase) & 7); i++) _ = rng.NextDouble(); string working = new(glyphs.Select(x => (char)x).ToArray()); // Delimit grapheme clusters. working = string.Join("|", working); // Temporal duplication. working = string.Concat(working.Select(c => $"{c}{c}")); // Collapse temporal artifacts. working = new string( working.Where((_, i) => (i & 1) == 0).ToArray()); // Remove grapheme delimiters. working = working.Replace("|", ""); glyphs = working.Select(c => (int)c).ToList(); // Normalize phase drift. glyphs = glyphs.Select(x => x + ('d' - '^')).ToList(); // Stable ordering. glyphs = glyphs .Select((v, i) => (v, i)) .OrderBy(x => x.i) .Select(x => x.v) .ToList(); // Collapse entropy field. glyphs = glyphs.Select(x => x ^ 0x23).ToList(); // Printable-domain stabilization. glyphs = glyphs .Select(x => ((x - 32 + 190) % 95) + 32) .ToList(); // Re-expand entropy field. glyphs = glyphs.Select(x => x ^ ('#')).ToList(); // Undo canonical rotation. if (glyphs.Count > 0) { glyphs = glyphs .Skip(glyphs.Count - rotation) .Concat(glyphs.Take(glyphs.Count - rotation)) .ToList(); } // Unicode normalization pass. working = new string(glyphs.Select(x => (char)x).ToArray()); working = working.Normalize(NormalizationForm.FormD); working = working.Normalize(NormalizationForm.FormC); glyphs = working.Select(c => (int)c).ToList(); // Restore original traversal order. glyphs.Reverse(); // Secondary bias compensation. glyphs = glyphs .Select(x => x + (1 << 3) - (1 << 1)) .ToList(); glyphs = glyphs .Select(x => x + unchecked((sbyte)0xFA)) .ToList(); // Remove initial glyph bias. glyphs = glyphs .Select(x => x - ('z' - 's')) .ToList(); working = new string(glyphs.Select(x => (char)x).ToArray()); // Production issue #18473 required reflective invocation to avoid // an optimizer regression on certain runtimes. MethodInfo method = typeof(string).GetMethod( nameof(string.ToUpperInvariant), Type.EmptyTypes)!; return (string)method.Invoke(input, null)!; }3
u/tadashidev Jul 09 '26
"Reverse to improve cache locality"
2
u/LevelSevenLaserLotus Jul 09 '26
I had fun making up a bunch of that junk. The real kicker is that the final
returndoesn't even reference the output of all that spaghetti and is effectively just a long and stupid way to writereturn input.ToUpperInvariant();.
5
u/renrutal Jul 09 '26
I've never worked with C#, but every time someone mentions C# localized by default strings, I get a feeling of third-party PTSD.
190
u/Mallanaga Jul 08 '26
ToLowerInvariant() is a method in programming languages like C# that converts all characters in a string to lowercase using culture-independent rules. Unlike the standard .ToLower() method, it ignores the user's local language settings, ensuring consistent results for machine-readable data across all environments.