Skip to content

Commit ef03c30

Browse files
authored
Merge pull request #14 from altasoft/feature/StringManipulationFunctions
Updated Executor to generate string methods if type is "string".
2 parents 2a4b32d + c233c00 commit ef03c30

5 files changed

+218
-0
lines changed

src/AltaSoft.DomainPrimitives.Generator/Executor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,12 @@ private static void Process(GeneratorData data, string ctorCode, DomainPrimitive
484484
MethodGeneratorHelper.GenerateMandatoryMethods(data, builder);
485485
builder.NewLine();
486486

487+
if (data.PrimitiveTypeFriendlyName == "string")
488+
{
489+
MethodGeneratorHelper.GenerateStringMethods(builder);
490+
builder.NewLine();
491+
}
492+
487493
MethodGeneratorHelper.GenerateEquatableOperators(data.ClassName, data.TypeSymbol.IsValueType, builder);
488494
builder.NewLine();
489495

src/AltaSoft.DomainPrimitives.Generator/Helpers/MethodGeneratorHelper.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,74 @@ public static void GenerateParsable(GeneratorData data, SourceCodeBuilder builde
830830
builder.CloseBracket();
831831
}
832832

833+
/// <summary>
834+
/// Generates string methods
835+
/// </summary>
836+
/// <param name="builder">The source code builder.</param>
837+
public static void GenerateStringMethods(SourceCodeBuilder builder)
838+
{
839+
builder
840+
.AppendLine("/// <summary>")
841+
.AppendLine("/// Gets the character at the specified index.")
842+
.AppendLine("/// </summary>")
843+
.AppendLine("public char this[int i]")
844+
.OpenBracket()
845+
.AppendLine("get => _value[i];")
846+
.CloseBracket()
847+
.NewLine();
848+
849+
builder
850+
.AppendLine("/// <summary>")
851+
.AppendLine("/// Gets the character at the specified index.")
852+
.AppendLine("/// </summary>")
853+
.AppendLine("public char this[Index index]")
854+
.OpenBracket()
855+
.AppendLine("get => _value[index];")
856+
.CloseBracket()
857+
.NewLine();
858+
859+
builder
860+
.AppendLine("/// <summary>")
861+
.AppendLine("/// Gets the substring by specified range.")
862+
.AppendLine("/// </summary>")
863+
.AppendLine("public string this[Range range]")
864+
.OpenBracket()
865+
.AppendLine("get => _value[range];")
866+
.CloseBracket()
867+
.NewLine();
868+
869+
builder
870+
.AppendLine("/// <summary>")
871+
.AppendLine("/// Gets the number of characters.")
872+
.AppendLine("/// </summary>")
873+
.AppendLine("/// <returns>The number of characters in underlying string value.</returns>")
874+
.AppendLine("public int Length => _value.Length;")
875+
.NewLine();
876+
877+
builder
878+
.AppendLine("/// <summary>")
879+
.AppendLine("/// Returns a substring of this string.")
880+
.AppendLine("/// </summary>")
881+
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
882+
.AppendLine("public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);")
883+
.NewLine();
884+
885+
builder
886+
.AppendLine("/// <summary>")
887+
.AppendLine("/// Returns a substring of this string.")
888+
.AppendLine("/// </summary>")
889+
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
890+
.AppendLine("public string Substring(int startIndex) => _value.Substring(startIndex);")
891+
.NewLine();
892+
893+
builder
894+
.AppendLine("/// <summary>")
895+
.AppendLine("/// Returns the entire string as an array of characters.")
896+
.AppendLine("/// </summary>")
897+
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
898+
.AppendLine("public char[] ToCharArray() => _value.ToCharArray();");
899+
}
900+
833901
/// <summary>
834902
/// Generates equality and inequality operators for the specified type.
835903
/// </summary>

tests/AltaSoft.DomainPrimitives.Generator.Tests/Snapshots/DomainPrimitiveGeneratorTest.StringOfStringValue_GeneratesAllInterfacesAndConverters#StringOfStringValue.g.verified.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,54 @@ public void ValidateOrThrow(StringValue value)
113113
}
114114

115115

116+
/// <summary>
117+
/// Gets the character at the specified index.
118+
/// </summary>
119+
public char this[int i]
120+
{
121+
get => _value[i];
122+
}
123+
124+
/// <summary>
125+
/// Gets the character at the specified index.
126+
/// </summary>
127+
public char this[Index index]
128+
{
129+
get => _value[index];
130+
}
131+
132+
/// <summary>
133+
/// Gets the substring by specified range.
134+
/// </summary>
135+
public string this[Range range]
136+
{
137+
get => _value[range];
138+
}
139+
140+
/// <summary>
141+
/// Gets the number of characters.
142+
/// </summary>
143+
/// <returns>The number of characters in underlying string value.</returns>
144+
public int Length => _value.Length;
145+
146+
/// <summary>
147+
/// Returns a substring of this string.
148+
/// </summary>
149+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
150+
public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);
151+
152+
/// <summary>
153+
/// Returns a substring of this string.
154+
/// </summary>
155+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
156+
public string Substring(int startIndex) => _value.Substring(startIndex);
157+
158+
/// <summary>
159+
/// Returns the entire string as an array of characters.
160+
/// </summary>
161+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
162+
public char[] ToCharArray() => _value.ToCharArray();
163+
116164
/// <inheritdoc/>
117165
[MethodImpl(MethodImplOptions.AggressiveInlining)]
118166
public override bool Equals(object? obj) => obj is StringOfStringValue other && Equals(other);

tests/AltaSoft.DomainPrimitives.Generator.Tests/Snapshots/DomainPrimitiveGeneratorTest.StringOfStringValue_GeneratesAllInterfacesAndConverters#StringValue.g.verified.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,54 @@ public void ValidateOrThrow(string value)
113113
}
114114

115115

116+
/// <summary>
117+
/// Gets the character at the specified index.
118+
/// </summary>
119+
public char this[int i]
120+
{
121+
get => _value[i];
122+
}
123+
124+
/// <summary>
125+
/// Gets the character at the specified index.
126+
/// </summary>
127+
public char this[Index index]
128+
{
129+
get => _value[index];
130+
}
131+
132+
/// <summary>
133+
/// Gets the substring by specified range.
134+
/// </summary>
135+
public string this[Range range]
136+
{
137+
get => _value[range];
138+
}
139+
140+
/// <summary>
141+
/// Gets the number of characters.
142+
/// </summary>
143+
/// <returns>The number of characters in underlying string value.</returns>
144+
public int Length => _value.Length;
145+
146+
/// <summary>
147+
/// Returns a substring of this string.
148+
/// </summary>
149+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
150+
public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);
151+
152+
/// <summary>
153+
/// Returns a substring of this string.
154+
/// </summary>
155+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
156+
public string Substring(int startIndex) => _value.Substring(startIndex);
157+
158+
/// <summary>
159+
/// Returns the entire string as an array of characters.
160+
/// </summary>
161+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
162+
public char[] ToCharArray() => _value.ToCharArray();
163+
116164
/// <inheritdoc/>
117165
[MethodImpl(MethodImplOptions.AggressiveInlining)]
118166
public override bool Equals(object? obj) => obj is StringValue other && Equals(other);

tests/AltaSoft.DomainPrimitives.Generator.Tests/Snapshots/DomainPrimitiveGeneratorTest.StringValue_GeneratesAllInterfacesAndConverters#StringValue.g.verified.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,54 @@ public void ValidateOrThrow(string value)
113113
}
114114

115115

116+
/// <summary>
117+
/// Gets the character at the specified index.
118+
/// </summary>
119+
public char this[int i]
120+
{
121+
get => _value[i];
122+
}
123+
124+
/// <summary>
125+
/// Gets the character at the specified index.
126+
/// </summary>
127+
public char this[Index index]
128+
{
129+
get => _value[index];
130+
}
131+
132+
/// <summary>
133+
/// Gets the substring by specified range.
134+
/// </summary>
135+
public string this[Range range]
136+
{
137+
get => _value[range];
138+
}
139+
140+
/// <summary>
141+
/// Gets the number of characters.
142+
/// </summary>
143+
/// <returns>The number of characters in underlying string value.</returns>
144+
public int Length => _value.Length;
145+
146+
/// <summary>
147+
/// Returns a substring of this string.
148+
/// </summary>
149+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
150+
public string Substring(int startIndex, int length) => _value.Substring(startIndex, length);
151+
152+
/// <summary>
153+
/// Returns a substring of this string.
154+
/// </summary>
155+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
156+
public string Substring(int startIndex) => _value.Substring(startIndex);
157+
158+
/// <summary>
159+
/// Returns the entire string as an array of characters.
160+
/// </summary>
161+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
162+
public char[] ToCharArray() => _value.ToCharArray();
163+
116164
/// <inheritdoc/>
117165
[MethodImpl(MethodImplOptions.AggressiveInlining)]
118166
public override bool Equals(object? obj) => obj is StringValue other && Equals(other);

0 commit comments

Comments
 (0)