khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

Did you know that you should use TextElementEnumerator to determine the length of a string if the input might have Unicode emojis? Check out the sample below. #dotnet #csharp

I'm curious if future versions of #dotnet should do this check implicitly. 🤔

meziantou,
@meziantou@hachyderm.io avatar
khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@meziantou Interestingly, the StringInfo class doesn’t have a “get length” method. There is a GetLengthOfNextElement but that just uses string.Length again.

hypeartist,
coderanger,

@khalidabuhakmeh I’m with you; why would you ever want the opposite? Pragmatic over technical accuracy in my opinion

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@coderanger I agree. This is likely going to catch a lot of people as subtle bugs in their UIs when dealing with user inputs.

rafaelldi,
@rafaelldi@mastodon.social avatar
ythos,
@ythos@dotnet.social avatar

@khalidabuhakmeh We actually ended up using ReadOnlySpan<char>.EnumerateRunes(); for our Json Schema validation code.

ythos,
@ythos@dotnet.social avatar

@khalidabuhakmeh (it defines the length based on runes not graphemes)

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar

@ythos Neat. This Unicode world is going to be the death of me. 😅

heaths,
@heaths@fosstodon.org avatar

@khalidabuhakmeh I imagine the perf degradation isn't worth it in most cases. How often does the average (if there is such a thing) app process a string with possible emoji? Better to think of String.Length as the number of wide chars. How often do you need to know the rendered length? And will the renderer even handle combining marks correctly?

  • All
  • Subscribed
  • Moderated
  • Favorites
  • dotnet
  • DreamBathrooms
  • mdbf
  • ngwrru68w68
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • osvaldo12
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • InstantRegret
  • tacticalgear
  • anitta
  • ethstaker
  • provamag3
  • cisconetworking
  • tester
  • GTA5RPClips
  • cubers
  • everett
  • modclub
  • megavids
  • normalnudes
  • Leos
  • JUstTest
  • lostlight
  • All magazines