|
2 | 2 |
|
3 | 3 | Source only package for Text extension methods and text building utilities for .NET |
4 | 4 |
|
5 | | -This package provides advanced text manipulation utilities including named placeholder formatting, enhanced StringBuilder operations, and high-performance string building with minimal heap allocations. These tools are designed for scenarios requiring efficient string composition, template processing, and conditional text generation. |
| 5 | +This package provides advanced text manipulation utilities including named placeholder formatting and enhanced StringBuilder operations. These tools are designed for scenarios requiring efficient string composition, template processing, and conditional text generation. |
6 | 6 |
|
7 | 7 | ## Core Classes |
8 | 8 |
|
@@ -113,73 +113,10 @@ var result = new StringBuilder() |
113 | 113 | - **Fluent Interface**: All methods return `StringBuilder` for method chaining |
114 | 114 | - **Type Safety**: Generic support for any collection type |
115 | 115 |
|
116 | | -### ValueStringBuilder |
117 | | - |
118 | | -A high-performance, stack-friendly string builder that minimizes heap allocations by using stack-allocated or pooled buffers. Ideal for performance-critical scenarios with predictable string sizes. |
119 | | - |
120 | | -```csharp |
121 | | -using System.Text; |
122 | | - |
123 | | -// Stack-allocated buffer for small strings |
124 | | -Span<char> buffer = stackalloc char[256]; |
125 | | -using var builder = new ValueStringBuilder(buffer); |
126 | | - |
127 | | -builder.Append("Processing items: "); |
128 | | -for (int i = 0; i < items.Length; i++) |
129 | | -{ |
130 | | - if (i > 0) builder.Append(", "); |
131 | | - builder.Append(items[i].ToString()); |
132 | | -} |
133 | | - |
134 | | -string result = builder.ToString(); // Efficient conversion to string |
135 | | -
|
136 | | -// Pool-based allocation for larger strings |
137 | | -using var largeBuilder = new ValueStringBuilder(1024); |
138 | | -largeBuilder.Append("Large content..."); |
139 | | - |
140 | | -// Insert operations |
141 | | -largeBuilder.Insert(0, "Prefix: "); |
142 | | -largeBuilder.Insert(largeBuilder.Length, " :Suffix"); |
143 | | - |
144 | | -// Span-based operations for maximum efficiency |
145 | | -ReadOnlySpan<char> content = largeBuilder.AsSpan(); |
146 | | -bool success = largeBuilder.TryCopyTo(destinationBuffer, out int written); |
147 | | - |
148 | | -// Character-level access |
149 | | -for (int i = 0; i < builder.Length; i++) |
150 | | -{ |
151 | | - ref char c = ref builder[i]; // Direct reference to buffer |
152 | | - if (c == 'a') c = 'A'; // In-place modification |
153 | | -} |
154 | | - |
155 | | -// Advanced buffer management |
156 | | -builder.EnsureCapacity(500); // Pre-allocate capacity |
157 | | -Span<char> appendBuffer = builder.AppendSpan(10); // Get span for direct writing |
158 | | -// Write directly to appendBuffer... |
159 | | -
|
160 | | -// Null-terminated strings for interop |
161 | | -ref char pinned = ref builder.GetPinnableReference(terminate: true); |
162 | | -``` |
163 | | - |
164 | | -**Key Features:** |
165 | | - |
166 | | -- **Zero Heap Allocation**: Uses stack buffers or pooled arrays to avoid GC pressure |
167 | | -- **High Performance**: Direct buffer manipulation with minimal overhead |
168 | | -- **Flexible Initialization**: Stack-allocated span or pooled array backing |
169 | | -- **Direct Buffer Access**: Reference-based character access for in-place modifications |
170 | | -- **Span Integration**: Full integration with `Span<char>` and `ReadOnlySpan<char>` |
171 | | -- **Memory Pool Integration**: Automatic return of rented arrays via `Dispose()` |
172 | | -- **Capacity Management**: Manual capacity control for optimal performance |
173 | | - |
174 | 116 | ## Performance Notes |
175 | 117 |
|
176 | 118 | - **NameFormatter**: Optimized property reflection with minimal overhead for simple properties; nested properties use efficient dot-notation parsing |
177 | 119 | - **StringBuilderExtensions**: All operations maintain StringBuilder's performance characteristics while adding convenience |
178 | | -- **ValueStringBuilder**: Designed for high-performance scenarios: |
179 | | - - Stack allocation eliminates heap allocations for small strings (< 256 chars typically) |
180 | | - - Pool allocation reduces GC pressure for larger strings |
181 | | - - Direct buffer access enables in-place modifications without string copies |
182 | | - - Span-based operations provide zero-copy string processing |
183 | 120 |
|
184 | 121 | ## Compiler Configuration |
185 | 122 |
|
|
0 commit comments