VariableNames
Utilities for transforming variable names into a more readable form.
internal static class VariableNamesExamples
Section titled “Examples”The following example transforms variable names into a more readable form.
using Hertzole.SourceGen;
public static class Naming{ public static void Demonstrate() { // Returns "ValueChanged" ('m_' prefix removed and first letter uppercased). string nicified = VariableNames.NicifyVariableName("m_valueChanged");
// Returns "valueChanged" ('_' prefix removed). string withoutPrefix = VariableNames.RemovePrefix("_valueChanged");
// Returns "global::Value". string prefixed = VariableNames.AppendGlobalPrefix("Value");
// Returns true for "OnValueChanged". bool isEvent = VariableNames.StartsWithOn("OnValueChanged"); }}Methods
Section titled “Methods”AppendGlobalPrefix(string, ArrayBuilder<char>)
Section titled “AppendGlobalPrefix(string, ArrayBuilder<char>)”Appends global:: at the start of the provided string and appends it to the specified builder.
public static int AppendGlobalPrefix(string value, ArrayBuilder<char> builder);The value to append the prefix to.
builder
Section titled “builder”The builder to write the result to.
Remarks
Section titled “Remarks”If the provided value already starts with global:: this method does nothing.
Returns
Section titled “Returns”The number of characters written to the builder.
AppendGlobalPrefix(string)
Section titled “AppendGlobalPrefix(string)”Appends global:: at the start of the provided string.
public static string AppendGlobalPrefix(string value);The value to append the prefix to.
Remarks
Section titled “Remarks”If the provided value already starts with global:: this method does nothing.
Returns
Section titled “Returns”The value with the prefix appended.
AppendGlobalPrefix(ReadOnlySpan<char>, ArrayBuilder<char>)
Section titled “AppendGlobalPrefix(ReadOnlySpan<char>, ArrayBuilder<char>)”Appends global:: at the start of the provided value span and appends it to the specified builder.
public static int AppendGlobalPrefix(ReadOnlySpan<char> value, ArrayBuilder<char> builder);The value to append the prefix to.
builder
Section titled “builder”The builder to write the result to.
Remarks
Section titled “Remarks”If the provided value already starts with global:: this method does nothing.
Returns
Section titled “Returns”The number of characters written to the builder.
AppendGlobalPrefix(ReadOnlySpan<char>, Span<char>)
Section titled “AppendGlobalPrefix(ReadOnlySpan<char>, Span<char>)”Appends global:: at the start of the provided value span and copies the result into the provided destination span.
public static int AppendGlobalPrefix(ReadOnlySpan<char> value, Span<char> destination);The value to append the prefix to.
destination
Section titled “destination”The destination span to copy the result into.
Remarks
Section titled “Remarks”If the provided value already starts with global:: this method does nothing.
You can use GetNameWithGlobalPrefixLength to get the length of the resulting span.
Returns
Section titled “Returns”The number of characters written to the destination.
GetNameWithGlobalPrefixLength(ReadOnlySpan<char>)
Section titled “GetNameWithGlobalPrefixLength(ReadOnlySpan<char>)”Calculates the required length needed for a buffer to support appending global:: at the start of the provided string.
public static int GetNameWithGlobalPrefixLength(ReadOnlySpan<char> value);The string to get the length for.
Returns
Section titled “Returns”The length required for the buffer.
GetNameWithGlobalPrefixLength(string)
Section titled “GetNameWithGlobalPrefixLength(string)”Calculates the required length needed for a buffer to support appending global:: at the start of the provided string.
public static int GetNameWithGlobalPrefixLength(string value);The string to get the length for.
Returns
Section titled “Returns”The length required for the buffer.
GetNiceNameLength(ReadOnlySpan<char>)
Section titled “GetNiceNameLength(ReadOnlySpan<char>)”Calculates the required length needed for a buffer to support the new nice name.
public static int GetNiceNameLength(ReadOnlySpan<char> value);The string to get the length for.
Returns
Section titled “Returns”The length required for a nice name.
GetNiceNameLength(string)
Section titled “GetNiceNameLength(string)”Calculates the required length needed for a buffer to support the new nice name.
public static int GetNiceNameLength(string value);The string to get the length for.
Returns
Section titled “Returns”The length required for a nice name.
NicifyVariableName(ReadOnlySpan<char>, Span<char>)
Section titled “NicifyVariableName(ReadOnlySpan<char>, Span<char>)”Removes common prefixes (e.g. m_, _, k) and uppercases the first character.
public static int NicifyVariableName(ReadOnlySpan<char> value, Span<char> destination);The variable name to transform.
destination
Section titled “destination”The destination span to copy the result into.
Remarks
Section titled “Remarks”You can use GetNiceNameLength to get the length of the resulting span.
Returns
Section titled “Returns”The number of characters written to the destination.
NicifyVariableName(string, ArrayBuilder<char>)
Section titled “NicifyVariableName(string, ArrayBuilder<char>)”Removes common prefixes (e.g. m_, _, k) and uppercases the first character and appends it to the specified builder.
public static int NicifyVariableName(string value, ArrayBuilder<char> builder);The variable name to transform.
builder
Section titled “builder”The builder to write the result to.
Returns
Section titled “Returns”The number of characters written to the builder.
NicifyVariableName(string)
Section titled “NicifyVariableName(string)”Removes common prefixes (e.g. m_, _, k) and uppercases the first character.
public static string NicifyVariableName(string value);The variable name to transform.
Returns
Section titled “Returns”The nicified variable name.
NicifyVariableName(ReadOnlySpan<char>, ArrayBuilder<char>)
Section titled “NicifyVariableName(ReadOnlySpan<char>, ArrayBuilder<char>)”Removes common prefixes (e.g. m_, _, k) and uppercases the first character and appends it to the specified builder.
public static int NicifyVariableName(ReadOnlySpan<char> value, ArrayBuilder<char> builder);The variable name to transform.
builder
Section titled “builder”The builder to write the result to.
Returns
Section titled “Returns”The number of characters written to the builder.
RemovePrefix(string, ArrayBuilder<char>)
Section titled “RemovePrefix(string, ArrayBuilder<char>)”Removes common variable name prefixes such as m_, _, and k and writes the result to the specified builder.
public static int RemovePrefix(string value, ArrayBuilder<char> builder);The variable name to process.
builder
Section titled “builder”The builder to write the result to.
Returns
Section titled “Returns”The number of characters written to the builder.
RemovePrefix(string)
Section titled “RemovePrefix(string)”Removes common variable name prefixes such as m_, _, and k.
public static string RemovePrefix(string value);The variable name to process.
Returns
Section titled “Returns”The variable name with the prefix removed.
RemovePrefix(ReadOnlySpan<char>, ArrayBuilder<char>)
Section titled “RemovePrefix(ReadOnlySpan<char>, ArrayBuilder<char>)”Removes common variable name prefixes such as m_, _, and k and writes the result to the specified builder.
public static int RemovePrefix(ReadOnlySpan<char> value, ArrayBuilder<char> builder);The variable name to process.
builder
Section titled “builder”The builder to write the result to.
Returns
Section titled “Returns”The number of characters written to the builder.
RemovePrefix(ReadOnlySpan<char>, Span<char>)
Section titled “RemovePrefix(ReadOnlySpan<char>, Span<char>)”Removes common variable name prefixes such as m_, _, and k.
public static int RemovePrefix(ReadOnlySpan<char> value, Span<char> destination);The variable name to process.
destination
Section titled “destination”The destination span to copy the result into.
Returns
Section titled “Returns”The number of characters written to the destination.
StartsWithOn(ReadOnlySpan<char>)
Section titled “StartsWithOn(ReadOnlySpan<char>)”Determines whether the value starts with on or On followed by an uppercase character (e.g. OnValueChanged).
public static bool StartsWithOn(ReadOnlySpan<char> value);The value to check.
Returns
Section titled “Returns”true if the value starts with on or On followed by an uppercase character; otherwise false.
StartsWithOn(string?)
Section titled “StartsWithOn(string?)”Determines whether the value starts with on or On followed by an uppercase character (e.g. OnValueChanged).
public static bool StartsWithOn(string? value);The value to check.
Returns
Section titled “Returns”true if the value starts with on or On followed by an uppercase character; otherwise false.
UppercaseStart(ReadOnlySpan<char>, ArrayBuilder<char>)
Section titled “UppercaseStart(ReadOnlySpan<char>, ArrayBuilder<char>)”Uppercases the first character of the value and writes the result to the specified builder.
public static void UppercaseStart(ReadOnlySpan<char> value, ArrayBuilder<char> builder);The input string.
builder
Section titled “builder”The builder to write the result to.
UppercaseStart(string, ArrayBuilder<char>)
Section titled “UppercaseStart(string, ArrayBuilder<char>)”Uppercases the first character of the value and writes the result to the specified builder.
public static void UppercaseStart(string value, ArrayBuilder<char> builder);The input string.
builder
Section titled “builder”The builder to write the result to.
UppercaseStart(ReadOnlySpan<char>, Span<char>)
Section titled “UppercaseStart(ReadOnlySpan<char>, Span<char>)”Uppercases the first character of the value and writes the result to the destination.
public static void UppercaseStart(ReadOnlySpan<char> value, Span<char> destination);The input value.
destination
Section titled “destination”The destination span to copy the result into.
UppercaseStart(string)
Section titled “UppercaseStart(string)”Returns a new string with the first character uppercased.
public static string UppercaseStart(string value);The input string.
Returns
Section titled “Returns”A new string with the first character uppercased.