ext/yajl_ext.c in yajl-ruby-0.6.7 vs ext/yajl_ext.c in yajl-ruby-0.6.8

- old
+ new

@@ -21,10 +21,35 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "yajl_ext.h" +inline const char * strnchr(const char * s, size_t count, char c) { + for (; count-- && *s != '\0'; ++s) { + if (*s == c) { + return s; + } + } + return NULL; +} + +inline double strntod(const char * s, size_t count) { + char buf[count+1]; + memcpy(buf, s, count); + buf[count] = 0; + + return strtod(buf, NULL); +} + +inline VALUE rb_cstrn2inum(const char * s, size_t count) { + char buf[count+1]; + memcpy(buf, s, count); + buf[count] = 0; + + return rb_cstr2inum(buf, 10); +} + /* Helpers for building objects */ inline void yajl_check_and_fire_callback(void * ctx) { yajl_parser_wrapper * wrapper; GetParser((VALUE)ctx, wrapper); @@ -224,18 +249,16 @@ yajl_check_and_fire_callback(ctx); return 1; } static int yajl_found_number(void * ctx, const char * numberVal, unsigned int numberLen) { - char buf[numberLen+1]; - memcpy(buf, numberVal, numberLen); - buf[numberLen] = 0; - - if (strchr(buf, '.') || strchr(buf, 'e') || strchr(buf, 'E')) { - yajl_set_static_value(ctx, rb_float_new(strtod(buf, NULL))); + if (strnchr(numberVal, numberLen, '.') || + strnchr(numberVal, numberLen, 'e') || + strnchr(numberVal, numberLen, 'E')) { + yajl_set_static_value(ctx, rb_float_new(strntod(numberVal, numberLen))); } else { - yajl_set_static_value(ctx, rb_cstr2inum(buf, 10)); + yajl_set_static_value(ctx, rb_cstrn2inum(numberVal, numberLen)); } return 1; } static int yajl_found_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen) {