Skip to content
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

Support Span<T> conversion #29

Merged
merged 2 commits into from
May 21, 2021
Merged

Support Span<T> conversion #29

merged 2 commits into from
May 21, 2021

Conversation

gukoff
Copy link
Contributor

@gukoff gukoff commented May 21, 2021

Description

Support Span conversion.

REVIEW THE FIRST COMMIT

Motivation and Context

No support existed before.

How Has This Been Tested?

Verified regenerated projects work in Visual Studio.

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dependencies (update dependencies and changes associated, has no impact on code or features)
  • Unit Tests (work on Unit Tests, has no impact on code or features)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@nfbot nfbot added the Type: enhancement New feature or request label May 21, 2021
@@ -682,21 +691,24 @@ string outputDirectory
string line;
while (null != (line = input.ReadLine()))
{
foreach (var replacement in replacements)
// Replacing longer matches first is a safeguard heuristic.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Everything below this line is just refactoring that creeped into this PR

Copy link
Member

@Ellerbach Ellerbach left a comment

Choose a reason for hiding this comment

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

{
if (array != null)
{
if ((start + length > array.Length) || (start >= array.Length))
Copy link
Member

Choose a reason for hiding this comment

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

should be this: if ((length > array.Length - start) || (start > array.Length))

Check things here: https://github.com/nanoframework/CoreLibrary/blob/develop/nanoFramework.CoreLibrary/System/SpanByte.cs

Copy link
Contributor Author

@gukoff gukoff May 21, 2021

Choose a reason for hiding this comment

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

Can start be equal to array length, unless length=0?

Copy link
Contributor Author

@gukoff gukoff May 21, 2021

Choose a reason for hiding this comment

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

This looks correct:

if (start < 0 ||
    length < 0 ||
    start + length > array.Length ||
    (start == array.Length && array.Length > 0))

{
get
{
int realIndex = _start + index;
Copy link
Member

Choose a reason for hiding this comment

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

the get should be:

if (index &gt; _length)                
{                    
throw new ArgumentOutOfRangeException();                }
}
return _array[_start + index];

Copy link
Contributor Author

@gukoff gukoff May 21, 2021

Choose a reason for hiding this comment

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

Ah, I see. Length is not the length of the array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way it's >=, not >

Copy link
Member

@Ellerbach Ellerbach May 21, 2021

Choose a reason for hiding this comment

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

Good catch for this one. Mind a PR on mscorlib? :-)


set
{
int realIndex = _start + index;
Copy link
Member

Choose a reason for hiding this comment

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

set should be:

if (index > _length)
{
throw new ArgumentOutOfRangeException();
}
 _array[_start + index] = value;

/// <exception cref="System.ArgumentOutOfRangeException">start is less than zero or greater than System.Span.Length.</exception>
public Span<T> Slice(int start)
{
return Slice(start, _length - start);
Copy link
Member

Choose a reason for hiding this comment

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

missing the check for the length

if ((start > _length) || (start < 0))
{
throw new ArgumentOutOfRangeException();
}

return new SpanByte(_array, _start + start, _length - start);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should check it in the "main" metod Slice(start, length)

Copy link
Member

Choose a reason for hiding this comment

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

fair

/// Defines an implicit conversion of a System.Span to a System.ReadOnlySpan.
/// </summary>
/// <param name="span">The object to convert to a System.ReadOnlySpan.</param>
public static implicit operator ReadOnlySpan<T>(Span<T> span)
Copy link
Member

Choose a reason for hiding this comment

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

remove this one, we don't need ReadOnlySpan, we've simplify and get read of the ReadOnlySpan to only have Span

@gukoff
Copy link
Contributor Author

gukoff commented May 21, 2021

  • Made edits in Span.cs
  • ignored SpanByte
  • added XML-docs explaining the logic

@gukoff
Copy link
Contributor Author

gukoff commented May 21, 2021

If I see it right, SpanByte in mscorlib currently has a bug and allows access 1 cell beyond the available length.

If the version in this PR looks good, I can move it to core library.

/// <exception cref="System.ArgumentOutOfRangeException">start is less than zero or greater than System.Span.Length.</exception>
public Span<T> Slice(int start)
{
return Slice(start, _length - start);
Copy link
Member

Choose a reason for hiding this comment

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

fair

@Ellerbach Ellerbach enabled auto-merge (squash) May 21, 2021 16:28
@Ellerbach
Copy link
Member

@gukoff thanks! do you mind resolving the conflicts so we can merge this one soon?

auto-merge was automatically disabled May 21, 2021 17:54

Head branch was pushed to by a user without write access

@Ellerbach Ellerbach enabled auto-merge (squash) May 21, 2021 17:55
@Ellerbach Ellerbach merged commit ad489d5 into nanoframework:develop May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants