3 ms·
How would you design the API?
by icebraining 10y ago
How would you design the API?
- forgottenacc57 10y agoExplicitly, obviously and unambiguously.
- icebraining 10y agoThat's a wishlist, not a design. Do you suppose they tried to make it implicit, non-obvious and ambiguous?
- crdoconnor 10y agoHe's got a point. I'd probably use something like to_bytes and to_str to disambiguate.
- dom0 10y agoencode/decode were likely not changed from Py2 to make porting easier. Otherwise it would likely have been to_bytes and str.from_bytes - much like "int" in Py3 with to_bytes and int.from_bytes.
- baq 10y agoI'd copy directly from Rust.
- deleted 10y ago[deleted]
- icebraining 10y agoSay I get a file and the name of the encoding it's in (like an XML file with a declaration), how I decode it in Rust?
- majewsky 10y agoQt has QString::fromUtf8 which takes a QByteArray, and QString.toUtf8 which gives a QByteArray. (And similar for a few other encodings.) That was always obvious enough for me. http://doc.qt.io/qt-5/qstring.html http://doc.qt.io/qt-5/qstring.html
- icebraining 10y agoLike I asked baq, say I don't know the encoding ahead of time, but get it dynamically (like in the declaration of an XML file). How would I decode the string?
- protomikron 10y agoI would try utf-8, and otherwise just panic ("invalid input: string is not utf-8 encoded"). Obviously it depends on the context, but I think parsers should be strict, otherwise ambiguities leak into your system.
- icebraining 10y agoI'm not saying "try multiple encodings till you get something". I'm asking about the API when the encoding is not static, but provided at runtime, like in XML files, eg: <?xml version="1.0" encoding="ISO-8859-1"?>
- majewsky 10y agoYou would have the QByteArray first (e.g. read from a file or socket), parse the <?xml?>, thus find the encoding, convert the rest to QString with that, and parse the rest from the QString.
- icebraining 10y agoMy question is how does one "convert the rest to QString with that", in terms of actual code?