majintao0131 / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

inconsistent handling of char in yaml-cpp #132

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. take the following input
    #include <yaml-cpp/yaml.h>

    int main()
    {
        YAML::Emitter em;
        em << YAML::BeginSeq;
        em << 'c';
        em << 8745;
        em << YAML::EndSeq;
        std::cout << em.c_str() << std::endl;
        /* output:
         * - 99
         * - 8745
         */

        char c; int i;
        std::stringstream ss("- 99\n- 8745");
        YAML::Parser parser(ss);
        YAML::Node node;
        parser.GetNextDocument(node);
        node[0] >> c;
        node[1] >> i;
        std::cout << c << " " << int(c) << " " << i << std::endl;
        // 9 57 8745
    }

What is the expected output? What do you see instead?
When I emit a char, it emits it like if I casted it to int before. However upon 
loading, it loads it as an ascii character '9', instead of 99 (and by the way, 
in this case it should throw an exception or something, since only 1 character 
of the 2 character string is "parsed"). It should either output it as `e` (and 
not `99`), or parse it as a number, then cast to a char. Or better yet, output 
`'e'` or `99` depending on some emitter manipulator, and parse both form (like 
you can specify how to quote strings).

What version of the product are you using? On what operating system?
hg tip (403:2920baca8009)
gentoo linux x86_64, gcc 4.6.2

Please provide any additional information below.

Original issue reported on code.google.com by DirtY.iC...@gmail.com on 14 Nov 2011 at 6:50

GoogleCodeExporter commented 9 years ago
 Nice catch!

Fixed, r5a731a858119.

Thanks!

Original comment by jbe...@gmail.com on 14 Nov 2011 at 10:23

GoogleCodeExporter commented 9 years ago
Almost works, except if i have an unsigned char...

Something like
inline Emitter& operator << (Emitter& emitter, unsigned char v) { return 
emitter.Write((char) v); }
seems to work...

Original comment by DirtY.iC...@gmail.com on 14 Nov 2011 at 10:43

GoogleCodeExporter commented 9 years ago
Looks good, r2d28ec774955.

Original comment by jbe...@gmail.com on 14 Nov 2011 at 11:00