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

Add String.Trim overloads that take a single char#9009

Merged
jkotas merged 3 commits intodotnet:masterfrom
justinvp:string_trim
Jan 23, 2017
Merged

Add String.Trim overloads that take a single char#9009
jkotas merged 3 commits intodotnet:masterfrom
justinvp:string_trim

Conversation

@justinvp
Copy link
Copy Markdown

@justinvp justinvp commented Jan 19, 2017

Sorry for the larger diff -- I decided to cleanup the order and formatting of the Trim methods while I was at it.

Second commit cleans up some uses of Trim/TrimEnd in CoreLib.

https://github.com/dotnet/corefx/issues/14337

Copy link
Copy Markdown
Member

@karelz karelz left a comment

Choose a reason for hiding this comment

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

LGTM - 2 minor comments, feel free to ignore them if you disagree.

{
for (start = 0; start < Length; start++)
{
if (!char.IsWhiteSpace(this[start])) break;
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: 'break' on a new line? (given you're already cleaning up the code ... not sure if it is a code style rule, maybe it is just my personal preference)

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.

For what its worth, the coding guidelines allow single line if without braces. Anything else must have braces.

That isn't how I was brought up, but those are the rules :)

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.

Fixed.

The guidelines say:

A single line statement block can go without braces but the block must be properly indented on its own line and it must not be nested in other statement blocks that use braces

Given that last part and the fact that it is nested in other statement blocks here, I went ahead and moved break to it's own line surrounded by braces.


return TrimHelper(TrimBoth);
}
private string TrimHelper(int trimType)
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.

I'd personally change the 3 const ints TrimHead, TrimTrail, TrimBoth, into enum ... given you're already cleaning up and refactoring the code ... just a thought ...

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.

Fixed.

// Removes a set of characters from the beginning of this string.
public unsafe string TrimStart(params char[] trimChars)
{
if (null == trimChars || trimChars.Length == 0)
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, I think we try to avoid Yoda conditionals.

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.

Fixed.


return TrimHelper(TrimBoth);
}
private string TrimHelper(int trimType)
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.

Might be reasonable to change this method name to something like TrimWhitespaceHelper

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.

Fixed.

Copy link
Copy Markdown
Member

@danmoseley danmoseley left a comment

Choose a reason for hiding this comment

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

@stephentoub is it okay to change a public method to unsafe?

@stephentoub
Copy link
Copy Markdown
Member

is it okay to change a public method to unsafe

Yes, unsafe doesn't actually affect the signature in metadata.

@justinvp
Copy link
Copy Markdown
Author

Feedback addressed. Also fixed a confusing/incorrect comment.

@danmoseley
Copy link
Copy Markdown
Member

@dotnet-bot test this please (failed to restore.. transient?)

@justinvp
Copy link
Copy Markdown
Author

@dotnet-bot test Linux ARM Emulator Cross Release Build please (build timeout)

@jkotas jkotas merged commit fc2448f into dotnet:master Jan 23, 2017
@jkotas
Copy link
Copy Markdown
Member

jkotas commented Jan 23, 2017

@justinvp Thank you. Could you please port this change to CoreRT when you get a chance?

@karelz
Copy link
Copy Markdown
Member

karelz commented Jan 23, 2017

@jkotas I had troubles with Squash & merge - any tips what you did?

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Jan 23, 2017

@karelz Just pressed the green button as usual ... nothing special.

@karelz
Copy link
Copy Markdown
Member

karelz commented Jan 23, 2017

Hmmm, I was not brave enough with 'Merge' apparently ... Squash & Merge basically told me "no"

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Jan 23, 2017

I have done Squash & Merge.

@karelz
Copy link
Copy Markdown
Member

karelz commented Jan 23, 2017

Hmmm, weird. Probably problem between the keyboard and the chair here ...

@karelz karelz modified the milestone: 2.0.0 Aug 28, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Add String.Trim overloads that take a single char
* Cleanup uses of string.Trim/TrimEnd


Commit migrated from dotnet/coreclr@fc2448f
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants