Skip to content

Commit

Permalink
Merge pull request #14 from altasoft/feature/StringManipulationFunctions
Browse files Browse the repository at this point in the history
Updated Executor to generate string methods if type is "string".
  • Loading branch information
temonk authored Sep 5, 2024
2 parents 2a4b32d + c233c00 commit ef03c30
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/AltaSoft.DomainPrimitives.Generator/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,12 @@ private static void Process(GeneratorData data, string ctorCode, DomainPrimitive
MethodGeneratorHelper.GenerateMandatoryMethods(data, builder);
builder.NewLine();

if (data.PrimitiveTypeFriendlyName == "string")
{
MethodGeneratorHelper.GenerateStringMethods(builder);
builder.NewLine();
}

MethodGeneratorHelper.GenerateEquatableOperators(data.ClassName, data.TypeSymbol.IsValueType, builder);
builder.NewLine();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,74 @@ public static void GenerateParsable(GeneratorData data, SourceCodeBuilder builde
builder.CloseBracket();
}

/// <summary>
/// Generates string methods
/// </summary>
/// <param name="builder">The source code builder.</param>
public static void GenerateStringMethods(SourceCodeBuilder builder)
{
builder
.AppendLine("/// <summary>")
.AppendLine("/// Gets the character at the specified index.")
.AppendLine("/// </summary>")
.AppendLine("public char this[int i]")
.OpenBracket()
.AppendLine("get => _value[i];")
.CloseBracket()
.NewLine();

builder
.AppendLine("/// <summary>")
.AppendLine("/// Gets the character at the specified index.")
.AppendLine("/// </summary>")
.AppendLine("public char this[Index index]")
.OpenBracket()
.AppendLine("get => _value[index];")
.CloseBracket()
.NewLine();

builder
.AppendLine("/// <summary>")
.AppendLine("/// Gets the substring by specified range.")
.AppendLine("/// </summary>")
.AppendLine("public string this[Range range]")
.OpenBracket()
.AppendLine("get => _value[range];")
.CloseBracket()
.NewLine();

builder
.AppendLine("/// <summary>")
.AppendLine("/// Gets the number of characters.")
.AppendLine("/// </summary>")
.AppendLine("/// <returns>The number of characters in underlying string value.</returns>")
.AppendLine("public int Length => _value.Length;")
.NewLine();

builder
.AppendLine("/// <summary>")
.AppendLine("/// Returns a substring of this string.")
.AppendLine("/// </summary>")
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
.AppendLine("public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);")
.NewLine();

builder
.AppendLine("/// <summary>")
.AppendLine("/// Returns a substring of this string.")
.AppendLine("/// </summary>")
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
.AppendLine("public string Substring(int startIndex) => _value.Substring(startIndex);")
.NewLine();

builder
.AppendLine("/// <summary>")
.AppendLine("/// Returns the entire string as an array of characters.")
.AppendLine("/// </summary>")
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
.AppendLine("public char[] ToCharArray() => _value.ToCharArray();");
}

/// <summary>
/// Generates equality and inequality operators for the specified type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,54 @@ public void ValidateOrThrow(StringValue value)
}


/// <summary>
/// Gets the character at the specified index.
/// </summary>
public char this[int i]
{
get => _value[i];
}

/// <summary>
/// Gets the character at the specified index.
/// </summary>
public char this[Index index]
{
get => _value[index];
}

/// <summary>
/// Gets the substring by specified range.
/// </summary>
public string this[Range range]
{
get => _value[range];
}

/// <summary>
/// Gets the number of characters.
/// </summary>
/// <returns>The number of characters in underlying string value.</returns>
public int Length => _value.Length;

/// <summary>
/// Returns a substring of this string.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);

/// <summary>
/// Returns a substring of this string.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int startIndex) => _value.Substring(startIndex);

/// <summary>
/// Returns the entire string as an array of characters.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char[] ToCharArray() => _value.ToCharArray();

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? obj) => obj is StringOfStringValue other && Equals(other);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,54 @@ public void ValidateOrThrow(string value)
}


/// <summary>
/// Gets the character at the specified index.
/// </summary>
public char this[int i]
{
get => _value[i];
}

/// <summary>
/// Gets the character at the specified index.
/// </summary>
public char this[Index index]
{
get => _value[index];
}

/// <summary>
/// Gets the substring by specified range.
/// </summary>
public string this[Range range]
{
get => _value[range];
}

/// <summary>
/// Gets the number of characters.
/// </summary>
/// <returns>The number of characters in underlying string value.</returns>
public int Length => _value.Length;

/// <summary>
/// Returns a substring of this string.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);

/// <summary>
/// Returns a substring of this string.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int startIndex) => _value.Substring(startIndex);

/// <summary>
/// Returns the entire string as an array of characters.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char[] ToCharArray() => _value.ToCharArray();

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? obj) => obj is StringValue other && Equals(other);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,54 @@ public void ValidateOrThrow(string value)
}


/// <summary>
/// Gets the character at the specified index.
/// </summary>
public char this[int i]
{
get => _value[i];
}

/// <summary>
/// Gets the character at the specified index.
/// </summary>
public char this[Index index]
{
get => _value[index];
}

/// <summary>
/// Gets the substring by specified range.
/// </summary>
public string this[Range range]
{
get => _value[range];
}

/// <summary>
/// Gets the number of characters.
/// </summary>
/// <returns>The number of characters in underlying string value.</returns>
public int Length => _value.Length;

/// <summary>
/// Returns a substring of this string.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);

/// <summary>
/// Returns a substring of this string.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int startIndex) => _value.Substring(startIndex);

/// <summary>
/// Returns the entire string as an array of characters.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char[] ToCharArray() => _value.ToCharArray();

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? obj) => obj is StringValue other && Equals(other);
Expand Down

0 comments on commit ef03c30

Please sign in to comment.