-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: string/ReadOnlySpan<char> Truncate #85603
Comments
The span version of public static class MemoryExtensions
{
- public ReadOnlySpan<char> Truncate(this ReadOnlySpan<char> span, char until);
+ public ReadOnlySpan<T> Truncate<T>(this ReadOnlySpan<T> span, T until)
+ where T : IEquatable<T>;
} I often have a similar method in projects, it's useful when dealing with buffers containing null-terminated strings. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationFor Copy-paste from #31655I propose a new method on String to perform a fairly common task: Truncating a string to a maximum length. Scenarios:
In my experience, this is quite a common need. I often need to define a helper function for this.
Two more from myself:
Key difference from #31655 that there is no I also added
var index = str.IndexOf('');
if (index != -1)
str = str.Substring(0, index); that is also pretty repeated pattern API Proposalnamespace System
{
public class String
{
public string Truncate(int maxLength);
public string Truncate(char until);
}
public readonly ref struct ReadOnlySpan<T>
{
public Span<T> Truncate(int maxLength);
}
public static class MemoryExtensions
{
public ReadOnlySpan<char> Truncate(this ReadOnlySpan<char> span, char until);
}
} API UsageMain use-cases described above, I just link some examples from /runtime. Most of them are for runtime/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/AsnDecoder.cs Lines 548 to 549 in 57bfe47
(More cases in this and other Tar files) runtime/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs Lines 807 to 808 in d9e8673
runtime/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Lines 3351 to 3359 in d260e50
runtime/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs Lines 708 to 713 in d9e8673
Lines 23 to 26 in a193cb5
Alternative Designs
RisksThere are so much
|
Maybe add an overload that takes maxLength and a value, so that it truncates to the last occurance of the |
Updated a proposal with generic overload |
I don't think this proposal is in the right shape. It differs quite a bit from other APIs we have on the span types. In general, we want most methods to be extension methods in I'm not sure we want to take this proposal in as a general purpose one. Truncate can have different meanings depending on context. Consider for example what a user might expect it to mean for There is also a concern around taking both |
This issue has been marked |
It's also ambiguous when |
Thanks! Totally forgot about that. public static class MemoryExtensions
{
// Same on Span<T>?
public static ReadOnlySpan<T> Truncate<T>(this ReadOnlySpan<T> span, int maxLength);
}
public class String
{
public string Truncate(int maxLength);
}
What about simple |
Background and motivation
For
int
overload there is a #31655 with good use-cases and motivation.Copy-paste from #31655
I propose a new method on String to perform a fairly common task: Truncating a string to a maximum length. Scenarios:In my experience, this is quite a common need. I often need to define a helper function for this.
Semantics:
Two more from myself:
string.Substring(0, maxLength)
orspan.Slice(0, maxLength)
without manually passing0
😄Key difference from #31655 that there is no
truncationSuffix
. It is too specific for core library and probably should be placed in UI frameworks (also discussed in original issue). Kotlin has similar methodTake
, Python just doesn't throw on substringingI also added
ROS<char>
version to avoid allocating. Also keep in mind thatROS<>
even doesn't haveEnumerable.Take
workaround and should be manuallyif
-checked for lengthchar
overload is basicallythat is also pretty repeated pattern
API Proposal
API Usage
Main use-cases described above, I just link some examples from /runtime. Most of them are for
ROS<>
since BCL tries to avoid using.Substring
, but obviously it's not possible to useROS<>
everywhere instead stringFor
int
overloadMath.Min
pattern is pretty common:(More cases in other Asn files)
runtime/src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/AsnDecoder.cs
Lines 548 to 549 in 57bfe47
(More cases in this and other Tar files)
runtime/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs
Lines 807 to 808 in d9e8673
runtime/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
Lines 3351 to 3359 in d260e50
char
overload:runtime/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Lines 708 to 713 in d9e8673
runtime/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/RuntimeInformation.cs
Lines 23 to 26 in a193cb5
Alternative Designs
ROS<>
version probably could be generic insteadchar
(and use different index search), but I don't have any examples for non-char usagesSpan<T>
? But I don't think there are too much mutablechar
spansRisks
There are so much
IndexOf
variations likeLastIndexOf
,AnyIndexOf
that should be supported or completely ignoredThe text was updated successfully, but these errors were encountered: