MCPcopy Create free account
hub / github.com/BoomingTech/Piccolo / tryParseDouble

Function tryParseDouble

engine/3rdparty/tinyobjloader/tiny_obj_loader.h:866–996  ·  view source on GitHub ↗

Tries to parse a floating point number located at s. s_end should be a location in the string where reading should absolutely stop. For example at the end of the string, to prevent buffer overflows. Parses the following EBNF grammar: sign = "+" | "-" ; END = ? anything not in digit ? digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; integer = [sign] , digit , {digit} ;

Source from the content-addressed store, hash-verified

864// - parse failure.
865//
866static bool tryParseDouble(const char *s, const char *s_end, double *result) {
867 if (s >= s_end) {
868 return false;
869 }
870
871 double mantissa = 0.0;
872 // This exponent is base 2 rather than 10.
873 // However the exponent we parse is supposed to be one of ten,
874 // thus we must take care to convert the exponent/and or the
875 // mantissa to a * 2^E, where a is the mantissa and E is the
876 // exponent.
877 // To get the final double we will use ldexp, it requires the
878 // exponent to be in base 2.
879 int exponent = 0;
880
881 // NOTE: THESE MUST BE DECLARED HERE SINCE WE ARE NOT ALLOWED
882 // TO JUMP OVER DEFINITIONS.
883 char sign = '+';
884 char exp_sign = '+';
885 char const *curr = s;
886
887 // How many characters were read in a loop.
888 int read = 0;
889 // Tells whether a loop terminated due to reaching s_end.
890 bool end_not_reached = false;
891 bool leading_decimal_dots = false;
892
893 /*
894 BEGIN PARSING.
895 */
896
897 // Find out what sign we've got.
898 if (*curr == '+' || *curr == '-') {
899 sign = *curr;
900 curr++;
901 if ((curr != s_end) && (*curr == '.')) {
902 // accept. Somethig like `.7e+2`, `-.5234`
903 leading_decimal_dots = true;
904 }
905 } else if (IS_DIGIT(*curr)) { /* Pass through. */
906 } else if (*curr == '.') {
907 // accept. Somethig like `.7e+2`, `-.5234`
908 leading_decimal_dots = true;
909 } else {
910 goto fail;
911 }
912
913 // Read the integer part.
914 end_not_reached = (curr != s_end);
915 if (!leading_decimal_dots) {
916 while (end_not_reached && IS_DIGIT(*curr)) {
917 mantissa *= 10;
918 mantissa += static_cast<int>(*curr - 0x30);
919 curr++;
920 read++;
921 end_not_reached = (curr != s_end);
922 }
923

Callers 1

parseRealFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected