this post was submitted on 02 Oct 2023
2 points (100.0% liked)

Programming

20182 readers
210 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 0 points 2 years ago* (last edited 2 years ago) (1 children)

I love the comparison of string length of the same UTF-8 string in four programming languages (only the last one is correct, by the way):

Python 3:

len("๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ")

5

JavaScript / Java / C#:

"๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ".length

7

Rust:

println!("{}", "๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ".len());

17

Swift:

print("๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ".count)

1

[โ€“] [email protected] 1 points 2 years ago* (last edited 2 years ago)

That depends on your definition of correct lmao. Rust explicitly counts utf-8 scalar values, because that's the length of the raw bytes contained in the string. There are many times where that value is more useful than the grapheme count.