Skip to content

框架内未能集成数字0gc的转为指定编码的字符串方法 #130

@RiceNooob

Description

@RiceNooob

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

框架内未能集成数字0gc的转为指定编码的字符串方法 目前方法实现通过 $"MNUM={MNUM};" 字符串插值实现
部分代码如下:
public void Build(ref TWriter writer) where TWriter : IBytesWriter
{
FillFieldUtil.WriteStringField(ref writer, $"##QN={QN};");
FillFieldUtil.WriteStringField(ref writer, $"MNUM={MNUM};");
FillFieldUtil.WriteStringField(ref writer, $"STATUS={STATUS};");
FillFieldUtil.WriteStringField(ref writer, $"CURTOK={CURTOK}**");
}

Describe the solution you'd like

是否存在更加优雅的写法 如同框架中示例中集成的字符串0GC的实现 代码如下
public static void WriteStringField(ref TWriter writer, string value, int length) where TWriter : IBytesWriter
{
var memory = writer.GetMemory(length);
memory.Span[..length].Clear();// 先清空内存,确保不足长度的部分用空格填充,因为数据可能来自于池化内存,所以需要清空
var newWriter = new BytesWriter(memory);
var stringValue1Span = value.AsSpan(0, Math.Min(length, value.Length));// 截取字符串,确保不会超过指定长度

WriterExtension.WriteNormalString(ref newWriter, stringValue1Span, DefaultEncoding);
writer.Advance(length);

}

Additional context

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions