ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
3.04k stars 262 forks source link

Add read_number public functions #97

Closed pavelxdd closed 1 year ago

pavelxdd commented 1 year ago

I really like the yyjson read_number implementation and want to use it in my projects. The problem is that the current implementation is a static function which can't be used from other code.

I've added a simple wrapper unsafe_yyjson_read_number around read_number function, which parses number from the memory buffer into yyjson_val (or yyjson_mut_val) variable.

codecov[bot] commented 1 year ago

Codecov Report

Merging #97 (2ccd693) into master (b2a6dfe) will increase coverage by 0.00%. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master      #97   +/-   ##
=======================================
  Coverage   98.41%   98.42%           
=======================================
  Files           2        2           
  Lines        2335     2347   +12     
=======================================
+ Hits         2298     2310   +12     
  Misses         37       37           
Impacted Files Coverage Δ
src/yyjson.h 87.05% <ø> (ø)
src/yyjson.c 98.85% <100.00%> (+<0.01%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

pavelxdd commented 1 year ago

I've updated the commit with following changes:

1) Removed unsafe_yyjson_read_number in favor of yyjson_read_number. yyjson_mut_read_number is just a wrapper around yyjson_read_number.

2) Return end pointer instead of boolean if parsing was successful, or return NULL instead.

3) Support yyjson_read_err optional parameter.

pavelxdd commented 1 year ago

https://github.com/ibireme/yyjson/blob/b2a6dfeb9c89cc53adc6203896a66850a5973373/src/yyjson.c#L3508

The current error message is misleading if no zero sign is present in buffer. For example .123 gives this message, and sets the cur to hdr - 1, so we can't safely cur - hdr after this error.

Hence, I've added a _pos > hdr check in return_err macro.

pavelxdd commented 1 year ago

I've added test cases based on test_number text files.

Also added support for YYJSON_READ_NUMBER_AS_RAW and YYJSON_READ_ALLOW_INF_AND_NAN read flags.

ibireme commented 1 year ago

Thanks, it looks much better now~