Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

create ReadOnlySpan<char> from string#7276

Merged
jkotas merged 3 commits intodotnet:SpanOfTfrom
adamsitnik:spanFromString
Sep 21, 2016
Merged

create ReadOnlySpan<char> from string#7276
jkotas merged 3 commits intodotnet:SpanOfTfrom
adamsitnik:spanFromString

Conversation

@adamsitnik
Copy link
Member

@KrzysztofCwalina @jkotas the possibility of creating readonly span from string was missing, so I implemented it

/// <param name="text">The target string.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null
/// reference (Nothing in Visual Basic).</exception>
public static ReadOnlySpan<char> Slice(this string text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that Slice is a good name for this method. @KrzysztofCwalina ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to expose this as an implicit cast?

Copy link
Member

@KrzysztofCwalina KrzysztofCwalina Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is not on String. Well, in this case case won't work. Given that, we would have to add another name, which is probably not worth it, i.e. Slice() seems like the next best choice besides the operator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should add the implicit cast operator and keep the Slice method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KrzysztofCwalina I can not add implicit cast. I get compiler error User-defined conversion must convert to or from the enclosing type (if I add implicit cast to ReadOnlySpan of T to cast to ROS of char)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what I meant. In this case, I am fine with Slice(), i.e I have no better naming suggestions.

if (text == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);

return new ReadOnlySpan<char>(ref JitHelpers.GetStringData(text), text.Length);
Copy link
Member

@jkotas jkotas Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not need a new JitHelpers method. Adding a new method like ref char GetFirstCharRef() { return ref m_firstChar; } should be enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas this has simplified things a lot! I pushed an update

<Member Name="NonPortableCast&lt;TFrom,TTo&gt;(System.Span&lt;TFrom&gt;)" />
<Member Name="NonPortableCast&lt;TFrom,TTo&gt;(System.ReadOnlySpan&lt;TFrom&gt;)" />
</Type>
<Type Name="System.ReadOnlySpanExtensions">
Copy link
Member

@jkotas jkotas Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to add this with Condition="FEATURE_SPAN_OF_T" like the other span apis.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas I have pushed commit with the condition

@jkotas
Copy link
Member

jkotas commented Sep 20, 2016

LGTM otherwise

@KrzysztofCwalina
Copy link
Member

looks good to me too.

@jkotas
Copy link
Member

jkotas commented Sep 21, 2016

Thanks!

@jkotas jkotas merged commit 00cfddb into dotnet:SpanOfT Sep 21, 2016
jkotas pushed a commit to jkotas/coreclr that referenced this pull request Oct 29, 2016
jkotas pushed a commit to jkotas/coreclr that referenced this pull request Oct 29, 2016
jkotas pushed a commit to jkotas/coreclr that referenced this pull request Oct 29, 2016
jkotas pushed a commit to jkotas/coreclr that referenced this pull request Oct 29, 2016
jkotas pushed a commit to jkotas/coreclr that referenced this pull request Oct 31, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants