Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String#truncate_bytes #27319

Merged
merged 1 commit into from
Feb 18, 2018
Merged

String#truncate_bytes #27319

merged 1 commit into from
Feb 18, 2018

Conversation

jeremy
Copy link
Member

@jeremy jeremy commented Dec 10, 2016

Limit to N bytes without breaking multibytes chars.

Can be done with foo.mb_chars.limit(n.bytes), but that's much slower.

This joins our #truncate and #truncate_words family.

@matthewd
Copy link
Member

How do we feel about how this will behave in the presence of combining characters?

ISTM we should be dropping the entire grapheme, not effectively replacing it with a different one... but the simplicity of this implementation definitely holds some competing appeal.

@javan
Copy link
Contributor

javan commented Dec 10, 2016

It's worth documenting at least.

>> "hï".truncate_bytes(2, omission: nil)
=> "hi"

>> "💅🏾".truncate_bytes(5, omission: nil)
=> "💅"

>> "👩‍👩‍👧‍👦".truncate_bytes(18, omission: nil)
=> "👩‍👩‍👧"

>> "👩‍👩‍👧‍👦".truncate_bytes(12, omission: nil)
=> "👩‍👩"

>> "👩‍👩‍👧‍👦".truncate_bytes(8, omission: nil)
=> "👩‍"

🤘

@jeremy
Copy link
Member Author

jeremy commented Dec 10, 2016

Looking at truncating around grapheme clusters: significantly trickier, but doable.

Multibyte::Chars#limit also splits:

>> grapheme = "👩‍👩‍👧‍👦"
=> "👩‍👩‍👧‍👦"
>> grapheme.size
=> 7
>> grapheme.bytesize
=> 25
>> grapheme.chars
=> ["👩", "‍", "👩", "‍", "👧", "‍", "👦"]
>> (1..grapheme.bytesize).map { |i| grapheme.mb_chars.limit(i).to_s }
=> ["", "", "", "👩", "👩", "👩", "👩‍", "👩‍", "👩‍", "👩‍", "👩‍👩", "👩‍👩", "👩‍👩", "👩‍👩‍", "👩‍👩‍", "👩‍👩‍", "👩‍👩‍", "👩‍👩‍👧", "👩‍👩‍👧", "👩‍👩‍👧", "👩‍👩‍👧‍", "👩‍👩‍👧‍", "👩‍👩‍👧‍", "👩‍👩‍👧‍", "👩‍👩‍👧‍👦"]
>> (1..grapheme.bytesize).map { |i| grapheme.byteslice(0,i).scrub('') }
=> ["", "", "", "👩", "👩", "👩", "👩‍", "👩‍", "👩‍", "👩‍", "👩‍👩", "👩‍👩", "👩‍👩", "👩‍👩‍", "👩‍👩‍", "👩‍👩‍", "👩‍👩‍", "👩‍👩‍👧", "👩‍👩‍👧", "👩‍👩‍👧", "👩‍👩‍👧‍", "👩‍👩‍👧‍", "👩‍👩‍👧‍", "👩‍👩‍👧‍", "👩‍👩‍👧‍👦"]

@amatsuda
Copy link
Member

amatsuda commented Feb 2, 2017

I suppose once we merged #26743, the grapheme handling will become way faster because it's implemented in C.

@jeremy jeremy force-pushed the as/string-truncate-bytes branch from 47b0774 to 1263444 Compare February 16, 2017 01:30
@jeremy jeremy force-pushed the as/string-truncate-bytes branch from 1263444 to e3a4a51 Compare February 26, 2017 08:10
@jeremy
Copy link
Member Author

jeremy commented Feb 26, 2017

Switched to using /\X/ to match grapheme clusters with the aim of letting this PR sit until we rely on Ruby 2.4+.

@jeremy jeremy force-pushed the as/string-truncate-bytes branch from e3a4a51 to c4e9639 Compare April 13, 2017 18:01
@jeremy jeremy force-pushed the as/string-truncate-bytes branch 3 times, most recently from 77df1d8 to 31441a7 Compare February 18, 2018 07:32
@jeremy jeremy added this to the 6.0.0 milestone Feb 18, 2018
This faithfully preserves grapheme clusters (characters composed of other
characters and combining marks) and other multibyte characters.
@jeremy jeremy force-pushed the as/string-truncate-bytes branch from 31441a7 to 9f44380 Compare February 18, 2018 08:14
@jeremy jeremy merged commit 4940cc4 into rails:master Feb 18, 2018
@jeremy jeremy deleted the as/string-truncate-bytes branch February 18, 2018 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants