-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend ReadOnlySpan<byte> optimization for static data to work with ASCII/UTF8 strings #48850
Comments
My personal preference would for some string literal modifier to exist like |
This would mesh generally with the proposals we've been shooting around internally re: Utf8String theStr = utf8"Hello world!"; // or similar And since the current proposal is to have a free conversion from Utf8String to ROS<byte> (just like how there's a free conversion from string to ROS<char>), this should generate the desired behavior in the end. |
I hear talk of something like this, which would be more terse and a bit better: static ReadOnlySpan<byte> ContinueBytes => u8"HTTP/1.1 100 Continue\r\n\r\n"; Working via implicit |
Isn't |
Not 8 bit string data (i.e. ASCII and UTF8); they are a list of bytes, so UTF16 static ReadOnlySpan<char> Hello => "Hello".AsSpan(); But if you want to do 8 bit you have to do: // "HTTP/1.1 100 Continue\r\n\r\n"
static ReadOnlySpan<byte> ContinueBytes => new byte[] { (byte)'H', (byte)'T', (byte)'T', (byte)'P', (byte)'/', (byte)'1', (byte)'.', (byte)'1', (byte)' ', (byte)'1', (byte)'0', (byte)'0', (byte)' ', (byte)'C', (byte)'o', (byte)'n', (byte)'t', (byte)'i', (byte)'n', (byte)'u', (byte)'e', (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n' }; |
It feels like having to write Once there is a utf8 literal (or a wellknown/intrinsic conversion from string), hooking up the optimization should be fairly easy. |
I think string literals are good special cases to have. Long term I would love something like C++'s constexpr to generalize things like this. |
According to LDM Sept. 16, 2019, UTF-8 string literals are emitted as UTF-16, at least in the initial implementation. |
I think uft8 strings addressed this already? What I was looking for was to how embed some binary data as part of code generation, similar to https://github.com/protocolbuffers/protobuf/blob/312986896dafdbf2475601be8fa0a2faefc40b2f/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.pb.cs#L25 Currently that can be done using |
Could this (#24621) work with byte strings? As writing them out is fairly impenetrable e.g.
So it would be nice if it worked with UTF8 and/or ASCII encoding, so this worked instead (both preferably):
Example: dotnet/aspnetcore#7422
/cc @VSadov @stephentoub @jkotas @KrzysztofCwalina @jaredpar @jcouv
The text was updated successfully, but these errors were encountered: