Optimizing some string manipulation
I want to both substring an input string at the last occurrence of
'/'
and normalize it into only alphanumeric (a-z
, A-Z
, 0-9
) characters, turning any characters unable to be normalized (meaning characters with diacritics turning into their non-diacritic versions (ä
-> a
)) into _
.
Here's what I've got so far;
However, this is hardly faster than using Substring
and Normalize
(with some custom code involving CharUnicodeInfo.GetUnicodeCategory
).
Any ideas?8 Replies
Ah, right, and if the final string begins with a number, it should prepend
_
as well.
As an example; /Foo/123Bar (Bäz)
would get turned into _123Bar__Baz_
.Sounds like what you're really doing is turning an arbitrary string into a valid identifier (without unicode)
That unbounded stackalloc is a bad idea btw
It's going to not only be less efficient than a stackalloc of constant size but potentially hard crash with a stack overflow if you pass in a large string
yup, that's basically it
good point on the stackalloc. i know that the max length i'm gonna pass in is 256, so i can just cap it at that
Declaring pNorm inside the fixed is also weird and potentially more expensive than necessary
A stackalloc is already fixed
In that example you're getting a Span and then pinning it unnecessarily
I'd look into this further but it's late and I'm on my phone so I don't wanna suffer with that experience lol
no worries, you've already helped a good amount
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
yup
as well as substringing it from the last
/
onwards, and if the first character is a digit, prepend an underscoreUnknown User•3y ago
Message Not Public
Sign In & Join Server To View