|
|
|
SearchCategories
|
Article not found.
General Encoding Strategies
Posted about 1 year ago
in Character Encodings.
This is the third post in my series on Character Encodings. Please see the table of contents for the series if you have not yet read the previous posts. Before we get into specifics, let's try to distill a few best practices for working with encodings. I'm sure you can tell that there's a lot that needs to be considered with encodings, so let's try to focus in on a few key points that will help us the most. Use UTF-8 Everywhere You CanWe know UTF-8 isn't perfect, but it's pretty darn close to perfect. There is no other single encoding you could pick that has the potential to satisfy such a wide audience. It's our best bet. For these reasons, UTF-8 is quickly becoming the preferred encoding for the Web, email, and more. If you have a say over what encoding or encodings your software will accept, support, and deliver, choose UTF-8 whenever you can. This is absolutely the best default. Get in the Habit of Documenting Your EncodingsWe learned that you must know a data's encoding to properly work with it. While there are tools to help you guess an encoding, you really want to try and avoid being in this position. Part of how to make that happen is to be a good citizen and make sure you are documenting your encodings at every step. If you send an email, make sure it specifies a correct character set. Add a meta tag to Web pages to state the encoding. View the source of this page for an example. Document encodings accepted and returned from your API's. This will raise everyone's encoding awareness, which helps us all. Develop Your Encoding Safe SensesYou need to get into the habit of thinking, "Is this encoding safe?" When you call a method, ask the question. When you hand your data off to some process, reality check some results. Have you ever done something like This may sound like paranoia, but it's really not as bad as it seems. There tend to just be a few key points where you need to go out of your way to protect the data and it's asking this question repeatedly that teaches you to spot those. To give an example, while enhancing the standard CSV library for Ruby 1.9's m17n implementation, I needed to use some user provided data in a
Luckily, my instincts were just good enough to wonder, is that safe? I fed some UTF-32 data to
Now, this was just a case of Ruby 1.9 still being raw around the edges. It looks like this has been fixed in current builds:
Still the point stands, you can't even trust Ruby at some times. Be cautious. The natural conclusion of this is that you want to know how encodings are handled all through the pipeline your data will pass through. Does your HTML arrange to receive form data in UTF-8? Is Ruby in UTF-8 mode when it receives that data? Does the MySQL table you store that data in have an encoding set to UTF-8? Modern versions of Rails even handle two of those three steps for you. That's why it's important to look into the tools you use. These strategies aren't all you will need, but they are a terrific start. This is not too much to remember and it will greatly increase your awareness of the issues. That's the most important thing. |
|
|
|
If you do find yourself in a situation where you don't know a character encoding and you are forced to guess it (again try to avoid this whenever possible), Andrew S. Townley posted a message to Ruby Talk showing how to use the rchardet gem to guess an encoding.