This project is archived and is in readonly mode.
should be able to truncate a multibyte string to a max # of bytes
-
sarah (at ultrasaurus)
FYI: happy to create a patch if y'all want this change (paired with Wolfram Arnold on this)
-
Alovak
Yesterday I saw described truncation bug with multi-byte string on Basecamp.
-
Wolfram Arnold
Oh, that's a good corroboration. We'll do a patch then.
-
CancelProfileIsBroken
- Tag set to bugmash
-
Elad Meidar
- Tag changed from bugmash to bugmash, multibyte
since it's a bit staling i've attached a patch that applies on both master and 2-3-stable
-
Mike Enriquez
-1 I don't think this fits into the responsibility for Chars. Since we don't have a "String".limit_characters we don't need a "String".mb_chars.limit_bytes.
I haven't had the need to limit_bytes (or characters) based on a database restriction because a validation error is typically used to restrict the size. Truncating the data and saving seems like an application specific thing and can be done by slicing and removing the left over bytes. Note that "こんいちわ".length # => 15, which is the number of bytes.
-
Matías Flores
+1 verified Elad's patch applies cleanly on both master and 2-3-stable and all tests pass.
-
sarah (at ultrasaurus)
Mike,
Sure you can find out the length in bytes for a multi-byte string, but to actually truncate it at a character boundary requires some effort (as you can see in the implementation above).
Validation errors are appropriate when there is user input and as a fail-safe. In the case where we ran into this, we had a field in the database which is filled in as an annotation (for administrators and for debugging) based on a different longer field. For auto-generated strings that need to be truncated, a validation isn't helpful.
Sarah
-
Manfred Stienstra
I don't really feel the use-case, but I guess it's a valid reason. Not sure if this is something to include in core. Attached is a cleaned up version of the proposed patch.
-
Repository
- State changed from new to committed
(from [935bd0fef8e26f4ec65fe411a1d29942493f8d46]) Add ActiveSupport::Multibyte::Chars#limit.
The limit method limits the number of bytes in a string. Useful when the
storage space of the string is limited, for instance in a database column
definition.Sharpen up the implementation of translate offset.
[#3192 state:committed] http://github.com/rails/rails/commit/935bd0fef8e26f4ec65fe411a1d299...
