News for hackers. Stuff that matters.
Re:Ftfy – fix Unicode that's broken in various ways by cosmie on Tuesday January 09, 2018 @12:04AM (#16103765)
Ftfy and Unidecode[1] are two of the first libraries I reach for when munging around with a new dataset. My third favorite is the regex filter [^ -~]. If you know you’re working with ASCII data, that single regex against any untrusted data is resolves soooo many potential headaches.

I manage and source data for a lead generation/marketing firm, and dealing with free text data from all over the place is a nightmare. Even when working with billion dollar firms, data purchases take the form of CSV files with limited or no documentation on structure or formatting or encoding, sales-oriented data dictionaries, and FTP drops. I have a preprocessing script in python that strips out lines that can’t be parsed as utf8, stage it into a Redshift cluster, then hit it with a UDF in Redshift called kitchen_sink that encapsulates a lot of my text cleanup and validation heuristics like the above.

Re:Ftfy – fix Unicode that's broken in various ways by jonathan_n on Tuesday January 09, 2018 @02:45AM (#16104316)
This. Ftfy has saved my as many many times. Thanks for the regex. brilliant.
Re:Ftfy – fix Unicode that's broken in various ways by cosmie on Tuesday January 09, 2018 @02:59AM (#16104372)
I don't remember where I came across that regex, but it's saved me from so many headaches I quite literally get giddy any time I can insert it into a processing stream.

A developer upstream naively split on /n and leave some errant /r characters everywhere? Fixed.

Embedded backspaces or nulls or tabs or any sort of control character? Gone.

Non-ASCII data in a field you know should be ASCII only? Ha, not today good sir!

Until you've had to deal with the hell that is raw, free form data from who-knows-where, you cannot even fathom how satisfying it is to be able to deploy that regex (when appropriate) and know beyond a doubt that you can't have any more gremlins hiding in that particular field/data that'll hit you later.

Re:Ftfy – fix Unicode that's broken in various ways by thiagocsf on Tuesday January 09, 2018 @06:55AM (#16105455)
It’s nice not having to deal with languages other than English.

In Portuguese, which I’ve worked with, you develop other tricks, like replacing à, á, â or ã with a. But, in order to do this, you still need to find out the encoding used before you can create the “ASCII” equivalent.

Fun trivia: coco means coconut; cocô means poo. So, by replacing ô with o, you’re guaranteed a chuckle at some stage.

Re:Ftfy – fix Unicode that's broken in various ways by Piskvorrr on Tuesday January 09, 2018 @07:51AM (#16105766)
True. But if that specific case is dealing in, let's say, URLs, then all of the content should be ASCII - either as a direct character representation, or encoded, again to ASCII.

Never did I see the parent mention "this is sufficient for humans", or just "...for English" - even that would be a naïve assumption (see? ;)).

Re:Ftfy – fix Unicode that's broken in various ways by rspeer on Tuesday January 09, 2018 @08:07AM (#16105846)
If your definition of "URLs" includes "IRIs" (a term nobody really uses but which encompasses the idea that you can use Unicode in URLs), then this isn't a good assumption to make.

I would rather pass around a link to https://ja.wikipedia.org/wiki/メインページ than to https://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3... , and lots of software will support both. And if you want all URLs to be ASCII, you'll need to convert the first into the second, not just delete the Japanese characters.

Re:Ftfy – fix Unicode that's broken in various ways by zlynx on Tuesday January 09, 2018 @12:42PM (#16108017)
If the URL is "on the wire" it had better be 7-bit ASCII. Actually even more restrictive than that. Because that's the spec. https://tools.ietf.org/html/rfc3986

In user interaction with a browser or wherever else it seems that anything goes.

Re:Ftfy – fix Unicode that's broken in various ways by rspeer on Tuesday January 09, 2018 @01:03PM (#16108203)
That's the spec but this is also the spec: https://www.ietf.org/rfc/rfc3987.txt

It's true that protocols such as HTTP only use ASCII URIs on the wire. If you are implementing an HTTP client yourself, you will need to implement percent-encoding.

Which is different from saying "well, I know URLs should be ASCII on the wire, so I can safely delete all the non-ASCII characters from a URL." That's not true.

Re:Ftfy – fix Unicode that's broken in various ways by Piskvorrr on Wednesday January 10, 2018 @03:11AM (#16113271)
As soon as you add or remove to the string, it no longer points to the same resource; I considered this too obvious to mention. OTOH, for _validation_, this is useful: "you have a ZWJ character in an URL, that's unlikely". And yes, I understand that there are protocols that allow you to pass around the full Unicode or aunt Matilda or whatever - I should have been more specific.
Re:Ftfy – fix Unicode that's broken in various ways by Piskvorrr on Wednesday January 10, 2018 @03:12AM (#16113280)
The first version is much more readable and less hacky; alas, unless you are positive that all the software you're interfacing with can handle Unicode (never have I ever been in such a glorious situation), fallback to URL-encoding it is.
Re:Ftfy – fix Unicode that's broken in various ways by kuro68k on Wednesday January 10, 2018 @02:32AM (#16113138)
I was hoping it was a replacement for Unicode... Unicode is so broken that we need something new.
Re:Ftfy – fix Unicode that's broken in various ways by aw3c2 on Tuesday January 09, 2018 @03:10AM (#16104418)
What does that regex do?
Re:Ftfy – fix Unicode that's broken in various ways by cosmie on Tuesday January 09, 2018 @03:23AM (#16104464)
Matches anything that's not between the space and tilde in the ASCII code range[1], which is the entire range of printable ASCII characters. It's similar to [a-Z] you see a lot, but expanded to include the space character, numbers, and punctuation. The regex [ -~] lets you match those characters, whereas [^ -~] negates that and matches anything that's not a printable ASCII character (useful for regex replace functions).

If you look at the table at the top of [1], you'll notice all of the characters at the beginning of the ASCII range which are non-printing and therefore invisible. Plus, at the end for some insane reason, the DELETE character. If there's no valid reason for any of these characters to exist in your dataset, nor for higher code point (UTF) characters to exist, then [^ -~] will match them and let you strip them out all in one go.

[1] http://www.asciitable.com/

Re:Ftfy – fix Unicode that's broken in various ways by dfox on Tuesday January 09, 2018 @07:04AM (#16105504)
The reason for DEL being 0x7f is that it is originally intended as marker for "this character was deleted and should be skipped over" and not as command to delete previous/next character. And you can change any ASCII character into 0x7f by ORing it with with 0x7f, that is, punching all the holes in punched tape (or punchcard, but ASCII usually was not used for them and punching all holes on punchcard is not advisable for mechanical reasons).
Re:Ftfy – fix Unicode that's broken in various ways by mehrdadn on Tuesday January 09, 2018 @10:02AM (#16106597)
Love these historical notes that make everything make sense. Thanks for posting!
Re:Ftfy – fix Unicode that's broken in various ways by sundarurfriend on Tuesday January 09, 2018 @01:45PM (#16108618)
> The regex [ -~] lets you match those characters, whereas [^ -~] negates that and matches anything that's not a printable ASCII character

Oh dear, I think I finally understand that mysterious phenomenon in some websites where, text I write in my native language gets saved and displayed as empty text, but writing in English works. I've come across this a few times over the years, in random forgotten corners of the Internet. It could well be that this regex got written down somewhere as good security practice, and some developers out there copied it into the code without thinking about whether the ASCII restriction is applicable.

Re:Ftfy – fix Unicode that's broken in various ways by cosmie on Tuesday January 09, 2018 @03:54PM (#16109944)
It's probably a more mundane issue related to either older versions of HTML (which did not support UTF8[1]), or issues with an older database that stored things as latin1 or ASCII by default, or issues with an older programming language that doesn't seamlessly support non-latin1 or non-ascii characters without deliberately doing so (like Python 2).

That said, I do agree it's not something that should be used without thought. I mentioned that my usage generally involves knowledge that the data set has no valid reason to contain higher code points, and usually involves usage of Unidecode[2] to convert higher code points into ASCII equivalents (which in many cases strips out contextual knowledge, but is sometimes an acceptable trade off for stability and predictability of the data sent downstream).

[1] https://www.w3schools.com/html/html_charset.asp [2] https://pypi.python.org/pypi/Unidecode

Re:Ftfy – fix Unicode that's broken in various ways by aw3c2 on Tuesday January 09, 2018 @11:44AM (#16107453)
Thank you very much for this detailed reply! Made my day :)
Re:Ftfy – fix Unicode that's broken in various ways by xstartup on Tuesday January 09, 2018 @05:52AM (#16105108)
Do you've any idea whether it's possible to search invisible characters using google?
Re:Ftfy – fix Unicode that's broken in various ways by cosmie on Tuesday January 09, 2018 @04:16PM (#16110167)
I'm not sure, but Google Search in general strips punctuation that isn't specifically designated as a search operator[1], and likely strips control/non-printing characters in a similar manner. Testing it[2] with a few URL-encoded control and invisible characters[3], they seem to be ignored. But they do make it into the query parameter, at least until you edit it for a subsequent search, at which point they get converted into spaces/+ symbols in the query.

[1] https://support.google.com/websearch/answer/2466433?hl=en

[2] https://www.google.com/search?hl=en&q="%0B%08+Foo+%7F"

[3] You can see the full string being searched here, it's 7 characters long: https://r12a.github.io/uniview/?charlist=%08%20Foo%20%7F

Re:Ftfy – fix Unicode that's broken in various ways by LittlePeter on Wednesday January 10, 2018 @12:17PM (#16116763)
Note that when using GNU grep (version 2.25) you have to use -P flag for Perl compatible regular expressions. E.g.:

echo "lörem ipsum" | grep -P "[^ -~]" --color

and not

echo "lörem ipsum" | grep "[^ -~]" --color