| | | 1 | | using System; |
| | | 2 | | using System.Buffers; |
| | | 3 | | |
| | | 4 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 5 | | |
| | | 6 | | internal static class Naming |
| | | 7 | | { |
| | | 8 | | public static ReadOnlySpan<char> FormatVariableName(ReadOnlySpan<char> value) |
| | | 9 | | { |
| | 351 | 10 | | value = RemovePrefix(value); |
| | 351 | 11 | | value = UppercaseStart(value); |
| | | 12 | | |
| | 351 | 13 | | return value; |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | private static ReadOnlySpan<char> RemovePrefix(ReadOnlySpan<char> value) |
| | | 17 | | { |
| | 351 | 18 | | if (value.Length > 2 && value[1] == '_') |
| | | 19 | | { |
| | 8 | 20 | | return value.Slice(2); |
| | | 21 | | } |
| | | 22 | | |
| | 343 | 23 | | if (value.Length > 1 && value[0] == '_') |
| | | 24 | | { |
| | 1 | 25 | | return value.Slice(1); |
| | | 26 | | } |
| | | 27 | | |
| | 342 | 28 | | return value; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | private static ReadOnlySpan<char> UppercaseStart(ReadOnlySpan<char> value) |
| | | 32 | | { |
| | 351 | 33 | | if (value.Length == 0) |
| | | 34 | | { |
| | 1 | 35 | | return value; |
| | | 36 | | } |
| | | 37 | | |
| | 350 | 38 | | if (value[0] == char.ToUpperInvariant(value[0])) |
| | | 39 | | { |
| | 308 | 40 | | return value; |
| | | 41 | | } |
| | | 42 | | |
| | 42 | 43 | | char[] newValue = ArrayPool<char>.Shared.Rent(value.Length); |
| | | 44 | | try |
| | | 45 | | { |
| | 42 | 46 | | value.CopyTo(newValue); |
| | | 47 | | |
| | 42 | 48 | | newValue[0] = char.ToUpperInvariant(value[0]); |
| | | 49 | | |
| | 42 | 50 | | return new ReadOnlySpan<char>(newValue, 0, value.Length); |
| | | 51 | | } |
| | | 52 | | finally |
| | | 53 | | { |
| | 42 | 54 | | ArrayPool<char>.Shared.Return(newValue); |
| | 42 | 55 | | } |
| | 42 | 56 | | } |
| | | 57 | | |
| | | 58 | | public static string CreateCallbackName(string name, in ScriptableType scriptableType, in CallbackFlags flags) |
| | | 59 | | { |
| | 345 | 60 | | using ArrayBuilder<char> builder = new ArrayBuilder<char>(32); |
| | | 61 | | |
| | 345 | 62 | | ReadOnlySpan<char> prettyName = FormatVariableName(name.AsSpan()); |
| | | 63 | | |
| | 345 | 64 | | if (StartsWithOn(prettyName)) |
| | | 65 | | { |
| | | 66 | | // If it starts with "on", just uppercase the O and keep the rest. |
| | 6 | 67 | | builder.Add('O'); |
| | 6 | 68 | | builder.AddRange(prettyName.Slice(1)); |
| | | 69 | | } |
| | | 70 | | else |
| | | 71 | | { |
| | 339 | 72 | | builder.AddRange("On"); |
| | 339 | 73 | | builder.AddRange(prettyName); |
| | | 74 | | } |
| | | 75 | | |
| | 345 | 76 | | switch (scriptableType) |
| | | 77 | | { |
| | | 78 | | case ScriptableType.Value: |
| | 33 | 79 | | AppendFlagSuffix(in builder, in scriptableType, in flags); |
| | 33 | 80 | | break; |
| | | 81 | | case ScriptableType.Event: |
| | | 82 | | case ScriptableType.GenericEvent: |
| | 303 | 83 | | builder.AddRange("Invoked"); |
| | 303 | 84 | | break; |
| | | 85 | | case ScriptableType.Pool: |
| | | 86 | | case ScriptableType.List: |
| | | 87 | | case ScriptableType.Dictionary: |
| | 8 | 88 | | builder.AddRange("Changed"); |
| | 8 | 89 | | break; |
| | | 90 | | default: |
| | 1 | 91 | | throw new ArgumentOutOfRangeException(); |
| | | 92 | | } |
| | | 93 | | |
| | 344 | 94 | | return builder.ToString(); |
| | 344 | 95 | | } |
| | | 96 | | |
| | | 97 | | public static void AppendFlagSuffix(in ArrayBuilder<char> builder, in ScriptableType type, in CallbackFlags flags) |
| | | 98 | | { |
| | 453 | 99 | | if (type == ScriptableType.Value) |
| | | 100 | | { |
| | 158 | 101 | | if ((flags & CallbackFlags.PostInvoke) != 0) |
| | | 102 | | { |
| | 106 | 103 | | builder.AddRange("Changed"); |
| | | 104 | | } |
| | 52 | 105 | | else if ((flags & CallbackFlags.PreInvoke) != 0) |
| | | 106 | | { |
| | 52 | 107 | | builder.AddRange("Changing"); |
| | | 108 | | } |
| | | 109 | | } |
| | 347 | 110 | | } |
| | | 111 | | |
| | | 112 | | private static bool StartsWithOn(ReadOnlySpan<char> value) |
| | | 113 | | { |
| | | 114 | | // Check if it starts with "on" or "On" and the third character is uppercase. |
| | | 115 | | // Checking the third character ensures we don't match words like "only" or "once". |
| | 345 | 116 | | return value.Length >= 3 && (value[0] == 'o' || value[0] == 'O') && (value[1] == 'n' || value[1] == 'N') && char |
| | | 117 | | } |
| | | 118 | | } |