Skip to content

String#truncate_bytes#27319

Merged
jeremy merged 1 commit into
rails:masterfrom
jeremy:as/string-truncate-bytes
Feb 18, 2018
Merged

String#truncate_bytes#27319
jeremy merged 1 commit into
rails:masterfrom
jeremy:as/string-truncate-bytes

Conversation

@jeremy

@jeremy jeremy commented Dec 10, 2016

Copy link
Copy Markdown
Member

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
Copy Markdown
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

javan commented Dec 10, 2016

Copy link
Copy Markdown
Contributor

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

jeremy commented Dec 10, 2016

Copy link
Copy Markdown
Member Author

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

amatsuda commented Feb 2, 2017

Copy link
Copy Markdown
Member

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

jeremy commented Feb 26, 2017

Copy link
Copy Markdown
Member Author

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