From: knu@... Date: 2015-12-14T14:59:08+00:00 Subject: [ruby-core:72116] [Ruby trunk - Feature #11814] String#valid_encoding? without force_encoding Issue #11814 has been updated by Akinori MUSHA. Martin D��rst wrote: > A few comments on this program: Thanks! > - Encoding::ASCII_8BIT will pick up garbage. Encoding::US_ASCII is much better. Maybe not. You could choose to perform the CAP encoding when the encoding was unknown (ASCII_8BIT), or just use the binary garbage as is if the storage was capable of saving binary file names (like ZFS). > - Encoding::ISO_8859_1 is always valid, for all bytes, so ASCII8BIT (or US-ASCII) never get used. Ah, so true. It���s my bad. Anyway I put ASCII_8BIT as a sentinel so `encoding` would never be nil, so US_ASCII was not an option. > - There are many more encodings, but distinguishing them is difficult/impossible with this method. I know, but in most cases you have some idea as to what the possible encodings are and it is sufficient to try just a few encodings in such cases. This example was meant to be one of them. If you need more, a BOM-based encoding detector could be another use case for valid_encoding?(enc), I don't know. I already named a few gems for serious use, so please don't be so strict about these casual use cases. ---------------------------------------- Feature #11814: String#valid_encoding? without force_encoding https://bugs.ruby-lang.org/issues/11814#change-55534 * Author: Usaku NAKAMURA * Status: Rejected * Priority: Normal * Assignee: ---------------------------------------- Now we have to set a encoding to a string to validate it, just like: ```ruby str.force_encoding('euc-jp').valid_encoding? # => true or false ``` But to modify the string is not so smart. knu-san requires the way to validate a string without modifiing it [*1]. Then, I propose to add an optional encoding parameter to `String#valid_encoding?`. ```ruby str.valid_encoding?('euc-jp') # => true or false ``` A patch is attached. [*1] https://twitter.com/knu/status/676009662655934465 (in Japanese) ---Files-------------------------------- valid_encoding.patch (4.4 KB) -- https://bugs.ruby-lang.org/