- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
This article needs some reformatting, because the second line only contains 2-space strings as first argument to the Replace method.
Admin
Here we go, another "write the fastest method challenge" 🤣🤣
Admin
Bah. Who cares about performance?
Admin
@colejohnson66 It's O(n log(n)) since the number of spaces is halved per iteration. This might even be faster than a regex solution for most inputs.
Admin
Easy. Just render it as html, which will collaps all whitespace.
Admin
Wait, wait, wait, wait there. Why do they need to do that in the first place?
It's difficult to know if there's a legitimate objective without context, but it sounds suspicious. Are they trying to display HTML documents on the server with their own insane renderer or something like that?
Don't just rashly reach for regexes before it's clear what the actual purpose even is.
Admin
I think the single space in the first line was intentional. The programmer knew that
and said, "well, that's what I want. Replace all the spaces with a single space."
Then when that didn't work, and under a deadline, they put together something that kind of worked, although they didn't really know why, but it met the unit tests and that means they can close this difficult ticket.
Admin
There's a lot of cases where single spaces are desired for example in a name field.
Admin
Admin
Considering that regarding the article this method is called a lot, proper benchmarking is in order:
Colejohnson66's version obviously scales very badly with spaces. Can't wait for Mr. TAs version :-)
Admin
Oh yeah, the arguments, because the post length is limited:
Admin
Oh, I added a source generated regex plus Regex.Replace as an additional reference, which is obviously the most easiest way to do it:
Admin
Names are hard: https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
I'd say don't make too many assumptions. Someone like Elon Musk is ready to name a child with some edge cases to break your form fields to troll you. And I don't know of any international law or treaty mandating that a name can't have 2 consecutive spaces.
Admin
.. which is precisely what has happened to the frist "My text" in the article, leading to the confusing ' "My text" becomes "My text" .'
Admin
Just imagine doing something like this to ... umm.... format python.
Admin
Won't break my python code because my code uses Tabs!
Admin
There's no fun, you already did everything I thought about doing 🤣🤣
Admin
Some people use their kids' names as passwords. Elon Musk used his password as his kid's name.
Admin
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have /[2-9]|[1-9][0-9]+/ problems.
Admin
What about starting with a search for the first instance of a double space? That would speed up the case where it doesn't occur (you just return the string or a copy thereof), and otherwise you copy over all the text up to that point into your StringBuilder (which would technically only need to be length - 1 in size).
Admin
... which also happened to the code-without-markdown-backticks in the last line of the article. The double space in
Replace(" ", " ")
is collapsed to a single space by the browser. So to the reader it appears like the author suggests to solve the problem of longer sequences of spaces by adding code that does not change the number of spaces.Admin
Oh the epic fail of putting multiple whitespace in HTML. Remy, was that the WTF?
Admin
xD
Admin
You couldn't even write that name out by hand so I really don't think it's a case worth worrying about.
Admin
MaxiTB, can you please explain why this isn't an infinite loop?
while (value[index] == ' ');
Admin
[code] | Method | value | Mean | Error | StdDev | Median | Gen0 | Allocated | |--------- |--------------------- |------------:|----------:|----------:|------------:|-------:|----------:| | MaxiTbV1 | | 0.5587 ns | 0.0210 ns | 0.0175 ns | 0.5513 ns | - | - | | MrTaV1 | | 0.4497 ns | 0.0163 ns | 0.0145 ns | 0.4520 ns | - | - | | MaxiTbV1 | (...) [60] | 28.3136 ns | 0.6050 ns | 0.9239 ns | 27.7948 ns | - | - | | MrTaV1 | (...) [60] | 19.9140 ns | 0.1200 ns | 0.1123 ns | 19.8919 ns | - | - | | MaxiTbV1 | S(...)d [81] | 142.0714 ns | 1.6329 ns | 1.5274 ns | 141.8260 ns | 0.0880 | 736 B | | MrTaV1 | S(...)d [81] | 113.0683 ns | 1.0635 ns | 1.0445 ns | 113.2615 ns | 0.0181 | 152 B | | MaxiTbV1 | Space(...)ement [40] | 26.1228 ns | 0.3691 ns | 0.3082 ns | 26.2077 ns | 0.0306 | 256 B | | MrTaV1 | Space(...)ement [40] | 13.5999 ns | 0.1451 ns | 0.1286 ns | 13.5850 ns | - | - | [/code/
Admin
Admin
Admin
Ever since I first read this article, I've thought that the "right" way to deal with names (if simply treating them as arbitrary identifiers, akin to usernames, isn't an option) is to have a separate full/legal name and preferred/friendly name. If these are names of your own users, you can have them enter their full name and try to guess their preferred name; if you get it wrong, they can change it right there in the form, or post-registration.
...and then you have systems like my day job, which integrate with a ton of other systems that express most of those falsehoods, so -sigh-.
Admin
It's a presentation issue, a lot of people consider title cased with single spaces "cleaner"
Admin
Wouldn't stackalloc be even faster than ArrayPool? I'm not familiar with C#, but I looked it up since I figured a language in the C family had to have something for allocating on the stack. Since the array doesn't have to persist beyond this method call, it should be perfectly safe.
Admin
Potentially so, but there may be a risk with a large stack array running out the stack size, this can be potentially solved by checking the size and going one of two ways, but with ArrayPool, the performance is already so good, that the complication of conditionally using stackalloc may be slower than just going to ArrayPool.
Admin
It's not a while by itself, it's a misleadingly formatted do-while. It's not infinite because in between checking the condition in the while, it runs the do block which increments the index.