| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json |
| | | 10 | | { |
| | | 11 | | internal abstract class JsonSeparatorNamingPolicy : JsonNamingPolicy |
| | | 12 | | { |
| | | 13 | | private readonly char? _separator; |
| | | 14 | | private readonly WordCasing _wordCasing; |
| | | 15 | | |
| | 0 | 16 | | internal JsonSeparatorNamingPolicy(bool lowercase, char separator) |
| | 0 | 17 | | { |
| | 0 | 18 | | Debug.Assert(char.IsPunctuation(separator)); |
| | | 19 | | |
| | 0 | 20 | | _separator = separator; |
| | 0 | 21 | | _wordCasing = lowercase ? WordCasing.LowerCase : WordCasing.UpperCase; |
| | 0 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | internal JsonSeparatorNamingPolicy(WordCasing wordCasing) |
| | 0 | 25 | | { |
| | 0 | 26 | | Debug.Assert(wordCasing is WordCasing.PascalCase); |
| | | 27 | | |
| | 0 | 28 | | _separator = null; |
| | 0 | 29 | | _wordCasing = wordCasing; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public sealed override string ConvertName(string name) |
| | 0 | 33 | | { |
| | 0 | 34 | | ArgumentNullException.ThrowIfNull(name); |
| | | 35 | | |
| | 0 | 36 | | return ConvertNameCore(_separator, _wordCasing, name.AsSpan()); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static string ConvertNameCore(char? separator, WordCasing wordCasing, ReadOnlySpan<char> chars) |
| | 0 | 40 | | { |
| | 0 | 41 | | char[]? rentedBuffer = null; |
| | | 42 | | |
| | | 43 | | // While we can't predict the expansion factor of the resultant string, |
| | | 44 | | // start with a buffer that is at least 20% larger than the input. |
| | 0 | 45 | | int initialBufferLength = (int)(1.2 * chars.Length); |
| | 0 | 46 | | Span<char> destination = initialBufferLength <= JsonConstants.StackallocCharThreshold |
| | 0 | 47 | | ? stackalloc char[JsonConstants.StackallocCharThreshold] |
| | 0 | 48 | | : (rentedBuffer = ArrayPool<char>.Shared.Rent(initialBufferLength)); |
| | | 49 | | |
| | 0 | 50 | | SeparatorState state = SeparatorState.NotStarted; |
| | 0 | 51 | | int charsWritten = 0; |
| | | 52 | | |
| | 0 | 53 | | for (int i = 0; i < chars.Length; i++) |
| | 0 | 54 | | { |
| | | 55 | | // NB this implementation does not handle surrogate pair letters |
| | | 56 | | // cf. https://github.com/dotnet/runtime/issues/90352 |
| | | 57 | | |
| | 0 | 58 | | char current = chars[i]; |
| | 0 | 59 | | UnicodeCategory category = char.GetUnicodeCategory(current); |
| | | 60 | | |
| | 0 | 61 | | switch (category) |
| | | 62 | | { |
| | | 63 | | case UnicodeCategory.UppercaseLetter: |
| | | 64 | | |
| | 0 | 65 | | bool isWordBoundary = false; |
| | | 66 | | |
| | 0 | 67 | | switch (state) |
| | | 68 | | { |
| | | 69 | | case SeparatorState.NotStarted: |
| | 0 | 70 | | isWordBoundary = true; |
| | 0 | 71 | | break; |
| | | 72 | | |
| | | 73 | | case SeparatorState.LowercaseLetterOrDigit: |
| | | 74 | | case SeparatorState.SpaceSeparator: |
| | | 75 | | // An uppercase letter following a sequence of lowercase letters or spaces |
| | | 76 | | // denotes the start of a new grouping: emit a separator character. |
| | 0 | 77 | | isWordBoundary = true; |
| | 0 | 78 | | if (separator.HasValue) |
| | 0 | 79 | | { |
| | 0 | 80 | | WriteChar(separator.Value, ref destination); |
| | 0 | 81 | | } |
| | 0 | 82 | | break; |
| | | 83 | | |
| | | 84 | | case SeparatorState.UppercaseLetter: |
| | | 85 | | // We are reading through a sequence of two or more uppercase letters. |
| | | 86 | | // Uppercase letters are grouped together with the exception of the |
| | | 87 | | // final letter, assuming it is followed by lowercase letters. |
| | | 88 | | // For example, the value 'XMLReader' should render as 'xml_reader', |
| | | 89 | | // however 'SHA512Hash' should render as 'sha512-hash'. |
| | 0 | 90 | | if (i + 1 < chars.Length && char.IsLower(chars[i + 1])) |
| | 0 | 91 | | { |
| | 0 | 92 | | isWordBoundary = true; |
| | 0 | 93 | | if (separator.HasValue) |
| | 0 | 94 | | { |
| | 0 | 95 | | WriteChar(separator.Value, ref destination); |
| | 0 | 96 | | } |
| | 0 | 97 | | } |
| | 0 | 98 | | break; |
| | | 99 | | |
| | | 100 | | default: |
| | 0 | 101 | | Debug.Fail($"Unexpected state {state}"); |
| | | 102 | | break; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | current = wordCasing switch |
| | 0 | 106 | | { |
| | 0 | 107 | | WordCasing.LowerCase => char.ToLowerInvariant(current), |
| | 0 | 108 | | WordCasing.PascalCase => isWordBoundary ? current : char.ToLowerInvariant(current), |
| | 0 | 109 | | _ => current, |
| | 0 | 110 | | }; |
| | | 111 | | |
| | 0 | 112 | | WriteChar(current, ref destination); |
| | 0 | 113 | | state = SeparatorState.UppercaseLetter; |
| | 0 | 114 | | break; |
| | | 115 | | |
| | | 116 | | case UnicodeCategory.LowercaseLetter: |
| | | 117 | | case UnicodeCategory.DecimalDigitNumber: |
| | | 118 | | |
| | 0 | 119 | | bool isWordStart = state is SeparatorState.SpaceSeparator or SeparatorState.NotStarted; |
| | | 120 | | |
| | 0 | 121 | | if (state is SeparatorState.SpaceSeparator) |
| | 0 | 122 | | { |
| | | 123 | | // Normalize preceding spaces to one separator. |
| | 0 | 124 | | if (separator.HasValue) |
| | 0 | 125 | | { |
| | 0 | 126 | | WriteChar(separator.Value, ref destination); |
| | 0 | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | if (category is UnicodeCategory.LowercaseLetter) |
| | 0 | 131 | | { |
| | 0 | 132 | | current = wordCasing switch |
| | 0 | 133 | | { |
| | 0 | 134 | | WordCasing.UpperCase => char.ToUpperInvariant(current), |
| | 0 | 135 | | WordCasing.PascalCase => isWordStart ? char.ToUpperInvariant(current) : current, |
| | 0 | 136 | | _ => current, |
| | 0 | 137 | | }; |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | WriteChar(current, ref destination); |
| | 0 | 141 | | state = SeparatorState.LowercaseLetterOrDigit; |
| | 0 | 142 | | break; |
| | | 143 | | |
| | | 144 | | case UnicodeCategory.SpaceSeparator: |
| | | 145 | | // Space characters are trimmed from the start and end of the input string |
| | | 146 | | // but are normalized to separator characters if between letters. |
| | 0 | 147 | | if (state != SeparatorState.NotStarted) |
| | 0 | 148 | | { |
| | 0 | 149 | | state = SeparatorState.SpaceSeparator; |
| | 0 | 150 | | } |
| | 0 | 151 | | break; |
| | | 152 | | |
| | | 153 | | default: |
| | | 154 | | // Non-alphanumeric characters (including the separator character and surrogates) |
| | | 155 | | // are written as-is to the output and reset the separator state. |
| | | 156 | | // E.g. 'ABC???def' maps to 'abc???def' in snake_case. |
| | | 157 | | |
| | 0 | 158 | | WriteChar(current, ref destination); |
| | 0 | 159 | | state = SeparatorState.NotStarted; |
| | 0 | 160 | | break; |
| | | 161 | | } |
| | 0 | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | string result = destination.Slice(0, charsWritten).ToString(); |
| | | 165 | | |
| | 0 | 166 | | if (rentedBuffer is not null) |
| | 0 | 167 | | { |
| | 0 | 168 | | destination.Slice(0, charsWritten).Clear(); |
| | 0 | 169 | | ArrayPool<char>.Shared.Return(rentedBuffer); |
| | 0 | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | return result; |
| | | 173 | | |
| | | 174 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 175 | | void WriteChar(char value, ref Span<char> destination) |
| | 0 | 176 | | { |
| | 0 | 177 | | if (charsWritten == destination.Length) |
| | 0 | 178 | | { |
| | 0 | 179 | | ExpandBuffer(ref destination); |
| | 0 | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | destination[charsWritten++] = value; |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | void ExpandBuffer(ref Span<char> destination) |
| | 0 | 186 | | { |
| | 0 | 187 | | int newSize = checked(destination.Length * 2); |
| | 0 | 188 | | char[] newBuffer = ArrayPool<char>.Shared.Rent(newSize); |
| | 0 | 189 | | destination.CopyTo(newBuffer); |
| | | 190 | | |
| | 0 | 191 | | if (rentedBuffer is not null) |
| | 0 | 192 | | { |
| | 0 | 193 | | destination.Slice(0, charsWritten).Clear(); |
| | 0 | 194 | | ArrayPool<char>.Shared.Return(rentedBuffer); |
| | 0 | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | rentedBuffer = newBuffer; |
| | 0 | 198 | | destination = rentedBuffer; |
| | 0 | 199 | | } |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | private enum SeparatorState |
| | | 203 | | { |
| | | 204 | | NotStarted, |
| | | 205 | | UppercaseLetter, |
| | | 206 | | LowercaseLetterOrDigit, |
| | | 207 | | SpaceSeparator, |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | internal enum WordCasing |
| | | 211 | | { |
| | | 212 | | LowerCase, |
| | | 213 | | UpperCase, |
| | | 214 | | PascalCase, |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | |