digitalmars.D.learn - Am I using std.encoding correctly?
I have some binary files that I'm reading. At compile time it's unknown what types I'm reading, and if they're strings, how it's encoded. I'm doing something like this: Variant value; switch(type) { ... case Type.STRING: value = cast(dchar[])[]; const(ubyte)[] buffer = raw[0 .. length]; while (buffer.length > 0) value ~= encodingScheme.decode(buffer); break; ... } I know there's safeDecode, but I'm also fairly confident that all strings can decode safely, already, and if it isn't I'd want an exception thrown from it. Is this correct usage? I guess I was a little surprised there was no decodeString that basically did this.
Nov 14 2015
On 14.11.2015 15:55, Charles wrote:I know there's safeDecode, but I'm also fairly confident that all strings can decode safely, already, and if it isn't I'd want an exception thrown from it.Documentation [1] says "The input to this function MUST be validly encoded." It says nothing about an Exception being thrown when the input is invalid. I'd interpret that "MUST" as "there is no defined behavior for bad input". So if there's a chance that the input is invalid, don't use plain `decode`.Is this correct usage? I guess I was a little surprised there was no decodeString that basically did this.I can't find a simpler way to decode a whole string either. Would be a useful addition. Make it a range. Or if we're both just missing it, then it should probably be documented more prominently.
Nov 14 2015