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

String overloads #7621

Closed
AlexGhiondea wants to merge 2 commits into
dotnet:masterfrom
AlexGhiondea:StringOverloads
Closed

String overloads #7621
AlexGhiondea wants to merge 2 commits into
dotnet:masterfrom
AlexGhiondea:StringOverloads

Conversation

@AlexGhiondea
Copy link
Copy Markdown

@AlexGhiondea AlexGhiondea commented Oct 14, 2016

This PR addresses dotnet/corefx#5552 and was create to finish up PR #2945

Peter Jas and others added 2 commits October 13, 2016 20:32
…ditional overload signatures for string.Join to accept char separator. Since the implementation of: ```c# Join(String separator, String[] value, int startIndex, int count) ``` relies on `UnSafeCharBuffer.AppendString(string)` (which is not exposed to user land), I have added a char substitute for same.

Also removed an additional check for `separator==null`, which wasn't
present in `Join(String separator, IEnumerable<String> values)` either.
`StringBuilder.Append` and `UnSafeCharBuffer.AppendString` both handle
this case explicitly.
@AlexGhiondea
Copy link
Copy Markdown
Author

This PR replaces #2945

// Joins a string IEnumerable together as one string with a char separator between each original string.
//
[ComVisible(false)]
public static String Join(Char separator, IEnumerable<String> values) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@terrajobst @weshaggard @stephentoub - Do we still want to keep this overload?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why wouldn't we keep it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh I see @stephentoub comment on the old PR. I agree with @stephentoub that it isn't really needed unless we see some sort of other benefit.

@justinvp
Copy link
Copy Markdown

justinvp commented Oct 14, 2016

I don't know how folks feel about using unsafe code here, but in lieu of Span<char>, the implementations between the string and char separator overloads could be shared by doing something like:

public static unsafe string Join<T>(string separator, IEnumerable<T> values)
{
    fixed (char* pSeparator = separator)
    {
        int separatorLength = separator == null ? 0 : separator.Length;
        return Join(pSeparator, separatorLength, values);
    }
}

public static unsafe string Join<T>(char separator, IEnumerable<T> values)
{
    return Join(&separator, 1, values);
}

private static unsafe string Join<T>(char* separator, int separatorLength, IEnumerable<T> values)
{
    // Actual shared implementation here.
    // Uses the existing `StringBuilder.Append(char*,int)` to append the separator.
}

And done similarly for the other overloads.

Which is similar to how the the new string.Split overloads were implemented.

@AlexGhiondea
Copy link
Copy Markdown
Author

@dotnet-bot please test CentOS7.1 x64 Debug Build and Test

@AlexGhiondea
Copy link
Copy Markdown
Author

@dotnet-bot please test Linux ARM Emulator Cross Release Build



[System.Security.SecuritySafeCritical] // auto-generated
public void AppendChar( char charToAppend ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit why the space after ( and before )?

}

// Joins an array of strings together as one string with a char separator between each original string.
//
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: Remove dead //s

Copy link
Copy Markdown

@jamesqo jamesqo left a comment

Choose a reason for hiding this comment

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

There's a bug in the Join(char, object[]) overload where we return null if the 1st object is null. Also, I think it would be nice if you were able to format some of the code to the corefx coding conventions for better readability.


// Joins an array of strings together as one string with a char separator between each original string.
//
public static String Join(Char separator, params String[] value) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: Use string, char, and string.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also put the brace on a newline.

//
public static String Join(Char separator, params String[] value) {
if (value == null)
throw new ArgumentNullException("value");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: put braces here.

}

// Joins an object array of strings together as one string with a string separator between each original string.
//
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: dead // (same in other places)


// Joins an array of objects together as one string with a char separator between each original string.
//
[ComVisible(false)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Are the ComVisibles still necessary?

// Joins an array of objects together as one string with a char separator between each original string.
//
[ComVisible(false)]
public static String Join(Char separator, params Object[] values) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: string, char, object. (same in other places)

[ComVisible(false)]
public static String Join(Char separator, params Object[] values) {
if (values == null)
throw new ArgumentNullException("values");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: add braces. (same in other places)

if (values == null)
throw new ArgumentNullException("values");

if (values.Length == 0 || values[0] == null)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We don't want to propogate the bug where string.Join(string, object[]) returns null if the first item is null (even if the object has some non-null items) to more places.

Contract.Ensures(Contract.Result<String>() != null);
Contract.EndContractBlock();

using(IEnumerator<T> en = values.GetEnumerator())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: Spacing

Contract.Ensures(Contract.Result<String>() != null);
Contract.EndContractBlock();

using(IEnumerator<String> en = values.GetEnumerator()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: spacing, String.

[System.Security.SecuritySafeCritical] // auto-generated
public void AppendChar( char charToAppend ) {
if ( (m_totalSize - m_length) < 1 ) {
throw new IndexOutOfRangeException();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We should probably look into changing this method (probably delete the class entirely) afterwards. In its current state this does not look like it will be inlined.

@AlexGhiondea
Copy link
Copy Markdown
Author

It looks like this PR is not yet ready to be merged in.

There are still questions around the actual API surface area and the implementation needs more work.

I have marked the original issue as 'up for grabs' and I will close this PR for now.

@AlexGhiondea AlexGhiondea reopened this Oct 20, 2016
@karelz karelz added enhancement Product code improvement that does NOT require public API changes/additions and removed api addition labels Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement Product code improvement that does NOT require public API changes/additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants