jwodder / javaproperties

Python library for reading & writing Java .properties files
MIT License
30 stars 10 forks source link

Why escape colon and equals? #3

Closed onlyanegg closed 5 years ago

onlyanegg commented 5 years ago

Hi. I'm wondering why you chose to escape colons and equals signs. I've read through a couple references, and haven't seen anything saying that it's required or even acceptable. Is it valid for properties files?

Thanks :)

jwodder commented 5 years ago

The library's dumps() function mimics the behavior of java.util.Properties's store() method, whose documentation states in part:

The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.

I don't believe this is actually required for proper loading, but I do it anyway in order to match Java's behavior. Escaping arbitrary characters in this way is acceptable, as, to quote the documentation for the load() method:

The method does not treat a backslash character, \, before a non-valid escape character as an error; the backslash is silently dropped. For example, in a Java string the sequence "\z" would cause a compile time error. In contrast, this method silently drops the backslash. Therefore, this method treats the two character sequence "\b" as equivalent to the single character 'b'.

So \= in an element is equivalent to =.

onlyanegg commented 5 years ago

Thanks, @jwodder :)