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