Is there an existing issue for this?
Task description
#1293 (comment)
The BCL IBufferWriter<T> interface is purpose-built for allowing arbitrary code to grow and write to a buffer when the source has either a known or unknown buffer length. This allows for scenarios with complex business logic that would be wasteful to repeat if the buffer passed in is not already large enough, without the baggage of System.Text.StringBuilder, which cannot even be converted to a ReadOnlySpan<char> without doing an extra allocation.
IAppendable is an interface from Java that performs a similar operation and IBufferWriter<T> has the potential to eliminate it from the Lucene.NET codebase. However, for now we should limit this addition to CharTermAttribute and the Normalizer2 in ICU4N which will give us a cross-library proof of concept.
The IBufferWriter<T> interface should be inherited directly by ICharTermAttribute, which is a breaking API change. However, due to the limited use cases of IAttribute-derived types, this is unlikely to affect many end users. The implementation of the 3 members will then need to be on the CharTermAttribute class.
The implementation should use the existing growth semantics provided by InternalResizeBuffer. See the Append(ReadOnlySpan<char>) implementation for an example.
See the ArrayBufferWriter and its tests to see an optimized implementation example along with some tests we can use to verify the implementation.
Is there an existing issue for this?
Task description
#1293 (comment)
The BCL
IBufferWriter<T>interface is purpose-built for allowing arbitrary code to grow and write to a buffer when the source has either a known or unknown buffer length. This allows for scenarios with complex business logic that would be wasteful to repeat if the buffer passed in is not already large enough, without the baggage ofSystem.Text.StringBuilder, which cannot even be converted to aReadOnlySpan<char>without doing an extra allocation.IAppendableis an interface from Java that performs a similar operation andIBufferWriter<T>has the potential to eliminate it from the Lucene.NET codebase. However, for now we should limit this addition toCharTermAttributeand theNormalizer2in ICU4N which will give us a cross-library proof of concept.The
IBufferWriter<T>interface should be inherited directly byICharTermAttribute, which is a breaking API change. However, due to the limited use cases ofIAttribute-derived types, this is unlikely to affect many end users. The implementation of the 3 members will then need to be on theCharTermAttributeclass.The implementation should use the existing growth semantics provided by
InternalResizeBuffer. See theAppend(ReadOnlySpan<char>)implementation for an example.See the
ArrayBufferWriterand its tests to see an optimized implementation example along with some tests we can use to verify the implementation.