Skip to content

VariableNames

Utilities for transforming variable names into a more readable form.

C# Signature
internal static class VariableNames

The following example transforms variable names into a more readable form.

C#
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");
}
}

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.

C# Signature
public static int AppendGlobalPrefix(string value, ArrayBuilder<char> builder);

The value to append the prefix to.

The builder to write the result to.

If the provided value already starts with global:: this method does nothing.

The number of characters written to the builder.

Appends global:: at the start of the provided string.

C# Signature
public static string AppendGlobalPrefix(string value);

The value to append the prefix to.

If the provided value already starts with global:: this method does nothing.

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.

C# Signature
public static int AppendGlobalPrefix(ReadOnlySpan<char> value, ArrayBuilder<char> builder);

The value to append the prefix to.

The builder to write the result to.

If the provided value already starts with global:: this method does nothing.

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.

C# Signature
public static int AppendGlobalPrefix(ReadOnlySpan<char> value, Span<char> destination);

The value to append the prefix to.

The destination span to copy the result into.

If the provided value already starts with global:: this method does nothing.

You can use GetNameWithGlobalPrefixLength to get the length of the resulting span.

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.

C# Signature
public static int GetNameWithGlobalPrefixLength(ReadOnlySpan<char> value);

The string to get the length for.

The length required for the buffer.

Calculates the required length needed for a buffer to support appending global:: at the start of the provided string.

C# Signature
public static int GetNameWithGlobalPrefixLength(string value);

The string to get the length for.

The length required for the buffer.

Calculates the required length needed for a buffer to support the new nice name.

C# Signature
public static int GetNiceNameLength(ReadOnlySpan<char> value);

The string to get the length for.

The length required for a nice name.

Calculates the required length needed for a buffer to support the new nice name.

C# Signature
public static int GetNiceNameLength(string value);

The string to get the length for.

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.

C# Signature
public static int NicifyVariableName(ReadOnlySpan<char> value, Span<char> destination);

The variable name to transform.

The destination span to copy the result into.

You can use GetNiceNameLength to get the length of the resulting span.

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.

C# Signature
public static int NicifyVariableName(string value, ArrayBuilder<char> builder);

The variable name to transform.

The builder to write the result to.

The number of characters written to the builder.

Removes common prefixes (e.g. m_, _, k) and uppercases the first character.

C# Signature
public static string NicifyVariableName(string value);

The variable name to transform.

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.

C# Signature
public static int NicifyVariableName(ReadOnlySpan<char> value, ArrayBuilder<char> builder);

The variable name to transform.

The builder to write the result to.

The number of characters written to the builder.

Removes common variable name prefixes such as m_, _, and k and writes the result to the specified builder.

C# Signature
public static int RemovePrefix(string value, ArrayBuilder<char> builder);

The variable name to process.

The builder to write the result to.

The number of characters written to the builder.

Removes common variable name prefixes such as m_, _, and k.

C# Signature
public static string RemovePrefix(string value);

The variable name to process.

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.

C# Signature
public static int RemovePrefix(ReadOnlySpan<char> value, ArrayBuilder<char> builder);

The variable name to process.

The builder to write the result to.

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.

C# Signature
public static int RemovePrefix(ReadOnlySpan<char> value, Span<char> destination);

The variable name to process.

The destination span to copy the result into.

The number of characters written to the destination.

Determines whether the value starts with on or On followed by an uppercase character (e.g. OnValueChanged).

C# Signature
public static bool StartsWithOn(ReadOnlySpan<char> value);

The value to check.

true if the value starts with on or On followed by an uppercase character; otherwise false.

Determines whether the value starts with on or On followed by an uppercase character (e.g. OnValueChanged).

C# Signature
public static bool StartsWithOn(string? value);

The value to check.

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.

C# Signature
public static void UppercaseStart(ReadOnlySpan<char> value, ArrayBuilder<char> builder);

The input string.

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.

C# Signature
public static void UppercaseStart(string value, ArrayBuilder<char> builder);

The input string.

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.

C# Signature
public static void UppercaseStart(ReadOnlySpan<char> value, Span<char> destination);

The input value.

The destination span to copy the result into.

Returns a new string with the first character uppercased.

C# Signature
public static string UppercaseStart(string value);

The input string.

A new string with the first character uppercased.